ÔõÑùʹÓÃNGINXºÍPM2ÓÅ»¯VPSЧÀÍÆ÷µÄÏìӦʱ¼äºÍÍÌÍÂÁ¿
ÈçÏÂËùʾÊÇһƪ¹ØÓÚÔõÑùʹÓÃNGINXºÍPM2ÓÅ»¯VPSЧÀÍÆ÷µÄÏìӦʱ¼äºÍÍÌÍÂÁ¿µÄÎÄÕ£º
ÎÊÌ⣺ÔõÑùʹÓÃNGINXºÍPM2ÓÅ»¯VPSЧÀÍÆ÷µÄÏìӦʱ¼äºÍÍÌÍÂÁ¿
¸ÅÊö£º
ÔÚÏÖ´ú»¥ÁªÍøÓ¦Óó¡¾°ÖУ¬ÔõÑùÌá¸ßЧÀÍÆ÷µÄÏìӦʱ¼äºÍÍÌÍÂÁ¿ÊÇÿ¸ö¿ª·¢Õ߶¼ÐèÒªÃæÁÙµÄÌôÕ½¡£NGINXºÍPM2ÊÇÁ½¸öÇ¿Ê¢µÄ¹¤¾ß£¬¿ÉÒÔ×ÊÖúÎÒÃÇÇáËɵØÓÅ»¯Ð§ÀÍÆ÷µÄÐÔÄÜ¡£±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùʹÓÃNGINXºÍPM2À´ÓÅ»¯VPSЧÀÍÆ÷µÄÏìӦʱ¼äºÍÍÌÍÂÁ¿£¬²¢¸ø³öÏêϸµÄ´úÂëʾÀý¡£
Ò»¡¢NGINXµÄ×°ÖúÍÉèÖãº
×°ÖÃNGINX£º
ÔÚVPSЧÀÍÆ÷ÉÏ×°ÖÃNGINXºÜÊǼòÆÓ£¬¿ÉÒÔʹÓðü¹ÜÀí¹¤¾ß£¨Èçapt¡¢yumµÈ£©¾ÙÐÐ×°Öá£ÏêϸװÖð취Çë²Î¿¼NGINX¹Ù·½Îĵµ¡£
ÉèÖÃNGINX£º
NGINXµÄÉèÖÃÎļþλÓÚ/etc/nginx/nginx.conf£¬ÔÚÕâ¸öÎļþÖпÉÒÔ¶ÔЧÀÍÆ÷µÄÐÔÄܾÙÐе÷ÓÅ¡£ÒÔÏÂÊÇһЩ³£ÓõÄÉèÖÃÏ
worker_processes£ºÖ¸¶¨NGINXʹÓõÄÊÂÇéÀú³ÌÊý£¬Ò»Ñùƽ³£½¨ÒéÉèÖÃΪЧÀÍÆ÷µÄCPU½¹µãÊý¡£
worker_connections£ºÖ¸¶¨Ã¿¸öÊÂÇéÀú³Ì¿ÉÒÔ´¦ÀíµÄ²¢·¢ÅþÁ¬Êý¡£¿ÉÒÔƾ֤ЧÀÍÆ÷µÄÉèÖõ÷½â´ËÖµ£¬Ò»Ñùƽ³£½¨ÒéÉèÖÃΪ×î´ó¿ÉÖ§³ÖµÄÅþÁ¬Êý¡£
sendfile£º¿ªÆô´ËÑ¡Ïî¿ÉÒÔÌá¸ßÎļþ´«ÊäµÄЧÂÊ¡£
keepalive_timeout£ºÖ¸¶¨Ò»¸ö³¤ÅþÁ¬µÄ³¬Ê±Ê±¼ä£¬¿ÉÒÔ½µµÍ¿Í»§¶ËÓëЧÀÍÆ÷µÄÅþÁ¬½¨ÉèºÍ¹Ø±ÕµÄ¿ªÏú¡£
ʾÀýÉèÖÃÎļþÈçÏÂËùʾ£º
worker_processes 4; events { worker_connections 1024; } http { ... sendfile on; keepalive_timeout 65; ... }
µÇ¼ºó¸´ÖÆ
¶þ¡¢PM2µÄ×°ÖúÍÉèÖãº
×°ÖÃPM2£º
PM2ÊÇÒ»¸öÓÃÓÚÖÎÀíNode.jsÓ¦ÓõŤ¾ß£¬¿ÉÒÔ×ÊÖúÎÒÃÇʵÏÖ¸ºÔØƽºâºÍ×Ô¶¯ÖØÆôµÈ¹¦Ð§¡£Ê¹ÓÃÒÔÏÂÏÂÁî¿ÉÒÔ×°ÖÃPM2£º
npm install pm2 -g
µÇ¼ºó¸´ÖÆ
ÉèÖÃPM2£º
PM2µÄÉèÖÃÎļþΪecosystem.config.js£¬ÔÚÕâ¸öÎļþÖпÉÒÔÉèÖÃÐèÒªÆô¶¯µÄNode.jsÓ¦ÓõIJÎÊý¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄÉèÖÃʾÀý£º
module.exports = { apps : [{ name: 'app', script: 'app.js', instances: 'max', exec_mode: 'cluster', autorestart: true, watch: false, max_memory_restart: '1G', env: { NODE_ENV: 'production' } }] };
µÇ¼ºó¸´ÖÆ
name£ºÓ¦ÓÃÃû³Æ
script£ºÓ¦ÓõÄÈë¿ÚÎļþ·¾¶
instances£ºÆô¶¯µÄʵÀýÊý£¬ÉèÖÃΪmax¿ÉÒÔƾ֤»úеµÄCPU½¹µãÊý×Ô¶¯·ÖÅÉ
exec_mode£ºÖ´ÐÐģʽ£¬ÉèÖÃΪcluster¿ÉÒÔʵÏÖ¸ºÔØƽºâ
autorestart£ºÉèÖÃΪtrue¿ÉÒÔ¿ªÆô×Ô¶¯ÖØÆô
watch£ºÉèÖÃΪfalse¿ÉÒԹرÕÎļþ±ä»»µÄ¼à¿Ø
max_memory_restart£ºÉèÖÃÿ¸öʵÀýµÄ×î´óÄÚ´æʹÓÃÁ¿£¬Áè¼Ý¸Ãֵʱ»á×Ô¶¯ÖØÆôʵÀý
env£ºÉèÖÃNode.jsÓ¦ÓõÄÇéÐαäÁ¿
Èý¡¢NGINXÓëPM2µÄÍŽáʹÓãº
ÉèÖÃNGINX·´ÏòÊðÀí£º
ÉèÖÃNGINX×÷Ϊ·´ÏòÊðÀí£¬½«ÇëÇóת·¢¸øPM2Æô¶¯µÄNode.jsÓ¦Óá£ÒÔÏÂÊÇÒ»¸öʾÀýÉèÖãº
server { listen 80; server_name example.com; location / { proxy_pass http://localhost: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; } }
µÇ¼ºó¸´ÖÆ
listen£ºÖ¸¶¨Ð§ÀÍÆ÷¼àÌýµÄ¶Ë¿ÚºÅ
server_name£ºÖ¸¶¨°ó¶¨µÄÓòÃû»òIPµØµã
location /£ºÖ¸¶¨ÇëÇóµÄÆ¥Åä¹æÔòºÍÊðÀíÉèÖÃ
Æô¶¯Node.jsÓ¦Óãº
ʹÓÃPM2Æô¶¯Node.jsÓ¦Óá£ÒÔÏÂÊÇÒ»¸öÆô¶¯ÏÂÁîµÄʾÀý£º
pm2 start ecosystem.config.js
µÇ¼ºó¸´ÖÆ
ÏÂÁîÖ´Ðкó£¬PM2»á×Ô¶¯Íê³ÉÓ¦ÓõÄÆô¶¯£¬²¢Æ¾Ö¤ÉèÖÃÎļþÖеIJÎÊý¾ÙÐиºÔØƽºâºÍ×Ô¶¯ÖØÆôµÈ²Ù×÷¡£
½áÓ
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃNGINXºÍPM2À´ÓÅ»¯VPSЧÀÍÆ÷µÄÏìӦʱ¼äºÍÍÌÍÂÁ¿¡£Í¨¹ýÊʵ±µ÷½âNGINXµÄÉèÖòÎÊýºÍʹÓÃPM2¾ÙÐиºÔØƽºâºÍ×Ô¶¯ÖØÆôµÈ²Ù×÷£¬¿ÉÒÔÏÔÖøÌá¸ßЧÀÍÆ÷µÄÐÔÄܺÍÎȹÌÐÔ¡£Ï£Íû±¾ÎÄÄܶÔÔÚЧÀÍÆ÷ÓÅ»¯·½ÃæÓöµ½ÄÑÌâµÄ¿ª·¢ÕßÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNGINXºÍPM2ÓÅ»¯VPSЧÀÍÆ÷µÄÏìӦʱ¼äºÍÍÌÍÂÁ¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡