PM2 deployment
PM2 deployment
生成 ecosystem.json
pm2 ecosystem
查看ecosystem.json
{
/**
* Application configuration section
* http://pm2.keymetrics.io/docs/usage/application-declaration/
*/
apps : [
// First application
{
name : "API",
script : "app.js",
env: {
COMMON_VARIABLE: "true"
},
env_production : {
NODE_ENV: "production"
}
}
],
/**
* Deployment section
* http://pm2.keymetrics.io/docs/usage/deployment/
*/
deploy : {
production : {
user : "node",
host : "212.83.163.1",
ref : "origin/master",
repo : "git@github.com:repo.git",
path : "/var/www/production",
"post-deploy" : "npm install && pm2 startOrRestart ecosystem.json --env production"
},
dev : {
user : "root",
host : "192.168.0.121",
ref : "origin/master",
repo : "http://192.168.0.121:10080/lebond_server/lebond_server.git",
path : "/var/www/development",
"post-deploy" : "npm install && pm2 startOrRestart ecosystem.json --env dev",
env : {
NODE_ENV: "dev"
}
}
}
}
配置文件解析
- apps里配置的是app部署的想着参数,env/env_production里定义开发/生产环境变量
- deploy 项中配置的是不同的环境部署+start相关参数
部署命令
部署到测试环境
pm2 deploy ecosystem.json dev
这条命令为这以下几件事:
- 检查本地代码是否有没有提交的
- 登陆到远程部署服务器,并校验权限
- 更新git服务器代码
- pm2 执行部署命令:
npm install && pm2 startOrRestart ecosystem.json --env dev
频繁部署时出现的问题
pm2 stop all
pm2 delete all
pm2 deploy ecosystem.json dev
pm2 logs
报错信息:
2016-07-05 11:11:02: App name:LEBOND_API id:3 disconnected
PM2: 2016-07-05 11:11:02: App [LEBOND_API] with id [3] and pid [14302], exited with code [1] via signal [SIGINT]
PM2: 2016-07-05 11:11:02: Script /var/www/development/API/source/app.js had too many unstable restarts (16). Stopped. "errored"
PM2: 2016-07-05 11:17:28: Stopping app:LEBOND_API id:0
PM2: 2016-07-05 11:17:28: Stopping app:LEBOND_API id:1
PM2: 2016-07-05 11:17:28: Stopping app:LEBOND_API id:2
PM2: 2016-07-05 11:17:28: Stopping app:LEBOND_API id:3
[PM2] Streaming realtime logs for [all] processes
这时是因为:
Increasing GRACEFUL_LISTEN_TIMEOUT value fixes the issue - by default, it is set to 3 seconds, which means that pm2 gives up too quickly and goes on to next instance.
意思是因为pm2本身对于频繁的删除与部署应用有个延迟销毁进程的时间,默认是3分钟,这里提供了两种解决方案:
You can change the value like this:
PM2_GRACEFUL_LISTEN_TIMEOUT=15000 pm2 update
野蛮的方式,强制重启所有应用
pm2 kill rm -rf node_modules npm i pm2 start index.js -xn 'myapp'
部署时免密码输入
如果是linux环境:
生成公钥-私钥:
$ ssh-keygen -t rsa
$ ssh-copy-id node@myserver.com
在两台机器的两个用户之间建立安全的信任关系后,可实现执行scp命令时不需要输入用户密码。
- 在机器Client上root用户执行ssh-keygen命令,生成建立安全信任关系的证书。
[root@Client root]# ssh-keygen -b 1024 -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): <– 直接输入回车
Enter passphrase (empty for no passphrase): <– 直接输入回车
Enter same passphrase again: <– 直接输入回车
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is: ……
注意:在程序提示输入passphrase时直接输入回车,表示无证书密码。
上述命令将生成私钥证书id_rsa和公钥证书id_rsa.pub,存放在用户目录的.ssh子目录中。
- 将公钥证书id_rsa.pub复制到机器Server的root目录的.ssh子目录中,同时将文件名更换为authorized_keys。(如果Server机器用户的主目录下无.ssh文件,则是因为服务器没有用ssh做过任何远程操作,只要执行下ssh登陆远程机器就行,哪怕没登陆成功,只要执行就好,复制到server的公钥名字必须是authorized_keys)
[root@Client root]# scp -p .ssh/id_rsa.pub root@172.16.100.188:/root/.ssh/authorized_keys
root@192.168.3.206’s password:<– 输入机器Server的root用户密码
在执行上述命令时,两台机器的root用户之间还未建立安全信任关系,所以还需要输入机器Server的root用户密码。
经过以上2步,就在机器Client的root和机器Server的root之间建立安全信任关系。下面我们看看效果:
[root@Client root]# scp -p text root@192.168.3.206:/root
成功了!真的不再需要输入密码了。
最后再执行:
pm2 deploy ecosystem.json dev
一步搞定。。。。。
如果需要详情操作,请参考实现linux服务器之间无密码互访
Now initialize the remote folder with:
$ pm2 deploy <configuration_file> <environment> setup
E.g:
$ pm2 deploy ecosystem.json production setup
This command will create all the folders on your remote server.
Deploy your code
$ pm2 deploy ecosystem.json production
Now your code will be populated, installed and started with PM2
关于windows系统配置linux服务器间无密码登录
安装一个gitbash或者安装一个ssh命令行客户端,就可以和linux一样的配置了。。。。。。