欢迎扫码,加作者微信

pm2 入门教程

2025-10-23 01:34:41
2025-10-23 23:23:52

安装node命令

sudo yum install nodejs npm

安装pm2

sudo npm install -g pm2

配置ecosystem.config.cjs文件

shell 复制代码
module.exports = {
  apps: [
    // 测试环境 - 工具版本
    {
      name: 'quicking-web-dev',
      instances: 1,
      exec_mode: 'fork', // 改为 fork 模式而不是 cluster
      cwd: '/home/ec2-user/server/quicking-h5',
      script: './server/index.mjs',
      interpreter: '/usr/bin/node', // 或者实际的 node 路径
      log_date_format: 'YYYY-MM-DD HH:mm Z',
      error_file: './logs/err.log',
      out_file: './logs/out.log',
      log_file: './logs/combined.outerr.log',
      time: true,
      autorestart: true,
      max_restarts: 10,
      min_uptime: '10s',
      max_memory_restart: '2G',
      watch: false,
      ignore_watch: ['node_modules', 'logs', '.nuxt', '.output'],
      env: {
        PORT: 6999
      }
    },

    // 测试环境 - 社交版本
    {
      name: 'earning-web-dev',
      instances: 1,
      exec_mode: 'fork', // 改为 fork 模式而不是 cluster
      cwd: '/home/ec2-user/server/quicking-h5',
      script: './server/index.mjs',
      args: 'start',
      log_date_format: 'YYYY-MM-DD HH:mm Z',
      error_file: './logs/err.log',
      out_file: './logs/out.log',
      log_file: './logs/combined.outerr.log',
      time: true,
      autorestart: true,
      max_restarts: 10,
      min_uptime: '10s',
      max_memory_restart: '2G',
      watch: false,
      ignore_watch: ['node_modules', 'logs', '.nuxt', '.output'],
      env: {
        PORT: 6888
      }
    },

    // 正式环境 - 工具版本
    {
      name: 'quicking-web',
      instances: 1,
      exec_mode: 'fork', // 改为 fork 模式而不是 cluster
      cwd: '/home/ec2-user/server/quicking-h5',
      script: './server/index.mjs',
      args: 'start',
      log_date_format: 'YYYY-MM-DD HH:mm Z',
      error_file: './logs/err.log',
      out_file: './logs/out.log',
      log_file: './logs/combined.outerr.log',
      time: true,
      autorestart: true,
      max_restarts: 10,
      min_uptime: '10s',
      max_memory_restart: '2G',
      watch: false,
      ignore_watch: ['node_modules', 'logs', '.nuxt', '.output'],
      env: {
        PORT: 6999
      }
    },

    // 正式环境 - 社交版本
    {
      name: 'earning-web',
      instances: 1,
      exec_mode: 'fork', // 改为 fork 模式而不是 cluster
      cwd: '/home/ec2-user/server/quicking-h5',
      script: './server/index.mjs',
      args: 'start',
      log_date_format: 'YYYY-MM-DD HH:mm Z',
      error_file: './logs/err.log',
      out_file: './logs/out.log',
      log_file: './logs/combined.outerr.log',
      time: true,
      autorestart: true,
      max_restarts: 10,
      min_uptime: '10s',
      max_memory_restart: '2G',
      watch: false,
      ignore_watch: ['node_modules', 'logs', '.nuxt', '.output'],
      env: {
        PORT: 6888
      }
    }
  ]
}

服务器部署命令

rm -rf /home/ec2-user/server/quicking-h5/; mkdir /home/ec2-user/server/quicking-h5/; mkdir /home/ec2-user/server/quicking-h5/logs/; tar -zxvf /home/ec2-user/server/quicking-h5-bf/dist.tgz -C /home/ec2-user/server/quicking-h5; cd /home/ec2-user/server/quicking-h5 pm2 delete quicking-web #PORT=6999 pm2 start server/index.mjs --name quicking-web pm2 start /home/ec2-user/server/quicking-h5/ecosystem.config.cjs --only quicking-web

pm2常用命令

language 复制代码
# 启动应用
pm2 start ecosystem.config.cjs

# 启动应用并指定名称
pm2 start aecosystem.config.cjs --name my-app

# 启动应用并监听文件变化(开发模式)
pm2 start ecosystem.config.cjs --watch

# 启动应用并指定实例数(集群模式)
pm2 start ecosystem.config.cjs -i max

# 从 package.json 启动
pm2 start npm --name "my-app" -- start

# 列出所有应用
pm2 list
pm2 ls
pm2 status

# 查看特定应用信息
pm2 show app-name

# 监控所有应用
pm2 monit

# 停止应用
pm2 stop app-name
pm2 stop all

# 重启应用
pm2 restart app-name
pm2 restart all

# 重载应用(零停机重启)
pm2 reload app-name
pm2 reload all

# 删除应用
pm2 delete app-name
pm2 delete all


# 查看所有应用日志
pm2 logs

# 查看特定应用日志
pm2 logs app-name

# 查看最后 N 行日志
pm2 logs --lines 200

# 清空日志
pm2 flush

# 以 JSON 格式输出日志
pm2 logs --json


# 查看系统信息
pm2 show pm2

# 生成启动脚本
pm2 startup

# 保存当前进程列表
pm2 save

# 恢复保存的进程列表
pm2 resurrect

# 更新 PM2
pm2 update

# 查看详细的应用信息
pm2 show app-name

# 查看 PM2 自身日志
pm2 logs pm2

# 检查 PM2 版本和状态
pm2 --version
pm2 info

# 重置 PM2(清除所有应用)
pm2 kill
文章目录

生成中...

扫码赞赏

感谢您的支持与鼓励

安全支付

支付赞赏

您的支持是我们前进的动力

留言
快捷金额
自定义金额
¥

安全保障

采用银行级加密技术,保障您的支付安全

暂无评论,欢迎留下你的评论

暂无评论

期待您的精彩留言!
成为第一个评论的人吧 ✨

写下第一条评论
Copyright © 2025 粤ICP备19043740号-1
🎉 今日第 1 位访客 📊 年访问量 0 💝 累计赞赏 1000+