1.生成一組SSH密鑰git
ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -C "qqq@qq.com"
複製代碼
qqq@qq.com是本身的綁定郵箱npm
2.將新生產的私鑰生效json
ssh-agent bash && ssh-add ~/.ssh/id_rsa
exit
複製代碼
3.將下面命令輸出的公鑰複製centos
cat ~/.ssh/id_rsa.pub
複製代碼
4.而後centos下執行,git clone git@XXX.gitbash
拉取代碼成功說明配置好了。服務器
scp ~/.ssh/id_rsa.pub username@127.X.X.X:/root/.ssh/authorized_keys
複製代碼
username:用戶名app
127.X.X.X:你的遠程ip地址ssh
執行後會輸入一次密碼,而後顯示下面信息,說明配置好了post
id_rsa.pub 100% 402 10.2KB/s 00:00
複製代碼
一、全局安裝pm2,ui
cnpm install pm2 -g
複製代碼
二、新建ecosystem.json,內容大體以下
{
"apps" : [{
"name" : "admin",
"script" : "server.js",
"env": {
"COMMON_VARIABLE": "true"
},
"env_production" : {
"NODE_ENV": "production"
}
}],
"deploy" : {
"production" : {
"user" : "username",
"host" : ["123.x.x.x"],
"ref" : "origin/master",
"repo" : "git@xxx.git",
"path" : "/data/blog/admin",
"post-deploy" : "npm install && npm run build && pm2 startOrRestart ecosystem.json --env production",
"ssh_options": "StrictHostKeyChecking=no",
"env" : {
"NODE_ENV": "production"
}
}
}
}
複製代碼
更多配置參考官網:pm2.keymetrics.io/docs/usage/…
三、提交代碼,
git add .
git commit -m 'change ecosystem'
git push origin master
複製代碼
四、初始化服務器(Server)應用,部署pm2配置
pm2 deploy ecosystem.json production setup
複製代碼
成功後會打印:
...
○ hook post-setup
○ setup complete
--> Success
複製代碼
五、服務器從git代碼託管平臺獲取最新的代碼,並使用pm2自動部署
pm2 deploy ecosystem.json production
複製代碼