使用pm2管理nodejs应用


2019-3-16 CentOS7 Pm2 Git 自动化部署

pm2 管理 nodejs 应用

如果你是IT 大咖请略过这篇文章,这只是菜鸟在升级过程中的笔记。

pm2 的安装

pm2 的安装很简单

npm install pm2 -g  # 这里注意要安装到全局
1

pm2 的简单使用

pm2 start  入口 --name 应用名称 #即可启动应用  出现错误可以自动重新启动
pm2 restart 入口 --name 应用名称 #重启进程 all为重启所有进程

pm2 stop 应用名 或者进程号 # 停止

pm2 delete 应用名称  或者进程号 #从pm2 的进程池中删除对应的进程
pm2 list # 列出所有的在运行的列表

pm2 logs # 调取pm2 的日志 包括错误以及系统输出日志

# 生成一个基本的配置文件
pm2 describe app #查看某个进程具体情况
pm2 monit #查看进程的资源消耗情况
pm2 ecosystem # 生成配置为文件
pm2 startup centos #设置pm2开机自启(可选项:ubuntu, centos, redhat, gentoo, systemd, darwin, amazon)
pm2 save # 保存pm2 的设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

其他命令请查看手册

pm2 的配置文件

module.exports = {
  apps: [{
    name: '应用启动后在pm2 中的名称',
    script: 'server.js', # 启动的入口
    args: 'one two',
    instances: 1, // 启动的实例数量 如果开启集群模式就多填几个
    //  instances: 2, // 集群实例,可以只有一个,
    exec_mode: "cluster",// 开启的模式
    autorestart: true,// 是否自动启动
    watch: false,// 是否监视代码变动 ,这里要注意如果开启日志也会被监视 有可能会造成无限重启
    min_uptime: "60s", // 应用运行少于时间被认为是异常启动;
    max_restarts: 30, //重启次数
    error_file: "./logs/app-err.log",//错误日志的存储位置
    out_file: "./logs/app-out.log",//访问资源日志的存储位置
    cron_restart: '10 1 * * 6', //# 分时日月周 命令command  每周的周六  晚上1点10 分  重启
    wait_ready: true,//等待完成
    listen_timeout: 3000,//等待开启时间
    restart_delay: 6000,//延迟启动时间
    log_date_format: "YYYY-MM-DD HH:mm Z", // pm2 log添加日期
    max_memory_restart: '1G',//最大的内存,防止内存溢出
    env: {
      NODE_ENV: 'development'//配置环境变量
    },
    env_production: {
      NODE_ENV: 'production'//配置环境变量
    }
  }],

};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

使用脚本启动

上面是一个有用的基本配置文件,如果你是开发过程中建议开启 watch 然后启动的时候

pm2 start 配置文件的名称  --env # 最后这部分和配置文件中要开始的模式名称相同
1

如果再正式环境中不开开启 watch

启动脚本

pm2 start 配置文件的名称  --env_production
1

自动化部署

  1. 先要在服务器上生成 git 关于远端仓库的 ssh 密匙,同时

     ssh-keygen -t rsa -C "xxx@xxx.com"
    
    1
  2. 在~/.ssh 目录下有 id_rsa 和 id_rsa.pub 两个文件,其中 id_rsa.pub 文件里存放的即是公钥 key。

  3. 登录到 GitHub,Add SSH key,把 id_rsa.pub 的内容上传。

  4. 本地的应用代码关联此 Github 仓库,在应用文件夹的根目录执行:

    git remote add origin https://github.com/需要关联的项目地址
    git push -u origin master
    
    1
    2
  5. 以上环节都是在服务器中进行的,接下来在本地项目开发中执行的

    1. 在项目的配置文件中加入自动话部署的脚本

      # pm2 有服务器的自动化部署脚本
      deploy: { //git自动化部署脚本
          production: {//确定是生产环境
          user: 'node',//服务器的用户名
          host: '212.83.163.1', //服务器主机ip  公网
          port:'22',//服务器的端口
          ref: 'origin/master', //分支
          'ssh_options': 'StrictHostKeyChecking=no', // SSH 公钥检查
          repo: 'git@github.com:repo.git', //代码源
          path: '/home/new-land/production', //下载到的目录
          'pre-deploy': 'git fetch --all' //部署前执行,
          'post-deploy': 'npm install && pm2 reload  ecosystem.config.js --env production'//部署后执行命令
              // env:
              //     NODE_ENV: development//执行后的环境
          }
      }
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
    2. 开通本地到远程服务器的无密码登录,在本地生成 sshkey 或者在远端生成 ssh key 在现在到本地

    3. 在本地应用目录下,执行 pm2 deploy 命令 shell pm2 deploy ecosystem.json production setup

    4. 如果提示错误 Host key verification failed. 则执行ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts

    5. 如果 Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known host 则先编辑 etc/hosts 文件 加入 github 的 dns 52.74.223.119 github.com

    6. 如果 the authenticity of host cant't be established 错误则是没有 ssh 检查,用 vim 打开 在/etc/ssh/ssh_config 这个文件 并且后加入

      StrictHostKeyChecking no
      UserKnownHostsFile /dev/null
      
      1
      2

      重启 ssh service ssh restart

    7. 如果出现 setup complete Success 则成功

    8. 实际部署 在本地 执行 git 正常提交命令

      git add .
      git commit -m "update ecosystem"
      git push
      pm2 deploy ecosystem.json production #即可完成部署
      
      1
      2
      3
      4