ÔõÑùʹÓÃNGINXºÍPM2ÓÅ»¯VPSЧÀÍÆ÷µÄÍøÂçÐÔÄÜ
ÔõÑùʹÓÃNGINXºÍPM2ÓÅ»¯VPSЧÀÍÆ÷µÄÍøÂçÐÔÄÜ
¼ò½é£º
ÔÚÔËÐÐÍøÕ¾»òÓ¦ÓóÌÐòʱ£¬Ð§ÀÍÆ÷µÄÍøÂçÐÔÄÜÊÇÖÁ¹ØÖ÷ÒªµÄ¡£ÎªÁËÌá¸ßЧÀÍÆ÷µÄÍøÂçÐÔÄÜ£¬ÎÒÃÇ¿ÉÒÔʹÓÃNGINXºÍPM2Á½¸öÇ¿Ê¢µÄ¹¤¾ßÀ´¾ÙÐÐÓÅ»¯¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNGINXºÍPM2À´ÓÅ»¯VPSЧÀÍÆ÷µÄÍøÂçÐÔÄÜ£¬²¢ÌṩһЩÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢×°ÖúÍÉèÖÃNGINX
×°ÖÃNGINX
ʹÓÃÒÔÏÂÏÂÁîÔÚVPSЧÀÍÆ÷ÉÏ×°ÖÃNGINX£º
sudo apt update sudo apt install nginx
µÇ¼ºó¸´ÖÆ
ÉèÖÃNGINX
ÔÚ×°ÖÃÍê³Éºó£¬ÎÒÃÇÐèÒªÉèÖÃNGINXÒÔÌá¸ßЧÀÍÆ÷µÄÍøÂçÐÔÄÜ¡£·¿ªNGINXµÄĬÈÏÉèÖÃÎļþ£º
sudo nano /etc/nginx/nginx.conf
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÔÚÉèÖÃÎļþÖоÙÐÐÒÔϸü¸Ä£º
ÔöÌíworker_processesÉèÖÃÀ´Ö¸¶¨NGINXʹÓõÄÀú³ÌÊýÄ¿£¬Ò»Ñùƽ³£ÉèÖÃΪCPU½¹µãÊýÄ¿µÄÁ½±¶£º
worker_processes auto;
µÇ¼ºó¸´ÖÆ
ÔöÌíworker_connectionsÉèÖÃÀ´Ö¸¶¨Ã¿¸öÊÂÇéÀú³ÌµÄ²¢·¢ÅþÁ¬Êý£¬ÒÔÖ§³Ö¸ü¶àµÄ²¢·¢ÇëÇó£º
events { worker_connections 2048; }
µÇ¼ºó¸´ÖÆ
ÔöÌíkeepalive_timeoutÉèÖÃÀ´Ö¸¶¨ÔÚÒ»¸ö³¤ÆÚÅþÁ¬ÉÏÆÚ´ýÏÂÒ»¸öÇëÇóµÄʱ¼ä£º
http { keepalive_timeout 65; }
µÇ¼ºó¸´ÖÆ
Íê³É¸ü¸Äºó£¬ÉúÑIJ¢Í˳öÉèÖÃÎļþ¡£ÖØмÓÔØNGINXÒÔʹ¸ü¸ÄÉúЧ£º
sudo systemctl reload nginx
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
¶þ¡¢Ê¹ÓÃNGINX¾ÙÐиºÔØƽºâ
×°Öò¢ÉèÖÃPM2
ʹÓÃÒÔÏÂÏÂÁîÔÚVPSЧÀÍÆ÷ÉÏ×°ÖÃPM2£º
sudo npm install pm2 -g
µÇ¼ºó¸´ÖÆ
Æô¶¯Ó¦ÓóÌÐò
Ê×ÏÈ£¬Ê¹ÓÃÒÔÏÂÏÂÁîÔÚPM2ÖÐÆô¶¯ÄãµÄÓ¦ÓóÌÐò£º
pm2 start app.js
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬app.jsÊÇÄãµÄÓ¦ÓóÌÐòµÄÈë¿ÚÎļþ¡£Äã¿ÉÒÔƾ֤ÄãµÄÏÖÕæÏàÐξÙÐÐÐ޸ġ£
ÉèÖÃNGINX·´ÏòÊðÀí
·¿ªNGINXµÄĬÈÏÉèÖÃÎļþ£º
sudo nano /etc/nginx/nginx.conf
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÔÚhttp¿éÏ£¬Ìí¼ÓÒÔÏÂÉèÖÃÀ´ÉèÖ÷´ÏòÊðÀíºÍ¸ºÔØƽºâ£º
http { ... upstream backend { server 127.0.0.1:3000; server 127.0.0.1:3001; } server { listen 80; server_name your_domain.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } ... }
µÇ¼ºó¸´ÖÆ
ÉÏÊöÉèÖÃÖУ¬ÎÒÃǼÙÉèÓ¦ÓóÌÐòÔÚÍâµØµÄ3000ºÍ3001¶Ë¿ÚÉÏÔËÐС£Äã¿ÉÒÔƾ֤ÏÖÕæÏàÐξÙÐÐÐ޸ġ£
Íê³É¸ü¸Äºó£¬ÉúÑIJ¢Í˳öÉèÖÃÎļþ¡£ÖØмÓÔØNGINXÒÔʹ¸ü¸ÄÉúЧ£º
sudo systemctl reload nginx
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
×ܽ᣺
ͨ¹ý×°ÖúÍÉèÖÃNGINX¾ÙÐÐÓÅ»¯£¬ÎÒÃÇÌá¸ßÁËЧÀÍÆ÷µÄÍøÂçÐÔÄÜ¡£Ê¹ÓÃNGINX¾ÙÐиºÔØƽºâ£¬ÎÒÃÇ¿ÉÒÔ·Ö·¢ÇëÇ󵽶à¸öÓ¦ÓóÌÐòʵÀýÉÏ£¬´Ó¶øÌá¸ßЧÀÍÆ÷µÄ²¢·¢´¦Öóͷ£ÄÜÁ¦¡£ÔÚÖÖÖÖÓ¦ÓóÌÐòºÍ³¡¾°ÖУ¬ÕâЩÓÅ»¯¶¼¿ÉÒÔ×ÊÖúÎÒÃÇÌá¸ßЧÀÍÆ÷µÄÍøÂçÐÔÄÜ£¬Ìṩ¸üºÃµÄÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNGINXºÍPM2ÓÅ»¯VPSЧÀÍÆ÷µÄÍøÂçÐÔÄܵÄÏêϸҪÁìºÍ´úÂëʾÀý¡£Ï£Íû¶ÔÄãÓÐËù×ÊÖú£¡
ÒÔÉϾÍÊÇÔõÑùʹÓÃNGINXºÍPM2ÓÅ»¯VPSЧÀÍÆ÷µÄÍøÂçÐÔÄܵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡