{"id":17148,"date":"2024-11-18T16:24:34","date_gmt":"2024-11-18T08:24:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/17148\/"},"modified":"2024-11-18T16:24:34","modified_gmt":"2024-11-18T08:24:34","slug":"nginx-pm2-vps-%e6%9e%84%e5%bb%ba%e9%ab%98%e5%8f%af%e7%94%a8%e5%92%8c%e5%8f%af%e6%89%a9%e5%b1%95%e7%9a%84%e5%ba%94%e7%94%a8%e6%9c%8d%e5%8a%a1%e6%9e%b6%e6%9e%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/17148\/","title":{"rendered":"NGINX PM2 VPS: \u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169577338424448.jpg\" class=\"aligncenter\" title=\"NGINX PM2 VPS: \u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\u63d2\u56fe\" alt=\"NGINX PM2 VPS: \u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\u63d2\u56fe\" \/><\/p>\n<p>NGINX PM2 VPS: \u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784<\/p>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\u53d8\u5f97\u8d8a\u6765\u8d8a\u91cd\u8981\u3002NGINX\u3001PM2\u548cVPS\u662f\u5e38\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NGINX\u3001PM2\u548cVPS\u6765\u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p><strong>1. NGINX\uff1a\u9ad8\u6027\u80fd\u7684\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668<\/strong><\/p>\n<p>NGINX\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u7528\u4e8e\u8d1f\u8f7d\u5747\u8861\u3001\u7f13\u5b58\u9759\u6001\u8d44\u6e90\u3001SSL\u7ec8\u7aef\u7b49\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684NGINX\u914d\u7f6e\u793a\u4f8b\uff0c\u7528\u4e8e\u5c06\u6240\u6709\u6d41\u91cf\u8f6c\u53d1\u5230\u540e\u7aef\u5e94\u7528\u670d\u52a1\u5668\uff1a<\/p>\n<pre>http {\n    upstream backend {\n        server backend1.example.com;\n        server backend2.example.com;\n    }\n\n    server {\n        listen 80;\n        server_name example.com;\n\n        location \/ {\n            proxy_pass http:\/\/backend;\n        }\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u914d\u7f6e\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3abackend\u7684\u540e\u7aef\u670d\u52a1\u5668\u7ec4\uff0c\u5305\u542b\u4e86\u591a\u4e2a\u540e\u7aef\u5e94\u7528\u670d\u52a1\u5668\u3002proxy_pass\u6307\u4ee4\u5c06\u6d41\u91cf\u8f6c\u53d1\u5230\u540e\u7aef\u670d\u52a1\u5668\u7ec4\u3002<\/p>\n<p><strong>2. PM2\uff1a\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177<\/strong><\/p>\n<p>PM2\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u548c\u76d1\u63a7Node.js\u5e94\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684PM2\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre>{\n    \"apps\": [{\n        \"name\": \"my-app\",\n        \"script\": \"app.js\",\n        \"instances\": \"max\",\n        \"exec_mode\": \"cluster\",\n        \"error_file\": \"logs\/error.log\",\n        \"out_file\": \"logs\/out.log\",\n        \"log_date_format\": \"YYYY-MM-DD HH:mm:ss\",\n        \"env\": {\n            \"NODE_ENV\": \"production\"\n        }\n    }]\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u914d\u7f6e\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3amy-app\u7684\u5e94\u7528\uff0c\u5e76\u6307\u5b9a\u4e86\u5e94\u7528\u7684\u4e3b\u6587\u4ef6app.js\u3002instances\u6307\u5b9a\u4e86\u5e94\u7528\u7684\u5b9e\u4f8b\u6570\uff0cexec_mode\u6307\u5b9a\u4e86\u5e94\u7528\u7684\u6267\u884c\u6a21\u5f0f\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u914d\u7f6e\u5e94\u7528\u7684\u65e5\u5fd7\u6587\u4ef6\u3001\u73af\u5883\u53d8\u91cf\u7b49\u3002<\/p>\n<p><strong>3. VPS\uff1a\u865a\u62df\u4e13\u7528\u670d\u52a1\u5668<\/strong><\/p>\n<p>VPS\u662f\u4e00\u79cd\u865a\u62df\u5316\u6280\u672f\uff0c\u53ef\u4ee5\u5c06\u4e00\u53f0\u7269\u7406\u670d\u52a1\u5668\u5212\u5206\u4e3a\u591a\u4e2a\u865a\u62df\u4e13\u7528\u670d\u52a1\u5668\u3002VPS\u63d0\u4f9b\u4e86\u72ec\u7acb\u7684\u8d44\u6e90\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5e94\u7528\u7684\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684VPS\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre>{\n    \"name\": \"my-vps\",\n    \"provider\": \"digitalocean\",\n    \"region\": \"nyc3\",\n    \"size\": \"s-1vcpu-1gb\",\n    \"ssh_keys\": [\"my-public-key\"],\n    \"backups\": false\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u914d\u7f6e\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3amy-vps\u7684VPS\uff0c\u5e76\u6307\u5b9a\u4e86VPS\u7684\u63d0\u4f9b\u5546\u3001\u533a\u57df\u3001\u5927\u5c0f\u3001SSH\u5bc6\u94a5\u3001\u5907\u4efd\u7b49\u914d\u7f6e\u3002<\/p>\n<p><strong>4. \u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784<\/strong><\/p>\n<p>\u8981\u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06NGINX\u3001PM2\u548cVPS\u7ed3\u5408\u8d77\u6765\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\u793a\u610f\u56fe\uff1a<\/p>\n<pre>          +---------+\n          |   NGINX |\n          +---------+\n               |\n          +---------+\n          |   PM2   |\n          +---------+\n               |\n          +---------+\n          |   VPS   |\n          +---------+\n               |\n          +---------+\n          |   APP   |\n          +---------+<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u67b6\u6784\u4e2d\uff0cNGINX\u8d1f\u8d23\u63a5\u6536\u5916\u90e8\u8bf7\u6c42\uff0c\u5e76\u5c06\u6d41\u91cf\u8f6c\u53d1\u5230\u540e\u7aef\u7684PM2\u96c6\u7fa4\u3002PM2\u8d1f\u8d23\u7ba1\u7406\u548c\u76d1\u63a7Node.js\u5e94\u7528\u7684\u8fdb\u7a0b\u3002VPS\u63d0\u4f9b\u4e86\u72ec\u7acb\u7684\u8d44\u6e90\uff0c\u4fdd\u8bc1\u4e86\u5e94\u7528\u7684\u53ef\u7528\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<p><strong>5. \u5177\u4f53\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528NGINX\u3001PM2\u548cVPS\u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728VPS\u4e0a\u5b89\u88c5NGINX\u548cPM2\uff0c\u5e76\u914d\u7f6eNGINX\u7684\u53cd\u5411\u4ee3\u7406\uff1a<\/p>\n<pre># \u5b89\u88c5 NGINX\nsudo apt-get install nginx\n\n# \u5b89\u88c5 PM2\nsudo npm install -g pm2\n\n# \u521b\u5efa NGINX \u914d\u7f6e\u6587\u4ef6\nsudo nano \/etc\/nginx\/conf.d\/my-app.conf<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728NGINX\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>upstream backend {\n    server 127.0.0.1:3000;\n    server 127.0.0.1:3001;\n}\n\nserver {\n    listen 80;\n    server_name my-app.com;\n\n    location \/ {\n        proxy_pass http:\/\/backend;\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u3002\u7136\u540e\uff0c\u5c06Node.js\u5e94\u7528\u90e8\u7f72\u5230VPS\u4e0a\uff0c\u5e76\u4f7f\u7528PM2\u7ba1\u7406\u5e94\u7528\u7684\u8fdb\u7a0b\uff1a<\/p>\n<pre># \u5728 VPS \u4e0a\u514b\u9686\u5e94\u7528\u4ee3\u7801\ngit clone https:\/\/github.com\/my-app.git\n\n# \u8fdb\u5165\u5e94\u7528\u76ee\u5f55\ncd my-app\n\n# \u5b89\u88c5\u5e94\u7528\u4f9d\u8d56\nnpm install\n\n# \u542f\u52a8\u5e94\u7528\npm2 start app.js -i max<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4fdd\u5b58\u5e76\u9000\u51fa\u6587\u4ef6\u3002\u6700\u540e\uff0c\u91cd\u542fNGINX\u670d\u52a1\u5668\uff1a<\/p>\n<pre>sudo systemctl restart nginx<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u6210\u529f\u5730\u6784\u5efa\u4e86\u4e00\u4e2a\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\uff0c\u53ef\u4ee5\u901a\u8fc7NGINX\u7684\u57df\u540d\u8bbf\u95ee\u6211\u4eec\u7684\u5e94\u7528\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528NGINX\u3001PM2\u548cVPS\u6765\u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\u3002\u901a\u8fc7\u5408\u7406\u914d\u7f6e\u548c\u7ba1\u7406\u8fd9\u4e9b\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7684\u53ef\u7528\u6027\u548c\u6027\u80fd\uff0c\u5e76\u5b9e\u73b0\u5feb\u901f\u6269\u5c55\u3002\u5e0c\u671b\u8fd9\u4e9b\u4ee3\u7801\u793a\u4f8b\u80fd\u591f\u5bf9\u8bfb\u8005\u6784\u5efa\u81ea\u5df1\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fNGINX PM2 VPS: \u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\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: \u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784 \u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\u53d8\u5f97\u8d8a\u6765\u8d8a\u91cd\u8981\u3002NGINX\u3001PM2\u548cVPS\u662f\u5e38\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NGINX\u3001PM2\u548cVPS\u6765\u6784\u5efa\u9ad8\u53ef\u7528\u548c\u53ef\u6269\u5c55\u7684\u5e94\u7528\u670d\u52a1\u67b6\u6784\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 1. NGINX\uff1a\u9ad8\u6027\u80fd\u7684\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668 NGINX\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\uff0c\u53ef\u4ee5\u7528\u4e8e\u8d1f\u8f7d\u5747\u8861\u3001\u7f13\u5b58\u9759\u6001\u8d44\u6e90\u3001SSL\u7ec8\u7aef\u7b49\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684NGINX\u914d\u7f6e\u793a\u4f8b\uff0c\u7528\u4e8e\u5c06\u6240\u6709\u6d41\u91cf\u8f6c\u53d1\u5230\u540e\u7aef\u5e94\u7528\u670d\u52a1\u5668\uff1a http { upstream backend { server backend1.example.com; server backend2.example.com; } server { listen 80; server_name example.com; location \/ { proxy_pass http:\/\/backend; } } } \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u8ff0\u914d\u7f6e\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3abackend\u7684\u540e\u7aef\u670d\u52a1\u5668\u7ec4\uff0c\u5305\u542b\u4e86\u591a\u4e2a\u540e\u7aef\u5e94\u7528\u670d\u52a1\u5668\u3002proxy_pass\u6307\u4ee4\u5c06\u6d41\u91cf\u8f6c\u53d1\u5230\u540e\u7aef\u670d\u52a1\u5668\u7ec4\u3002 2. PM2\uff1a\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177 PM2\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u548c\u76d1\u63a7Node.js\u5e94\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684PM2\u914d\u7f6e\u793a\u4f8b\uff1a { &#8220;apps&#8221;: [{ &#8220;name&#8221;: &#8220;my-app&#8221;, &#8220;script&#8221;: &#8220;app.js&#8221;, &#8220;instances&#8221;: &#8220;max&#8221;, &#8220;exec_mode&#8221;: &#8220;cluster&#8221;, &#8220;error_file&#8221;: &#8220;logs\/error.log&#8221;, &#8220;out_file&#8221;: &#8220;logs\/out.log&#8221;, &#8220;log_date_format&#8221;: &#8220;YYYY-MM-DD HH:mm:ss&#8221;, [&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-17148","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17148","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=17148"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17148\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=17148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=17148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=17148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}