DAILY DOCDAILY DOC
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
  • node

    • NODE
    • parseArgs
    • compose
    • date-format
    • dayjs
    • encrypt
    • env-cmd
    • glob
    • Koa 洋葱模型
    • lodash
    • logger
    • log4js
    • mongod
    • nanoid
    • node-red
    • node-stream
    • node:test
    • nodemailer
    • nodemon
    • nodered
    • parse-curl
    • pm2
    • toml
    • ws
    • yargs
    • zx

pm2

npm install pm2@latest -g # install globally

常用

pm2 ls
pm2 start index.js
pm2 start index.sh
pm2 stop all
pm2 delete all
pm2 restart xxx
pm2 reload all # hot reload zero down time

# log
pm2 logs # all log
pm2 log xxx  # xxx log
pm2 flush xxx # flush log
pm2 logs --format 

Startup Scripts Generation

系统重启时能自动启动相关应用

# Generate Startup Script
$ pm2 startup

# Freeze your process list across server restart
$ pm2 save

# Remove Startup Script
$ pm2 unstartup

Ecosystem File

pm2 init simple  # 项目目录生成  ecosystem.config.js 文件
# Start all applications
pm2 start ecosystem.config.js

# Stop all
pm2 stop ecosystem.config.js

# Restart all
pm2 restart ecosystem.config.js

# Reload all
pm2 reload ecosystem.config.js

# Delete all
pm2 delete ecosystem.config.js

启动参数

--only 只启动 test1

--env prod 配置环境变量 prod: true 可在 process.evn.prod 获取

pm2 start ecosystem.config.js --only test1 --env prod
ecosystem.config.js

module.exports = {
  apps: [
    {
      name: 'test1',
      script: './test/test1.js',
      env_prod: {
        prod: true,
      },
      env_dev: {
        dev: true,
      },
      env_test: {
        test: true,
      },
    },
    {
      name: 'test2',
      script: './test/test2.js',
      env_prod: {
        prod: true,
      },
      env_dev: {
        dev: true,
      },
      env_test: {
        test: true,
      },
    },
  ],
};

nginx 反向代理 pm2后端服务
upstream my_nodejs_upstream {
    server 127.0.0.1:3001;
    keepalive 64;
}

server {
    listen 443 ssl;
    
    server_name www.my-website.com;
    ssl_certificate_key /etc/ssl/main.key;
    ssl_certificate     /etc/ssl/main.crt;
   
    location / {
    	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
    	proxy_set_header Host $http_host;
        
    	proxy_http_version 1.1;
    	proxy_set_header Upgrade $http_upgrade;
    	proxy_set_header Connection "upgrade";
        
    	proxy_pass http://my_nodejs_upstream/;
    	proxy_redirect off;
    	proxy_read_timeout 240s;
    }
}

Refer

  • npm pm2
  • pm2
Last Updated:
Contributors: rosendo
Prev
parse-curl
Next
toml