NGINX PM2 VPS: ÈÃÄãµÄÍøÕ¾¸ü¿ì¡¢¸ü¿É¿¿
NGINX PM2 VPS: ÈÃÄãµÄÍøÕ¾¸ü¿ì¡¢¸ü¿É¿¿£¬ÐèÒªÏêϸ´úÂëʾÀý
Ëæ×Å»¥ÁªÍøµÄѸÃÍÉú³¤£¬ÍøÕ¾µÄ»á¼ûÁ¿ºÍÓû§ÒªÇóÒ²Ô½À´Ô½¸ß¡£ÎªÁËÓ¦¶ÔÕâÒ»ÌôÕ½£¬Ðí¶àÍøÕ¾ÖÎÀíÔ±×îÏÈʹÓÃÐéÄâרÓÃЧÀÍÆ÷£¨Virtual Private Server£¬¼ò³ÆVPS£©À´Ìá¸ßÍøÕ¾µÄÐÔÄܺͿɿ¿ÐÔ¡£¶øÔÚVPSÖУ¬×éºÏʹÓÃNGINXºÍPM2ÊÇÒ»¸öºÜÊDz»´íµÄÑ¡Ôñ£¬¿ÉÒÔʹÄãµÄÍøÕ¾¸ü¿ì¡¢¸ü¿É¿¿¡£
NGINXÊÇÒ»¸ö¸ßÐÔÄܵÄHTTPºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬Ëü¿ÉÒÔ´¦Àí´ó×ڵIJ¢·¢ÅþÁ¬£¬²¢ÌṩÎÞаµÄÉèÖÃÑ¡Ïî¡£Ïà±È¹Å°åµÄApacheЧÀÍÆ÷£¬NGINXÔÚ´¦Àí¾²Ì¬ÄÚÈݺ͸߲¢·¢ÇëÇóʱԽ·¢¸ßЧ¡£Í¨¹ý½«¾²Ì¬ÎļþµÄ´¦Àí½»¸øNGINX£¬¶ø½«¶¯Ì¬ÇëÇóת·¢¸øºó¶ËÓ¦ÓÃЧÀÍÆ÷£¬¿ÉÒÔ´ó´óÌá¸ßÍøÕ¾µÄÏìÓ¦ËÙÂÊ¡£
PM2ÊÇÒ»¸öÏÖ´ú»¯µÄNode.jsÀú³ÌÖÎÀíÆ÷£¬¿ÉÒÔÔÚºǫ́ÔËÐжà¸öNode.jsÓ¦Ó㬲¢Í¨¹ý¼à¿ØºÍ×Ô¶¯ÖØÆô¹¦Ð§¼á³ÖÕâЩӦÓõÄÎȹÌÐÔ¡£PM2¿ÉÒÔͨ¹ýÉèÖöà¸öÓ¦ÓÃʵÀý¡¢¸ºÔØƽºâºÍÈÕÖ¾ÖÎÀíµÈ¹¦Ð§£¬È·±£ÄãµÄNode.jsÓ¦ÓÃÔڸ߸ºÔØÇéÐÎÏÂÒ²Äܼá³ÖÎȹÌÔËÐС£
ÔÚÉèÖÃNGINXºÍPM2µÄÀú³ÌÖУ¬ÓÐһЩҪº¦µÄ´úÂëʾÀý¿ÉÒÔ×ÊÖúÄã¸üºÃµØʵÏÖÕâÁ½¸ö¹¤¾ßµÄÐͬÊÂÇé¡£ÏÂÃæÊÇÒ»¸öÏêϸµÄʾÀý£º
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃNGINXºÍPM2¡£Äã¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÔÚÄãµÄVPSÉÏ×°ÖÃËüÃÇ£º
# ×°ÖÃNGINX sudo apt-get update sudo apt-get install nginx # ×°ÖÃPM2 sudo npm install -g pm2
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÎÒÃÇÐèÒªÉèÖÃNGINXÀ´´¦Àí¾²Ì¬Îļþ£¬²¢½«¶¯Ì¬ÇëÇóת·¢¸øPM2ÖÎÀíµÄNode.jsÓ¦Ó᣷¿ªNGINXµÄÉèÖÃÎļþ£¨Í¨³£Î»ÓÚ/etc/nginx/nginx.conf»ò/etc/nginx/sites-available/default£©£¬Ìí¼ÓÒÔÏ´úÂëʾÀý£º
# ÉèÖþ²Ì¬Îļþ·¾¶ location /static { alias /path/to/your/static/files; } # ת·¢¶¯Ì¬ÇëÇóµ½PM2ÖÎÀíµÄÓ¦Óà location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬/path/to/your/static/filesÓ¦Ì滻ΪÄãÏÖʵµÄ¾²Ì¬Îļþ·¾¶£¬localhost:3000Ó¦Ì滻ΪÄãµÄNode.jsÓ¦ÓüàÌýµÄµØµãºÍ¶Ë¿Ú¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÆô¶¯PM2²¢ÔÚºǫ́ÔËÐÐÄãµÄNode.jsÓ¦Óá£Ê×ÏÈ£¬½øÈëÄãµÄNode.jsÓ¦ÓÃËùÔÚµÄĿ¼£¬²¢Ê¹ÓÃÒÔÏÂÏÂÁîÆô¶¯Ó¦Óãº
pm2 start app.js
µÇ¼ºó¸´ÖÆ
Õ⽫»áÆô¶¯ÄãµÄNode.jsÓ¦Ó㬲¢ÓÉPM2À´ÖÎÀíËü¡£Äã¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÉó²éÓ¦ÓõÄÔËÐÐ״̬£º
pm2 list
µÇ¼ºó¸´ÖÆ
×îºó£¬ÎÒÃÇÐèÒªÖØÐÂÆô¶¯NGINX£¬Ê¹Æä¼ÓÔØÎÒÃǸոÕÐ޸ĵÄÉèÖá£Ê¹ÓÃÒÔÏÂÏÂÁîÖØÐÂÆô¶¯NGINX£º
sudo service nginx restart
µÇ¼ºó¸´ÖÆ
Íê³ÉÉÏÊö°ì·¨ºó£¬ÄãµÄÍøÕ¾½«½ÓÄÉNGINX×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷£¬²¢Í¨¹ýPM2ÖÎÀíµÄNode.jsÓ¦ÓÃÌṩ¶¯Ì¬ÄÚÈÝ¡£ÕâÑùÒ»À´£¬ÄãµÄÍøÕ¾½«Äܹ»¸ü¿ìµØÏìÓ¦ÇëÇ󣬲¢ÇÒÔڸ߸ºÔØÇéÐÎÏÂÈÔÈ»¼á³ÖÎȹÌÔËÐС£
×ÛÉÏËùÊö£¬NGINXºÍPM2µÄ×éºÏ¿ÉÒÔʹÄãµÄÍøÕ¾¸ü¿ì¡¢¸ü¿É¿¿¡£Í¨¹ýÏêϸµÄ´úÂëʾÀý£¬Äã¿ÉÒÔºÜÈÝÒ×µØÉèÖúÍʹÓÃËüÃÇ¡£Ï£ÍûÕâƪÎÄÕÂÄܹ»×ÊÖúÄãÔÚVPSÇéÐÎÖÐÓÅ»¯ÄãµÄÍøÕ¾ÐÔÄÜ£¬Ìṩ¸üºÃµÄÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇNGINX PM2 VPS: ÈÃÄãµÄÍøÕ¾¸ü¿ì¡¢¸ü¿É¿¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡