{"id":17164,"date":"2024-11-18T14:55:44","date_gmt":"2024-11-18T06:55:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/17164\/"},"modified":"2024-11-18T14:55:44","modified_gmt":"2024-11-18T06:55:44","slug":"%e6%95%99%e4%bd%a0%e5%a6%82%e4%bd%95%e5%9c%a8vps%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8a%e9%83%a8%e7%bd%b2nginx%e5%92%8cpm2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/17164\/","title":{"rendered":"\u6559\u4f60\u5982\u4f55\u5728VPS\u670d\u52a1\u5668\u4e0a\u90e8\u7f72NGINX\u548cPM2"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169579230441293.jpg\" class=\"aligncenter\" title=\"\u6559\u4f60\u5982\u4f55\u5728VPS\u670d\u52a1\u5668\u4e0a\u90e8\u7f72NGINX\u548cPM2\u63d2\u56fe\" alt=\"\u6559\u4f60\u5982\u4f55\u5728VPS\u670d\u52a1\u5668\u4e0a\u90e8\u7f72NGINX\u548cPM2\u63d2\u56fe\" \/><\/p>\n<p>\u6559\u4f60\u5982\u4f55\u5728VPS\u670d\u52a1\u5668\u4e0a\u90e8\u7f72NGINX\u548cPM2<\/p>\n<p>\u524d\u8a00\uff1a<br \/>VPS\u670d\u52a1\u5668\u7684\u90e8\u7f72\u662f\u7f51\u7ad9\u6216\u5e94\u7528\u7684\u5173\u952e\u6b65\u9aa4\u4e4b\u4e00\u3002\u5728\u6b64\uff0c\u6211\u5c06\u5411\u5927\u5bb6\u4ecb\u7ecd\u5982\u4f55\u5728VPS\u670d\u52a1\u5668\u4e0a\u90e8\u7f72NGINX\u548cPM2\uff0c\u8fd9\u4e24\u4e2a\u5de5\u5177\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u5347\u7f51\u7ad9\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5b89\u88c5\u914d\u7f6e\u8fc7\u7a0b\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<ol>\n<li>\u5b89\u88c5NGINX\uff1a<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5NGINX\u4f5c\u4e3a\u6211\u4eec\u7684Web\u670d\u52a1\u5668\u3002<\/p>\n<p>1.1 \u66f4\u65b0 apt-get \u5305\u7ba1\u7406\u5de5\u5177\uff1a<\/p>\n<pre>sudo apt-get update<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>1.2 \u5b89\u88c5NGINX\uff1a<\/p>\n<pre>sudo apt-get install nginx<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u914d\u7f6e\u6587\u4ef6\u4f4d\u7f6e\uff1a\/etc\/\/nginx.conf\u3002<\/p>\n<ol>\n<li>\u914d\u7f6eNGINX\uff1a<\/li>\n<\/ol>\n<p>2.1 \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ad9\u70b9\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre>sudo nano \/etc\/nginx\/sites-available\/myapp<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2.2 \u5728\u8be5\u6587\u4ef6\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>server {\n  listen 80;\n  server_name your_domain.com;\n\n  location \/ {\n    proxy_set_header X-Real-IP $remote_addr;\n    proxy_set_header Host $host;\n    proxy_pass http:\/\/localhost:3000;\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6ce8\u610f\uff1a\u5c06 &#8220;your_domain.com&#8221; \u66ff\u6362\u4e3a\u4f60\u7684\u57df\u540d\uff0c &#8220;http:\/\/localhost:3000&#8221; \u66ff\u6362\u4e3a\u4f60\u7684\u5b9e\u9645\u5e94\u7528\u7a0b\u5e8f\u7684\u5730\u5740\u3002 <\/p>\n<p>2.3 \u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\uff1a\u6309\u4e0b Ctrl + X\uff0c\u7136\u540e\u8f93\u5165 Y \u4fdd\u5b58\u66f4\u6539\u3002<\/p>\n<p>2.4 \u521b\u5efa\u4e00\u4e2a\u7b26\u53f7\u94fe\u63a5\uff1a<\/p>\n<pre>sudo ln -s \/etc\/nginx\/sites-available\/myapp \/etc\/nginx\/sites-enabled\/<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u5b89\u88c5PM2\uff1a<\/li>\n<\/ol>\n<p>PM2\u662f\u4e00\u4e2aNode.js\u7684\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\uff0c\u5b83\u53ef\u786e\u4fdd\u4f60\u7684\u5e94\u7528\u5728\u670d\u52a1\u5668\u4e0a\u6301\u7eed\u8fd0\u884c\uff0c\u5e76\u80fd\u8fdb\u884c\u6545\u969c\u6062\u590d\u3002<\/p>\n<p>3.1 \u5b89\u88c5PM2\uff1a<\/p>\n<pre>sudo npm install -g pm2<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>3.2 \u542f\u52a8\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre>pm2 start app.js<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6ce8\u610f\uff1aapp.js \u662f\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u5165\u53e3\u6587\u4ef6\uff0c\u4f60\u9700\u8981\u5c06\u5176\u66ff\u6362\u4e3a\u4f60\u81ea\u5df1\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<p>3.3 \u751f\u6210\u542f\u52a8\u811a\u672c\uff1a<\/p>\n<pre>pm2 startup systemd<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd0\u884c\u8fd9\u4e2a\u547d\u4ee4\u540e\uff0c\u4f60\u5c06\u770b\u5230\u4e00\u4e2a\u63d0\u793a\uff0c\u5c06\u5b83\u7ed9\u51fa\u7684\u547d\u4ee4\u590d\u5236\u5230\u7ec8\u7aef\u4e2d\u5e76\u8fd0\u884c\u3002\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u4f7fPM2\u5728\u7cfb\u7edf\u91cd\u542f\u65f6\u81ea\u52a8\u8fd0\u884c\u3002<\/p>\n<ol>\n<li>\u914d\u7f6eNGINX\u53cd\u5411\u4ee3\u7406\uff1a<\/li>\n<\/ol>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6eNGINX\u5c06\u6240\u6709\u6d41\u91cf\u8f6c\u53d1\u5230PM2\u542f\u52a8\u7684\u5e94\u7528\u7a0b\u5e8f\u4e0a\u3002<\/p>\n<p>4.1 \u6253\u5f00NGINX\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre>sudo nano \/etc\/nginx\/nginx.conf<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>4.2 \u4fee\u6539\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre>http{\n  ...\n  server {\n    listen 80;\n    server_name your_domain.com;\n\n    location \/ {\n      proxy_set_header X-Real-IP $remote_addr;\n      proxy_set_header Host $host;\n      proxy_pass http:\/\/localhost:3000;\n      proxy_http_version 1.1;\n      proxy_set_header Upgrade $http_upgrade;\n      proxy_set_header Connection 'upgrade';\n      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n      proxy_cache_bypass $http_upgrade;\n    }\n  }\n  ...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6ce8\u610f\uff1a\u5c06 &#8220;your_domain.com&#8221; \u66ff\u6362\u4e3a\u4f60\u7684\u57df\u540d\uff0c &#8220;http:\/\/localhost:3000&#8221; \u66ff\u6362\u4e3a\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u5730\u5740\u3002<\/p>\n<ol>\n<li>\u91cd\u542fNGINX\u548cPM2\uff1a<\/li>\n<\/ol>\n<p>\u5728\u5b8c\u6210\u4ee5\u4e0a\u914d\u7f6e\u540e\uff0c\u6211\u4eec\u9700\u8981\u91cd\u65b0\u542f\u52a8NGINX\u548cPM2\uff0c\u4f7f\u66f4\u6539\u751f\u6548\u3002<\/p>\n<p>5.1 \u91cd\u542fNGINX\uff1a<\/p>\n<pre>sudo systemctl restart nginx<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>5.2 \u91cd\u542fPM2\uff1a<\/p>\n<pre>pm2 restart all<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u5df2\u7ecf\u6210\u529f\u5728VPS\u670d\u52a1\u5668\u4e0a\u90e8\u7f72\u4e86NGINX\u548cPM2\u3002NGINX\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\uff0c\u5c06\u6240\u6709\u6d41\u91cf\u8f6c\u53d1\u5230PM2\u542f\u52a8\u7684\u5e94\u7528\u4e0a\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u7f51\u7ad9\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002\u5728\u4ee5\u540e\u7684\u5f00\u53d1\u548c\u7ef4\u62a4\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528PM2\u7684\u5176\u4ed6\u529f\u80fd\uff0c\u5982\u65e5\u5fd7\u7ba1\u7406\u548c\u76d1\u63a7\u7b49\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<br \/>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728VPS\u670d\u52a1\u5668\u4e0a\u90e8\u7f72NGINX\u548cPM2\uff0c\u5e76\u63d0\u4f9b\u4e86\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u63d0\u5347\u7f51\u7ad9\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\uff0c\u662f\u7f51\u7ad9\u90e8\u7f72\u8fc7\u7a0b\u4e2d\u5fc5\u4e0d\u53ef\u5c11\u7684\u5de5\u5177\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u8ba9\u4f60\u7684\u7f51\u7ad9\u66f4\u52a0\u51fa\u8272\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6559\u4f60\u5982\u4f55\u5728VPS\u670d\u52a1\u5668\u4e0a\u90e8\u7f72NGINX\u548cPM2\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6559\u4f60\u5982\u4f55\u5728VPS\u670d\u52a1\u5668\u4e0a\u90e8\u7f72NGINX\u548cPM2 \u524d\u8a00\uff1aVPS\u670d\u52a1\u5668\u7684\u90e8\u7f72\u662f\u7f51\u7ad9\u6216\u5e94\u7528\u7684\u5173\u952e\u6b65\u9aa4\u4e4b\u4e00\u3002\u5728\u6b64\uff0c\u6211\u5c06\u5411\u5927\u5bb6\u4ecb\u7ecd\u5982\u4f55\u5728VPS\u670d\u52a1\u5668\u4e0a\u90e8\u7f72NGINX\u548cPM2\uff0c\u8fd9\u4e24\u4e2a\u5de5\u5177\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u5347\u7f51\u7ad9\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5b89\u88c5\u914d\u7f6e\u8fc7\u7a0b\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u5b89\u88c5NGINX\uff1a \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5NGINX\u4f5c\u4e3a\u6211\u4eec\u7684Web\u670d\u52a1\u5668\u3002 1.1 \u66f4\u65b0 apt-get \u5305\u7ba1\u7406\u5de5\u5177\uff1a sudo apt-get update \u767b\u5f55\u540e\u590d\u5236 1.2 \u5b89\u88c5NGINX\uff1a sudo apt-get install nginx \u767b\u5f55\u540e\u590d\u5236 \u914d\u7f6e\u6587\u4ef6\u4f4d\u7f6e\uff1a\/etc\/\/nginx.conf\u3002 \u914d\u7f6eNGINX\uff1a 2.1 \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ad9\u70b9\u914d\u7f6e\u6587\u4ef6\uff1a sudo nano \/etc\/nginx\/sites-available\/myapp \u767b\u5f55\u540e\u590d\u5236 2.2 \u5728\u8be5\u6587\u4ef6\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u5185\u5bb9\uff1a server { listen 80; server_name your_domain.com; location \/ { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http:\/\/localhost:3000; } } \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\uff1a\u5c06 &#8220;your_domain.com&#8221; \u66ff\u6362\u4e3a\u4f60\u7684\u57df\u540d\uff0c &#8220;http:\/\/localhost:3000&#8221; \u66ff\u6362\u4e3a\u4f60\u7684\u5b9e\u9645\u5e94\u7528\u7a0b\u5e8f\u7684\u5730\u5740\u3002 2.3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-17164","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=17164"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17164\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=17164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=17164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=17164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}