一、node项目部署流程



二、配置部署脚本文件
在项目根目录添加pm2的部署脚本文件 ecosystem.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| { "apps": [ "name": "movie", "script": "app.js", "env": { "COMON_VARIABLE": "true" }, "env_production": { "NODE_ENV": "production" } ], "deploy": { "production": { "user": "poetries", "host": ["120.120.14.21"], "port": "3922", "ref": "origin/master", "repo": "git@github.com:poetries/poetries.github.io.git", "path": "/www/movie/production", "ssh_options": "StrictHostKeyChecking=no", "post-deploy": "npm install --registry=https://registry.npm.taobao.org && grunt build && pm2 startOrRestart ecosystem.json --env production", "env": { "NODE_ENV": "production" } } } }
|
三、配置nginx的server
在nginx安装目录下的vhost中新建一个xx-3000.conf的配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| upstream movie { server 127.0.0.1:3000; }
server { listen 80; server_name movie.poetries.top; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-nginx-Proxy true; proxy_pass http: proxy_redirect off; } location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt) { root /www/movie/public; } }
|
四、开启防火墙
Ubuntu的设置
- 打开
sudo vi /etc/iptables.up.rules
- 生效
sudo iptables-restore < /etc/iptables.up.rules
1 2 3 4
| -A INPUT -s 127.0.0.1 -p tcp --destination-port 3001 -m state ESTABLISHED -j ACCEPT
-A OUTPUT -s 127.0.0.1 -p tcp --destination-port 3001 -m state ESTABLISHED -j ACCEPT
|
五、部署命令
pm2 deploy ecosystem.json production setup 初始化
pm2 deploy ecosystem.json production 部署