{"id":17080,"date":"2024-11-18T17:18:10","date_gmt":"2024-11-18T09:18:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/17080\/"},"modified":"2024-11-18T17:18:10","modified_gmt":"2024-11-18T09:18:10","slug":"nginx-pm2-vps-%e8%ae%a9%e4%bd%a0%e7%9a%84%e7%bd%91%e7%ab%99%e6%9b%b4%e5%bf%ab%e3%80%81%e6%9b%b4%e5%8f%af%e9%9d%a0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/17080\/","title":{"rendered":"NGINX PM2 VPS: \u8ba9\u4f60\u7684\u7f51\u7ad9\u66f4\u5feb\u3001\u66f4\u53ef\u9760"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169568680140120.jpg\" class=\"aligncenter\" title=\"NGINX PM2 VPS: \u8ba9\u4f60\u7684\u7f51\u7ad9\u66f4\u5feb\u3001\u66f4\u53ef\u9760\u63d2\u56fe\" alt=\"NGINX PM2 VPS: \u8ba9\u4f60\u7684\u7f51\u7ad9\u66f4\u5feb\u3001\u66f4\u53ef\u9760\u63d2\u56fe\" \/><\/p>\n<p>NGINX PM2 VPS: \u8ba9\u4f60\u7684\u7f51\u7ad9\u66f4\u5feb\u3001\u66f4\u53ef\u9760\uff0c\u9700\u8981\u5177\u4f53\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u8fc5\u731b\u53d1\u5c55\uff0c\u7f51\u7ad9\u7684\u8bbf\u95ee\u91cf\u548c\u7528\u6237\u8981\u6c42\u4e5f\u8d8a\u6765\u8d8a\u9ad8\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e00\u6311\u6218\uff0c\u5f88\u591a\u7f51\u7ad9\u7ba1\u7406\u5458\u5f00\u59cb\u4f7f\u7528\u865a\u62df\u4e13\u7528\u670d\u52a1\u5668\uff08Virtual Private Server\uff0c\u7b80\u79f0VPS\uff09\u6765\u63d0\u9ad8\u7f51\u7ad9\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002\u800c\u5728VPS\u4e2d\uff0c\u7ec4\u5408\u4f7f\u7528NGINX\u548cPM2\u662f\u4e00\u4e2a\u975e\u5e38\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u53ef\u4ee5\u4f7f\u4f60\u7684\u7f51\u7ad9\u66f4\u5feb\u3001\u66f4\u53ef\u9760\u3002<\/p>\n<p>NGINX\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684HTTP\u548c\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u5b83\u53ef\u4ee5\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\uff0c\u5e76\u63d0\u4f9b\u7075\u6d3b\u7684\u914d\u7f6e\u9009\u9879\u3002\u76f8\u6bd4\u4f20\u7edf\u7684Apache\u670d\u52a1\u5668\uff0cNGINX\u5728\u5904\u7406\u9759\u6001\u5185\u5bb9\u548c\u9ad8\u5e76\u53d1\u8bf7\u6c42\u65f6\u66f4\u52a0\u9ad8\u6548\u3002\u901a\u8fc7\u5c06\u9759\u6001\u6587\u4ef6\u7684\u5904\u7406\u4ea4\u7ed9NGINX\uff0c\u800c\u5c06\u52a8\u6001\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u540e\u7aef\u5e94\u7528\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7f51\u7ad9\u7684\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n<p>PM2\u662f\u4e00\u4e2a\u73b0\u4ee3\u5316\u7684Node.js\u8fdb\u7a0b\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u5728\u540e\u53f0\u8fd0\u884c\u591a\u4e2aNode.js\u5e94\u7528\uff0c\u5e76\u901a\u8fc7\u76d1\u63a7\u548c\u81ea\u52a8\u91cd\u542f\u529f\u80fd\u4fdd\u6301\u8fd9\u4e9b\u5e94\u7528\u7684\u7a33\u5b9a\u6027\u3002PM2\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u591a\u4e2a\u5e94\u7528\u5b9e\u4f8b\u3001\u8d1f\u8f7d\u5747\u8861\u548c\u65e5\u5fd7\u7ba1\u7406\u7b49\u529f\u80fd\uff0c\u786e\u4fdd\u4f60\u7684Node.js\u5e94\u7528\u5728\u9ad8\u8d1f\u8f7d\u60c5\u51b5\u4e0b\u4e5f\u80fd\u4fdd\u6301\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<p>\u5728\u914d\u7f6eNGINX\u548cPM2\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6709\u4e00\u4e9b\u5173\u952e\u7684\u4ee3\u7801\u793a\u4f8b\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5b9e\u73b0\u8fd9\u4e24\u4e2a\u5de5\u5177\u7684\u534f\u540c\u5de5\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u793a\u4f8b\uff1a<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5NGINX\u548cPM2\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5728\u4f60\u7684VPS\u4e0a\u5b89\u88c5\u5b83\u4eec\uff1a<\/p>\n<pre># \u5b89\u88c5NGINX\nsudo apt-get update\nsudo apt-get install nginx\n\n# \u5b89\u88c5PM2\nsudo npm install -g pm2<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6eNGINX\u6765\u5904\u7406\u9759\u6001\u6587\u4ef6\uff0c\u5e76\u5c06\u52a8\u6001\u8bf7\u6c42\u8f6c\u53d1\u7ed9PM2\u7ba1\u7406\u7684Node.js\u5e94\u7528\u3002\u6253\u5f00NGINX\u7684\u914d\u7f6e\u6587\u4ef6\uff08\u901a\u5e38\u4f4d\u4e8e\/etc\/\/nginx.conf\u6216\/etc\/nginx\/sites-available\/default\uff09\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre># \u914d\u7f6e\u9759\u6001\u6587\u4ef6\u8def\u5f84\nlocation \/static {\n    alias \/path\/to\/your\/static\/files;\n}\n\n# \u8f6c\u53d1\u52a8\u6001\u8bf7\u6c42\u5230PM2\u7ba1\u7406\u7684\u5e94\u7528\nlocation \/ {\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 Host $host;\n    proxy_cache_bypass $http_upgrade;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\/path\/to\/your\/static\/files\u5e94\u66ff\u6362\u4e3a\u4f60\u5b9e\u9645\u7684\u9759\u6001\u6587\u4ef6\u8def\u5f84\uff0clocalhost:3000\u5e94\u66ff\u6362\u4e3a\u4f60\u7684Node.js\u5e94\u7528\u76d1\u542c\u7684\u5730\u5740\u548c\u7aef\u53e3\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u542f\u52a8PM2\u5e76\u5728\u540e\u53f0\u8fd0\u884c\u4f60\u7684Node.js\u5e94\u7528\u3002\u9996\u5148\uff0c\u8fdb\u5165\u4f60\u7684Node.js\u5e94\u7528\u6240\u5728\u7684\u76ee\u5f55\uff0c\u5e76\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8\u5e94\u7528\uff1a<\/p>\n<pre>pm2 start app.js<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u4f1a\u542f\u52a8\u4f60\u7684Node.js\u5e94\u7528\uff0c\u5e76\u7531PM2\u6765\u7ba1\u7406\u5b83\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u5e94\u7528\u7684\u8fd0\u884c\u72b6\u6001\uff1a<\/p>\n<pre>pm2 list<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u91cd\u65b0\u542f\u52a8NGINX\uff0c\u4f7f\u5176\u52a0\u8f7d\u6211\u4eec\u521a\u521a\u4fee\u6539\u7684\u914d\u7f6e\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u91cd\u65b0\u542f\u52a8NGINX\uff1a<\/p>\n<pre>sudo service nginx restart<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5b8c\u6210\u4e0a\u8ff0\u6b65\u9aa4\u540e\uff0c\u4f60\u7684\u7f51\u7ad9\u5c06\u91c7\u7528NGINX\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u5e76\u901a\u8fc7PM2\u7ba1\u7406\u7684Node.js\u5e94\u7528\u63d0\u4f9b\u52a8\u6001\u5185\u5bb9\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4f60\u7684\u7f51\u7ad9\u5c06\u80fd\u591f\u66f4\u5feb\u5730\u54cd\u5e94\u8bf7\u6c42\uff0c\u5e76\u4e14\u5728\u9ad8\u8d1f\u8f7d\u60c5\u51b5\u4e0b\u4ecd\u7136\u4fdd\u6301\u7a33\u5b9a\u8fd0\u884c\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0cNGINX\u548cPM2\u7684\u7ec4\u5408\u53ef\u4ee5\u4f7f\u4f60\u7684\u7f51\u7ad9\u66f4\u5feb\u3001\u66f4\u53ef\u9760\u3002\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u914d\u7f6e\u548c\u4f7f\u7528\u5b83\u4eec\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u5728VPS\u73af\u5883\u4e2d\u4f18\u5316\u4f60\u7684\u7f51\u7ad9\u6027\u80fd\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fNGINX PM2 VPS: \u8ba9\u4f60\u7684\u7f51\u7ad9\u66f4\u5feb\u3001\u66f4\u53ef\u9760\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>NGINX PM2 VPS: \u8ba9\u4f60\u7684\u7f51\u7ad9\u66f4\u5feb\u3001\u66f4\u53ef\u9760\uff0c\u9700\u8981\u5177\u4f53\u4ee3\u7801\u793a\u4f8b \u968f\u7740\u4e92\u8054\u7f51\u7684\u8fc5\u731b\u53d1\u5c55\uff0c\u7f51\u7ad9\u7684\u8bbf\u95ee\u91cf\u548c\u7528\u6237\u8981\u6c42\u4e5f\u8d8a\u6765\u8d8a\u9ad8\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e00\u6311\u6218\uff0c\u5f88\u591a\u7f51\u7ad9\u7ba1\u7406\u5458\u5f00\u59cb\u4f7f\u7528\u865a\u62df\u4e13\u7528\u670d\u52a1\u5668\uff08Virtual Private Server\uff0c\u7b80\u79f0VPS\uff09\u6765\u63d0\u9ad8\u7f51\u7ad9\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002\u800c\u5728VPS\u4e2d\uff0c\u7ec4\u5408\u4f7f\u7528NGINX\u548cPM2\u662f\u4e00\u4e2a\u975e\u5e38\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u53ef\u4ee5\u4f7f\u4f60\u7684\u7f51\u7ad9\u66f4\u5feb\u3001\u66f4\u53ef\u9760\u3002 NGINX\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684HTTP\u548c\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u5b83\u53ef\u4ee5\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\uff0c\u5e76\u63d0\u4f9b\u7075\u6d3b\u7684\u914d\u7f6e\u9009\u9879\u3002\u76f8\u6bd4\u4f20\u7edf\u7684Apache\u670d\u52a1\u5668\uff0cNGINX\u5728\u5904\u7406\u9759\u6001\u5185\u5bb9\u548c\u9ad8\u5e76\u53d1\u8bf7\u6c42\u65f6\u66f4\u52a0\u9ad8\u6548\u3002\u901a\u8fc7\u5c06\u9759\u6001\u6587\u4ef6\u7684\u5904\u7406\u4ea4\u7ed9NGINX\uff0c\u800c\u5c06\u52a8\u6001\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u540e\u7aef\u5e94\u7528\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7f51\u7ad9\u7684\u54cd\u5e94\u901f\u5ea6\u3002 PM2\u662f\u4e00\u4e2a\u73b0\u4ee3\u5316\u7684Node.js\u8fdb\u7a0b\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u5728\u540e\u53f0\u8fd0\u884c\u591a\u4e2aNode.js\u5e94\u7528\uff0c\u5e76\u901a\u8fc7\u76d1\u63a7\u548c\u81ea\u52a8\u91cd\u542f\u529f\u80fd\u4fdd\u6301\u8fd9\u4e9b\u5e94\u7528\u7684\u7a33\u5b9a\u6027\u3002PM2\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u591a\u4e2a\u5e94\u7528\u5b9e\u4f8b\u3001\u8d1f\u8f7d\u5747\u8861\u548c\u65e5\u5fd7\u7ba1\u7406\u7b49\u529f\u80fd\uff0c\u786e\u4fdd\u4f60\u7684Node.js\u5e94\u7528\u5728\u9ad8\u8d1f\u8f7d\u60c5\u51b5\u4e0b\u4e5f\u80fd\u4fdd\u6301\u7a33\u5b9a\u8fd0\u884c\u3002 \u5728\u914d\u7f6eNGINX\u548cPM2\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6709\u4e00\u4e9b\u5173\u952e\u7684\u4ee3\u7801\u793a\u4f8b\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5b9e\u73b0\u8fd9\u4e24\u4e2a\u5de5\u5177\u7684\u534f\u540c\u5de5\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u793a\u4f8b\uff1a \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5NGINX\u548cPM2\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5728\u4f60\u7684VPS\u4e0a\u5b89\u88c5\u5b83\u4eec\uff1a # \u5b89\u88c5NGINX sudo apt-get update sudo apt-get install nginx # \u5b89\u88c5PM2 sudo npm install -g pm2 \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6eNGINX\u6765\u5904\u7406\u9759\u6001\u6587\u4ef6\uff0c\u5e76\u5c06\u52a8\u6001\u8bf7\u6c42\u8f6c\u53d1\u7ed9PM2\u7ba1\u7406\u7684Node.js\u5e94\u7528\u3002\u6253\u5f00NGINX\u7684\u914d\u7f6e\u6587\u4ef6\uff08\u901a\u5e38\u4f4d\u4e8e\/etc\/\/nginx.conf\u6216\/etc\/nginx\/sites-available\/default\uff09\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\uff1a # \u914d\u7f6e\u9759\u6001\u6587\u4ef6\u8def\u5f84 location \/static { alias \/path\/to\/your\/static\/files; } # \u8f6c\u53d1\u52a8\u6001\u8bf7\u6c42\u5230PM2\u7ba1\u7406\u7684\u5e94\u7528 location \/ { proxy_pass http:\/\/localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection &#8216;upgrade&#8217;; proxy_set_header Host $host; [&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-17080","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17080","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=17080"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17080\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=17080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=17080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=17080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}