NGINX PM2 VPS: ¹¹½¨¸ß¿ÉÓúͿÉÀ©Õ¹µÄÓ¦ÓÃЧÀͼܹ¹
NGINX PM2 VPS: ¹¹½¨¸ß¿ÉÓúͿÉÀ©Õ¹µÄÓ¦ÓÃЧÀͼܹ¹
Ëæ×Å»¥ÁªÍøÊÖÒյĿìËÙÉú³¤£¬¹¹½¨¸ß¿ÉÓúͿÉÀ©Õ¹µÄÓ¦ÓÃЧÀͼܹ¹±äµÃÔ½À´Ô½Ö÷Òª¡£NGINX¡¢PM2ºÍVPSÊdz£ÓõŤ¾ß£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖÕâһĿµÄ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNGINX¡¢PM2ºÍVPSÀ´¹¹½¨¸ß¿ÉÓúͿÉÀ©Õ¹µÄÓ¦ÓÃЧÀͼܹ¹£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
1. NGINX£º¸ßÐÔÄܵķ´ÏòÊðÀíЧÀÍÆ÷
NGINXÊÇÒ»¸ö¸ßÐÔÄܵķ´ÏòÊðÀíЧÀÍÆ÷£¬¿ÉÒÔÓÃÓÚ¸ºÔØƽºâ¡¢»º´æ¾²Ì¬×ÊÔ´¡¢SSLÖն˵ȹ¦Ð§¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄNGINXÉèÖÃʾÀý£¬ÓÃÓÚ½«ËùÓÐÁ÷Á¿×ª·¢µ½ºó¶ËÓ¦ÓÃЧÀÍÆ÷£º
http { upstream backend { server backend1.example.com; server backend2.example.com; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÉèÖÃÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪbackendµÄºó¶ËЧÀÍÆ÷×飬°üÀ¨Á˶à¸öºó¶ËÓ¦ÓÃЧÀÍÆ÷¡£proxy_passÖ¸ÁÁ÷Á¿×ª·¢µ½ºó¶ËЧÀÍÆ÷×é¡£
2. PM2£ºÀú³ÌÖÎÀí¹¤¾ß
PM2ÊÇÒ»¸öÇ¿Ê¢µÄÀú³ÌÖÎÀí¹¤¾ß£¬¿ÉÒÔ×ÊÖúÎÒÃÇÖÎÀíºÍ¼à¿ØNode.jsÓ¦Óá£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄPM2ÉèÖÃʾÀý£º
{ "apps": [{ "name": "my-app", "script": "app.js", "instances": "max", "exec_mode": "cluster", "error_file": "logs/error.log", "out_file": "logs/out.log", "log_date_format": "YYYY-MM-DD HH:mm:ss", "env": { "NODE_ENV": "production" } }] }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÉèÖÃÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪmy-appµÄÓ¦Ó㬲¢Ö¸¶¨ÁËÓ¦ÓõÄÖ÷Îļþapp.js¡£instancesÖ¸¶¨ÁËÓ¦ÓõÄʵÀýÊý£¬exec_modeÖ¸¶¨ÁËÓ¦ÓõÄÖ´ÐÐģʽ¡£±ðµÄ£¬ÎÒÃÇ»¹¿ÉÒÔÉèÖÃÓ¦ÓõÄÈÕÖ¾Îļþ¡¢ÇéÐαäÁ¿µÈ¡£
3. VPS£ºÐéÄâרÓÃЧÀÍÆ÷
VPSÊÇÒ»ÖÖÐéÄ⻯ÊÖÒÕ£¬¿ÉÒÔ½«Ò»Ì¨ÎïÀíЧÀÍÆ÷»®·ÖΪ¶à¸öÐéÄâרÓÃЧÀÍÆ÷¡£VPSÌṩÁË×ÔÁ¦µÄ×ÊÔ´£¬¿ÉÒÔÖª×ãÓ¦ÓõÄÐèÇó¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄVPSÉèÖÃʾÀý£º
{ "name": "my-vps", "provider": "digitalocean", "region": "nyc3", "size": "s-1vcpu-1gb", "ssh_keys": ["my-public-key"], "backups": false }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöÉèÖÃÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪmy-vpsµÄVPS£¬²¢Ö¸¶¨ÁËVPSµÄÌṩÉÌ¡¢ÇøÓò¡¢¾Þϸ¡¢SSHÃÜÔ¿¡¢±¸·ÝµÈÉèÖá£
4. ¹¹½¨¸ß¿ÉÓúͿÉÀ©Õ¹µÄÓ¦ÓÃЧÀͼܹ¹
Òª¹¹½¨¸ß¿ÉÓúͿÉÀ©Õ¹µÄÓ¦ÓÃЧÀͼܹ¹£¬ÎÒÃÇ¿ÉÒÔ½«NGINX¡¢PM2ºÍVPSÍŽáÆðÀ´¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÓ¦ÓÃЧÀͼܹ¹Ê¾Òâͼ£º
+---------+ | NGINX | +---------+ | +---------+ | PM2 | +---------+ | +---------+ | VPS | +---------+ | +---------+ | APP | +---------+
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö¼Ü¹¹ÖУ¬NGINXÈÏÕæÎüÊÕÍⲿÇëÇ󣬲¢½«Á÷Á¿×ª·¢µ½ºó¶ËµÄPM2¼¯Èº¡£PM2ÈÏÕæÖÎÀíºÍ¼à¿ØNode.jsÓ¦ÓõÄÀú³Ì¡£VPSÌṩÁË×ÔÁ¦µÄ×ÊÔ´£¬°ü¹ÜÁËÓ¦ÓõĿÉÓÃÐԺͿÉÀ©Õ¹ÐÔ¡£
5. Ïêϸ´úÂëʾÀý
ÏÂÃæÊÇÒ»¸öÏêϸµÄ´úÂëʾÀý£¬ÑÝʾÁËÔõÑùʹÓÃNGINX¡¢PM2ºÍVPS¹¹½¨¸ß¿ÉÓúͿÉÀ©Õ¹µÄÓ¦ÓÃЧÀͼܹ¹¡£
Ê×ÏÈ£¬ÔÚVPSÉÏ×°ÖÃNGINXºÍPM2£¬²¢ÉèÖÃNGINXµÄ·´ÏòÊðÀí£º
# ×°Öà NGINX sudo apt-get install nginx # ×°Öà PM2 sudo npm install -g pm2 # ½¨Éè NGINX ÉèÖÃÎļþ sudo nano /etc/nginx/conf.d/my-app.conf
µÇ¼ºó¸´ÖÆ
ÔÚNGINXÉèÖÃÎļþÖУ¬Ìí¼ÓÒÔÏÂÄÚÈÝ£º
upstream backend { server 127.0.0.1:3000; server 127.0.0.1:3001; } server { listen 80; server_name my-app.com; location / { proxy_pass http://backend; } }
µÇ¼ºó¸´ÖÆ
ÉúÑIJ¢Í˳öÎļþ¡£È»ºó£¬½«Node.jsÓ¦Óð²Åŵ½VPSÉÏ£¬²¢Ê¹ÓÃPM2ÖÎÀíÓ¦ÓõÄÀú³Ì£º
# ÔÚ VPS ÉÏ¿Ë¡ӦÓôúÂë git clone https://github.com/my-app.git # ½øÈëÓ¦ÓÃĿ¼ cd my-app # ×°ÖÃÓ¦ÓÃÒÀÀµ npm install # Æô¶¯Ó¦Óà pm2 start app.js -i max
µÇ¼ºó¸´ÖÆ
ÉúÑIJ¢Í˳öÎļþ¡£×îºó£¬ÖØÆôNGINXЧÀÍÆ÷£º
sudo systemctl restart nginx
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬ÎÒÃÇÒѾÀֳɵع¹½¨ÁËÒ»¸ö¸ß¿ÉÓúͿÉÀ©Õ¹µÄÓ¦ÓÃЧÀͼܹ¹£¬¿ÉÒÔͨ¹ýNGINXµÄÓòÃû»á¼ûÎÒÃǵÄÓ¦Óá£
½áÂÛ
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃNGINX¡¢PM2ºÍVPSÀ´¹¹½¨¸ß¿ÉÓúͿÉÀ©Õ¹µÄÓ¦ÓÃЧÀͼܹ¹¡£Í¨¹ýºÏÀíÉèÖúÍÖÎÀíÕâЩ¹¤¾ß£¬ÎÒÃÇ¿ÉÒÔÌá¸ßÓ¦ÓõĿÉÓÃÐÔºÍÐÔÄÜ£¬²¢ÊµÏÖ¿ìËÙÀ©Õ¹¡£Ï£ÍûÕâЩ´úÂëʾÀýÄܹ»¶Ô¶ÁÕß¹¹½¨×Ô¼ºµÄÓ¦ÓÃЧÀͼܹ¹ÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇNGINX PM2 VPS: ¹¹½¨¸ß¿ÉÓúͿÉÀ©Õ¹µÄÓ¦ÓÃЧÀͼܹ¹µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡