准备
- Linux 系统服务器(一般服务器自带)
- 域名(可选)
- 待部署的 Node 应用
服务器环境配置
- Node.js
- MongoDB
- nginx
服务器准备
1、购买服务器
注: 使用阿里云服务器esc的话,环境都会默认配置好, 自己不需要再安装 Ubuntu 20.04 ,登录服务器便自带环境
2、登录服务器
这里以 Ubuntu 20.04 为例。
在进行下面的操作之前先更新软件包:
apt update
apt upgrade
建议将 Ubuntu 镜像源切换为国内镜像地址,比如清华大学 Ubuntu 镜像源。
sudo sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
sudo sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
安装 Node
建议使用 nvm 安装和管理 Node 服务。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
修改 nvm 安装 node 的镜像源:
# .bashrc
export NVM_NODEJS_ORG_MIRROR=http://npm.taobao.org/mirrors/node
安装 Node:
nvm install 14.15.4
nvm alias default 14.15.4
node --version
把 npm 安装地址修改为淘宝镜像源:
npm i -g nrm --registry=https://registry.npm.taobao.org
nrm ls
nrm use taobao
安装 MongoDB
建议参考 MongoDB 官方推荐的安装方式。
如果乌班图版本选择错误,则会启动服务失败,看好购买的服务器操作系统是否和mongod 版本匹配
安装 MongoDB:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
sudo apt-get install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
sudo apt-get update
sudo apt-get install -y mongodb-org
管理 MongoDB:
# 启动 MongoDB
sudo systemctl start mongod
# 查看 MongoDB 启动状态
sudo systemctl status mongod
# 将 MongoDB 设置为开机启动
sudo systemctl enable mongod
# 停止 MongoDB
sudo systemctl stop mongod
# 重启 MongoDB
sudo systemctl restart mongod
# 使用自带的命令行客户端连接 MongoDB
mongo
安装 nginx
例如 Ubuntu 20.04 的安装方式建议参考:https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04。
sudo apt install nginx
查看进程:
lsof -i: 端口号
杀死进程
kill-9 pid
管理 nginx 服务:
# 启动 nginx
sudo systemctl start nginx
# 查看 nginx 运行状态
systemctl status nginx
# 停止 nginx
sudo systemctl stop nginx
# 重启 nginx
sudo systemctl restart nginx
# 热重启:如果仅更改配置,Nginx通常可以重新加载而不断开连接
sudo systemctl reload nginx
# 添加开机启动 nginx
sudo systemctl enable nginx
# 禁止开机启动 nginx
sudo systemctl disable nginx
安装 Git
apt install git
git --version
手动部署
关于 Egg.js 应用部署:https://eggjs.org/zh-cn/core/deployment.html。
1、将代码提交到 GitHub 远程仓库
2、在远程服务器下载 GitHub 远程仓库
3、启动运行
cd 项目目录
npm i --production
npm start
启动成功之后可以通过 ip 地址访问测试一下:http://服务器ip地址:7001
。
注意:云服务器防火墙需要开放 7001、80 端口的访问权限。
如果需要通过域名访问:
1、添加域名解析记录
2、配置 nginx 代理
# /etc/nginx/conf.d/youtubeclone
server {
# 监听端口
listen 80;
# 域名可以有多个,用空格隔开
# server_name www.w3cschool.cn w3cschool.cn;
server_name onino.vip;
# 对 / 启用反向代理
location / {
proxy_set_header X-Real-IP $remote_addr;
# 后端的Web服务器可以通过 X-Forwarded-For 获取用户真实 IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 获取真实的请求主机名
proxy_set_header Host $http_host;
# 标识该请求由 nginx 转发
proxy_set_header X-Nginx-Proxy true;
# 代理到本地的 3000 端口服务
proxy_pass http://127.0.0.1:7001;
}
}
3、重启 nginx 服务
4、测试通过域名访问
如果需要更新:
- 本地开发
- 提交更新到远程仓库
- 在远程服务器拉取变更,重新启动服务
设置后端在服务器后台运行 https://xkcoding.com/2018/09/28/run-node-service-in-background.html
自动部署
1、如果是私有 Git 仓库需要配置服务器通过 SSH 连接 github 的权限
# 生成 SSH key
ssh-keygen -o
# 查看并复制公钥
cat ~/.ssh/id_rsa.pub
2、配置 GitHub 仓库 secrets
- USERNAME 服务器用户
- PASSWORD 服务器用户密码
- HOST 服务器主机地址
- PORT 服务器主机端口号
- ACCESSKEYID 阿里云视频点播服务 access id
- ACCESSKEYSECRET 阿里云视频点播服务 access key sec
3、编写 GitHub Actions 脚本
# .github/workflows/nodejs.yml
name: Node.js CI
on:
push:
branches: [ master ]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
node-version: [10]
os: [ubuntu-latest]
steps:
- name: deploy
uses: appleboy/ssh-action@master
env:
ACCESSKEYID: ${{ secrets.ACCESSKEYID }}
ACCESSKEYSECRET: ${{ secrets.ACCESSKEYSECRET }}
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
port: ${{ secrets.PORT }}
envs: ACCESSKEYID,ACCESSKEYSECRET
script: |
export ACCESSKEYID=$ACCESSKEYID
export ACCESSKEYSECRET=$ACCESSKEYSECRET
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
cd /root/node-egg
git pull origin master
npm install --production
npm run stop
npm run start
4、将源码提交到 GitHub 远程仓库
5、等待执行自动部署
6、查看部署结果