NGINXºÍPM2: ÌáÉýVPSЧÀÍÆ÷µÄÐÔÄܺÍÎȹÌÐÔ
NGINXºÍPM2: ÌáÉýVPSЧÀÍÆ÷µÄÐÔÄܺÍÎȹÌÐÔ£¬ÐèÒªÏêϸ´úÂëʾÀý
СÐò£º
ÏÖ´ú»¥ÁªÍøʱ´ú£¬Ð§ÀÍÆ÷µÄÐÔÄܺÍÎȹÌÐÔ¹ØÓÚÍøÕ¾µÄÕý³£ÔËÐÐÖÁ¹ØÖ÷Òª¡£ÔÚ´¦Àí¸ß²¢·¢ÇëÇóʱ£¬Ð§ÀÍÆ÷µÄÐÔÄܺÍÎȹÌÐÔÖ±½ÓÓ°ÏìÓû§ÌåÑ顣ΪÁËÌáÉýЧÀÍÆ÷µÄÐÔÄܺÍÎȹÌÐÔ£¬NGINXºÍPM2ÊÇÁ½¸öºÜÊÇÓÐÓõŤ¾ß¡£NGINXÊÇÒ»¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷£¬PM2ÊÇÒ»¸öÀú³ÌÖÎÀí¹¤¾ß¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNGINXºÍPM2À´ÌáÉýVPSЧÀÍÆ÷µÄÐÔÄܺÍÎȹÌÐÔ£¬²¢¸ø³öһЩÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢×°ÖúÍÉèÖÃNGINX
×°ÖÃNGINX
ÔÚVPSЧÀÍÆ÷ÉÏ×°ÖÃNGINXºÜÊǼòÆÓ¡£Ê×ÏÈ£¬¸üÐÂЧÀÍÆ÷µÄÈí¼þ°üÁÐ±í£¬È»ºóʹÓÃapt-getÏÂÁî×°ÖÃNGINX¡£
sudo apt-get update sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
ÉèÖÃNGINX
×°ÖÃÍê³Éºó£¬ÐèÒª¶ÔNGINX¾ÙÐÐһЩ»ù±¾ÉèÖá£Ö÷Òª°üÀ¨ÐÞ¸ÄNGINXĬÈÏÉèÖÃÎļþºÍ½¨ÉèÐéÄâÖ÷»úÉèÖÃÎļþ¡£
ÐÞ¸ÄĬÈÏÉèÖÃÎļþ
ĬÈÏÉèÖÃÎļþλÓÚ/etc/nginx/nginx.conf¡£¿ÉÒÔʹÓÃÈκÎÎı¾±à¼Æ÷·¿ª²¢Ð޸ġ£Æ¾Ö¤Ð§ÀÍÆ÷Ó²¼þµÄÉèÖ㬿ÉÒÔÐÞ¸ÄһЩ²ÎÊý£¬Èçworker_processesºÍworker_connectionsµÈ£¬ÒÔÓÅ»¯NGINXÐÔÄÜ¡£
½¨ÉèÐéÄâÖ÷»úÉèÖÃÎļþ
ÐéÄâÖ÷»úÉèÖÃÎļþλÓÚ/etc/nginx/sites-available/Ŀ¼ÖС£¿ÉÒÔ½¨ÉèÒ»¸öеÄÉèÖÃÎļþÀ´ÎªÃ¿¸öÍøÕ¾½¨ÉèÒ»¸ö×ÔÁ¦µÄÐéÄâÖ÷»ú¡£ÒÔÏÂÊÇÒ»¸öʾÀýµÄÐéÄâÖ÷»úÉèÖÃÎļþ£º
server { listen 80; server_name example.com; 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; } }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÒÔÉÏʾÀýÉèÖÃÁËÒ»¸ö»ù±¾µÄ·´ÏòÊðÀíЧÀÍÆ÷£¬Î´À´×Ôexample.comµÄÇëÇóת·¢µ½ÍâµØµÄ¶Ë¿Ú3000ÉÏ¡£
Æô¶¯NGINX²¢ÑéÖ¤ÉèÖÃ
Íê³ÉÉèÖúó£¬ÐèÒªÆô¶¯NGINX£¬²¢Ê¹ÓÃä¯ÀÀÆ÷ÑéÖ¤ÉèÖÃÊÇ·ñÉúЧ¡£
sudo service nginx start
µÇ¼ºó¸´ÖÆ
·¿ªä¯ÀÀÆ÷£¬»á¼ûÉèÖõÄÓòÃû»òIPµØµã£¬ÈôÊÇÒ»ÇÐÕý³££¬½«»á¿´µ½NGINXµÄĬÈϽӴýÒ³Ãæ¡£
¶þ¡¢×°ÖúÍÉèÖÃPM2
×°ÖÃPM2
ʹÓÃnpmÈ«¾Ö×°ÖÃPM2ºÜÊǼòÆÓ¡£
sudo npm install -g pm2
µÇ¼ºó¸´ÖÆ
Æô¶¯ºÍÖÎÀíÓ¦ÓóÌÐò
ʹÓÃPM2Æô¶¯ºÍÖÎÀíÓ¦ÓóÌÐòºÜÊÇÀû±ã¡£ÔÚÏîÄ¿µÄ¸ùĿ¼Ï£¬Ê¹ÓÃÒÔÏÂÏÂÁîÆô¶¯Ó¦ÓóÌÐò£º
pm2 start app.js
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÄúÒ²¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¶ÔÓ¦ÓóÌÐò¾ÙÐÐÆäËû²Ù×÷£º
Éó²éÓ¦ÓóÌÐòÁбí
pm2 list
µÇ¼ºó¸´ÖÆ
×èÖ¹Ó¦ÓóÌÐò
pm2 stop app
µÇ¼ºó¸´ÖÆ
ÖØÐÂÆô¶¯Ó¦ÓóÌÐò
pm2 restart app
µÇ¼ºó¸´ÖÆ
Èý¡¢NGINXºÍPM2µÄÍŽáʹÓÃ
ÉèÖÃNGINX·´ÏòÊðÀí
ÔÚNGINXµÄÐéÄâÖ÷»úÉèÖÃÎļþÖУ¬½«HTTPÇëÇóת·¢ÖÁPM2ÔËÐеÄÓ¦ÓóÌÐò¡£
server { listen 80; server_name example.com; 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; } }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ʹÓÃPM2ÖÎÀíÓ¦ÓóÌÐò
ÔÚÏîÄ¿µÄ¸ùĿ¼Ï£¬Ê¹ÓÃPM2Æô¶¯Ó¦ÓóÌÐò¡£
pm2 start app.js
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÒÔÉϽ«Æô¶¯Ò»¸öNode.jsÓ¦ÓóÌÐò£¬¼àÌý3000¶Ë¿Ú¡£
²âÊÔºÍÑéÖ¤
ͨ¹ýÒÔÉÏÉèÖúÍÆô¶¯£¬NGINXÎüÊÕµ½µÄHTTPÇëÇ󽫻ᱻת·¢ÖÁPM2ÔËÐеÄÓ¦ÓóÌÐò¡£·¿ªä¯ÀÀÆ÷£¬»á¼ûÉèÖõÄÓòÃû»òIPµØµã£¬ÈôÊÇÒ»ÇÐÕý³££¬½«»á¿´µ½Ó¦ÓóÌÐòµÄÒ³Ãæ¡£
½áÂÛ£º
ͨ¹ýÍŽáʹÓÃNGINXºÍPM2£¬ÎÒÃÇ¿ÉÒÔÌáÉýVPSЧÀÍÆ÷µÄÐÔÄܺÍÎȹÌÐÔ¡£Ê¹ÓÃNGINX×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷¿ÉÒÔÌá¸ß²¢·¢ÇëÇóµÄ´¦ÀíÄÜÁ¦£¬Ê¹ÓÃPM2¿ÉÒÔÀû±ãµØÖÎÀíÓ¦ÓóÌÐòµÄÆô¶¯ºÍ¼à¿Ø¡£Ï£Íû±¾ÎĵĴúÂëʾÀý¹ØÓÚʹÓÃNGINXºÍPM2ÌáÉýЧÀÍÆ÷ÐÔÄܺÍÎȹÌÐԵĶÁÕßÃÇÓÐËù×ÊÖú¡£
×Ü×ÖÊý£º634×Ö
ÒÔÉϾÍÊÇNGINXºÍPM2: ÌáÉýVPSЧÀÍÆ÷µÄÐÔÄܺÍÎȹÌÐÔµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡