准备

  • Linux 系统服务器(一般服务器自带)
  • 域名(可选)
  • 待部署的 Node 应用

服务器环境配置

  • Node.js
  • MongoDB
  • nginx

服务器准备

1、购买服务器

注: 使用阿里云服务器esc的话,环境都会默认配置好, 自己不需要再安装 Ubuntu 20.04 ,登录服务器便自带环境

image.png

2、登录服务器

image.png
这里以 Ubuntu 20.04 为例。
在进行下面的操作之前先更新软件包:

  1. apt update
  2. apt upgrade

建议将 Ubuntu 镜像源切换为国内镜像地址,比如清华大学 Ubuntu 镜像源

  1. sudo sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list
  2. sudo sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list

安装 Node

建议使用 nvm 安装和管理 Node 服务。

  1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

修改 nvm 安装 node 的镜像源:

  1. # .bashrc
  2. export NVM_NODEJS_ORG_MIRROR=http://npm.taobao.org/mirrors/node

安装 Node:

  1. nvm install 14.15.4
  2. nvm alias default 14.15.4
  3. node --version

把 npm 安装地址修改为淘宝镜像源:

  1. npm i -g nrm --registry=https://registry.npm.taobao.org
  2. nrm ls
  3. nrm use taobao

安装 MongoDB

建议参考 MongoDB 官方推荐的安装方式

如果乌班图版本选择错误,则会启动服务失败,看好购买的服务器操作系统是否和mongod 版本匹配

安装 MongoDB:

  1. wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
  2. sudo apt-get install gnupg
  3. wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
  4. 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
  5. sudo apt-get update
  6. sudo apt-get install -y mongodb-org

管理 MongoDB:

  1. # 启动 MongoDB
  2. sudo systemctl start mongod
  3. # 查看 MongoDB 启动状态
  4. sudo systemctl status mongod
  5. # 将 MongoDB 设置为开机启动
  6. sudo systemctl enable mongod
  7. # 停止 MongoDB
  8. sudo systemctl stop mongod
  9. # 重启 MongoDB
  10. sudo systemctl restart mongod
  11. # 使用自带的命令行客户端连接 MongoDB
  12. mongo

安装 nginx

例如 Ubuntu 20.04 的安装方式建议参考:https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04

  1. sudo apt install nginx

查看进程:

lsof -i: 端口号

杀死进程

kill-9 pid

管理 nginx 服务:

  1. # 启动 nginx
  2. sudo systemctl start nginx
  3. # 查看 nginx 运行状态
  4. systemctl status nginx
  5. # 停止 nginx
  6. sudo systemctl stop nginx
  7. # 重启 nginx
  8. sudo systemctl restart nginx
  9. # 热重启:如果仅更改配置,Nginx通常可以重新加载而不断开连接
  10. sudo systemctl reload nginx
  11. # 添加开机启动 nginx
  12. sudo systemctl enable nginx
  13. # 禁止开机启动 nginx
  14. sudo systemctl disable nginx

安装 Git

  1. apt install git
  2. git --version

手动部署

关于 Egg.js 应用部署:https://eggjs.org/zh-cn/core/deployment.html

1、将代码提交到 GitHub 远程仓库
2、在远程服务器下载 GitHub 远程仓库
3、启动运行

  1. cd 项目目录
  2. npm i --production
  3. npm start

启动成功之后可以通过 ip 地址访问测试一下:http://服务器ip地址:7001

注意:云服务器防火墙需要开放 7001、80 端口的访问权限。

如果需要通过域名访问:
1、添加域名解析记录
2、配置 nginx 代理

  1. # /etc/nginx/conf.d/youtubeclone
  2. server {
  3. # 监听端口
  4. listen 80;
  5. # 域名可以有多个,用空格隔开
  6. # server_name www.w3cschool.cn w3cschool.cn;
  7. server_name onino.vip;
  8. # 对 / 启用反向代理
  9. location / {
  10. proxy_set_header X-Real-IP $remote_addr;
  11. # 后端的Web服务器可以通过 X-Forwarded-For 获取用户真实 IP
  12. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  13. # 获取真实的请求主机名
  14. proxy_set_header Host $http_host;
  15. # 标识该请求由 nginx 转发
  16. proxy_set_header X-Nginx-Proxy true;
  17. # 代理到本地的 3000 端口服务
  18. proxy_pass http://127.0.0.1:7001;
  19. }
  20. }

3、重启 nginx 服务
4、测试通过域名访问
如果需要更新:

  • 本地开发
  • 提交更新到远程仓库
  • 在远程服务器拉取变更,重新启动服务

设置后端在服务器后台运行 https://xkcoding.com/2018/09/28/run-node-service-in-background.html

自动部署

image.png
1、如果是私有 Git 仓库需要配置服务器通过 SSH 连接 github 的权限

  1. # 生成 SSH key
  2. ssh-keygen -o
  3. # 查看并复制公钥
  4. cat ~/.ssh/id_rsa.pub

将 SSH 公钥添加到 GitHub 账户中

2、配置 GitHub 仓库 secrets

  • USERNAME 服务器用户
  • PASSWORD 服务器用户密码
  • HOST 服务器主机地址
  • PORT 服务器主机端口号
  • ACCESSKEYID 阿里云视频点播服务 access id
  • ACCESSKEYSECRET 阿里云视频点播服务 access key sec

3、编写 GitHub Actions 脚本

  1. # .github/workflows/nodejs.yml
  2. name: Node.js CI
  3. on:
  4. push:
  5. branches: [ master ]
  6. jobs:
  7. build:
  8. runs-on: ${{ matrix.os }}
  9. strategy:
  10. fail-fast: false
  11. matrix:
  12. node-version: [10]
  13. os: [ubuntu-latest]
  14. steps:
  15. - name: deploy
  16. uses: appleboy/ssh-action@master
  17. env:
  18. ACCESSKEYID: ${{ secrets.ACCESSKEYID }}
  19. ACCESSKEYSECRET: ${{ secrets.ACCESSKEYSECRET }}
  20. with:
  21. host: ${{ secrets.HOST }}
  22. username: ${{ secrets.USERNAME }}
  23. password: ${{ secrets.PASSWORD }}
  24. port: ${{ secrets.PORT }}
  25. envs: ACCESSKEYID,ACCESSKEYSECRET
  26. script: |
  27. export ACCESSKEYID=$ACCESSKEYID
  28. export ACCESSKEYSECRET=$ACCESSKEYSECRET
  29. export NVM_DIR="$HOME/.nvm"
  30. [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
  31. [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
  32. cd /root/node-egg
  33. git pull origin master
  34. npm install --production
  35. npm run stop
  36. npm run start

4、将源码提交到 GitHub 远程仓库
5、等待执行自动部署
6、查看部署结果

配置 HTTPS

HTTP 配置升级 HTTPS

一、将前端资源部署到服务器上

前端资源部署到服务器上

二、使用OSS 部署前端资源

前端资源部署到 oss