{"id":17162,"date":"2024-11-18T12:04:54","date_gmt":"2024-11-18T04:04:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/17162\/"},"modified":"2024-11-18T12:04:54","modified_gmt":"2024-11-18T04:04:54","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8nginx%e5%92%8cpm2%e9%85%8d%e7%bd%aevps%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/17162\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u914d\u7f6eVPS\u670d\u52a1\u5668"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169579047451779.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u914d\u7f6eVPS\u670d\u52a1\u5668\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u914d\u7f6eVPS\u670d\u52a1\u5668\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u914d\u7f6eVPS\u670d\u52a1\u5668<\/p>\n<p>\u5728\u642d\u5efaWeb\u670d\u52a1\u5668\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528NGINX\u548cPM2\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u914d\u7f6e\u65b9\u5f0f\u3002NGINX\u662f\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684Web\u670d\u52a1\u5668\uff0c\u5e38\u7528\u4e8e\u53cd\u5411\u4ee3\u7406\u548c\u8d1f\u8f7d\u5747\u8861\u3002\u800cPM2\u662f\u4e00\u4e2a\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u548c\u7ba1\u7406Node.js\u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u914d\u7f6eVPS\u670d\u52a1\u5668\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u7b2c\u4e00\u6b65\uff1a\u5b89\u88c5NGINX\u548cPM2<\/p>\n<p>\u9996\u5148\uff0c\u767b\u5f55\u5230VPS\u670d\u52a1\u5668\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5NGINX\u548cPM2\uff1a<\/p>\n<pre># \u5b89\u88c5NGINX\nsudo apt-get update\nsudo apt-get install nginx\n\n# \u5b89\u88c5Node.js\u548cPM2\ncurl -sL https:\/\/deb.nodesource.com\/setup_14.x | sudo -E bash -\nsudo apt-get install -y nodejs\nsudo npm install -g pm2<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7b2c\u4e8c\u6b65\uff1a\u914d\u7f6eNGINX<\/p>\n<p>\u914d\u7f6eNGINX\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5c06HTTP\u6d41\u91cf\u5bfc\u5411\u5230\u6b63\u786e\u7684\u7aef\u53e3\u548c\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u670d\u52a1\u5668\u4e0a\u521b\u5efa\u4e00\u4e2aNGINX\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre>sudo nano \/etc\/nginx\/sites-available\/default<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u6253\u5f00\u7684\u6587\u4ef6\u4e2d\uff0c\u5c06\u4ee5\u4e0b\u5185\u5bb9\u7c98\u8d34\u5230\u5176\u4e2d\uff1a<\/p>\n<pre>server {\n  listen 80;\n  server_name your_domain.com;\n\n  location \/ {\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  }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u914d\u7f6e\u4e2d\uff0c\u5c06&#8221;your_domain.com&#8221;\u66ff\u6362\u4e3a\u4f60\u7684\u57df\u540d\u6216\u670d\u52a1\u5668\u7684IP\u5730\u5740\uff0c\u5e76\u5c06&#8221;localhost:3000&#8243;\u66ff\u6362\u4e3a\u4f60\u5e94\u7528\u7a0b\u5e8f\u8fd0\u884c\u7684\u7aef\u53e3\u3002<\/p>\n<p>\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6\u540e\uff0c\u91cd\u65b0\u542f\u52a8NGINX\u670d\u52a1\uff1a<\/p>\n<pre>sudo service nginx restart<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7b2c\u4e09\u6b65\uff1a\u914d\u7f6ePM2<\/p>\n<p>\u4f7f\u7528PM2\u6765\u8fd0\u884c\u548c\u7ba1\u7406Node.js\u5e94\u7528\u7a0b\u5e8f\u975e\u5e38\u7b80\u5355\u3002\u5728\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u76ee\u5f55\u4e0b\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre>pm2 start app.js --name my-app<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u547d\u4ee4\u5c06\u542f\u52a8\u4e00\u4e2a\u540d\u4e3a&#8221;my-app&#8221;\u7684\u8fdb\u7a0b\uff0c\u5e76\u5c06\u5e94\u7528\u7a0b\u5e8f\u7684\u5165\u53e3\u6587\u4ef6\u8bbe\u4e3a&#8221;app.js&#8221;\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u9002\u5f53\u7684\u66ff\u6362\u3002<\/p>\n<p>\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u4f7f\u7528\u73af\u5883\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u6307\u5b9a\u73af\u5883\u53d8\u91cf\uff1a<\/p>\n<pre>pm2 start app.js --name my-app --env production<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u6307\u5b9a\u5de5\u4f5c\u76ee\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre>pm2 start app.js --name my-app --cwd \/path\/to\/app<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528PM2\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u67e5\u770b\u5f53\u524d\u8fd0\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre>pm2 list<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u91cd\u65b0\u542f\u52a8\u6216\u505c\u6b62\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>pm2 restart my-app\npm2 stop my-app<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6700\u540e\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u5728\u670d\u52a1\u5668\u91cd\u542f\u540e\u81ea\u52a8\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>pm2 startup<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>PM2\u5c06\u751f\u6210\u4e00\u4e2a\u547d\u4ee4\uff0c\u5e76\u5c06\u5176\u590d\u5236\u5230\u7ec8\u7aef\u4e2d\u8fd0\u884c\u3002\u6b64\u547d\u4ee4\u5c06\u5728\u670d\u52a1\u5668\u542f\u52a8\u65f6\u81ea\u52a8\u542f\u52a8PM2\u548c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u5df2\u6210\u529f\u4f7f\u7528NGINX\u548cPM2\u914d\u7f6e\u4e86VPS\u670d\u52a1\u5668\u3002NGINX\u5c06\u8d1f\u8d23\u5c06HTTP\u6d41\u91cf\u5bfc\u5411\u5230\u6b63\u786e\u7684\u7aef\u53e3\u548c\u5e94\u7528\u7a0b\u5e8f\uff0c\u800cPM2\u5c06\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u548c\u7ba1\u7406\u4f60\u7684Node.js\u5e94\u7528\u7a0b\u5e8f\u3002\u4f7f\u7528NGINX\u548cPM2\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u7684Web\u670d\u52a1\u5668\u3002<\/p>\n<p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u914d\u7f6eVPS\u670d\u52a1\u5668\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>\u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u914d\u7f6eVPS\u670d\u52a1\u5668 \u5728\u642d\u5efaWeb\u670d\u52a1\u5668\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528NGINX\u548cPM2\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u914d\u7f6e\u65b9\u5f0f\u3002NGINX\u662f\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684Web\u670d\u52a1\u5668\uff0c\u5e38\u7528\u4e8e\u53cd\u5411\u4ee3\u7406\u548c\u8d1f\u8f7d\u5747\u8861\u3002\u800cPM2\u662f\u4e00\u4e2a\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u548c\u7ba1\u7406Node.js\u5e94\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NGINX\u548cPM2\u914d\u7f6eVPS\u670d\u52a1\u5668\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u7b2c\u4e00\u6b65\uff1a\u5b89\u88c5NGINX\u548cPM2 \u9996\u5148\uff0c\u767b\u5f55\u5230VPS\u670d\u52a1\u5668\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5NGINX\u548cPM2\uff1a # \u5b89\u88c5NGINX sudo apt-get update sudo apt-get install nginx # \u5b89\u88c5Node.js\u548cPM2 curl -sL https:\/\/deb.nodesource.com\/setup_14.x | sudo -E bash &#8211; sudo apt-get install -y nodejs sudo npm install -g pm2 \u767b\u5f55\u540e\u590d\u5236 \u7b2c\u4e8c\u6b65\uff1a\u914d\u7f6eNGINX \u914d\u7f6eNGINX\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5c06HTTP\u6d41\u91cf\u5bfc\u5411\u5230\u6b63\u786e\u7684\u7aef\u53e3\u548c\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u670d\u52a1\u5668\u4e0a\u521b\u5efa\u4e00\u4e2aNGINX\u914d\u7f6e\u6587\u4ef6\uff1a sudo nano \/etc\/nginx\/sites-available\/default \u767b\u5f55\u540e\u590d\u5236 \u5728\u6253\u5f00\u7684\u6587\u4ef6\u4e2d\uff0c\u5c06\u4ee5\u4e0b\u5185\u5bb9\u7c98\u8d34\u5230\u5176\u4e2d\uff1a server { listen 80; server_name your_domain.com; location \/ { proxy_pass http:\/\/localhost:3000; proxy_http_version 1.1; proxy_set_header [&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-17162","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17162","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=17162"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/17162\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=17162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=17162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=17162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}