ÔõÑùÉèÖÃNGINXºÍPM2ʵÏÖVPSЧÀÍÆ÷µÄ·´ÏòÊðÀí
ÔõÑùÉèÖÃNGINXºÍPM2ʵÏÖVPSЧÀÍÆ÷µÄ·´ÏòÊðÀí
ÔÚ¹¹½¨ÍøÂçÓ¦ÓÃʱ£¬·´ÏòÊðÀíÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄ¿´·¨¡£·´ÏòÊðÀíЧÀÍÆ÷¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖ¸ºÔØƽºâ¡¢¸ß¿ÉÓÃÐÔºÍÇå¾²ÐԵȹ¦Ð§¡£ÔÚVPSЧÀÍÆ÷ÖÐʹÓÃNGINXºÍPM2À´ÉèÖ÷´ÏòÊðÀíÊÇÒ»¸ö·Ç¾³£¼ûµÄ³¡¾°¡£±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùÉèÖÃNGINXºÍPM2ʵÏÖVPSЧÀÍÆ÷µÄ·´ÏòÊðÀí£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
×°ÖÃNGINXºÍPM2
Ê×ÏÈ£¬ÐèÒªÔÚVPSЧÀÍÆ÷ÉÏ×°ÖÃNGINXºÍPM2¡£¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî×°Öãº
# ×°ÖÃNGINX sudo apt-get install nginx # ×°ÖÃPM2 npm install -g pm2
µÇ¼ºó¸´ÖÆ
ÉèÖÃNGINX
NGINXÊÇÒ»¸ö¸ßÐÔÄܵÄWebЧÀÍÆ÷ºÍ·´ÏòÊðÀíЧÀÍÆ÷¡£ÉèÖÃNGINXʱ£¬ÐèÒª½¨ÉèÒ»¸öеÄЧÀÍÆ÷¿éÓÃÓÚÊðÀíÇëÇó¡£ÒÔÏÂÊÇÒ»¸öʾÀýµÄNGINXÉèÖÃÎļþ£º
# ·¿ªÄ¬ÈÏNGINXÉèÖÃÎļþ sudo nano /etc/nginx/sites-available/default # ÔÚÎļþÖÐÌí¼ÓÒÔÏÂÉèÖà server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1: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; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬½«80¶Ë¿ÚµÄÇëÇóÊðÀíµ½ÍâµØµÄ3000¶Ë¿Ú¡£Æ¾Ö¤ÏÖÕæÏàÐÎÐÞ¸Äserver_nameºÍproxy_pass×ֶΡ£
Æô¶¯PM2Ó¦ÓÃ
PM2ÊÇÒ»¸öÓÃÓÚÖÎÀíºÍÊØ»¤Node.jsÓ¦ÓõÄÀú³ÌÖÎÀíÆ÷¡£ÔÚÉèÖÃNGINX֮ǰ£¬ÐèÒªÏÈÆô¶¯PM2Ó¦Óá£ÒÔÏÂÊÇÒ»¸öʾÀýµÄÆô¶¯ÏÂÁ
# Æô¶¯Ó¦Óà pm2 start app.js
µÇ¼ºó¸´ÖÆ
ƾ֤ÏÖÕæÏàÐÎÌæ»»app.jsΪÏÖʵµÄÓ¦Óþ籾·¾¶¡£
Æô¶¯NGINXºÍPM2
Íê³ÉNGINXºÍPM2µÄÉèÖúó£¬ÐèÒªÆô¶¯ËüÃÇÒÔʹÉèÖÃÉúЧ¡£
# Æô¶¯NGINX sudo service nginx start # Æô¶¯PM2 pm2 save pm2 startup
µÇ¼ºó¸´ÖÆ
ÉèÖ÷À»ðǽ£¨¿ÉÑ¡£©
×îºó£¬»¹¿ÉÒÔÉèÖ÷À»ðǽÒÔÏÞÖƹØÓÚЧÀÍÆ÷µÄ»á¼û¡£ÀýÈ磬ÈôÊÇÖ»ÔÊÐíÌض¨µÄIPµØµã»á¼ûЧÀÍÆ÷£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
# ÔÊÐíÌض¨IPµØµã»á¼ûЧÀÍÆ÷ sudo ufw allow from your_ip_address to any port 80
µÇ¼ºó¸´ÖÆ
ƾ֤ÏÖÕæÏàÐÎÌæ»»your_ip_addressΪÏÖʵµÄIPµØµã¡£
×ܽá
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇ¿ÉÒÔÉèÖÃNGINXºÍPM2ʵÏÖVPSЧÀÍÆ÷µÄ·´ÏòÊðÀí¡£NGINXÈÏÕæÊðÀíÇëÇ󣬽«Æäת·¢µ½ÍâµØµÄNode.jsÓ¦Óá£PM2×÷ΪÀú³ÌÖÎÀíÆ÷£¬ÈÏÕæÆô¶¯ºÍÖÎÀíNode.jsÓ¦Óá£ÕâÑù¿ÉÒÔʵÏÖ¸ºÔØƽºâ¡¢¸ß¿ÉÓÃÐÔºÍÇå¾²ÐԵȹ¦Ð§£¬ÌáÉýÍøÂçÓ¦ÓõÄÐÔÄܺÍÎȹÌÐÔ¡£
Ï£Íû±¾ÎĶÔÄãÃ÷È·ÔõÑùÉèÖÃNGINXºÍPM2ʵÏÖVPSЧÀÍÆ÷µÄ·´ÏòÊðÀíÓÐËù×ÊÖú¡£ÈôÓÐÒÉÎÊ£¬ÇëËæʱÁôÑÔ¡£
ÒÔÉϾÍÊÇÔõÑùÉèÖÃNGINXºÍPM2ʵÏÖVPSЧÀÍÆ÷µÄ·´ÏòÊðÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡