PM2速查表

基础操作

安装pm2

yarn global add pm2
pm2 install pm2-logrotate  #可选,日志需要做轮转就要安装,配置方式参考https://github.com/keymetrics/pm2-logrotate
pm2 completion install

启动一个任务

pm2 start app.js

作业列表

pm2 ls

删除作业

pm2 delete app

作业启动与停止

pm2 stop app
pm2 start app
pm2 restart app
pm2 restart ecosystem.config.js --update-env

生成作业配置模板

pm2 init

作业列表的导入和导出

pm2 save
pm2 resurrect

监控与日志

pm2 logs app
pm2 flush     #清空所有应用的日志
pm2 monit

在配置文件中指定日志存放位置

module.exports = {
  apps: [{
      name: 'app',
      script: 'app.js',
      output: './out.log',
      error: './error.log',
	    log: './combined.outerr.log',
    }]
}

集群模式下使用merge参数融合日志文件

module.exports = {
  apps: [{
      name: 'app',
      script: 'app.js',
      output: './out.log',
      error: './error.log',
      merge_logs: true,
    }]
}

集群模式

pm2 start app.js -i 4
pm2 start app.js -i max

开发工具

开启文件监控

module.exports = {
  apps : [{
    name: "app",
    script: "./app.js",
    watch: true,
  }]
}

CLI命令行监控

pm2 start app.js --watch

pm2可以作为静态服务器

pm2 serve ./nodejs/ 6060

配置文件启动静态服务

module.exports = {
  apps: [{
    name: "static-file",
    script: "serve",
    env: {
      PM2_SERVE_PATH: ".",
      PM2_SERVE_PORT: 8080,
    },
  }]
}

使用SSH发布

ssh-keygen -t rsa
ssh-copy-id node@myserver.com

配置作业文件

module.exports = {
  apps: [{
    name: "app",
    script: "app.js"
  }],
  deploy: {
    production: {
      key: "/path/to/some.pem",
      user: "ubuntu",
      host: ["192.168.0.13"],
      ssh_options: "StrictHostKeyChecking=no",
      ref: "origin/master",
      repo: "git@github.com:Username/repository.git",
      path: "/var/www/my-repository",
      pre-setup: "apt-get install git ; ls -la",
      post-setup: "ls -la",
      pre-deploy-local: "echo 'This is a local executed command'"
      post-deploy: "npm install",
    },
  }
}

开始部署

pm2 deploy production setup
pm2 deploy production update  #更新
pm2 deploy production revert 1 #回滚
pm2 deploy production exec "pm2 reload all" #执行远程命令

转载于:https://my.oschina.net/xiaomaijiang/blog/1841862

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值