NGINXºÍPM2: ¹¹½¨Çå¾²µÄVPSЧÀÍÆ÷ÇéÐκÍÊý¾Ý±£»¤Õ½ÂÔ
NGINXºÍPM2: ¹¹½¨Çå¾²µÄVPSЧÀÍÆ÷ÇéÐκÍÊý¾Ý±£»¤Õ½ÂÔ£¬ÐèÒªÏêϸ´úÂëʾÀý
Ëæ×Å»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬VPS£¨Virtual Private Server£©³ÉΪÁËÐí¶àÆóÒµºÍСÎÒ˽ÈËÑ¡ÔñµÄÍйÜЧÀÍ¡£VPSÌṩÁ˸ü¸ßµÄÇå¾²ÐÔºÍ×Ô½ç˵»¯µÄÉèÖÃÑ¡ÏʹÎÒÃÇÄܹ»¸üºÃµØ±£»¤Ð§ÀÍÆ÷ºÍÊý¾Ý¡£
NGINXÊÇÒ»¸öÆÕ±éʹÓõĿªÔ´Ð§ÀÍÆ÷Èí¼þ£¬¿ÉÒÔ×÷Ϊ·´ÏòÊðÀí¡¢¸ºÔØƽºâÆ÷ºÍWebЧÀÍÆ÷ʹÓá£PM2ÊÇÒ»¸öÏȽøµÄNode.jsÀú³ÌÖÎÀíÆ÷£¬¿ÉÒÔ×ÊÖúÎÒÃÇÇáËɵØÖÎÀíºÍ¼à¿ØÎÒÃǵÄNode.jsÓ¦Óá£
ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃNGINXºÍPM2À´¹¹½¨Ò»¸öÇå¾²µÄVPSЧÀÍÆ÷ÇéÐκÍÊý¾Ý±£»¤Õ½ÂÔ¡£ÎÒÃǽ«ÌṩÏêϸµÄ´úÂëʾÀýÀ´ËµÃ÷ÿ¸ö°ì·¨¡£
µÚÒ»²½ÊÇ×°ÖúÍÉèÖÃNGINX¡£¼ÙÉèÎÒÃÇÕýÔÚʹÓÃUbuntu²Ù×÷ϵͳ¡£Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃNGINX£º
sudo apt-get update sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
ÀÖ³É×°Öúó£¬ÎÒÃÇÐèÒªÉèÖÃNGINXÒÔÆôÓÃSSL/TLS¼ÓÃÜ¡£·¿ªNGINXµÄÉèÖÃÎļþ/etc/nginx/nginx.conf£¬¾ÙÐÐÒÔÏÂÐ޸ģº
http { # ... server { listen 80; server_name example.com; location / { return 301 https://$host$request_uri; } } server { listen 443 ssl; server_name example.com; ssl_certificate /path/to/certificate.crt; ssl_certificate_key /path/to/private_key.key; location / { proxy_pass http://localhost:3000; } } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏʾÀýÖУ¬ÎÒÃÇÉèÖÃÁËÒ»¸öHTTPµ½HTTPSµÄÖض¨Ïò£¬²¢ÔÚ443¶Ë¿ÚÆôÓÃSSL/TLS¡£±ðµÄ£¬ÎÒÃÇ»¹ÉèÖÃÁËÒ»¸ö·´ÏòÊðÀí£¬½«ËùÓÐÇëÇóת·¢µ½ÍâµØµÄ3000¶Ë¿Ú£¬ÕâÊÇÎÒÃÇNode.jsÓ¦ÓÃÔËÐеĶ˿ڡ£
½ÓÏÂÀ´ÊÇ×°ÖúÍÉèÖÃPM2¡£Í¨¹ýÒÔÏÂÏÂÁî×°ÖÃPM2£º
sudo npm install -g pm2
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃPM2À´ÖÎÀíºÍ¼à¿ØÎÒÃǵÄNode.jsÓ¦Ó᣼ÙÉèÎÒÃǵÄÓ¦ÓÃλÓÚ/path/to/your/appĿ¼Ï£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÆô¶¯Ó¦Óãº
pm2 start /path/to/your/app/index.js --name your-app-name
µÇ¼ºó¸´ÖÆ
PM2»¹ÌṩһЩÓÐÓõÄÏÂÁîÀ´ÖÎÀíÎÒÃǵÄÓ¦Óã¬ÀýÈç×èÖ¹Ó¦Óá¢ÖØÆôÓ¦Óá¢Éó²éÈÕÖ¾µÈµÈ¡£¸üÏêϸµÄÏÂÁî¿ÉÒÔÔÚPM2µÄ¹Ù·½ÎĵµÖÐÕÒµ½¡£
³ýÁËʹÓÃNGINXºÍPM2À´¹¹½¨Çå¾²µÄVPSЧÀÍÆ÷ÇéÐΣ¬ÎÒÃÇ»¹ÐèÒª½ÓÄÉһЩÊý¾Ý±£»¤Õ½ÂÔ¡£ÒÔÏÂÊÇһЩ½¨Ò飺
°´ÆÚ±¸·ÝÊý¾Ý¡£Ê¹ÓÃÏà¹Ø¹¤¾ß£¨ÀýÈçrsync¡¢scp£©°´ÆÚ½«ÎÞ·¨ÖØÏÖµÄÊý¾Ý±¸·Ýµ½ÆäËûµØ·½£¬ÒÔ·ÀÊý¾Ýɥʧ¡£
ʹÓÃÇ¿ÃÜÂëºÍÃÜÔ¿¡£È·±£Ð§ÀÍÆ÷ºÍÓ¦ÓóÌÐòµÄµÇ¼ƾ֤×㹻ǿʢ£¬²¢Ê¹ÓÃSSHÃÜÔ¿À´ÔöÌíÇå¾²ÐÔ¡£
°´ÆÚ¸üкÍÉý¼¶Èí¼þ¡£¼á³ÖЧÀÍÆ÷ÇéÐκÍÓ¦ÓóÌÐòµÄÇå¾²ÐÔ£¬ÊµÊ±ÐÞ²¹ÒÑÖªÎó²î¡£
ʹÓ÷À»ðǽºÍÍøÂçÇå¾²¹¤¾ß¡£ÉèÖÃÊʵ±µÄ·À»ðǽ¹æÔòºÍÍøÂçÇå¾²¹¤¾ßÀ´×èֹδ¾ÊÚȨµÄ»á¼û¡£
Çë×¢ÖØ£¬ÒÔÉÏÖ»ÊÇһЩ½¨Ò飬ÏêϸµÄÊý¾Ý±£»¤Õ½ÂÔӦƾ֤Äú×Ô¼ºµÄÐèÇóºÍÏÖÕæÏàÐξÙÐж¨ÖÆ¡£
×ÜÖ®£¬Ê¹ÓÃNGINXºÍPM2¿ÉÒÔ×ÊÖúÎÒÃǹ¹½¨Çå¾²µÄVPSЧÀÍÆ÷ÇéÐκÍÊý¾Ý±£»¤Õ½ÂÔ¡£Í¨¹ýÉèÖÃNGINXÆôÓÃSSL/TLS¼ÓÃܺͷ´ÏòÊðÀí£¬ÒÔ¼°Ê¹ÓÃPM2ÖÎÀíºÍ¼à¿ØÎÒÃǵÄNode.jsÓ¦Óã¬ÎÒÃÇ¿ÉÒÔÌṩ¸üºÃµÄЧÀÍÆ÷Çå¾²ÐÔºÍÊý¾Ý±£»¤¡£Í¬Ê±£¬½ÓÄÉÊʵ±µÄÊý¾Ý±£»¤Õ½ÂÔÒ²ÊǺÜÊÇÖ÷ÒªµÄ£¬ÒÔ±ÜÃâÊý¾ÝɥʧºÍδ¾ÊÚȨµÄ»á¼û¡£
ÒÔÉϾÍÊÇNGINXºÍPM2: ¹¹½¨Çå¾²µÄVPSЧÀÍÆ÷ÇéÐκÍÊý¾Ý±£»¤Õ½ÂÔµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡