基础操作
安装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" #执行远程命令