pm2运行json文件及pm2-web的配置使用

zhuangzi 2019-08-22

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

pm2运行json文件及pm2-web的配置使用

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;  
        }  
}

相关推荐