前置條件
- 服務(wù)器和本地電腦都安裝了 pm2
參考 http://www.itdecent.cn/p/5021b0871790
- 通過ssh秘鑰方式可以登錄服務(wù)器
參考 http://www.itdecent.cn/p/0311ea113dc5
- 服務(wù)器有權(quán)限訪問 Git 倉庫
參考 http://www.itdecent.cn/p/bc9bee602d2a
配置文件
在項目根目錄下新建一個ecosystem.json文件,配置內(nèi)容如下
{
// Applications part
"apps" : [{
"name" : "API",
"script" : "app.js",
"env": {
"COMMON_VARIABLE": "true"
},
// Environment variables injected when starting with --env production
// http://pm2.keymetrics.io/docs/usage/application-declaration/#switching-to-different-environments
"env_production" : {
"NODE_ENV": "production"
}
},{
"name" : "WEB",
"script" : "web.js"
}],
// Deployment part
// Here you describe each environment
"deploy" : {
"production" : {
"user" : "node",
// Multi host is possible, just by passing IPs/hostname as an array
"host" : ["212.83.163.1", "212.83.163.2", "212.83.163.3"],
// Branch
"ref" : "origin/master",
// Git repository to clone
"repo" : "git@github.com:repo.git",
// Path of the application on target servers
"path" : "/var/www/production",
// Can be used to give options in the format used in the configura-
// tion file. This is useful for specifying options for which there
// is no separate command-line flag, see 'man ssh'
// can be either a single string or an array of strings
"ssh_options": "StrictHostKeyChecking=no",
// To prepare the host by installing required software (eg: git)
// even before the setup process starts
// can be multiple commands separated by the character ";"
// or path to a script on your local machine
"pre-setup" : "apt-get install git",
// Commands / path to a script on the host machine
// This will be executed on the host after cloning the repository
// eg: placing configurations in the shared dir etc
"post-setup": "ls -la",
// Commands to execute locally (on the same machine you deploy things)
// Can be multiple commands separated by the character ";"
"pre-deploy-local" : "echo 'This is a local executed command'"
// Commands to be executed on the server after the repo has been cloned
"post-deploy" : "npm install && pm2 startOrRestart ecosystem.json --env production"
// Environment variables that must be injected in all applications on this env
"env" : {
"NODE_ENV": "production"
}
},
"staging" : {
"user" : "node",
"host" : "212.83.163.1",
"ref" : "origin/master",
"repo" : "git@github.com:repo.git",
"path" : "/var/www/development",
"ssh_options": ["StrictHostKeyChecking=no", "PasswordAuthentication=no"],
"post-deploy" : "pm2 startOrRestart ecosystem.json --env dev",
"env" : {
"NODE_ENV": "staging"
}
}
}
}
完整的配置文件可參考https://pm2.keymetrics.io/docs/usage/deployment/
除了json格式外,配置文件還可以是js或yaml的格式,這里比較推薦使用js格式,其他格式參考官方文檔自行研究。
ecosystem.config.js
module.exports = {
apps: [
{
name: 'test-app',
script: 'app.js', // 入口文件目錄
append_env_to_name: true, // 加上這句后可以實現(xiàn)開發(fā)版本和生產(chǎn)版本部署在同一臺服務(wù)器
// 默認環(huán)境變量
env: {},
// 環(huán)境變量在使用 --env production 作為啟動參數(shù)時生效
// http://pm2.keymetrics.io/docs/usage/application-declaration/#switching-to-different-environments
env_production: {
PORT: 8321,
MONGODB_URL: 'mongodb://loalhost:27017/prod'
},
env_development: {
PORT: 8325,
MONGODB_URL: 'mongodb://loalhost:27017/dev'
}
}
],
deploy: {
// "production" 指定部署的環(huán)境名稱
production: {
// SSH key path, default to $HOME/.ssh
key: '~/.ssh/id_rsa',
// SSH user
user: 'ubuntu',
// SSH host
host: [
{
host: 'server_ip_address',
port: 'ssh_port'
}
],
// SSH options with no command-line flag, see 'man ssh'
// can be either a single string or an array of strings
ssh_options: 'StrictHostKeyChecking=no',
// GIT remote/branch
ref: 'origin/main',
// GIT remote
repo: 'git@github.com:alfalfaw/express-demo.git',
// path 指定項目目錄
path: '/path/to/project/express-demo',
// Pre-setup 在 setup 之前執(zhí)行,如安裝 git
// 'pre-setup': 'apt-get install git ; ls -la',
// Post-setup 在 setup 之后執(zhí)行
// 'post-setup': 'ls -la',
// 每次 update 都會執(zhí)行
'pre-deploy-local': "echo '生產(chǎn)環(huán)境部署中'",
// post-deploy action
'post-deploy': 'npm install && pm2 restart ecosystem.config.js --env production'
},
dev: {
// SSH key path, default to $HOME/.ssh
key: '~/.ssh/id_rsa',
// SSH user
user: 'ubuntu',
// SSH host
host: [
{
host: 'server_ip_address',
port: 'ssh_port'
}
],
// SSH options with no command-line flag, see 'man ssh'
// can be either a single string or an array of strings
ssh_options: 'StrictHostKeyChecking=no',
// GIT remote/branch
ref: 'origin/dev',
// GIT remote
repo: 'git@github.com:alfalfaw/express-demo.git',
// path 指定項目目錄
path: '/path/to/project/express-demo-dev',
// Pre-setup 在 setup 之前執(zhí)行,如安裝 git
// 'pre-setup': 'apt-get install git ; ls -la',
// Post-setup 在 setup 之后執(zhí)行
// 'post-setup': 'ls -la',
// 每次 update 都會執(zhí)行
'pre-deploy-local': "echo '開發(fā)環(huán)境部署中'",
// post-deploy action
'post-deploy': 'npm install && pm2 restart ecosystem.config.js --env development'
}
}
}
項目部署
# 首次部署
pm2 deploy production setup
# 更新版本
pm2 deploy production update
# 返回上一個版本
pm2 deploy production revert 1
# 重啟所有應(yīng)用
pm2 deploy production exec "pm2 reload all"
完整的命令是pm2 deploy <configuration_file> <environment> setup,在未指定配置文件的情況下,默認讀取ecosystem.config.js,如果沒有,則讀取ecosystem.json
如果配置多個app,可以指定name重啟某一個app
pm2 start ecosystem.config.js --only api-app
pm2 restart ecosystem.config.js --only api-app
pm2 reload ecosystem.config.js --only api-app
pm2 delete ecosystem.config.js --only api-app