¼òÃ÷½Ì³Ì: ÔõÑùʹÓÃNGINXºÍPM2ÓÅ»¯VPSЧÀÍÆ÷µÄ×ÊÔ´ÖÎÀí
¼òÃ÷½Ì³Ì: ÔõÑùʹÓÃNGINXºÍPM2ÓÅ»¯VPSЧÀÍÆ÷µÄ×ÊÔ´ÖÎÀí
Ç°ÑÔ:
ÔÚÏÖ´ú»¥ÁªÍøµÄʱ´ú£¬Ëæ×ÅÍøÕ¾ºÍÓ¦ÓóÌÐòµÄÁ÷Á¿Ô½À´Ô½´ó£¬Ð§ÀÍÆ÷×ÊÔ´ÖÎÀí±äµÃÖÁ¹ØÖ÷Òª¡£ÎªÁËÌá¸ßЧÀÍÆ÷µÄÐÔÄܺͿɿ¿ÐÔ£¬ºÏÀíµØÉèÖúÍÓÅ»¯Ð§ÀÍÆ÷×ÊÔ´ÊǺÜÊÇÐëÒªµÄ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNGINXºÍPM2À´ÓÅ»¯VPSЧÀÍÆ÷µÄ×ÊÔ´ÖÎÀí£¬´Ó¶øÌáÉýЧÀÍÆ÷µÄÐÔÄܺͿɿ¿ÐÔ¡£
Ò»¡¢NGINXµÄ×°ÖúÍÉèÖÃ
×°ÖÃNGINX
ÔÚUbuntuÉÏ£¬¿ÉÒÔͨ¹ýÏÂÃæµÄÏÂÁîÀ´×°ÖÃNGINX:
$sudo apt-get update $sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
ÉèÖÃNGINX
ĬÈÏÇéÐÎÏ£¬NGINXµÄÉèÖÃÎļþÊÇ´æ·ÅÔÚ/etc/nginxĿ¼ÏµÄnginx.confÎļþ¡£ÔÚÕâ¸öÉèÖÃÎļþÖУ¬Äã¿ÉÒÔÉèÖüàÌýµÄ¶Ë¿Ú£¬ÐéÄâÖ÷»úµÈ¡£Ïêϸ¿ÉÒԲο¼NGINXµÄ¹Ù·½Îĵµ¡£
¶þ¡¢PM2µÄ×°ÖúÍÉèÖÃ
×°ÖÃPM2
¿ÉÒÔͨ¹ýnpmÀ´È«¾Ö×°ÖÃPM2:
$sudo npm install -g pm2
µÇ¼ºó¸´ÖÆ
ÉèÖÃPM2
ÔÚʹÓÃPM2ÖÎÀíÓ¦ÓóÌÐò֮ǰ£¬ÐèÒªÏȽ¨ÉèÒ»¸öÆô¶¯¾ç±¾¡£ÔÚÏîÄ¿µÄ¸ùĿ¼Ï½¨ÉèÒ»¸öecosystem.config.jsÎļþ£¬²¢½«ÒÔÏÂÄÚÈݸ´ÖƵ½ÎļþÖÐ:
module.exports = { apps: [ { name: "app", script: "app.js", instances: "max", autorestart: true, watch: true, ignore_watch: ["node_modules", "logs"], exec_mode: "cluster" } ] };
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÉèÖÃÎļþÖУ¬nameÊÇÓ¦ÓóÌÐòµÄÃû³Æ£¬scriptÊÇÆô¶¯¾ç±¾µÄ·¾¶£¬instancesÊÇÀú³ÌµÄÊýÄ¿£¬autorestartÊÇÊÇ·ñ×Ô¶¯ÖØÆô£¬watchÊÇÊÇ·ñ¼àÌýÎļþµÄת±ä×Ô¶¯ÖØÆô£¬ignore_watchÊǺöÂÔ¼àÌýµÄĿ¼£¬exec_modeÊÇÀú³ÌµÄÖ´ÐÐģʽ¡£
Èý¡¢NGINXºÍPM2µÄÍŽáʹÓÃ
ÉèÖ÷´ÏòÊðÀí
¿ÉÒÔͨ¹ýÉèÖ÷´ÏòÊðÀíÀ´ÓÅ»¯ÇëÇóµÄ¸ºÔØƽºâºÍÁ÷Á¿ÖÎÀí¡£ÎÒÃÇ¿ÉÒÔÔÚNGINXµÄÉèÖÃÎļþÖÐÉèÖ÷´ÏòÊðÀí£¬½«ÇëÇóת·¢¸øPM2ÖÎÀíµÄÓ¦ÓóÌÐò¡£
ÔÚNGINXµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏ´úÂ룬½«ÇëÇóת·¢¸øÍâµØµÄ3000¶Ë¿Ú:
server { listen 80; server_name your_domain.com; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
µÇ¼ºó¸´ÖÆ
Æô¶¯PM2Ó¦ÓóÌÐò
ÔÚÏîÄ¿µÄ¸ùĿ¼Ï£¬ÔËÐÐÒÔÏÂÏÂÁîÆô¶¯PM2ÖÎÀíµÄÓ¦ÓóÌÐò:
$pm2 start ecosystem.config.js
µÇ¼ºó¸´ÖÆ
ÖØмÓÔØNGINXÉèÖÃ
ÔÚÐÞ¸ÄÍêNGINXµÄÉèÖÃÎļþºó£¬Ê¹ÓÃÒÔÏÂÏÂÁîÖØмÓÔØÉèÖÃÎļþ:
$sudo service nginx reload
µÇ¼ºó¸´ÖÆ
½áÓï:
ͨ¹ýºÏÀíµÄÉèÖúÍÓÅ»¯NGINXºÍPM2£¬ÎÒÃÇ¿ÉÒÔÓÅ»¯VPSЧÀÍÆ÷µÄ×ÊÔ´ÖÎÀí£¬ÌáÉýЧÀÍÆ÷µÄÐÔÄܺͿɿ¿ÐÔ¡£±¾ÎÄÏÈÈÝÁËÔõÑù×°ÖúÍÉèÖÃNGINXºÍPM2£¬²¢¼òÆÓÏÈÈÝÁËÔõÑùÍŽáʹÓÃËüÃÇÀ´ÓÅ»¯VPSЧÀÍÆ÷µÄ×ÊÔ´ÖÎÀí¡£Ï£ÍûÕâƪ¼òÃ÷½Ì³Ì¹ØÓÚÄãÓÅ»¯Ð§ÀÍÆ÷×ÊÔ´ÖÎÀíÓÐËù×ÊÖú¡£
´úÂëʾÀý:
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄExpressÓ¦ÓóÌÐòµÄÆô¶¯¾ç±¾app.js:
const express = require("express"); const app = express(); app.get("/", (req, res) => { res.send("Hello, World!"); }); app.listen(3000, () => { console.log("Server is listening on port 3000"); });
µÇ¼ºó¸´ÖÆ
×¢ÖØ£ºÒÔÉÏ´úÂëÖ»ÊÇÒ»¸öʾÀý£¬ÏÖʵÉÏÄã¿ÉÄÜÐèҪƾ֤Äã×Ô¼ºµÄÏîÄ¿ºÍÐèÇó¾ÙÐÐÏìÓ¦µÄÐ޸ĺÍÉèÖá£
²Î¿¼Á´½Ó£º
NGINX¹Ù·½Îĵµ: https://nginx.org/en/docs/
PM2¹Ù·½Îĵµ: https://pm2.keymetrics.io/docs/
ÒÔÉϾÍÊǼòÃ÷½Ì³Ì: ÔõÑùʹÓÃNGINXºÍPM2ÓÅ»¯VPSЧÀÍÆ÷µÄ×ÊÔ´ÖÎÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡