{"id":44940,"date":"2024-10-06T19:19:14","date_gmt":"2024-10-06T11:19:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/44940\/"},"modified":"2024-10-06T19:19:14","modified_gmt":"2024-10-06T11:19:14","slug":"linux-%e4%b8%8b%e5%ae%89%e8%a3%9d%e5%92%8c%e9%85%8d%e7%bd%ae-nginx-%e5%85%b7%e9%ab%94%e6%ad%a5%e9%a9%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/44940\/","title":{"rendered":"Linux \u4e0b\u5b89\u88dd\u548c\u914d\u7f6e Nginx \u5177\u9ad4\u6b65\u9a5f"},"content":{"rendered":"<h1 id=\"linux-%e4%b8%8b%e5%ae%89%e8%a3%9d%e5%92%8c%e9%85%8d%e7%bd%ae-nginx-%e5%85%b7%e9%ab%94%e6%ad%a5%e9%a9%9f-MoXitvUXnn\">Linux \u4e0b\u5b89\u88dd\u548c\u914d\u7f6e Nginx \u5177\u9ad4\u6b65\u9a5f<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u975c\u614b\u5167\u5bb9\u7684\u63d0\u4f9b\u3001\u53cd\u5411\u4ee3\u7406\u3001\u8ca0\u8f09\u5747\u8861\u7b49\u5834\u666f\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5728 Linux \u7cfb\u7d71\u4e0b\u5b89\u88dd\u548c\u914d\u7f6e Nginx \u7684\u5177\u9ad4\u6b65\u9a5f\uff0c\u5e6b\u52a9\u7528\u6236\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e4%b8%80%ef%bc%9a%e6%9b%b4%e6%96%b0%e7%b3%bb%e7%b5%b1-MoXitvUXnn\">\u6b65\u9a5f\u4e00\uff1a\u66f4\u65b0\u7cfb\u7d71<\/h2>\n<p>\u5728\u5b89\u88dd Nginx \u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u78ba\u4fdd\u7cfb\u7d71\u662f\u6700\u65b0\u7684\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u66f4\u65b0\u7cfb\u7d71\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt upgrade<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e4%ba%8c%ef%bc%9a%e5%ae%89%e8%a3%9d-nginx-MoXitvUXnn\">\u6b65\u9a5f\u4e8c\uff1a\u5b89\u88dd Nginx<\/h2>\n<p>\u5728 Ubuntu \u6216 Debian \u7cfb\u7d71\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd Nginx\uff1a<\/p>\n<pre><code>sudo apt install nginx<\/code><\/pre>\n<p>\u5c0d\u65bc CentOS \u7cfb\u7d71\uff0c\u5247\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sudo yum install epel-release\nsudo yum install nginx<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e4%b8%89%ef%bc%9a%e5%95%9f%e5%8b%95-nginx-MoXitvUXnn\">\u6b65\u9a5f\u4e09\uff1a\u555f\u52d5 Nginx<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u555f\u52d5 Nginx\uff1a<\/p>\n<pre><code>sudo systemctl start nginx<\/code><\/pre>\n<p>\u70ba\u4e86\u78ba\u4fdd Nginx \u5728\u7cfb\u7d71\u555f\u52d5\u6642\u81ea\u52d5\u555f\u52d5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>sudo systemctl enable nginx<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e5%9b%9b%ef%bc%9a%e6%aa%a2%e6%9f%a5-nginx-%e7%8b%80%e6%85%8b-MoXitvUXnn\">\u6b65\u9a5f\u56db\uff1a\u6aa2\u67e5 Nginx \u72c0\u614b<\/h2>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6aa2\u67e5 Nginx \u7684\u904b\u884c\u72c0\u614b\uff1a<\/p>\n<pre><code>sudo systemctl status nginx<\/code><\/pre>\n<p>\u5982\u679c Nginx \u6b63\u5728\u904b\u884c\uff0c\u60a8\u61c9\u8a72\u6703\u770b\u5230\u985e\u4f3c\u65bc\u201cactive (running)\u201d\u7684\u72c0\u614b\u4fe1\u606f\u3002<\/p>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e4%ba%94%ef%bc%9a%e9%85%8d%e7%bd%ae%e9%98%b2%e7%81%ab%e7%89%86-MoXitvUXnn\">\u6b65\u9a5f\u4e94\uff1a\u914d\u7f6e\u9632\u706b\u7246<\/h2>\n<p>\u5982\u679c\u60a8\u7684\u7cfb\u7d71\u555f\u7528\u4e86\u9632\u706b\u7246\uff0c\u5247\u9700\u8981\u5141\u8a31 HTTP \u548c HTTPS \u6d41\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u914d\u7f6e\u9632\u706b\u7246\uff1a<\/p>\n<pre><code>sudo ufw allow 'Nginx Full'<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e5%85%ad%ef%bc%9a%e9%85%8d%e7%bd%ae-nginx-MoXitvUXnn\">\u6b65\u9a5f\u516d\uff1a\u914d\u7f6e Nginx<\/h2>\n<p>Nginx \u7684\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u4f4d\u65bc \/etc\/nginx\/nginx.conf\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u7de8\u8f2f\u5668\uff08\u5982 nano \u6216 vim\uff09\u4f86\u7de8\u8f2f\u6b64\u6587\u4ef6\uff1a<\/p>\n<pre><code>sudo nano \/etc\/nginx\/nginx.conf<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u8981\u4fee\u6539\u4f3a\u670d\u5668\u7684\u914d\u7f6e\uff0c\u4f8b\u5982\u8a2d\u7f6e\u4f3a\u670d\u5668\u540d\u7a31\u3001\u6839\u76ee\u9304\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u914d\u7f6e\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name example.com;\n\n    location \/ {\n        root \/var\/www\/html;\n        index index.html index.htm;\n    }\n}<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e4%b8%83%ef%bc%9a%e6%b8%ac%e8%a9%a6%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6-MoXitvUXnn\">\u6b65\u9a5f\u4e03\uff1a\u6e2c\u8a66\u914d\u7f6e\u6587\u4ef6<\/h2>\n<p>\u5728\u4fee\u6539\u5b8c\u914d\u7f6e\u6587\u4ef6\u5f8c\uff0c\u5efa\u8b70\u5148\u6e2c\u8a66\u914d\u7f6e\u6587\u4ef6\u7684\u6b63\u78ba\u6027\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u6e2c\u8a66\uff1a<\/p>\n<pre><code>sudo nginx -t<\/code><\/pre>\n<p>\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u60a8\u5c07\u770b\u5230\u201csyntax is ok\u201d\u548c\u201ctest is successful\u201d\u7684\u6d88\u606f\u3002<\/p>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e5%85%ab%ef%bc%9a%e9%87%8d%e6%96%b0%e5%8a%a0%e8%bc%89-nginx-MoXitvUXnn\">\u6b65\u9a5f\u516b\uff1a\u91cd\u65b0\u52a0\u8f09 Nginx<\/h2>\n<p>\u5728\u78ba\u8a8d\u914d\u7f6e\u6587\u4ef6\u7121\u8aa4\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u91cd\u65b0\u52a0\u8f09 Nginx\uff0c\u4f7f\u914d\u7f6e\u751f\u6548\uff1a<\/p>\n<pre><code>sudo systemctl reload nginx<\/code><\/pre>\n<h2 id=\"%e6%ad%a5%e9%a9%9f%e4%b9%9d%ef%bc%9a%e8%a8%aa%e5%95%8f-nginx-MoXitvUXnn\">\u6b65\u9a5f\u4e5d\uff1a\u8a2a\u554f Nginx<\/h2>\n<p>\u5728\u700f\u89bd\u5668\u4e2d\u8f38\u5165\u60a8\u7684\u4f3a\u670d\u5668 IP \u5730\u5740\u6216\u57df\u540d\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u770b\u5230 Nginx \u7684\u6b61\u8fce\u9801\u9762\uff0c\u9019\u8868\u793a Nginx \u5df2\u6210\u529f\u5b89\u88dd\u548c\u914d\u7f6e\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-MoXitvUXnn\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u5728 Linux \u7cfb\u7d71\u4e0a\u6210\u529f\u5b89\u88dd\u548c\u914d\u7f6e Nginx\u3002\u9019\u6b3e\u4f3a\u670d\u5668\u4e0d\u50c5\u6027\u80fd\u512a\u8d8a\uff0c\u9084\u80fd\u5920\u9748\u6d3b\u61c9\u5c0d\u5404\u7a2e\u9700\u6c42\u3002\u5982\u679c\u60a8\u9700\u8981\u66f4\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u7684\u696d\u52d9\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u9084\u662f\u5176\u4ed6\u5730\u5340\u7684\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5728 Linux \u7cfb\u7d71\u4e0a\u5b89\u88dd\u548c\u914d\u7f6e Nginx \u7684\u5177\u9ad4\u6b65\u9a5f\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u548c\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-44940","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/44940","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=44940"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/44940\/revisions"}],"predecessor-version":[{"id":44941,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/44940\/revisions\/44941"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=44940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=44940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=44940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}