Skip to main content

应用部署

部署

PM2 具有简单但功能强大的部署系统,允许在生产环境中配置和更新应用程序。当您想一次在一台或多台服务器上的裸机服务器上部署应用程序时,这非常有用。

$ pm2 deploy <configuration_file> <environment> <command>

Commands:
setup run remote setup commands
update update deploy to the latest release
revert [n] revert to [n]th last deployment or 1
curr[ent] output current release commit
prev[ious] output previous release commit
exec|run <cmd> execute the given <cmd>
list list previous deploy commits
[ref] deploy to [ref], the "ref" setting, or latest tag

部署配置

要配置部署系统,请deploy向应用程序配置文件添加一个属性:

module.exports = {
apps : [{
script: 'api.js',
}, {
script: 'worker.js'
}],

// Deployment Configuration
deploy : {
production : {
"user" : "ubuntu",
"host" : ["192.168.0.13", "192.168.0.14", "192.168.0.15"],
"ref" : "origin/master",
"repo" : "git@github.com:Username/repository.git",
"path" : "/var/www/my-repository",
"post-deploy" : "npm install"
}
}
};
注意

确保本地文件夹中的应用程序配置文件名为 ecosystem.config.jspm2.config.js,因此您不需要为每个命令键入配置文件名。

远程配置

在配置远程服务器之前,请确认:

  • 远程服务器安装了 PM2
  • 远程服务器已授予 GIT 克隆目标存储库的权限

配置远程服务器后,您可以开始配置它们:

$ pm2 deploy production setup

:::cautiion 注意 由于app配置文件在本地文件夹中命名为 ecosystem.config.jspm2.config.js,所以不需要每次都指定文件名 :::

部署应用

配置远程服务器后,您现在可以部署应用程序:

$ pm2 deploy production
注意

如果 git 报错说有本地更改但仍想推送远程 GIT 上的内容,您可以使用该--force选项强制部署。

回滚到之前的部署

如果您需要回滚到以前的部署,您可以使用以下 revert 选项:

# Revert to -1 deployment
$ pm2 deploy production revert 1

执行重载

要执行一次性运行命令,您可以使用以下exec选项:

$ pm2 deploy production exec "pm2 reload all"

其他说明

生命周期

使用 PM2 进行部署时,您可以指定在安装之前/之后和更新之前/之后执行的操作:

"pre-setup" : "echo 'commands or local script path to be run on the host before the setup process starts'",
"post-setup": "echo 'commands or a script path to be run on the host after cloning the repo'",
"pre-deploy" : "pm2 startOrRestart ecosystem.json --env production",
"post-deploy" : "pm2 startOrRestart ecosystem.json --env production",
"pre-deploy-local" : "echo 'This is a local executed command'"

多主机部署

要同时部署到多个主机,您只需在属性下的数组中声明每个主机host。

"host" : ["212.83.163.1", "212.83.163.2", "212.83.163.3"],

指定 SSH 密钥

您只需要添加带有公钥路径的“key”属性,请参见下面的示例:

"production" : {
"key" : "/path/to/some.pem", // path to the public key to authenticate
"user" : "node", // user used to authenticate
"host" : "212.83.163.1", // where to connect
"ref" : "origin/master",
"repo" : "git@github.com:repo.git",
"path" : "/var/www/production",
"post-deploy" : "pm2 startOrRestart ecosystem.json --env production"
},

故障排除

SSH错误

在大多数情况下,这些错误是由于pm2没有正确的密钥来克隆您的存储库造成的。您需要在每一步验证密钥是否可用。

步骤1:如果您确定您的密钥正常工作,请首先尝试git clone your_repo.git在目标服务器上运行。如果成功,请继续下一步。如果失败,请确保您的密钥同时存储在服务器和您的 git 帐户中。

步骤2:默认情况下ssh-copy-id复制默认标识,通常命名为id_rsa。如果这不是合适的键:

ssh-copy-id -i path/to/my/key your_username@server.com

这会将您的公钥添加到~/.ssh/authorized_keys文件中。

步骤 3 如果出现以下错误:

--> Deploying to production environment
--> on host mysite.com
○ hook pre-setup
○ running setup
○ cloning git@github.com:user/repo.git
Cloning into '/var/www/app/source'...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and that the repository exists.

**Failed to clone**

Deploy failed

…您可能想要创建一个 ssh 配置文件。这是确保为您尝试克隆的任何给定存储库使用正确的 ssh 密钥的可靠方法。请参阅此示例

# ~/.ssh/config
Host alias
HostName myserver.com
User username
IdentityFile ~/.ssh/mykey
# Usage: `ssh alias`
# Alternative: `ssh -i ~/.ssh/mykey username@myserver.com`

Host deployment
HostName github.com
User username
IdentityFile ~/.ssh/github_rsa
# Usage:
# git@deployment:username/anyrepo.git
# This is for cloning any repo that uses that IdentityFile。This is a good way to make sure that your remote cloning commands use the appropriate key