服务器环境配置

  • Node.js
  • MongoDB
  • nginx

这里以 Ubuntu 18.04 为例。

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

切换镜像地址

进入linux服务器:
备份 sources.list文件

  1. sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup

vim sources.list
清空之前内容,选择Ubuntu版本并复制以下内容到sources.list文件

  1. # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
  2. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
  3. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
  4. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
  5. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
  6. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
  7. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
  8. deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
  9. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
  10. # 预发布软件源,不建议启用
  11. # deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
  12. # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

在进行下面的操作之前先更新软件包:

  1. apt update
  2. apt upgrade

切换镜像可能导致的问题

执行以下命令:

  1. sudo apt update
  2. sudo apt install nginx

报出:Unable to locate package
如果出现这样的情况,恢复sources.list原本配置,或使用其他镜像。

安装 Node

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

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

如果因为网络环境的原因无法连接到外网资源,通过https://www.ipaddress.com类似的网站解析出[https://raw.githubusercontent.com](https://raw.githubusercontent.com)的ip地址,vim /etc/hosts

  1. 199.232.96.133 raw.githubusercontent.com

保存好之后再次执行下载命令,下载好之后,nvm被放在/root/.nvm目录
关闭终端、重新连接服务器就可以使用nvm了。
修改 nvm 安装 node 的镜像源:
由于下载是从node官网下载的,速度会比较慢,配置国内镜像
vi .bashrc

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

source .bashrc(重新载入配置文件)

安装 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 官方推荐的安装方式。
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
Ubuntu20.04版本 安装 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

管理 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 远程仓库

服务器中使用ssh协议需要权限

image.png
通过ssh-keygen生成ssh-key, 默认生成到/root/.ssh/id_rsa目录下,接下来提示设置密码,不设置密码直接下一步。生成好之后复制公钥到github中->设置->ssh公钥
image.png
第一次连接时需要保存指纹信息,yes
image.png
如果下载报错可能是 /etc/ssh/ssh_config 文件的配置出了问题,我这里在文件末尾加了:PermitRootLogin yes 导致报错。

安装cnpm

  1. //安装cnpm
  2. npm install -g cnpm --registry=https://registry.npm.taobao.org
  3. // 建立软连接
  4. sudo ln -s /root/softwares/node_14/bin/cnpm /usr/local/bin

image.png
3、启动运行

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

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

注意:云服务器安全组添加7001端口,防火墙需要开放 7001 端口的访问权限。

到此,就可以通过ip访问相应接口了:
image.png

如果需要通过域名访问:
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 test.lipengzhou.com;
  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、测试通过域名访问

如果需要更新:

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

    自动部署

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

    生成 SSH key

    ssh-keygen -o

查看并复制公钥

cat ~/.ssh/id_rsa.pub

  1. [将 SSH 公钥添加到 GitHub 账户中](https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account)。
  2. 2、配置 GitHub 仓库 secrets
  3. - USERNAME 服务器用户
  4. - PASSWORD 服务器用户密码
  5. - HOST 服务器主机地址
  6. - PORT 服务器主机端口号
  7. - ACCESSKEYID 阿里云视频点播服务 access id
  8. - ACCESSKEYSECRET 阿里云视频点播服务 access key secret
  9. 3、编写 GitHub Actions 脚本
  10. ```bash
  11. # .github/workflows/nodejs.yml
  12. name: Node.js CI
  13. on:
  14. push:
  15. branches: [ master ]
  16. pull_request:
  17. branches: [ master ]
  18. schedule:
  19. - cron: '0 2 * * *'
  20. jobs:
  21. build:
  22. runs-on: ${{ matrix.os }}
  23. strategy:
  24. fail-fast: false
  25. matrix:
  26. node-version: [10]
  27. os: [ubuntu-latest]
  28. steps:
  29. - name: deploy
  30. uses: appleboy/ssh-action@master
  31. env:
  32. ACCESSKEYID: ${{ secrets.ACCESSKEYID }}
  33. ACCESSKEYSECRET: ${{ secrets.ACCESSKEYSECRET }}
  34. with:
  35. host: ${{ secrets.HOST }}
  36. username: ${{ secrets.USERNAME }}
  37. # key: ${{ secrets.KEY }}
  38. password: ${{ secrets.PASSWORD }}
  39. port: ${{ secrets.PORT }}
  40. envs: ACCESSKEYID,ACCESSKEYSECRET
  41. script: |
  42. export ACCESSKEYID=$ACCESSKEYID
  43. export ACCESSKEYSECRET=$ACCESSKEYSECRET
  44. export NVM_DIR="$HOME/.nvm"
  45. [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
  46. [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
  47. cd /root/youtubeclone-backend
  48. git pull origin master
  49. npm install --production
  50. npm run stop
  51. npm run start

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

配置 HTTPS

确保服务器防火墙允许访问 443

1、申请证书
image.png
image.png
2、在 nginx 服务器上安装证书
将压缩包移动到/etc/nginx/cert下并解压,cert目录是我们创建的目录
3、配置.conf文件

  1. #以下属性中,以ssl开头的属性表示与证书配置有关。
  2. server {
  3. listen 443 ssl;
  4. #配置HTTPS的默认访问端口为443。
  5. #如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
  6. #如果您使用Nginx 1.15.0及以上版本,请使用listen 443 ssl代替listen 443和ssl on。
  7. server_name www.webpj.online; #需要将yourdomain.com替换成证书绑定的域名。
  8. #ssl_certificate 是证书配置位置,默认会取/etc/nginx下找cert目录
  9. ssl_certificate cert/6253318_webpj.online.pem; #需要将cert-file-name.pem替换成已上传的证书文件的名称。
  10. ssl_certificate_key cert/6253318_webpj.online.key; #需要将cert-file-name.key替换成已上传的证书密钥文件的名称。
  11. ssl_session_timeout 5m;
  12. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  13. #表示使用的加密套件的类型。
  14. ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #表示使用的TLS协议的类型。
  15. ssl_prefer_server_ciphers on;
  16. location /api {
  17. proxy_set_header X-Real-IP $remote_addr;
  18. # 后端的Web服务器可以通过 X-Forwarded-For 获取用户真实 IP
  19. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  20. # 获取真实的请求主机名
  21. proxy_set_header Host $http_host;
  22. # 标识该请求由 nginx 转发
  23. proxy_set_header X-Nginx-Proxy true;
  24. # 代理到本地的 7001 端口服务
  25. proxy_pass http://127.0.0.1:7001/;
  26. }
  27. }
  28. server {
  29. listen 80;
  30. server_name www.webpj.online; #需要将yourdomain.com替换成证书绑定的域名。
  31. rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
  32. }

前端项目代理:

  1. server {
  2. listen 443 ssl;
  3. server_name www.webpj.online;
  4. #root /var/www/dist;
  5. ssl_certificate cert/6253318_webpj.online.pem;
  6. ssl_certificate_key cert/6253318_webpj.online.key;
  7. ssl_session_timeout 5m;
  8. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  9. #表示使用的加密套件的类型。
  10. ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #表示使用的TLS协议的类型。
  11. ssl_prefer_server_ciphers on;
  12. location /study{
  13. root /var/www/dist;
  14. try_files $uri $uri/ =404;
  15. }
  16. location /test{
  17. root /var/www/dist;
  18. try_files $uri $uri/ =404;
  19. }
  20. }
  21. server {
  22. listen 80;
  23. server_name www.webpj.online; #需要将yourdomain.com替换成证书绑定的域名。
  24. rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
  25. }

后端项目代理:

  1. server {
  2. listen 443 ssl;
  3. server_name www.webpj.online;
  4. #root /var/www/dist;
  5. ssl_certificate cert/6253318_webpj.online.pem;
  6. ssl_certificate_key cert/6253318_webpj.online.key;
  7. ssl_session_timeout 5m;
  8. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  9. #表示使用的加密套件的类型。
  10. ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #表示使用的TLS协议的类型。
  11. ssl_prefer_server_ciphers on;
  12. location /api{
  13. proxy_pass http://127.0.0.1:7001/;
  14. }
  15. location /express{
  16. proxy_pass http://127.0.0.1:3000/;
  17. }
  18. }
  19. server {
  20. listen 80;
  21. server_name www.webpj.online; #需要将yourdomain.com替换成证书绑定的域名。
  22. rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
  23. }

express项目托管

安装pm2: npm install pm2 -g
启动:找到项目目录,在目录下输入:pm2 start app.js
常用命令:
监听控制台PM2进程列表: pm2 list
重启:pm2 restart /
停止:pm2 stop /
删除:pm2 delete /
基本信息: pm2 info /
日志:pm2 log /