服务器环境配置
- Node.js
- MongoDB
- nginx
这里以 Ubuntu 18.04 为例。
建议将 Ubuntu 镜像源切换为国内镜像地址,比如清华大学 Ubuntu 镜像源。
切换镜像地址
进入linux服务器:
备份 sources.list文件
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
vim sources.list
清空之前内容,选择Ubuntu版本并复制以下内容到sources.list文件
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiversedeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiversedeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiversedeb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse# 预发布软件源,不建议启用# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
在进行下面的操作之前先更新软件包:
apt updateapt upgrade
切换镜像可能导致的问题
执行以下命令:
sudo apt updatesudo apt install nginx
报出:Unable to locate package
如果出现这样的情况,恢复sources.list原本配置,或使用其他镜像。
安装 Node
建议使用 nvm 安装和管理 Node 服务
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
199.232.96.133 raw.githubusercontent.com
保存好之后再次执行下载命令,下载好之后,nvm被放在/root/.nvm目录
关闭终端、重新连接服务器就可以使用nvm了。
修改 nvm 安装 node 的镜像源:
由于下载是从node官网下载的,速度会比较慢,配置国内镜像
vi .bashrc
# .bashrcexport NVM_NODEJS_ORG_MIRROR=http://npm.taobao.org/mirrors/node
source .bashrc(重新载入配置文件)
安装 Node:
nvm install 14.15.4nvm alias default 14.15.4node --version
把 npm 安装地址修改为淘宝镜像源:
npm i -g nrm --registry=https://registry.npm.taobao.orgnrm lsnrm use taobao
安装 MongoDB
建议参考 MongoDB 官方推荐的安装方式。
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
Ubuntu20.04版本 安装 MongoDB:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -sudo apt-get install gnupgwget -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.listsudo apt-get updatesudo apt-get install -y mongodb-org
管理 MongoDB:
# 启动 MongoDBsudo systemctl start mongod# 查看 MongoDB 启动状态sudo systemctl status mongod# 将 MongoDB 设置为开机启动sudo systemctl enable mongod# 停止 MongoDBsudo systemctl stop mongod# 重启 MongoDBsudo systemctl restart mongod# 使用自带的命令行客户端连接 MongoDBmongo
安装 nginx
例如 Ubuntu 20.04 的安装方式建议参考:https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04。
sudo apt install nginx
管理 nginx 服务:
# 启动 nginxsudo systemctl start nginx# 查看 nginx 运行状态systemctl status nginx# 停止 nginxsudo systemctl stop nginx# 重启 nginxsudo systemctl restart nginx# 热重启:如果仅更改配置,Nginx通常可以重新加载而不断开连接sudo systemctl reload nginx# 添加开机启动 nginxsudo systemctl enable nginx# 禁止开机启动 nginxsudo systemctl disable nginx
安装 Git
apt install gitgit --version
手动部署
关于 Egg.js 应用部署:https://eggjs.org/zh-cn/core/deployment.html。
1、将代码提交到 GitHub 远程仓库
2、在远程服务器下载 GitHub 远程仓库
服务器中使用ssh协议需要权限

通过ssh-keygen生成ssh-key, 默认生成到/root/.ssh/id_rsa目录下,接下来提示设置密码,不设置密码直接下一步。生成好之后复制公钥到github中->设置->ssh公钥
第一次连接时需要保存指纹信息,yes
如果下载报错可能是 /etc/ssh/ssh_config 文件的配置出了问题,我这里在文件末尾加了:PermitRootLogin yes 导致报错。
安装cnpm
//安装cnpmnpm install -g cnpm --registry=https://registry.npm.taobao.org// 建立软连接sudo ln -s /root/softwares/node_14/bin/cnpm /usr/local/bin

3、启动运行
cd 项目目录cnpm i --productionnpm start
启动成功之后可以通过 ip 地址访问测试一下:http://服务器ip地址:7001。
注意:云服务器安全组添加7001端口,防火墙需要开放 7001 端口的访问权限。
到此,就可以通过ip访问相应接口了:
如果需要通过域名访问:
1、添加域名解析记录
2、配置 nginx 代理
# /etc/nginx/conf.d/youtubecloneserver {# 监听端口listen 80;# 域名可以有多个,用空格隔开# server_name www.w3cschool.cn w3cschool.cn;server_name test.lipengzhou.com;# 对 / 启用反向代理location / {proxy_set_header X-Real-IP $remote_addr;# 后端的Web服务器可以通过 X-Forwarded-For 获取用户真实 IPproxy_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、测试通过域名访问
如果需要更新:
- 本地开发
- 提交更新到远程仓库
- 在远程服务器拉取变更,重新启动服务
自动部署

1、如果是私有 Git 仓库需要配置服务器通过 SSH 连接 github 的权限 ```shell生成 SSH key
ssh-keygen -o
查看并复制公钥
cat ~/.ssh/id_rsa.pub
[将 SSH 公钥添加到 GitHub 账户中](https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account)。2、配置 GitHub 仓库 secrets- USERNAME 服务器用户- PASSWORD 服务器用户密码- HOST 服务器主机地址- PORT 服务器主机端口号- ACCESSKEYID 阿里云视频点播服务 access id- ACCESSKEYSECRET 阿里云视频点播服务 access key secret3、编写 GitHub Actions 脚本```bash# .github/workflows/nodejs.ymlname: Node.js CIon:push:branches: [ master ]pull_request:branches: [ master ]schedule:- cron: '0 2 * * *'jobs:build:runs-on: ${{ matrix.os }}strategy:fail-fast: falsematrix:node-version: [10]os: [ubuntu-latest]steps:- name: deployuses: appleboy/ssh-action@masterenv:ACCESSKEYID: ${{ secrets.ACCESSKEYID }}ACCESSKEYSECRET: ${{ secrets.ACCESSKEYSECRET }}with:host: ${{ secrets.HOST }}username: ${{ secrets.USERNAME }}# key: ${{ secrets.KEY }}password: ${{ secrets.PASSWORD }}port: ${{ secrets.PORT }}envs: ACCESSKEYID,ACCESSKEYSECRETscript: |export ACCESSKEYID=$ACCESSKEYIDexport ACCESSKEYSECRET=$ACCESSKEYSECRETexport NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"cd /root/youtubeclone-backendgit pull origin masternpm install --productionnpm run stopnpm run start
4、将源码提交到 GitHub 远程仓库
5、等待执行自动部署
6、查看部署结果
配置 HTTPS
确保服务器防火墙允许访问 443
1、申请证书

2、在 nginx 服务器上安装证书
将压缩包移动到/etc/nginx/cert下并解压,cert目录是我们创建的目录
3、配置.conf文件
#以下属性中,以ssl开头的属性表示与证书配置有关。server {listen 443 ssl;#配置HTTPS的默认访问端口为443。#如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。#如果您使用Nginx 1.15.0及以上版本,请使用listen 443 ssl代替listen 443和ssl on。server_name www.webpj.online; #需要将yourdomain.com替换成证书绑定的域名。#ssl_certificate 是证书配置位置,默认会取/etc/nginx下找cert目录ssl_certificate cert/6253318_webpj.online.pem; #需要将cert-file-name.pem替换成已上传的证书文件的名称。ssl_certificate_key cert/6253318_webpj.online.key; #需要将cert-file-name.key替换成已上传的证书密钥文件的名称。ssl_session_timeout 5m;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;#表示使用的加密套件的类型。ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #表示使用的TLS协议的类型。ssl_prefer_server_ciphers on;location /api {proxy_set_header X-Real-IP $remote_addr;# 后端的Web服务器可以通过 X-Forwarded-For 获取用户真实 IPproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 获取真实的请求主机名proxy_set_header Host $http_host;# 标识该请求由 nginx 转发proxy_set_header X-Nginx-Proxy true;# 代理到本地的 7001 端口服务proxy_pass http://127.0.0.1:7001/;}}server {listen 80;server_name www.webpj.online; #需要将yourdomain.com替换成证书绑定的域名。rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。}
前端项目代理:
server {listen 443 ssl;server_name www.webpj.online;#root /var/www/dist;ssl_certificate cert/6253318_webpj.online.pem;ssl_certificate_key cert/6253318_webpj.online.key;ssl_session_timeout 5m;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;#表示使用的加密套件的类型。ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #表示使用的TLS协议的类型。ssl_prefer_server_ciphers on;location /study{root /var/www/dist;try_files $uri $uri/ =404;}location /test{root /var/www/dist;try_files $uri $uri/ =404;}}server {listen 80;server_name www.webpj.online; #需要将yourdomain.com替换成证书绑定的域名。rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。}
后端项目代理:
server {listen 443 ssl;server_name www.webpj.online;#root /var/www/dist;ssl_certificate cert/6253318_webpj.online.pem;ssl_certificate_key cert/6253318_webpj.online.key;ssl_session_timeout 5m;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;#表示使用的加密套件的类型。ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #表示使用的TLS协议的类型。ssl_prefer_server_ciphers on;location /api{proxy_pass http://127.0.0.1:7001/;}location /express{proxy_pass http://127.0.0.1:3000/;}}server {listen 80;server_name www.webpj.online; #需要将yourdomain.com替换成证书绑定的域名。rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。}
express项目托管
安装pm2: npm install pm2 -g
启动:找到项目目录,在目录下输入:pm2 start app.js
常用命令:
监听控制台PM2进程列表: pm2 list
重启:pm2 restart
停止:pm2 stop
删除:pm2 delete
基本信息: pm2 info
日志:pm2 log
