½ÌÄãÔõÑùÔÚVPSЧÀÍÆ÷ÉÏ°²ÅÅNGINXºÍPM2
½ÌÄãÔõÑùÔÚVPSЧÀÍÆ÷ÉÏ°²ÅÅNGINXºÍPM2
Ç°ÑÔ£º
VPSЧÀÍÆ÷µÄ°²ÅÅÊÇÍøÕ¾»òÓ¦ÓõÄÒªº¦°ì·¨Ö®Ò»¡£ÔÚ´Ë£¬ÎÒ½«Ïò¸÷ÈËÏÈÈÝÔõÑùÔÚVPSЧÀÍÆ÷ÉÏ°²ÅÅNGINXºÍPM2£¬ÕâÁ½¸ö¹¤¾ß¿ÉÒÔ¼«´óµØÌáÉýÍøÕ¾µÄÐÔÄܺÍÎȹÌÐÔ¡£±¾ÎĽ«ÏêϸÏÈÈÝ×°ÖÃÉèÖÃÀú³Ì£¬²¢ÌṩÏêϸµÄ´úÂëʾÀý¡£
×°ÖÃNGINX£º
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃNGINX×÷ΪÎÒÃǵÄWebЧÀÍÆ÷¡£
1.1 ¸üРapt-get °ü¹ÜÀí¹¤¾ß£º
sudo apt-get update
µÇ¼ºó¸´ÖÆ
1.2 ×°ÖÃNGINX£º
sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
ÉèÖÃÎļþλÖãº/etc/nginx/nginx.conf¡£
ÉèÖÃNGINX£º
2.1 ½¨ÉèÒ»¸öеÄÕ¾µãÉèÖÃÎļþ£º
sudo nano /etc/nginx/sites-available/myapp
µÇ¼ºó¸´ÖÆ
2.2 ÔÚ¸ÃÎļþÖУ¬ÊäÈëÒÔÏÂÄÚÈÝ£º
server { listen 80; server_name your_domain.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://localhost:3000; } }
µÇ¼ºó¸´ÖÆ
×¢ÖØ£º½« “your_domain.com” Ì滻ΪÄãµÄÓòÃû£¬ “http://localhost:3000” Ì滻ΪÄãµÄÏÖʵӦÓóÌÐòµÄµØµã¡£
2.3 ÉúÑIJ¢Í˳öÎļþ£º°´Ï Ctrl + X£¬È»ºóÊäÈë Y ÉúÑĸü¸Ä¡£
2.4 ½¨ÉèÒ»¸ö·ûºÅÁ´½Ó£º
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
µÇ¼ºó¸´ÖÆ
×°ÖÃPM2£º
PM2ÊÇÒ»¸öNode.jsµÄÀú³ÌÖÎÀí¹¤¾ß£¬Ëü¿ÉÈ·±£ÄãµÄÓ¦ÓÃÔÚЧÀÍÆ÷ÉÏÒ»Á¬ÔËÐУ¬²¢ÄܾÙÐйÊÕϻָ´¡£
3.1 ×°ÖÃPM2£º
sudo npm install -g pm2
µÇ¼ºó¸´ÖÆ
3.2 Æô¶¯ÄãµÄÓ¦ÓóÌÐò£º
pm2 start app.js
µÇ¼ºó¸´ÖÆ
×¢ÖØ£ºapp.js ÊÇÄãµÄÓ¦ÓóÌÐòµÄÈë¿ÚÎļþ£¬ÄãÐèÒª½«ÆäÌ滻ΪÄã×Ô¼ºµÄÎļþÃû¡£
3.3 ÌìÉúÆô¶¯¾ç±¾£º
pm2 startup systemd
µÇ¼ºó¸´ÖÆ
ÔËÐÐÕâ¸öÏÂÁîºó£¬Ä㽫¿´µ½Ò»¸öÌáÐÑ£¬½«Ëü¸ø³öµÄÏÂÁÖƵ½ÖÕ¶ËÖв¢ÔËÐС£Õ⽫½¨ÉèÒ»¸öЧÀÍʹPM2ÔÚϵͳÖØÆôʱ×Ô¶¯ÔËÐС£
ÉèÖÃNGINX·´ÏòÊðÀí£º
ÏÖÔÚ£¬ÎÒÃÇÐèÒªÉèÖÃNGINX½«ËùÓÐÁ÷Á¿×ª·¢µ½PM2Æô¶¯µÄÓ¦ÓóÌÐòÉÏ¡£
4.1 ·¿ªNGINXÉèÖÃÎļþ£º
sudo nano /etc/nginx/nginx.conf
µÇ¼ºó¸´ÖÆ
4.2 ÐÞ¸ÄÉèÖÃÎļþ£º
http{ ... server { listen 80; server_name your_domain.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache_bypass $http_upgrade; } } ... }
µÇ¼ºó¸´ÖÆ
×¢ÖØ£º½« “your_domain.com” Ì滻ΪÄãµÄÓòÃû£¬ “http://localhost:3000” Ì滻ΪÄãµÄÓ¦ÓóÌÐòµØµã¡£
ÖØÆôNGINXºÍPM2£º
ÔÚÍê³ÉÒÔÉÏÉèÖúó£¬ÎÒÃÇÐèÒªÖØÐÂÆô¶¯NGINXºÍPM2£¬Ê¹¸ü¸ÄÉúЧ¡£
5.1 ÖØÆôNGINX£º
sudo systemctl restart nginx
µÇ¼ºó¸´ÖÆ
5.2 ÖØÆôPM2£º
pm2 restart all
µÇ¼ºó¸´ÖÆ
ÖÁ´Ë£¬ÎÒÃÇÒѾÀÖ³ÉÔÚVPSЧÀÍÆ÷ÉÏ°²ÅÅÁËNGINXºÍPM2¡£NGINX×÷Ϊ·´ÏòÊðÀí£¬½«ËùÓÐÁ÷Á¿×ª·¢µ½PM2Æô¶¯µÄÓ¦ÓÃÉÏ£¬´Ó¶øÌá¸ßÁËÍøÕ¾µÄÐÔÄܺÍÎȹÌÐÔ¡£ÔÚÒÔºóµÄ¿ª·¢ºÍά»¤Àú³ÌÖУ¬Äã¿ÉÒÔʹÓÃPM2µÄÆäËû¹¦Ð§£¬ÈçÈÕÖ¾ÖÎÀíºÍ¼à¿ØµÈ¡£
×ܽ᣺
±¾ÎÄÏêϸÏÈÈÝÁËÔõÑùÔÚVPSЧÀÍÆ÷ÉÏ°²ÅÅNGINXºÍPM2£¬²¢ÌṩÁËÏêϸµÄ´úÂëʾÀý¡£ÕâЩ¹¤¾ß¿ÉÒÔ×ÊÖúÄãÌáÉýÍøÕ¾µÄÐÔÄܺÍÎȹÌÐÔ£¬ÊÇÍøÕ¾°²ÅÅÀú³ÌÖбز»¿ÉÉٵŤ¾ß¡£Ï£Íû±¾ÎÄÄܶÔÄãÓÐËù×ÊÖú£¬ÈÃÄãµÄÍøÕ¾Ô½·¢¾«²Ê£¡
ÒÔÉϾÍÊǽÌÄãÔõÑùÔÚVPSЧÀÍÆ÷ÉÏ°²ÅÅNGINXºÍPM2µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡