共计 5704 个字符,预计需要花费 15 分钟才能阅读完成。
pm2 运行 json 文件启动 node 项目及 pm2-web 的安装
定义运行配置文件
我们可以预定义一个配置文件,然后制定运行这个配置文件,比如我们定义一个文件 app.json,内容如下:
[root@jenkins-master linux]# cat app.json
{
"apps":[
{
"name": "service-sk_platform_professional_dev",
"cwd": "/data/source/service-sk_platform_professional_dev/publish/",
"script": "/data/source/service-sk_platform_professional_dev/publish/service/server.js",
"log_date_format": "YYYY-MM-DD HH:mm Z",
"error_file": "/data/pm2-log/errlogs/service-sk_platform_professional_dev-err.log",
"out_file": "/data/pm2-log/outlogs/service-sk_platform_professional_dev-out.log",
"pid_file": "/data/pm2-log/outlogs/service-sk_platform_professional_dev.pid",
"instances": 1,
"node_args": "--inspect=0.0.0.0:33111",
"min_uptime": "20s",
"max_restarts": 50,
"watch": false,
"merge_logs": true,
"exec_interpreter": "node",
"exec_mode": "fork",
}
]
}
[root@node-44 ~]# cat app.json
{
"apps":[
{
"name": "sk_service_calculate_convert_dev",
"cwd": "/data/source/sk_service_calculate_convert_dev/publish/",
"script": "/data/source/sk_service_calculate_convert_dev/publish/service/server.js",
"log_date_format": "YYYY-MM-DD HH:mm Z",
"error_file": "/data/pm2-log/errlogs/sk_service_calculate_convert_dev-err.log",
"out_file": "/data/pm2-log/outlogs/sk_service_calculate_convert_dev-out.log",
"pid_file": "/data/pm2-log/outlogs/sk_service_calculate_convert_dev.pid",
"instances": 1,
"min_uptime": "20s",
"max_restarts": 50,
"watch": false,
"merge_logs": true,
"exec_interpreter": "node",
"exec_mode": "fork",
},{
"name": "sk_service_calculate_convert_dev2",
"cwd": "/data/source/sk_service_calculate_convert_dev/publish/",
"script": "/data/source/sk_service_calculate_convert_dev/publish/service/server.js",
"args": "'debug' '12345'",
"log_date_format": "YYYY-MM-DD HH:mm Z",
"error_file": "/data/pm2-log/errlogs/sk_service_calculate_convert2_dev-err.log",
"out_file": "/data/pm2-log/outlogs/sk_service_calculate_convert2_dev-out.log",
"pid_file": "/data/pm2-log/outlogs/sk_service_calculate_convert2_dev.pid",
"instances": 1,
"min_uptime": "20s",
"max_restarts": 50,
"watch": false,
"merge_logs": true,
"exec_interpreter": "node",
"exec_mode": "fork",
},{
"name": "sk_service_calculate_convert_dev3",
"cwd": "/data/source/sk_service_calculate_convert_dev/publish/",
"script": "/data/source/sk_service_calculate_convert_dev/publish/service/server.js",
"args": "'debug' '12344'",
"log_date_format": "YYYY-MM-DD HH:mm Z",
"error_file": "/data/pm2-log/errlogs/sk_service_calculate_convert3_dev-err.log",
"out_file": "/data/pm2-log/outlogs/sk_service_calculate_convert3_dev-out.log",
"pid_file": "/data/pm2-log/outlogs/sk_service_calculate_convert3_dev.pid",
"instances": 1,
"min_uptime": "20s",
"max_restarts": 50,
"watch": false,
"merge_logs": true,
"exec_interpreter": "node",
"exec_mode": "fork",
},{
"name": "sk_service_calculate_convert_dev4",
"cwd": "/data/source/sk_service_calculate_convert_dev/publish/",
"script": "/data/source/sk_service_calculate_convert_dev/publish/service/server.js",
"args": "'debug' '12343'",
"log_date_format": "YYYY-MM-DD HH:mm Z",
"error_file": "/data/pm2-log/errlogs/sk_service_calculate_convert4_dev-err.log",
"out_file": "/data/pm2-log/outlogs/sk_service_calculate_convert4_dev-out.log",
"pid_file": "/data/pm2-log/outlogs/sk_service_calculate_convert4_dev.pid",
"instances": 1,
"min_uptime": "20s",
"max_restarts": 50,
"watch": false,
"merge_logs": true,
"exec_interpreter": "node",
"exec_mode": "fork",
}
]
}
for (int i = 0;i<"${node_number}".toInteger();i++){
Integer nodeport=10000+i
sh "cat /data/ansible/playbook/linux/linux_app.json |sed -e's/sourcepath_node_Jenvironment/${projectname}_${environment}_${i}/g'-e's/sourcepath_Jenvironment/${projectname}_${environment}/g'-e's/process_mode/${process_mode}/g'-e's/node_port/${nodeport}/g'>>${WORKSPACE}/publish/app.json"
sh(script: "echo -n',\r\n'>>${WORKSPACE}/publish/app.json")
}
- 然后可以通过, 运行这个 App。
pm2 start app.json
apps:json 结构,apps 是一个数组,每一个数组成员就是对应一个 pm2 中运行的应用
name: 应用程序名称
args: 脚本的参数域
cwd: 应用程序所在的目录
script: 应用程序的脚本路径
log_date_format:
node_args:node 的参数域
error_file: 自定义应用程序的错误日志文件
out_file: 自定义应用程序日志文件
pid_file: 自定义应用程序的 pid 文件
instances:
min_uptime: 最小运行时间,这里设置的是 60s 即如果应用程序在 60s 内退出,pm2 会认为程序异常退出,此时触发重启 max_restarts 设置数量
max_restarts: 设置应用程序异常退出重启的次数,默认 15 次(从 0 开始计数)cron_restart: 定时启动,解决重启能解决的问题
watch: 是否启用监控模式,默认是 false。如果设置成 true,当应用程序变动时,pm2 会自动重载。这里也可以设置你要监控的文件。merge_logs:
exec_interpreter: 应用程序的脚本类型,这里使用的 shell,默认是 nodejs
exec_mode: 应用程序启动模式,这里设置的是 cluster_mode(集群),默认是 fork
autorestart: 启用 / 禁用应用程序崩溃或退出时自动重启
vizion: 启用 / 禁用 vizion 特性 (版本控制)
总结 - 常用命令总结如下:
- 安装 pm2
npm install -g pm2
- 启动应用
pm2 start app.js
- 列出所有应用
pm2 list
- 查看资源消耗
pm2 monit
- 查看某一个应用状态
pm2 describe [app id]
- 查看所有日志
pm2 logs
- 重启应用
pm2 restart [app id]
- 停止应用
pm2 stop [app id]
- 开启 api 访问
pm2 web
安装 pm2-web
安装软件
安装时需要 g ++,make 等软件,下面的软件你自己挑(我是刚装完系统就都装了)yum -y install ntpdate lrzsz tree cmake gcc gcc-c++ autoconf l libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel libxslt-devel libtool-ltdl-devel make wget docbook-dtds asciidoc e2fsprogs-devel gd gd-devel openssl openssl-devel lsof git unzip gettext-devel gettext libevent libevent-devel pcre pcre-devel
npm install --unsafe-perm -g pm2-web
pm2-web
配置启动文件
[root@localhost ~]# cat pm2-web-config.json
{
"www": {
"host": "localhost",
"address": "0.0.0.0",
"port": 11111
}
}
[root@localhost ~]# cat pm2-web-start.sh
nohup pm2-web --config pm2-web-config.json >pm2-web.log&
[root@localhost ~]# sh +x pm2-web-start.sh
server {
listen 80;
server_name pm2-web.xxxx.com;
access_log /data/nginx/logs/node-web.xxx.com/node-web.xxxx.com.log main;
location / {
auth_basic "node-web monitor";
auth_basic_user_file /data/nginx/htpasswd.conf; // 设置用户访问控制
proxy_pass http://192.168.0.xxx:xxxx;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_connect_timeout 360;
proxy_send_timeout 360;
proxy_read_timeout 360;
}
}
正文完