0%

pm2 配置文件说明

如果我们想使用 pm2 来管理多个应用,可以通过配置文件来实现。

初始化配置文件

1
pm2 init simple

这个命令会在当前目录下生成一个 ecosystem.config.js 文件,这个文件就是 pm2 的配置文件。

文件内容大概如下:

1
2
3
4
5
6
module.exports = {
apps : [{
name : "app1",
script : "./app.js"
}]
}

根据配置文件来管理应用

  • pm2 reload 重新加载应用程序,但不会停止正在运行的应用程序实例。它会重新启动应用程序,但在新实例准备就绪之前,旧实例将继续提供服务。这意味着在应用程序重新加载期间,可能会有短暂的服务中断。
  • pm2 restart 停止当前运行的应用程序实例,并启动一个新的实例来替代它。这意味着在重启过程中会有一个短暂的服务中断,因为在新实例启动之前,旧实例将停止提供服务。

启动所有应用

1
pm2 start ecosystem.config.js

停止所有应用

1
pm2 stop ecosystem.config.js

重启所有应用

1
pm2 restart ecosystem.config.js

重载所有应用

1
pm2 reload ecosystem.config.js

删除所有应用

1
pm2 delete ecosystem.config.js

启动特定应用

1
pm2 start ecosystem.config.js --only app1

指定多个:

1
pm2 start ecosystem.config.js --only app1,app2

不同的环境(env)

你可以通过 env_* 来指定不同的环境,比如:

1
2
3
4
5
6
7
8
9
10
11
12
module.exports = {
apps : [{
name : "app1",
script : "./app.js",
env_production: {
NODE_ENV: "production"
},
env_development: {
NODE_ENV: "development"
}
}]
}

然后启动时指定环境:

1
2
pm2 start ecosystem.config.js --env production
pm2 start ecosystem.config.js --env development

配置项详解

配置项 类型 示例 说明
name string "app1" 应用名称
script string "./app.js" 启动脚本
cwd string "/path/to/app" 启动应用程序的目录
args string "-a 13 -b 12" 传递给脚本的参数
interpreter string "/usr/bin/python" 解释器
interpreter_args string "-u" 传递给解释器的参数

高级配置项

  • exec_mode 说明:
    • 在 fork 模式下,每个应用程序实例都在单独的进程中运行。这意味着每个应用程序实例都有自己的内存空间和资源。
    • 在 cluster 模式下,应用程序会以集群的方式运行,使用 Node.js 的 cluster 模块来创建多个子进程。这些子进程共享同一个端口,可以充分利用多核处理器的优势。
配置项 类型 示例 说明
instances number 4 启动多少个实例
exec_mode string "cluster" 执行模式,默认 fork
watch boolean true 是否监视文件变化,文件变动的时候重启进程
ignore_watch array ["node_modules", "logs"] 忽略监视的文件或目录
max_memory_restart string "1G" 当内存使用超过多少时重启进程
env object { NODE_ENV: "development" } 环境变量
env_production object { NODE_ENV: "production" } 生产环境的环境变量,当指定 --env 参数的时候生效
appendEnvToName boolean true 是否将环境变量追加到应用名称后面

日志配置

配置项 类型 示例 说明
log_date_format string "YYYY-MM-DD HH:mm Z" 日志日期格式
error_file string "/path/to/error.log" 错误日志文件,默认 $HOME/.pm2/logs/<app name>-error-<pid>.log
out_file string "/path/to/out.log" 标准输出日志文件,默认 $HOME/.pm2/logs/<app name>-out-<pid>.log
log_file string "/path/to/combined.log" 组合日志文件(标准输出+错误输出)
pid_file string "/path/to/pid" pid 文件,默认 $HOME/.pm2/pids/<app name>-<pid>.pid
combine_logs boolean true 日志文件名不添加 pid 后缀
time boolean true 在日志中添加时间戳

控制流程

配置项 类型 示例 说明
min_uptime number 1000 应用程序在多少毫秒内被认为是启动成功的
listen_timeout number 8000 应用程序启动后多少毫秒没有监听端口就认为启动失败
kill_timeout number 1600 pm2 发送 kill 信号给应用程序后多少毫秒后强制杀死进程
shutdown_with_message boolean false 是否在关闭进程时发送消息给进程
wait_ready boolean false
max_restarts number 10 启动失败之后,最大重试次数
autorestart boolean true 是否自动重启
cron_restart string "0 0 * * *" 定时重启,参考 cron
vizion boolean false 是否启用版本控制
post_update list ["npm install"] 更新后执行的命令
force boolean false 强制启动应用程序

部署

配置项 类型 示例 说明
key string "/path/to/key" ssh 私钥
user string ssh 用户名
host string ssh 主机
ssh_options object { "StrictHostKeyChecking": "no" } ssh 选项
ref string "origin/master" git 分支
repo string git 仓库
path string "/path/to/deploy" 部署路径
pre-setup string "echo 'commands or script to run before setup on target host'" 部署前执行的命令
post-setup string "echo 'commands or script to run after setup on target host'" 部署后执行的命令
pre-deploy-local string "echo 'commands or script to run on local machine before the setup process starts'" 本地部署前执行的命令
post-deploy string "echo 'commands or script to run on target host after the deploy process finishes'" 部署后执行的命令