现在DevOps如此的火,我们是不是也应该要达到生产部署一条龙服务呢?今天我就用pm2实现一个koa后台远程部署到阿里云ECS上。
全局安装pm2
npm install -g pm2 # yarn global add pm2
生成pm2配置文件
- 在项目根目录生成pm2执行如下代码,生成pm2配置文件ecosystem.config.js
pm2 ecosystem
- 对ecosystem.config.js进行修改,修改部分在depoly部分,有注释
module.exports = {
/**
* Application configuration section
* http://pm2.keymetrics.io/docs/usage/application-declaration/
*/
apps : [
// First application
{
name : 'koatest',
script : 'app.js',
env: {
COMMON_VARIABLE: 'true'
},
env_production : {
NODE_ENV: 'production'
}
},
// Second application
{
name : 'WEB',
script : 'web.js'
- }
],
/**
* Deployment section
* http://pm2.keymetrics.io/docs/usage/deployment/
*/
deploy : {
production : {
user : 'c4', //也可以useradd另建用户
host : '47.95.247.139', // 服务器地址
ref : 'origin/master',
repo : 'git@github.com:guzhongren/backend-based-on-Koa2.git', // github上的项目地址
path : '/home/c4/depoly', // 服务器上放项目的目录
'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
},
dev : {
user : 'c4',
host : '47.95.247.139',
ref : 'origin/master',
repo : 'git@github.com:guzhongren/backend-based-on-Koa2.git',
path : '/home/c4/depoly',
'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env dev',
env : {
NODE_ENV: 'dev'
}
}
}
};
- 如果有ssh或者github权限问题,还请自行解决。
部署
- 使用git将新的更改和文件提交到github上。
git add .
git commit -m 'something'
git pull
git push
- 本地执行以下命令,用于部署设置,需要注意的是,对应的ssh端口是默认的22。
pm2 deploy ecosystem.config.js production setup
成功后显示如下
D:\04.Study\01.Web\backend-based-on-Koa2>pm2 deploy ecosystem.config.js production setup
--> Deploying to production environment
--> on host 47.95.247.139
○ hook pre-setup
c4@47.95.247.139's password:
○ running setup
○ cloning git@github.com:guzhongren/backend-based-on-Koa2.git
○ full fetch
c4@47.95.247.139's password:
Cloning into '/home/c4/depoly/source'...
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
c4@47.95.247.139's password:
○ hook post-setup
○ setup complete
--> Success
- 部署设置完毕后,执行以下命令进行部署。
pm2 deploy ecosystem.config.js production
成功后如下:
D:\04.Study\01.Web\backend-based-on-Koa2>pm2 deploy ecosystem.config.js production
--> Deploying to production environment
--> on host 47.95.247.139
○ deploying origin/master
○ executing pre-deploy-local
○ hook pre-deploy
○ fetching updates
○ full fetch
c4@47.95.247.139's password:
Fetching origin
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
From github.com:guzhongren/backend-based-on-Koa2
bd63b11..d6214e4 master -> origin/master
○ resetting HEAD to origin/master
c4@47.95.247.139's password:
Permission denied, please try again.
c4@47.95.247.139's password:
HEAD is now at d6214e4 update
c4@47.95.247.139's password:
c4@47.95.247.139's password:
○ executing post-deploy `export && npm install && pm2 reload ecosystem.config.js --env production`
c4@47.95.247.139's password:
HOME=/home/c4
LANG=en_US.UTF-8
LOGNAME=c4
MAIL=/var/mail/c4
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
SHELL=/usr/bin/zsh
SSH_CLIENT='202.100.35.103 19581 22'
SSH_CONNECTION='202.100.35.103 19581 172.17.193.126 22'
USER=c4
XDG_RUNTIME_DIR=/run/user/1000
XDG_SESSION_ID=24
npm WARN optional Skipping failed optional dependency /chokidar/fsevents:
npm WARN zsh:1: command not found: pm2
notsup Not compatible with your operating system or architecture: fsevents@1.1.3
○ hook test
○ successfully deployed origin/master
--> Success
错误处理
- 权限问题
D:\04.Study\01.Web\backend-based-on-Koa2>pm2 deploy ecosystem.config.js production setup
--> Deploying to production environment
--> on host 47.95.247.139
○ hook pre-setup
mkdir: cannot create directory ‘/home/c4/depoly/shared’: Permission denied
mkdir: cannot create directory ‘/home/c4/depoly/shared’: Permission denied
mkdir: cannot create directory ‘/home/c4/depoly/source’: Permission denied
setup paths failed
Deploy failed
此问题为部署所在文件夹的权限,用chown脚本即可,如下实例
sudo chown -hR c4:c4 ./depoly
- github ssh问题
D:\04.Study\01.Web\backend-based-on-Koa2>pm2 deploy ecosystem.config.js production setup
--> Deploying to production environment
--> on host 47.95.247.139
○ hook pre-setup
c4@47.95.247.139's password:
Permission denied, please try again.
c4@47.95.247.139's password:
○ running setup
○ cloning git@github.com:guzhongren/backend-based-on-Koa2.git
○ full fetch
c4@47.95.247.139's password:
Cloning into '/home/c4/depoly/source'...
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
failed to clone
Deploy failed
解决办法
在部署机器上用ssh-keygen生成密钥。
- 执行如下命令
ssh-keygen -t rsa -C 'guzhongren@live.cn'
cat ~/.ssh/id_rsa.pub
- 将cat输出的文字拷贝到github->setting->SSH and GPG keys中。
pm2其他命令说明
命令 | 说明 |
---|---|
pm2 start app.js | 启动应用 |
pm2 list | 列出所有应用 |
pm2 monit | 查看资源消耗 |
pm2 describe [app/id] | 查看某一个应用的状态 |
pm2 logs | 查看所有日志 |
pm2 restart [app/id] | 重启应用 |
pm2 stop [app/id/all] | 停止应用 |
pm2 web | 访问 url:9615 |
总结
pm2使用hai还是非常简单的,有很多如Monitor之类的功能很实用,还需深度探索。