VPSЧÀÍÆ÷ÓÅ»¯: NGINXºÍPM2µÄ×î¼Ñʵ¼ù
VPSЧÀÍÆ÷ÓÅ»¯: NGINXºÍPM2µÄ×î¼Ñʵ¼ù£¬ÐèÒªÏêϸ´úÂëʾÀý
ͻ񻣼
ÔÚ¹¹½¨ºÍ°²ÅÅWebÓ¦ÓóÌÐòʱ£¬Ð§ÀÍÆ÷µÄÉèÖúÍÓÅ»¯ÊÎÑÝ×ÅÖÁ¹ØÖ÷ÒªµÄ½ÇÉ«¡£±¾ÎĽ«ÏÈÈÝÔõÑùͨ¹ýʹÓÃNGINXºÍPM2À´ÓÅ»¯VPSЧÀÍÆ÷µÄÐÔÄܺͿɿ¿ÐÔ¡£ÎÒÃǽ«ÏêϸÏÈÈÝNGINXºÍPM2µÄ×°ÖúÍÉèÖ㬲¢ÌṩÏêϸµÄ´úÂëʾÀýÀ´×ÊÖú¶ÁÕßÃ÷È·×î¼Ñʵ¼ù¡£
СÐò£º
Ëæ×ÅWebÓ¦ÓóÌÐòµÄÉú³¤£¬Ð§ÀÍÆ÷´¦Àí´ó×Ú²¢·¢ÇëÇóµÄÄÜÁ¦Ô½À´Ô½Ö÷Òª¡£ÎªÁËÖª×ãÕâÒ»ÐèÇó£¬ÎÒÃÇÐèÒªÓÅ»¯Ð§ÀÍÆ÷µÄÉèÖÃÒÔÌá¸ßÐÔÄܺͿɿ¿ÐÔ¡£NGINXÊÇÒ»¸öÇ¿Ê¢µÄÍøҳЧÀÍÆ÷Èí¼þ£¬Ëü¿ÉÒÔÓÐÓõش¦Àí¸ß²¢·¢ÇëÇ󣬲¢Ìṩ¸ºÔØƽºâºÍ·´ÏòÊðÀí¹¦Ð§¡£PM2ÊÇÒ»¸öÊ¢ÐеÄNode.jsÓ¦ÓóÌÐòÀú³ÌÖÎÀíÆ÷£¬Ëü¿ÉÒÔ×ÊÖúÎÒÃÇÆô¶¯ºÍ¼à¿ØÓ¦ÓóÌÐòµÄÔËÐÐ״̬¡£Í¨¹ýÍŽáʹÓÃNGINXºÍPM2£¬ÎÒÃÇ¿ÉÒÔÇáËɵØÌá¸ßЧÀÍÆ÷µÄÐÔÄܺͿɿ¿ÐÔ¡£
×°ÖúÍÉèÖÃNGINX£º
µÚÒ»²½ÊÇ×°ÖÃNGINX¡£¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÔÚVPSЧÀÍÆ÷ÉÏ×°ÖÃNGINX£º
sudo apt-get update sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÎÒÃÇÐèÒª¾ÙÐÐһЩ»ù±¾µÄÉèÖ᣷¿ªNGINXµÄÖ÷ÉèÖÃÎļþ/etc/nginx/nginx.conf¾ÙÐб༡£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖã¬Äã¿ÉÒÔƾ֤ÏÖʵÐèÒª¾ÙÐе÷½â£º
user www-data; worker_processes auto; pid /run/nginx.pid; events { worker_connections 1024; } http { ... 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; } } }
µÇ¼ºó¸´ÖÆ
ÔÚʾÀýÉèÖÃÖУ¬ÎÒÃÇÉèÖÃÁËÒ»¸ö¼òÆӵķ´ÏòÊðÀíÀ´×ª·¢ËùÓÐHTTPÇëÇóµ½ÍâµØµÄ3000¶Ë¿Ú¡£ÕâÀï¼ÙÉèÄãµÄWebÓ¦ÓóÌÐòÔËÐÐÔÚ3000¶Ë¿ÚÉÏ¡£Æ¾Ö¤ÏÖÕæÏàÐΣ¬Äã¿ÉÄÜÐèÒª½øÒ»²½ÉèÖÃSSLÖ¤ÊéºÍSSLÖ¤ÊéÏà¹ØµÄÉèÖá£
×°ÖúÍÉèÖÃPM2£º
½ÓÏÂÀ´£¬ÎÒÃǽ«×°ÖÃPM2À´ÖÎÀíÎÒÃǵÄNode.jsÓ¦ÓóÌÐò¡£¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÔÚVPSЧÀÍÆ÷ÉÏ×°ÖÃPM2£º
npm install pm2@latest -g
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´Æô¶¯Ó¦ÓóÌÐò²¢Ê¹ÆäÔÚºǫ́ÔËÐУº
pm2 start app.js
µÇ¼ºó¸´ÖÆ
Õ⽫Æô¶¯Ò»¸öÃûΪ”app.js”µÄNode.jsÓ¦ÓóÌÐò£¬²¢Ê¹ÓÃPM2À´¼à¿ØËüµÄÔËÐÐ״̬¡£Äã¿ÉÒÔʹÓÃPM2µÄÆäËûÏÂÁîÀ´Éó²éÈÕÖ¾¡¢¼à¿ØCPUºÍÄÚ´æµÈ¡£
ÍŽáʹÓÃNGINXºÍPM2£º
×îºó£¬ÎÒÃǽ«ÉèÖÃNGINXÀ´½«ÇëÇóת·¢µ½PM2Æô¶¯µÄNode.jsÓ¦ÓóÌÐò¡£·¿ªNGINXµÄÖ÷ÉèÖÃÎļþ£¬²¢¶Ô·´ÏòÊðÀíÉèÖþÙÐÐÐ޸ģ¬Ê¾ÀýÈçÏ£º
... 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; } } ...
µÇ¼ºó¸´ÖÆ
½«proxy_passµÄÖµÐÞ¸ÄΪPM2Æô¶¯µÄNode.jsÓ¦ÓóÌÐòËùʹÓõĶ˿ڡ£ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǽ«ÇëÇóת·¢µ½ÍâµØµÄ3000¶Ë¿Ú¡£Í¬Ê±£¬Äã¿ÉÄÜ»¹ÐèÒªÉèÖÃÆäËüÏà¹ØµÄ²ÎÊý£¬È绺´æÉèÖᢸºÔØƽºâºÍSSLµÈ¡£
½áÂÛ£º
ͨ¹ýºÏÀíµØÉèÖúÍÓÅ»¯VPSЧÀÍÆ÷£¬ÎÒÃÇ¿ÉÒÔÏÔÖøÌá¸ßWebÓ¦ÓóÌÐòµÄÐÔÄܺͿɿ¿ÐÔ¡£±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃNGINXºÍPM2À´ÊµÏÖÕâһĿµÄ£¬²¢ÌṩÁËÏêϸµÄ´úÂëʾÀý¡£¶ÁÕß¿ÉÒÔƾ֤ÏÖʵÐèÇó¾ÙÐÐÉèÖú͵÷½â£¬ÒÔ³ä·ÖÑéչЧÀÍÆ÷µÄDZÁ¦¡£Í¨¹ý×ñÕÕ×î¼Ñʵ¼ù£¬ÎÒÃÇ¿ÉÒÔ¹¹½¨³ö¸ßÐÔÄܺͿɿ¿µÄWebÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇVPSЧÀÍÆ÷ÓÅ»¯: NGINXºÍPM2µÄ×î¼Ñʵ¼ùµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡