How does
pm2 automatically deploy nodejs projects? The following article will introduce to you how to install and configure pm2, and implement automatic deployment of node projects. I hope it will be helpful to you!
pm2 (process manager) is a process management tool that maintains a process list and can be used to manage your node process is responsible for all running processes and checks the status of the node process. It also supports performance monitoring, load balancing and other functions.
Monitor file changes and automatically restart the program
Support performance monitoring
Load balancing
Automatic restart after program crash
Server restart Automatically restart at startup
Automated deployment project
npm install pm2 -g
Start a node program
pm2 start start.js
Start the process and specify the application program name
pm2 start app.js --name application1
Cluster mode startup
// -i 表示 number-instances 实例数量 // max 表示 PM2将自动检测可用CPU的数量 可以自己指定数量 pm2 start start.js -i max
Add process monitoring
// 在文件改变的时候会重新启动程序 pm2 start app.js --name start --watch
List All processes
pm2 list pm2 ls // 简写
Delete processes from the process list
// pm2 delete [appname] | id pm2 delete app // 指定进程名删除 pm2 delete 0 // 指定进程id删除
Delete all processes from the process list
pm2 delete all
View the specific situation of a process
pm2 describe app
pm2 monit
pm2 restart app // 重启指定名称的进程 pm2 restart all // 重启所有进程
pm2 logs app // 查看该名称进程的日志 pm2 logs all // 查看所有进程的日志
pm2 startup centos
pm2 save
npm install pm2 -g
# deploy.yaml apps: - script: ./start.js # 入口文件 name: 'app' # 程序名称 env: # 环境变量 COMMON_VARIABLE: true env_production: NODE_ENV: production deploy: # 部署脚本 production: # 生产环境 user: lentoo # 服务器的用户名 host: 192.168.2.166 # 服务器的ip地址 port: 22 # ssh端口 ref: origin/master # 要拉取的git分支 ssh_options: StrictHostKeyChecking=no # SSH 公钥检查 repo: https://github.com/**.git # 远程仓库地址 path: /home # 拉取到服务器某个目录下 pre-deploy: git fetch --all # 部署前执行 post-deploy: npm install && pm2 reload deploy.yaml --env production # 部署后执行 env: NODE_ENV: production
centos7
yum –y install git
ssh-keygen -t rsa -C "xxx@xxx.com"
pm2 deploy deploy.yaml production setup
pm2 deploy deploy.yaml production update
4. Possible pitfalls
pm2 deploy deploy.yaml production setup
Solution: Use the git command line to execute
For more node-related knowledge, please visit:
nodejs tutorialThe above is the detailed content of Teach you step by step how to install and configure pm2 to realize automatic deployment of node projects. For more information, please follow other related articles on the PHP Chinese website!