{"id":44966,"date":"2024-10-06T19:30:15","date_gmt":"2024-10-06T11:30:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/44966\/"},"modified":"2024-10-06T19:30:15","modified_gmt":"2024-10-06T11:30:15","slug":"linux-%e4%b8%ad%e9%85%8d%e7%bd%ae-nginx-web-%e4%bc%ba%e6%9c%8d%e5%99%a8%e9%81%8e%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/44966\/","title":{"rendered":"Linux \u4e2d\u914d\u7f6e Nginx Web \u4f3a\u670d\u5668\u904e\u7a0b"},"content":{"rendered":"<h1 id=\"linux-%e4%b8%ad%e9%85%8d%e7%bd%ae-nginx-web-%e4%bc%ba%e6%9c%8d%e5%99%a8%e9%81%8e%e7%a8%8b-viJDXcXyYu\">Linux \u4e2d\u914d\u7f6e Nginx Web \u4f3a\u670d\u5668\u904e\u7a0b<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684 Web \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\u4ecb\u7d39\u5728 Linux \u74b0\u5883\u4e2d\u914d\u7f6e Nginx Web \u4f3a\u670d\u5668\u7684\u904e\u7a0b\uff0c\u5e6b\u52a9\u8b80\u8005\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9d-nginx-viJDXcXyYu\">\u5b89\u88dd Nginx<\/h2>\n<p>\u5728\u958b\u59cb\u914d\u7f6e\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd Nginx\u3002\u4ee5\u4e0b\u662f\u57fa\u65bc Ubuntu \u548c CentOS \u7684\u5b89\u88dd\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"%e5%9c%a8-ubuntu-%e4%b8%8a%e5%ae%89%e8%a3%9d-nginx-viJDXcXyYu\">\u5728 Ubuntu \u4e0a\u5b89\u88dd Nginx<\/h3>\n<pre><code>sudo apt update\nsudo apt install nginx\n<\/code><\/pre>\n<h3 id=\"%e5%9c%a8-centos-%e4%b8%8a%e5%ae%89%e8%a3%9d-nginx-viJDXcXyYu\">\u5728 CentOS \u4e0a\u5b89\u88dd Nginx<\/h3>\n<pre><code>sudo yum install epel-release\nsudo yum install nginx\n<\/code><\/pre>\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\nsudo systemctl enable nginx\n<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-nginx-viJDXcXyYu\">\u914d\u7f6e Nginx<\/h2>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0cNginx \u7684\u914d\u7f6e\u6587\u4ef6\u4f4d\u65bc \/etc\/nginx\/nginx.conf\u3002\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\n<\/code><\/pre>\n<h3 id=\"%e5%9f%ba%e6%9c%ac%e9%85%8d%e7%bd%ae-viJDXcXyYu\">\u57fa\u672c\u914d\u7f6e<\/h3>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u8a2d\u7f6e\u4f3a\u670d\u5668\u7684\u57fa\u672c\u53c3\u6578\uff0c\u4f8b\u5982\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}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u914d\u7f6e\u4e2d\uff0c\u4f3a\u670d\u5668\u76e3\u807d 80 \u7aef\u53e3\uff0c\u4e26\u5c07\u8acb\u6c42\u6307\u5411 \/var\/www\/html \u76ee\u9304\u4e2d\u7684 index.html \u6216 index.htm \u6587\u4ef6\u3002<\/p>\n<h3 id=\"%e6%b8%ac%e8%a9%a6%e9%85%8d%e7%bd%ae-viJDXcXyYu\">\u6e2c\u8a66\u914d\u7f6e<\/h3>\n<p>\u5728\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u5f8c\uff0c\u61c9\u8a72\u6e2c\u8a66\u914d\u7f6e\u7684\u6b63\u78ba\u6027\uff1a<\/p>\n<pre><code>sudo nginx -t\n<\/code><\/pre>\n<p>\u5982\u679c\u6e2c\u8a66\u901a\u904e\uff0c\u5247\u53ef\u4ee5\u91cd\u65b0\u52a0\u8f09 Nginx \u4ee5\u61c9\u7528\u66f4\u6539\uff1a<\/p>\n<pre><code>sudo systemctl reload nginx\n<\/code><\/pre>\n<h2 id=\"%e8%a8%ad%e7%bd%ae%e9%98%b2%e7%81%ab%e7%89%86-viJDXcXyYu\">\u8a2d\u7f6e\u9632\u706b\u7246<\/h2>\n<p>\u70ba\u4e86\u78ba\u4fdd Nginx \u80fd\u5920\u6b63\u5e38\u904b\u884c\uff0c\u9700\u8a2d\u7f6e\u9632\u706b\u7246\u4ee5\u5141\u8a31 HTTP \u548c HTTPS \u6d41\u91cf\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 UFW \u548c Firewalld \u7684\u8a2d\u7f6e\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8-ufw-viJDXcXyYu\">\u4f7f\u7528 UFW<\/h3>\n<pre><code>sudo ufw allow 'Nginx Full'\n<\/code><\/pre>\n<h3 id=\"%e4%bd%bf%e7%94%a8-firewalld-viJDXcXyYu\">\u4f7f\u7528 Firewalld<\/h3>\n<pre><code>sudo firewall-cmd --permanent --add-service=http\nsudo firewall-cmd --permanent --add-service=https\nsudo firewall-cmd --reload\n<\/code><\/pre>\n<h2 id=\"%e5%95%9f%e7%94%a8-https-viJDXcXyYu\">\u555f\u7528 HTTPS<\/h2>\n<p>\u70ba\u4e86\u63d0\u9ad8\u7db2\u7ad9\u7684\u5b89\u5168\u6027\uff0c\u5efa\u8b70\u555f\u7528 HTTPS\u3002\u53ef\u4ee5\u4f7f\u7528 Let&#8217;s Encrypt \u63d0\u4f9b\u7684\u514d\u8cbb SSL \u8b49\u66f8\u3002\u9996\u5148\uff0c\u5b89\u88dd Certbot\uff1a<\/p>\n<h3 id=\"%e5%9c%a8-ubuntu-%e4%b8%8a%e5%ae%89%e8%a3%9d-certbot-viJDXcXyYu\">\u5728 Ubuntu \u4e0a\u5b89\u88dd Certbot<\/h3>\n<pre><code>sudo apt install certbot python3-certbot-nginx\n<\/code><\/pre>\n<h3 id=\"%e5%9c%a8-centos-%e4%b8%8a%e5%ae%89%e8%a3%9d-certbot-viJDXcXyYu\">\u5728 CentOS \u4e0a\u5b89\u88dd Certbot<\/h3>\n<pre><code>sudo yum install certbot python2-certbot-nginx\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u4f7f\u7528 Certbot \u81ea\u52d5\u914d\u7f6e SSL \u8b49\u66f8\uff1a<\/p>\n<pre><code>sudo certbot --nginx -d example.com -d www.example.com\n<\/code><\/pre>\n<p>\u5b8c\u6210\u5f8c\uff0cCertbot \u6703\u81ea\u52d5\u66f4\u65b0 Nginx \u914d\u7f6e\u4ee5\u555f\u7528 HTTPS\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-viJDXcXyYu\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5728 Linux \u74b0\u5883\u4e2d\u914d\u7f6e Nginx Web \u4f3a\u670d\u5668\u7684\u57fa\u672c\u904e\u7a0b\uff0c\u5305\u62ec\u5b89\u88dd\u3001\u914d\u7f6e\u3001\u8a2d\u7f6e\u9632\u706b\u7246\u53ca\u555f\u7528 HTTPS\u3002\u900f\u904e\u9019\u4e9b\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u642d\u5efa\u4e00\u500b\u5b89\u5168\u4e14\u9ad8\u6548\u7684\u7db2\u7ad9\u3002\u5982\u679c\u60a8\u9700\u8981\u66f4\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8003\u616e\u4f7f\u7528\u6211\u5011\u7684\u670d\u52d9\uff0c\u8b93\u60a8\u7684\u7db2\u7ad9\u904b\u884c\u5f97\u66f4\u6d41\u66a2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Linux \u4e2d\u914d\u7f6e Nginx Web \u4f3a\u670d\u5668\uff0c\u5f9e\u5b89\u88dd\u5230\u8a2d\u5b9a\uff0c\u5feb\u901f\u5efa\u7acb\u9ad8\u6548\u80fd\u7db2\u7ad9\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-44966","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/44966","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=44966"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/44966\/revisions"}],"predecessor-version":[{"id":44967,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/44966\/revisions\/44967"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=44966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=44966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=44966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}