nginx+pm2+nodejs部署
(可选)服务器端关闭root用户的ssh访问:
为服务器安全起见,关闭root账户的远程访问。
打开配置文件:
vi /etc/ssh/sshd_config
找到如下设置,并修改yes为no:
PermitRootLogin no
保存文件后,重启sshd服务:
service sshd restart
本地安装pm2
npm install pm2 -g
如果可得到版本号,则说明安装成功:
pm2 -v
配置SSH无密码登录
1.1、本地环境配置
#用户家目录 cd .ssh
#生成私钥和公钥 ssh-keygen -t rsa -b 4096 -C "bradball@qq.com"
#开启SSH代理 eval "$(ssh-agent -s)"
#将id_rsa加入代理中 ssh-add ~/.ssh/id_rsa
1.2、server环境搭建
前面步骤和本地环境相同。然后:
sudo vim authorized_keys
将本地id_rsa.pub写入 authorized_keys。
#授权 sudo chmod 600 authorized_keys
#重启ssh sudo service ssh restart
设置Github仓库(略)
本地pm2的ecosystem配置
{ "app": [{ "name" :"Ice", "script": "service.js", "env" :{ "COMMON_VARIABLE": "true" }, "env_production": { "NODE_ENV": "production" } }], "deploy": { "production": { "user": "brad", "host": ["xxx.78.174.xxx"], "ref": "origin/master", "port": "3006", "repo": "git@git.coding.net:dt_xz/ice.git", "path": "/home/brad/ice/production", "ssh_options": "StrictHostKeyChecking=no", "pre-deploy-local": "echo 'Deploy Done'", "env": { "NODE_ENV": "production" } } } }
production : { user : "登录远程服务器的用户名,此处填写我们创建的yishi", host : "远程服务器的IP或hostname,此处可以是数组同步部署多个服务器,不过鉴于我们只有一个服务器,因此我们填写123.57.205.23", ref : "远端名称及分支名,此处填写origin/master", repo : "git仓库地址,此处填写git@github.com:e10101/pm2app.git", path : "远程服务器部署目录,需要填写user具备写入权限的目录,此处填写/home/yishi/www/production", "post-deploy" : "部署后需要执行的命令,此处填写npm install && pm2 startOrRestart ecosystem.json --env production" },
提交代码,在本地运行命令:
pm2 deploy ecosystem.json production setup
提示错误:
Host key verification failed. 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
此时主要是在远程服务器中,并未将http://github.com(或码云)加入known_hosts,在服务器端通过如下命令设置:
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts