nodejs实践录:pm2配置文件ecosystem.config.js说明

版权声明:本文为迟思堂主人李迟原创文章,版权所有。可随便任意使用(包括学习研究商用),但由此带来的成果或后果,概与作者无关。胡乱修改的,不注明出处的,概不负责。 https://blog.csdn.net/subfate/article/details/86766243

本文介绍pm2配置文件ecosystem.config.js
ecosystem.config.js是pm2默认的配置文件,这个文件可以改为其它名称。本文统一使用默认名称。

ecosystem.config.js的官方说明,请移步http://pm2.keymetrics.io/docs/usage/application-declaration/查阅。

生成ecosystem.config.js文件

在工程目录输入:

$ pm2 ecosystem
File E:\project\newworld\nodejs\test\log_test\ecosystem.config.js generated

解释

默认的配置文件如下:

module.exports = {
  apps : [{
    name: 'API',
    script: 'app.js',

    // Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
    args: 'one two',
    instances: 1,
    autorestart: true,
    watch: false,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  }],

  deploy : {
    production : {
      user : 'node',
      host : '212.83.163.1',
      ref  : 'origin/master',
      repo : '[email protected]:repo.git',
      path : '/var/www/production',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
    }
  }
};

下面解释主要的字段。

  • apps: 应用程序列表,pm2可以管理多个程序。

集群模式的配置文件

配置文件示例如下:

module.exports = {
  // APP是一个数组,可以有多个
  // 参数参考:https://pm2.io/doc/en/runtime/reference/ecosystem-file/
  apps : [
  {
    name: 'app',
    script: 'app.js',
    args: 'null',
    instances: 2,  // 集群实例,可以只有一个,这样表现上与fork无异,但可以用scale
    exec_mode: "cluster", // 集群模式,如不指定,默认为fork
    autorestart: false,
    min_uptime: "60s",
    max_restarts: 3,
    watch: false,
    //error_file: "./logs/app-err.log",
    //out_file: "./logs/app-out.log",
    log: "./logs/app.log",
    //log_date_format: "YYYY-MM-DD HH:mm Z", // pm2 log添加日期
    max_memory_restart: '1G',
    //listen_timeout: 3000,
    kill_timeout: 3000,
    // wait_ready: true,
    env:
    {
      NODE_ENV: 'development'
    },
    env_production:
    {
      NODE_ENV: 'production'
    }
  }
  ],

  // 部署,暂未使用,不用理
  deploy : {
    production : {
      user : 'node',
      host : '212.83.163.1',
      ref  : 'origin/master',
      repo : '[email protected]:repo.git',
      path : '/var/www/production',
      'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
    }
  }
};

李迟 2019.2.5 周二

猜你喜欢

转载自blog.csdn.net/subfate/article/details/86766243