{"id":62616,"date":"2025-04-29T14:52:49","date_gmt":"2025-04-29T06:52:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/62616\/"},"modified":"2025-04-29T14:52:49","modified_gmt":"2025-04-29T06:52:49","slug":"%e7%ae%80%e6%98%8e%e6%95%99%e7%a8%8b-%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8nginx%e5%92%8cpm2%e4%bc%98%e5%8c%96vps%e6%9c%8d%e5%8a%a1%e5%99%a8%e7%9a%84%e8%b5%84%e6%ba%90%e7%ae%a1%e7%90%86-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/62616\/","title":{"rendered":"\u7b80\u660e\u6559\u7a0b: \u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u4f18\u5316VPS\u670d\u52a1\u5668\u7684\u8d44\u6e90\u7ba1\u7406"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169569727371353.jpg\" class=\"aligncenter\" title=\"\u7b80\u660e\u6559\u7a0b: \u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u4f18\u5316VPS\u670d\u52a1\u5668\u7684\u8d44\u6e90\u7ba1\u7406\u63d2\u56fe\" alt=\"\u7b80\u660e\u6559\u7a0b: \u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u4f18\u5316VPS\u670d\u52a1\u5668\u7684\u8d44\u6e90\u7ba1\u7406\u63d2\u56fe\" \/><\/p>\n<p>\u7b80\u660e\u6559\u7a0b: \u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u4f18\u5316VPS\u670d\u52a1\u5668\u7684\u8d44\u6e90\u7ba1\u7406<\/p>\n<p>\u524d\u8a00:<br \/>\u5728\u73b0\u4ee3\u4e92\u8054\u7f51\u7684\u65f6\u4ee3\uff0c\u968f\u7740\u7f51\u7ad9\u548c\u5e94\u7528\u7a0b\u5e8f\u7684\u6d41\u91cf\u8d8a\u6765\u8d8a\u5927\uff0c\u670d\u52a1\u5668\u8d44\u6e90\u7ba1\u7406\u53d8\u5f97\u81f3\u5173\u91cd\u8981\u3002\u4e3a\u4e86\u63d0\u9ad8\u670d\u52a1\u5668\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\uff0c\u5408\u7406\u5730\u914d\u7f6e\u548c\u4f18\u5316\u670d\u52a1\u5668\u8d44\u6e90\u662f\u975e\u5e38\u5fc5\u8981\u7684\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u6765\u4f18\u5316VPS\u670d\u52a1\u5668\u7684\u8d44\u6e90\u7ba1\u7406\uff0c\u4ece\u800c\u63d0\u5347\u670d\u52a1\u5668\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u4e00\u3001NGINX\u7684\u5b89\u88c5\u548c\u914d\u7f6e<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5NGINX<br \/>\u5728Ubuntu\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u5b89\u88c5NGINX:<\/p>\n<pre>$sudo apt-get update\n$sudo apt-get install nginx<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\u914d\u7f6eNGINX<br \/>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cNGINX\u7684\u914d\u7f6e\u6587\u4ef6\u662f\u5b58\u653e\u5728\/etc\/\u76ee\u5f55\u4e0b\u7684nginx.conf\u6587\u4ef6\u3002\u5728\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u914d\u7f6e\u76d1\u542c\u7684\u7aef\u53e3\uff0c\u865a\u62df\u4e3b\u673a\u7b49\u3002\u5177\u4f53\u53ef\u4ee5\u53c2\u8003NGINX\u7684\u5b98\u65b9\u6587\u6863\u3002<\/li>\n<\/ol>\n<p>\u4e8c\u3001PM2\u7684\u5b89\u88c5\u548c\u914d\u7f6e<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5PM2<br \/>\u53ef\u4ee5\u901a\u8fc7npm\u6765\u5168\u5c40\u5b89\u88c5PM2:<\/p>\n<pre>$sudo npm install -g pm2<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u914d\u7f6ePM2<br \/>\u5728\u4f7f\u7528PM2\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u4e4b\u524d\uff0c\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a\u542f\u52a8\u811a\u672c\u3002\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aecosystem.config.js\u6587\u4ef6\uff0c\u5e76\u5c06\u4ee5\u4e0b\u5185\u5bb9\u590d\u5236\u5230\u6587\u4ef6\u4e2d:<\/p>\n<pre>module.exports = {\n  apps: [\n {\n   name: \"app\",\n   script: \"app.js\",\n   instances: \"max\",\n   autorestart: true,\n   watch: true,\n   ignore_watch: [\"node_modules\", \"logs\"],\n   exec_mode: \"cluster\"\n }\n  ]\n};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0cname\u662f\u5e94\u7528\u7a0b\u5e8f\u7684\u540d\u79f0\uff0cscript\u662f\u542f\u52a8\u811a\u672c\u7684\u8def\u5f84\uff0cinstances\u662f\u8fdb\u7a0b\u7684\u6570\u91cf\uff0cautorestart\u662f\u662f\u5426\u81ea\u52a8\u91cd\u542f\uff0cwatch\u662f\u662f\u5426\u76d1\u542c\u6587\u4ef6\u7684\u53d8\u5316\u81ea\u52a8\u91cd\u542f\uff0cignore_watch\u662f\u5ffd\u7565\u76d1\u542c\u7684\u76ee\u5f55\uff0cexec_mode\u662f\u8fdb\u7a0b\u7684\u6267\u884c\u6a21\u5f0f\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4e09\u3001NGINX\u548cPM2\u7684\u7ed3\u5408\u4f7f\u7528<\/p>\n<ol>\n<li>\u914d\u7f6e\u53cd\u5411\u4ee3\u7406<br \/>\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u53cd\u5411\u4ee3\u7406\u6765\u4f18\u5316\u8bf7\u6c42\u7684\u8d1f\u8f7d\u5747\u8861\u548c\u6d41\u91cf\u7ba1\u7406\u3002\u6211\u4eec\u53ef\u4ee5\u5728NGINX\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u53cd\u5411\u4ee3\u7406\uff0c\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9PM2\u7ba1\u7406\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/li>\n<\/ol>\n<p>\u5728NGINX\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u672c\u5730\u76843000\u7aef\u53e3:<\/p>\n<pre>server {\n  listen 80;\n  server_name your_domain.com;\n\n  location \/ {\n    proxy_pass http:\/\/localhost:3000;\n    proxy_set_header Host $host;\n    proxy_set_header X-Real-IP $remote_addr;\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\n<p>\u542f\u52a8PM2\u5e94\u7528\u7a0b\u5e8f<br \/>\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8PM2\u7ba1\u7406\u7684\u5e94\u7528\u7a0b\u5e8f:<\/p>\n<pre>$pm2 start ecosystem.config.js<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u91cd\u65b0\u52a0\u8f7dNGINX\u914d\u7f6e<br \/>\u5728\u4fee\u6539\u5b8cNGINX\u7684\u914d\u7f6e\u6587\u4ef6\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6:<\/p>\n<pre>$sudo service nginx reload<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p>\u7ed3\u8bed:<br \/>\u901a\u8fc7\u5408\u7406\u7684\u914d\u7f6e\u548c\u4f18\u5316NGINX\u548cPM2\uff0c\u6211\u4eec\u53ef\u4ee5\u4f18\u5316VPS\u670d\u52a1\u5668\u7684\u8d44\u6e90\u7ba1\u7406\uff0c\u63d0\u5347\u670d\u52a1\u5668\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5b89\u88c5\u548c\u914d\u7f6eNGINX\u548cPM2\uff0c\u5e76\u7b80\u5355\u4ecb\u7ecd\u4e86\u5982\u4f55\u7ed3\u5408\u4f7f\u7528\u5b83\u4eec\u6765\u4f18\u5316VPS\u670d\u52a1\u5668\u7684\u8d44\u6e90\u7ba1\u7406\u3002\u5e0c\u671b\u8fd9\u7bc7\u7b80\u660e\u6559\u7a0b\u5bf9\u4e8e\u4f60\u4f18\u5316\u670d\u52a1\u5668\u8d44\u6e90\u7ba1\u7406\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b:<br \/>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Express\u5e94\u7528\u7a0b\u5e8f\u7684\u542f\u52a8\u811a\u672capp.js:<\/p>\n<pre>const express = require(\"express\");\nconst app = express();\n\napp.get(\"\/\", (req, res) =&amp;gt; {\n  res.send(\"Hello, World!\");\n});\n\napp.listen(3000, () =&amp;gt; {\n  console.log(\"Server is listening on port 3000\");\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6ce8\u610f\uff1a\u4ee5\u4e0a\u4ee3\u7801\u53ea\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5b9e\u9645\u4e0a\u4f60\u53ef\u80fd\u9700\u8981\u6839\u636e\u4f60\u81ea\u5df1\u7684\u9879\u76ee\u548c\u9700\u6c42\u8fdb\u884c\u76f8\u5e94\u7684\u4fee\u6539\u548c\u914d\u7f6e\u3002<\/p>\n<p>\u53c2\u8003\u94fe\u63a5\uff1a<\/p>\n<ul>\n<li>NGINX\u5b98\u65b9\u6587\u6863: https:\/\/nginx.org\/en\/docs\/<\/li>\n<li>PM2\u5b98\u65b9\u6587\u6863: https:\/\/pm2.keymetrics.io\/docs\/<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u7b80\u660e\u6559\u7a0b: \u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u4f18\u5316VPS\u670d\u52a1\u5668\u7684\u8d44\u6e90\u7ba1\u7406\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u660e\u6559\u7a0b: \u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u4f18\u5316VPS\u670d\u52a1\u5668\u7684\u8d44\u6e90\u7ba1\u7406 \u524d\u8a00:\u5728\u73b0\u4ee3\u4e92\u8054\u7f51\u7684\u65f6\u4ee3\uff0c\u968f\u7740\u7f51\u7ad9\u548c\u5e94\u7528\u7a0b\u5e8f\u7684\u6d41\u91cf\u8d8a\u6765\u8d8a\u5927\uff0c\u670d\u52a1\u5668\u8d44\u6e90\u7ba1\u7406\u53d8\u5f97\u81f3\u5173\u91cd\u8981\u3002\u4e3a\u4e86\u63d0\u9ad8\u670d\u52a1\u5668\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\uff0c\u5408\u7406\u5730\u914d\u7f6e\u548c\u4f18\u5316\u670d\u52a1\u5668\u8d44\u6e90\u662f\u975e\u5e38\u5fc5\u8981\u7684\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u6765\u4f18\u5316VPS\u670d\u52a1\u5668\u7684\u8d44\u6e90\u7ba1\u7406\uff0c\u4ece\u800c\u63d0\u5347\u670d\u52a1\u5668\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002 \u4e00\u3001NGINX\u7684\u5b89\u88c5\u548c\u914d\u7f6e \u5b89\u88c5NGINX\u5728Ubuntu\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4\u6765\u5b89\u88c5NGINX: $sudo apt-get update $sudo apt-get install nginx \u767b\u5f55\u540e\u590d\u5236 \u914d\u7f6eNGINX\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cNGINX\u7684\u914d\u7f6e\u6587\u4ef6\u662f\u5b58\u653e\u5728\/etc\/\u76ee\u5f55\u4e0b\u7684nginx.conf\u6587\u4ef6\u3002\u5728\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u914d\u7f6e\u76d1\u542c\u7684\u7aef\u53e3\uff0c\u865a\u62df\u4e3b\u673a\u7b49\u3002\u5177\u4f53\u53ef\u4ee5\u53c2\u8003NGINX\u7684\u5b98\u65b9\u6587\u6863\u3002 \u4e8c\u3001PM2\u7684\u5b89\u88c5\u548c\u914d\u7f6e \u5b89\u88c5PM2\u53ef\u4ee5\u901a\u8fc7npm\u6765\u5168\u5c40\u5b89\u88c5PM2: $sudo npm install -g pm2 \u767b\u5f55\u540e\u590d\u5236 \u914d\u7f6ePM2\u5728\u4f7f\u7528PM2\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u4e4b\u524d\uff0c\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a\u542f\u52a8\u811a\u672c\u3002\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aecosystem.config.js\u6587\u4ef6\uff0c\u5e76\u5c06\u4ee5\u4e0b\u5185\u5bb9\u590d\u5236\u5230\u6587\u4ef6\u4e2d: module.exports = { apps: [ { name: &#8220;app&#8221;, script: &#8220;app.js&#8221;, instances: &#8220;max&#8221;, autorestart: true, watch: true, ignore_watch: [&#8220;node_modules&#8221;, &#8220;logs&#8221;], exec_mode: &#8220;cluster&#8221; } ] }; \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0cname\u662f\u5e94\u7528\u7a0b\u5e8f\u7684\u540d\u79f0\uff0cscript\u662f\u542f\u52a8\u811a\u672c\u7684\u8def\u5f84\uff0cinstances\u662f\u8fdb\u7a0b\u7684\u6570\u91cf\uff0cautorestart\u662f\u662f\u5426\u81ea\u52a8\u91cd\u542f\uff0cwatch\u662f\u662f\u5426\u76d1\u542c\u6587\u4ef6\u7684\u53d8\u5316\u81ea\u52a8\u91cd\u542f\uff0cignore_watch\u662f\u5ffd\u7565\u76d1\u542c\u7684\u76ee\u5f55\uff0cexec_mode\u662f\u8fdb\u7a0b\u7684\u6267\u884c\u6a21\u5f0f\u3002 \u4e09\u3001NGINX\u548cPM2\u7684\u7ed3\u5408\u4f7f\u7528 \u914d\u7f6e\u53cd\u5411\u4ee3\u7406\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u53cd\u5411\u4ee3\u7406\u6765\u4f18\u5316\u8bf7\u6c42\u7684\u8d1f\u8f7d\u5747\u8861\u548c\u6d41\u91cf\u7ba1\u7406\u3002\u6211\u4eec\u53ef\u4ee5\u5728NGINX\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u53cd\u5411\u4ee3\u7406\uff0c\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9PM2\u7ba1\u7406\u7684\u5e94\u7528\u7a0b\u5e8f\u3002 \u5728NGINX\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff0c\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u672c\u5730\u76843000\u7aef\u53e3: server { listen [&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-62616","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/62616","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=62616"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/62616\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=62616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=62616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=62616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}