服务器
阿里云 https://www.aliyun.com/daily-act/ecs/activity_selection
下载安装包
1wget https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz
解压
1tar xf node-v10.9.0-linux-x64.tar.xz
设置软链接
1ln -s /root/node-v10.9.0-linux-x64/bin/node /usr/local/bin/node2ln -s /root/node-v10.9.0-linux-x64/bin/npm /usr/local/bin/npm`
查看Node版本和npm版本
1node -v2npm -v
设置npm源
1npm config set registry https://registry.npm.taobao.org
服务器安装pm2
1npm install -g pm22ln -s /root/node-v10.9.0-linux-x64/bin/pm2 /usr/local/bin/
配置ssh
- 本地生成秘钥对:
ssh-keygen -t rsa
demo_id_rsa - 将公钥放到服务器上:
1scp ~/.ssh/demo_id_rsa.pub root@39.107.238.161:/root/.ssh/authorized_keys
- 修改ssh配置
vi ~/.ssh/config
1Host john2HostName 39.107.238.1613User root4Port 225IdentityFile ~/.ssh/demo_id_rsa
- 服务器上修改ssh配置
vim /etc/ssh/sshd_config
PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys
- 最后就可以ssh登录了!
ssh john
将本地代码同步到服务器
1rsync -avzp -e "ssh" ./Internet/ john:/root/app
服务器上启动http
1pm2 start /root/app/http-server.js
本地修改发布命令
新建deploy.sh文件
1#!/bin/bash23HOST=john45rsync -avzp -e "ssh" ./Internet/ $HOST:/root/app6ssh $HOST "pm2 restart /root/app/http-server.js"78echo 'deploy success'
初始化npm命令
npm init
新增scripts "deploy": "./deploy.sh"
发布
npm run deploy
修改http-server的监听host
1const http = require('http')2const host = '0.0.0.0';3const port = 80;4http.createServer(function (req, res) {5 res.writeHead(## {6 'Content-Type': 'text/plain'7 })8 res.end('Hello World')9}).listen(port, host)1011console.log(`Server running at http://${host}:${port}/`)
ECS安全组添加80端口
查看服务器上是否已正常监听80端口
netstat -tpln
通过ip+端口访问
39.107.238.161:80