{"id":81240,"date":"2024-10-16T15:11:16","date_gmt":"2024-10-16T07:11:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/81240\/"},"modified":"2024-10-16T15:11:16","modified_gmt":"2024-10-16T07:11:16","slug":"nginx%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%ae%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-nginx%e6%95%b8%e6%93%9a%e5%ba%ab%e9%85%8d%e7%bd%ae-2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/81240\/","title":{"rendered":"Nginx\u5982\u4f55\u914d\u7f6e\u6578\u64da\u5eab\uff1f (Nginx\u6578\u64da\u5eab\u914d\u7f6e)"},"content":{"rendered":"<h1 id=\"nginx%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%ae%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-nginx%e6%95%b8%e6%93%9a%e5%ba%ab%e9%85%8d%e7%bd%ae-gNhMeekgrF\">Nginx\u5982\u4f55\u914d\u7f6e\u6578\u64da\u5eab\uff1f (Nginx\u6578\u64da\u5eab\u914d\u7f6e)<\/h1>\n<p>Nginx\u662f\u4e00\u6b3e\u9ad8\u6548\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u975c\u614b\u5167\u5bb9\u7684\u63d0\u4f9b\u548c\u53cd\u5411\u4ee3\u7406\u670d\u52d9\u3002\u96d6\u7136Nginx\u672c\u8eab\u4e26\u4e0d\u76f4\u63a5\u8655\u7406\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u53ef\u4ee5\u8207\u6578\u64da\u5eab\u670d\u52d9\u5668\uff08\u5982MySQL\u6216PostgreSQL\uff09\u5354\u540c\u5de5\u4f5c\uff0c\u4ee5\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u914d\u7f6eNginx\u4ee5\u652f\u6301\u6578\u64da\u5eab\u7684\u4f7f\u7528\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u5efa\u8b70\u3002<\/p>\n<h2 id=\"nginx%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e9%97%9c%e4%bf%82-gNhMeekgrF\">Nginx\u8207\u6578\u64da\u5eab\u7684\u95dc\u4fc2<\/h2>\n<p>Nginx\u901a\u5e38\u7528\u4f5c\u53cd\u5411\u4ee3\u7406\u4f3a\u670d\u5668\uff0c\u5c07\u5ba2\u6236\u7aef\u7684\u8acb\u6c42\u8f49\u767c\u5230\u5f8c\u7aef\u61c9\u7528\u7a0b\u5e8f\u6216\u6578\u64da\u5eab\u3002\u9019\u6a23\u7684\u914d\u7f6e\u53ef\u4ee5\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u4e26\u63d0\u4f9b\u8ca0\u8f09\u5747\u8861\u548c\u5b89\u5168\u6027\u3002Nginx\u672c\u8eab\u4e0d\u76f4\u63a5\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u4f46\u53ef\u4ee5\u901a\u904e\u61c9\u7528\u7a0b\u5e8f\uff08\u5982PHP\u3001Python\u6216Node.js\uff09\u4f86\u5be6\u73fe\u3002<\/p>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e9%85%8d%e7%bd%ae%e6%ad%a5%e9%a9%9f-gNhMeekgrF\">\u57fa\u672c\u914d\u7f6e\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e5%ae%89%e8%a3%9dnginx-gNhMeekgrF\">1. \u5b89\u88ddNginx<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u4f3a\u670d\u5668\u4e0a\u5b89\u88ddNginx\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install nginx\n<\/code><\/pre>\n<h3 id=\"2-%e5%ae%89%e8%a3%9d%e6%95%b8%e6%93%9a%e5%ba%ab-gNhMeekgrF\">2. \u5b89\u88dd\u6578\u64da\u5eab<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5b89\u88dd\u6240\u9700\u7684\u6578\u64da\u5eab\u3002\u4f8b\u5982\uff0c\u5b89\u88ddMySQL\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sudo apt install mysql-server\n<\/code><\/pre>\n<h3 id=\"3-%e9%85%8d%e7%bd%aenginx-gNhMeekgrF\">3. \u914d\u7f6eNginx<\/h3>\n<p>\u5728\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u9700\u8981\u914d\u7f6eNginx\u4ee5\u652f\u6301\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u57fa\u672c\u7684Nginx\u914d\u7f6e\u793a\u4f8b\uff0c\u5047\u8a2d\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u4f7f\u7528PHP\u4f86\u8207MySQL\u9032\u884c\u4ea4\u4e92\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name your_domain.com;\n\n    root \/var\/www\/html;\n    index index.php index.html index.htm;\n\n    location \/ {\n        try_files $uri $uri\/ =404;\n    }\n\n    location ~ .php$ {\n        include snippets\/fastcgi-php.conf;\n        fastcgi_pass unix:\/var\/run\/php\/php7.4-fpm.sock; # \u6839\u64da\u60a8\u7684PHP\u7248\u672c\u9032\u884c\u8abf\u6574\n        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n        include fastcgi_params;\n    }\n}\n<\/code><\/pre>\n<h3 id=\"4-%e9%85%8d%e7%bd%ae%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-gNhMeekgrF\">4. \u914d\u7f6e\u6578\u64da\u5eab\u9023\u63a5<\/h3>\n<p>\u5728\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u4e2d\uff0c\u60a8\u9700\u8981\u914d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4e00\u500bPHP\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\uff1a<\/p>\n<pre><code>&lt;?php\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"database_name\";\n\n\/\/ \u5275\u5efa\u9023\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n}\necho \"\u9023\u63a5\u6210\u529f\";\n?&gt;\n<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-gNhMeekgrF\">\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u914d\u7f6eNginx\u548c\u6578\u64da\u5eab\u6642\uff0c\u5b89\u5168\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u9632\u706b\u7246\u9650\u5236\u5c0d\u6578\u64da\u5eab\u7684\u8a2a\u554f\u3002<\/li>\n<li>\u78ba\u4fdd\u6578\u64da\u5eab\u7528\u6236\u64c1\u6709\u6700\u5c0f\u7684\u6b0a\u9650\u3002<\/li>\n<li>\u5b9a\u671f\u66f4\u65b0Nginx\u548c\u6578\u64da\u5eab\u4ee5\u4fee\u88dc\u5b89\u5168\u6f0f\u6d1e\u3002<\/li>\n<li>\u4f7f\u7528SSL\/TLS\u52a0\u5bc6\u4f86\u4fdd\u8b77\u6578\u64da\u50b3\u8f38\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%80%a7%e8%83%bd%e5%84%aa%e5%8c%96-gNhMeekgrF\">\u6027\u80fd\u512a\u5316<\/h2>\n<p>\u70ba\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u60a8\u53ef\u4ee5\u8003\u616e\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u7de9\u5b58\u6280\u8853\uff0c\u5982Redis\u6216Memcached\uff0c\u4f86\u6e1b\u5c11\u5c0d\u6578\u64da\u5eab\u7684\u8acb\u6c42\u3002<\/li>\n<li>\u555f\u7528Gzip\u58d3\u7e2e\u4ee5\u6e1b\u5c11\u50b3\u8f38\u7684\u6578\u64da\u91cf\u3002<\/li>\n<li>\u4f7f\u7528CDN\u4f86\u52a0\u901f\u975c\u614b\u8cc7\u6e90\u7684\u52a0\u8f09\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-gNhMeekgrF\">\u7e3d\u7d50<\/h2>\n<p>\u914d\u7f6eNginx\u4ee5\u652f\u6301\u6578\u64da\u5eab\u7684\u4f7f\u7528\u662f\u4e00\u500b\u91cd\u8981\u7684\u6b65\u9a5f\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u5b89\u5168\u6027\u3002\u901a\u904e\u6b63\u78ba\u7684\u914d\u7f6e\u548c\u512a\u5316\uff0c\u60a8\u53ef\u4ee5\u78ba\u4fdd\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u5728\u9ad8\u6d41\u91cf\u60c5\u6cc1\u4e0b\u4ecd\u7136\u80fd\u5920\u7a69\u5b9a\u904b\u884c\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f\u642d\u5efa\u7db2\u7ad9\u9084\u662f\u904b\u884c\u61c9\u7528\u7a0b\u5e8f\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u914d\u7f6eNginx\u4ee5\u652f\u6301\u6578\u64da\u5eab\u9023\u63a5\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u8207\u5b89\u5168\u6027\uff0c\u7c21\u5316\u6578\u64da\u7ba1\u7406\u6d41\u7a0b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-81240","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=81240"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81240\/revisions"}],"predecessor-version":[{"id":81241,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/81240\/revisions\/81241"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=81240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=81240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=81240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}