部署系統
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 金鑰
您只需新增「金鑰」屬性,並指定公鑰的路徑,請見以下範例
"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