部署

部署系统

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.js 或 pm2.config.js,这样您就不需要为每个命令键入配置文件名。

配置远程服务器

在配置远程服务器之前,请验证:

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

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

$ pm2 deploy production setup

**注意**:由于应用程序配置文件在本地文件夹中命名为 ecosystem.config.js 或 pm2.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
为此页面做出贡献