使用pm2部署nodejs项目

使用pm2部署nodejs项目

2018, Apr 17    

pm2

1.全局安装

npm install -g pm2

帮助

pm2 --help

2.项目内安装

npm install pm2

帮助

node_modules/.bin/pm2 --help

3.查看运行状态

pm2 list

4.监控

pm2 monit

5.查看运行状态

pm2 describe {id}

6.日志

pm2 log

7.重启应用

pm2 restart {id}

8.停止应用

pm2 stop apps.js

预定义配置文件

process.json

  "apps": [
    {
      "name": "ANodeBlog",
      "script": "bin/www",
      "watch": "../",
      "log_date_format": "YYYY-MM-DD HH:mm Z"
    }
  ]
}

运行

pm2 start process.json

开机启动

1.顺序执行启动脚本

pm2 start process.json

2.dump脚本

pm2 save

3.生成自动执行脚本

pm2 startup

4.生成命令黏贴并执行

sudo env PATH=$PATH:/usr/local/Cellar/node/8.1.0_1/bin /Users/yuliang/work/github/FeedProject/node_modules/pm2/bin/pm2 startup launchd -u yuliang --hp /Users/yuliang

5.执行结果

Target path
/Users/yuliang/Library/LaunchAgents/pm2.yuliang.plist
Command list
[ 'launchctl load -w /Users/yuliang/Library/LaunchAgents/pm2.yuliang.plist' ]
[PM2] Writing init configuration in /Users/yuliang/Library/LaunchAgents/pm2.yuliang.plist
[PM2] Making script booting at startup...
>>> Executing launchctl load -w /Users/yuliang/Library/LaunchAgents/pm2.yuliang.plist
[DONE]
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save
[PM2] Remove init script via:
$ pm2 unstartup launchd

6.验证 重启计算机,执行

ps | grep pm2          

进程已经启动

713 ttys000    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn pm2
pm2 list

应用已启动

│ App name           │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem    │ user    │ watching │
├────────────────────┼────┼──────┼─────┼────────┼─────────┼────────┼─────┼────────┼─────────┼──────────┤
│ FeedProject        │ 2  │ fork │ 621 │ online │ 0       │ 90s    │ 0%  │ 0 B    │ yuliang │ enabled  │
│ pm2-http-interface │ 1  │ fork │ 620 │ online │ 0       │ 90s    │ 0%  │ 0 B    │ yuliang │