×ðÁú¿­Ê±¹ÙÍøµÇ¼

½ÌÄãÔõÑùÔÚVPSЧÀÍÆ÷ÉÏ°²ÅŶà¸öÓ¦Óò¢Ê¹ÓÃNGINXºÍPM2¾ÙÐÐÖÎÀí

½ÌÄãÔõÑùÔÚVPSЧÀÍÆ÷ÉÏ°²ÅŶà¸öÓ¦Óò¢Ê¹ÓÃNGINXºÍPM2¾ÙÐÐÖÎÀí

¸ÅÊö£º

ÔÚÏÖ´ú»¥ÁªÍøÓ¦Óÿª·¢ÖУ¬Ê¹ÓÃVPSЧÀÍÆ÷À´°²ÅÅÓ¦ÓÃÊÇÒ»ÖÖ³£¼ûµÄ·½·¨¡£±¾ÎĽ«½ÌÄãÔõÑùÔÚVPSЧÀÍÆ÷ÉÏ°²ÅŶà¸öÓ¦Ó㬲¢Ê¹ÓÃNGINX×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷ºÍPM2×÷ΪӦÓÃÀú³ÌÖÎÀí¹¤¾ß¡£ÎÒÃǽ«Í¨¹ýÏêϸµÄ´úÂëʾÀýÀ´ÑÝʾÿһ²½µÄ²Ù×÷¡£

°ì·¨1£º×¼±¸VPSЧÀÍÆ÷

Ê×ÏÈ£¬ÄãÐèÒªÓÐһ̨VPSЧÀÍÆ÷£¬²¢ÓµÓÐÖÎÀíԱȨÏÞ¡£Äã¿ÉÒÔʹÓÃSSHµÇ¼µ½Ð§ÀÍÆ÷ÉÏ£¬ÒÔ±ã¾ÙÐкóÐøµÄ²Ù×÷¡£

°ì·¨2£º×°ÖÃNGINXºÍPM2

ÔÚVPSЧÀÍÆ÷ÉÏ×°ÖÃNGINXºÍPM2¡£Æ¾Ö¤ÄãËùʹÓõIJÙ×÷ϵͳ²î±ð£¬×°Ö÷½·¨Ò²»áÓÐËù²î±ð¡£ÒÔÏÂÊÇÔÚUbuntuϵͳÉÏ×°ÖõÄʾÀýÏÂÁ

×°ÖÃNGINX£º

sudo apt-get update

sudo apt-get install nginx

×°ÖÃPM2£º

sudo apt-get install nodejs npm

sudo npm install pm2 -g

°ì·¨3£º½¨ÉèÓ¦ÓÃ

ÔÚVPSЧÀÍÆ÷ÉϽ¨Éè¶à¸öÓ¦Óã¬Ã¿¸öÓ¦ÓÿÉÒÔÊÇÒ»¸ö×ÔÁ¦µÄÎļþ¼Ð¡£ÒÔÏÂÊÇÒ»¸öʾÀýÓ¦ÓõĽṹ£º

myapp/

|– index.js

|– package.json

ÔÚÿ¸öÓ¦ÓõÄÎļþ¼ÐÖУ¬½¨ÉèÒ»¸öÃûΪindex.jsµÄÎļþ£¬²¢ÔÚÆäÖбàдӦÓôúÂë¡£package.jsonÎļþÓÃÓÚÐÎòÄãµÄÓ¦ÓõÄÏà¹ØÐÅÏ¢ºÍÒÀÀµÏî¡£ÒÔÏÂÊÇÒ»¸öʾÀýindex.jsÎļþµÄ´úÂ룺

const http = require(‘http’);

const hostname = ‘127.0.0.1’;

const port = 3000;

const server = http.createServer((req, res) => {

res.statusCode = 200;

res.setHeader(‘Content-Type’, ‘text/plain’);

res.end(‘Hello, World!

‘);

});

server.listen(port, hostname, () => {

console.log(Server running at http://${hostname}:${port}/);

});

°ì·¨4£ºÆô¶¯Ó¦ÓÃ

ÔÚÿ¸öÓ¦ÓõÄÎļþ¼ÐÖУ¬Ê¹ÓÃPM2À´Æô¶¯Ó¦Óá£ÒÔÏÂÊÇʾÀýÏÂÁ

cd /path/to/myapp

pm2 start index.js –name myapp

Öظ´ÒÔÉÏ°ì·¨£¬ÎªÃ¿¸öÓ¦ÓÃÆô¶¯Ò»¸öÀú³Ì¡£

°ì·¨5£ºÉèÖÃNGINX

ÔÚNGINXÉèÖÃÎļþÖÐÌí¼Ó·´ÏòÊðÀíµÄÉèÖ㬽«ÇëÇóת·¢µ½ÏìÓ¦µÄÓ¦ÓÃÉÏ¡£ÒÔÏÂÊÇʾÀýÉèÖõĴúÂ룺

server {

listen 80;

server_name example.com;

location /app1 {

proxy_pass http://127.0.0.1:3000;

µÇ¼ºó¸´ÖÆ

}

location /app2 {

proxy_pass http://127.0.0.1:3001;

µÇ¼ºó¸´ÖÆ

}

# … ÆäËûÓ¦ÓõÄÉèÖÃ

# ¾²Ì¬×ÊÔ´µÄÉèÖÃ

location /static {

alias /path/to/static/files;

µÇ¼ºó¸´ÖÆ

}

}

ÐÞ¸ÄÒÔÉÏÉèÖÃÎļþÖеÄexample.comΪÄãµÄÓòÃû£¬²¢Æ¾Ö¤ÏÖÕæÏàÐÎÐÞ¸Äÿ¸öÓ¦ÓõÄÊðÀíµØµã¡£

°ì·¨6£ºÖØÆôNGINX

ÖØÆôNGINXÒÔʹÉèÖÃÉúЧ¡£ÔÚUbuntuϵͳÉÏ£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÖØÆôNGINX£º

sudo service nginx restart

ÖÁ´Ë£¬ÄãÒѾ­ÀÖ³ÉÔÚVPSЧÀÍÆ÷ÉÏ°²ÅÅÁ˶à¸öÓ¦Ó㬲¢ÇÒʹÓÃNGINX×÷Ϊ·´ÏòÊðÀíЧÀÍÆ÷½«ÇëÇóת·¢µ½ÏìÓ¦µÄÓ¦ÓÃÉÏ¡£Í¨¹ýʹÓÃPM2£¬Ä㻹Äܹ»Àû±ãµØÖÎÀíÓ¦ÓÃÀú³Ì¡£

×ܽ᣺

±¾ÎÄÏÈÈÝÁËÔõÑùÔÚVPSЧÀÍÆ÷ÉÏ°²ÅŶà¸öÓ¦Ó㬲¢Ê¹ÓÃNGINXºÍPM2¾ÙÐÐÖÎÀí¡£Í¨¹ýƾ֤ÒÔÉÏ°ì·¨²Ù×÷£¬Äã¿ÉÒÔÇáËɵؽ«¶à¸öÓ¦Óð²Åŵ½VPSЧÀÍÆ÷ÉÏ£¬²¢ÓÐÓõØÖÎÀíºÍÔËάÕâЩӦÓá£Ï£Íû±¾ÎĶÔÄãÓÐËù×ÊÖú£¡

ÒÔÉϾÍÊǽÌÄãÔõÑùÔÚVPSЧÀÍÆ÷ÉÏ°²ÅŶà¸öÓ¦Óò¢Ê¹ÓÃNGINXºÍPM2¾ÙÐÐÖÎÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

13452372176

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ