录用通知-自助系统的服务器部署指南

本指南介绍如何在 Linux 服务器上部署"录用通知-自助系统"的前后端服务。


📋 目录


服务器要求

硬件配置

配置项 最低要求 推荐配置
CPU 1 核 2 核及以上
内存 1 GB 2 GB 及以上
硬盘 20 GB 40 GB 及以上

操作系统

  • 推荐: Ubuntu 20.04 / 22.04 LTS 或 CentOS 7 / 8
  • 其他: Debian 10+ 等主流 Linux 发行版

需要的软件

  • Node.js >= 16.x
  • PostgreSQL >= 12
  • Nginx
  • PM2 (进程管理器)

部署架构

复制代码
                        Internet
                           │
                           │
                      ┌────▼────┐
                      │  Nginx  │  (80/443 端口)
                      │   :443  │  (HTTPS)
                      └────┬────┘
                           │
            ┌──────────────┴──────────────┐
            │                             │
       ┌────▼─────┐                 ┌────▼─────┐
       │  Frontend │                 │  Backend  │
       │   (静态文件)  │                 │  (PM2)    │
       │   :3000   │                 │   :4000   │
       └───────────┘                 └─────┬─────┘
                                          │
                                    ┌─────▼─────┐
                                    │ PostgreSQL│
                                    │   :5432   │
                                    └───────────┘

步骤一:服务器基础环境配置

1.1 连接服务器

使用 SSH 连接到你的服务器:

bash 复制代码
ssh root@你的服务器IP
# 如果使用密钥登录
ssh -i /path/to/your/key.pem root@你的服务器IP

1.2 更新系统

Ubuntu/Debian:

bash 复制代码
apt update && apt upgrade -y

CentOS:

bash 复制代码
yum update -y

1.3 安装 Node.js

安装 Node.js 18.x(推荐使用 NodeSource 仓库):

bash 复制代码
# 安装 NodeSource 仓库
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -

# 安装 Node.js
apt install -y nodejs

# 验证安装
node -v
npm -v

1.4 安装 PM2(全局)

PM2 用于管理 Node.js 进程:

bash 复制代码
npm install -g pm2

# 验证安装
pm2 -v

步骤二:安装 PostgreSQL 数据库

2.1 安装 PostgreSQL

Ubuntu/Debian:

bash 复制代码
apt install -y postgresql postgresql-contrib

CentOS:

bash 复制代码
yum install -y postgresql-server postgresql-contrib
postgresql-setup initdb
systemctl start postgresql
systemctl enable postgresql

2.2 配置数据库

bash 复制代码
# 切换到 postgres 用户
sudo -u postgres psql

# 在 PostgreSQL 命令行中执行以下命令:
sql 复制代码
-- 创建数据库
CREATE DATABASE acceptance_notices;

-- 创建用户并设置密码(请替换 'your_secure_password')
CREATE USER notice_user WITH PASSWORD 'your_secure_password';

-- 授予权限
GRANT ALL PRIVILEGES ON DATABASE acceptance_notices TO notice_user;

-- 退出
\q

2.3 配置 PostgreSQL 远程连接(可选)

如果需要远程连接数据库:

bash 复制代码
# 编辑配置文件
nano /etc/postgresql/14/main/postgresql.conf

修改以下行:

conf 复制代码
listen_addresses = '*'

编辑 pg_hba.conf:

bash 复制代码
nano /etc/postgresql/14/main/pg_hba.conf

添加以下行:

conf 复制代码
host    all             all             0.0.0.0/0               md5

重启 PostgreSQL:

bash 复制代码
systemctl restart postgresql

步骤三:部署后端服务

3.1 创建项目目录

bash 复制代码
# 创建项目根目录
mkdir -p /var/www/acceptance-notice
cd /var/www/acceptance-notice

3.2 上传代码到服务器

方式一:使用 Git(推荐)

bash 复制代码
# 如果你的代码在 Git 仓库
git clone https://your-repo-url.git .

# 或者上传后端文件夹
cd /var/www/acceptance-notice
# 然后使用 scp/sftp 上传代码

方式二:使用 SCP 上传

在你的本地电脑执行:

bash 复制代码
# 上传后端代码
scp -r ./backend root@你的服务器IP:/var/www/acceptance-notice/

# 上传前端代码
scp -r ./frontend root@你的服务器IP:/var/www/acceptance-notice/

3.3 安装后端依赖

bash 复制代码
cd /var/www/acceptance-notice/backend
npm install --production

3.4 配置环境变量

bash 复制代码
# 创建 .env 文件
nano .env

复制以下配置(修改相应的值):

env 复制代码
# 服务器端口
PORT=4000

# 数据库连接(修改用户名和密码)
DATABASE_URL=postgresql://notice_user:your_secure_password@localhost:5432/acceptance_notices

# JWT 密钥(请修改为随机字符串)
JWT_SECRET=your_very_secure_random_jwt_secret_key_here

# 文件存储路径
STORAGE_ROOT=./storage

# 默认管理员账户(请修改)
ADMIN_DEFAULT_USERNAME=admin
ADMIN_DEFAULT_PASSWORD=your_secure_admin_password

# 字体配置(Linux 服务器通常使用这个路径)
FONT_PATH=/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf
FONT_FAMILY_DEFAULT=DejaVu Sans
FONT_FAMILY_TITLE=DejaVu Sans

# 百度 OCR API 配置(需要去百度智能云申请)
BAIDU_APP_ID=your_baidu_app_id
BAIDU_API_KEY=your_baidu_api_key
BAIDU_SECRET_KEY=your_baidu_secret_key

# Nutrient SDK 配置(需要去 Nutrient 官网申请)
NUTRIENT_API_URL=https://api.nutrient.io
NUTRIENT_API_KEY=your_nutrient_api_key
NUTRIENT_DWS_VIEWER_API_URL=https://api.nutrient.io
NUTRIENT_DWS_VIEWER_API_KEY=your_nutrient_viewer_api_key
NUTRIENT_DWS_VIEWER_BASE_URL=https://cdn.nutrient.io/viewer/latest/
NUTRIENT_DWS_VIEWER_SCRIPT_URL=https://cdn.nutrient.io/viewer/latest/nutrient-viewer.js

3.5 运行数据库迁移

bash 复制代码
# 在 backend 目录下执行
npm run migrate

3.6 创建存储目录

bash 复制代码
# 创建文件存储目录
mkdir -p storage/templates
mkdir -p storage/generated
mkdir -p storage/temp

3.7 使用 PM2 启动后端服务

bash 复制代码
# 在 backend 目录下执行
pm2 start src/server.js --name "acceptance-backend"

# 保存 PM2 配置
pm2 save

# 查看运行状态
pm2 status

# 查看日志
pm2 logs acceptance-backend

步骤四:部署前端服务

4.1 安装前端依赖

bash 复制代码
cd /var/www/acceptance-notice/frontend
npm install

4.2 修改 API 地址

重要: 需要修改前端的 API 请求地址,使其指向生产服务器的后端地址。

检查 src 目录下的 API 配置文件,将所有 localhost:4000 替换为你的服务器域名或 IP:

bash 复制代码
# 查找所有包含 localhost 的文件
grep -r "localhost:4000" src/

# 批量替换(如果有 API_BASE_URL 这样的配置)

4.3 构建前端

bash 复制代码
# 构建生产版本
npm run build

构建完成后,会在 dist 目录生成静态文件。

4.4 安装并配置 serve(用于托管静态文件)

bash 复制代码
# 全局安装 serve
npm install -g serve

# 使用 PM2 启动前端静态文件服务
pm2 start serve --name "acceptance-frontend" -- --s -l 3000 dist

# 保存 PM2 配置
pm2 save

步骤五:使用 Nginx 反向代理

5.1 安装 Nginx

Ubuntu/Debian:

bash 复制代码
apt install -y nginx

CentOS:

bash 复制代码
yum install -y nginx
systemctl start nginx
systemctl enable nginx

5.2 创建 Nginx 配置文件

bash 复制代码
# 创建站点配置文件
nano /etc/nginx/sites-available/acceptance-notice

添加以下配置:

nginx 复制代码
# 后端 API 配置
server {
    listen 80;
    server_name api.yourdomain.com;  # 替换为你的域名或服务器 IP

    # 设置最大上传大小
    client_max_body_size 50M;

    location / {
        proxy_pass http://localhost:4000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }

    # 静态文件直接提供
    location /files/ {
        alias /var/www/acceptance-notice/backend/storage/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }
}

# 前端配置
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;  # 替换为你的域名

    root /var/www/acceptance-notice/frontend/dist;
    index index.html;

    # Gzip 压缩
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml+rss application/javascript application/json;

    # 前端路由支持
    location / {
        try_files $uri $uri/ /index.html;
    }

    # 静态资源缓存
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }
}

5.3 启用站点配置

bash 复制代码
# 创建符号链接
ln -s /etc/nginx/sites-available/acceptance-notice /etc/nginx/sites-enabled/

# 测试配置
nginx -t

# 重启 Nginx
systemctl restart nginx

步骤六:配置 SSL 证书(HTTPS)

6.1 使用 Let's Encrypt 免费证书(推荐)

安装 Certbot:

Ubuntu/Debian:

bash 复制代码
apt install -y certbot python3-certbot-nginx

CentOS:

bash 复制代码
yum install -y certbot python3-certbot-nginx

6.2 申请 SSL 证书

bash 复制代码
# 自动配置 SSL(替换为你的域名)
certbot --nginx -d yourdomain.com -d www.yourdomain.com -d api.yourdomain.com

# 按提示输入邮箱并同意服务条款

Certbot 会自动修改 Nginx 配置并设置证书自动续期。

6.3 手动配置 Nginx SSL(备选方案)

如果你有自己的 SSL 证书,修改 Nginx 配置:

nginx 复制代码
server {
    listen 443 ssl http2;
    server_name yourdomain.com www.yourdomain.com;

    ssl_certificate /path/to/your/fullchain.pem;
    ssl_certificate_key /path/to/your/privkey.pem;

    # SSL 配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    # 其他配置保持不变...
    root /var/www/acceptance-notice/frontend/dist;
    # ...
}

# HTTP 自动跳转 HTTPS
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    return 301 https://$server_name$request_uri;
}

步骤七:配置防火墙

7.1 使用 UFW(Ubuntu)

bash 复制代码
# 允许 SSH
ufw allow 22/tcp

# 允许 HTTP/HTTPS
ufw allow 80/tcp
ufw allow 443/tcp

# 启用防火墙
ufw enable

# 查看状态
ufw status

7.2 使用 firewalld(CentOS)

bash 复制代码
# 允许 HTTP/HTTPS
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https

# 允许 SSH
firewall-cmd --permanent --add-service=ssh

# 重载防火墙
firewall-cmd --reload

# 查看状态
firewall-cmd --list-all

步骤八:设置 PM2 开机自启

bash 复制代码
# 生成开机启动脚本
pm2 startup

# 按照提示执行输出的命令

# 保存当前进程列表
pm2 save

验证部署

检查服务状态

bash 复制代码
# 检查 PM2 进程
pm2 status

# 检查 Nginx
systemctl status nginx

# 检查 PostgreSQL
systemctl status postgresql

访问测试

  1. 前端访问 : https://yourdomain.com
  2. 后端 API : https://api.yourdomain.com/api
  3. 健康检查 : https://api.yourdomain.com/api/health (如果有)

常见问题

Q1: PM2 进程自动重启怎么办?

检查日志查看错误信息:

bash 复制代码
pm2 logs acceptance-backend
pm2 logs acceptance-frontend

常见原因:

  • 端口被占用
  • 数据库连接失败
  • 环境变量配置错误
  • 内存不足

Q2: 数据库连接失败?

检查:

  1. PostgreSQL 是否运行:systemctl status postgresql
  2. 数据库用户名密码是否正确
  3. 数据库是否已创建
  4. 防火墙是否开放 5432 端口

Q3: 前端页面空白?

检查:

  1. 前端是否成功构建:ls -la frontend/dist/
  2. Nginx 配置是否正确:nginx -t
  3. 查看 Nginx 错误日志:tail -f /var/log/nginx/error.log

Q4: 上传文件失败?

检查:

  1. Nginx 的 client_max_body_size 配置
  2. 后端存储目录权限:chmod -R 755 storage/
  3. 磁盘空间是否充足:df -h

Q5: 如何更新代码?

bash 复制代码
# 1. 拉取最新代码
cd /var/www/acceptance-notice
git pull

# 2. 后端更新
cd backend
npm install --production
pm2 restart acceptance-backend

# 3. 前端更新
cd ../frontend
npm install
npm run build
pm2 restart acceptance-frontend

维护命令速查

bash 复制代码
# PM2 命令
pm2 status              # 查看所有进程
pm2 restart all         # 重启所有进程
pm2 logs [app-name]     # 查看日志
pm2 monit              # 监控

# Nginx 命令
nginx -t               # 测试配置
systemctl restart nginx # 重启
systemctl status nginx  # 查看状态

# PostgreSQL 命令
systemctl status postgresql # 查看状态
sudo -u postgres psql        # 进入数据库

# 日志查看
tail -f /var/log/nginx/error.log      # Nginx 错误日志
tail -f /var/log/nginx/access.log     # Nginx 访问日志
journalctl -u nginx -f                # Nginx 系统日志

安全建议

  1. 定期更新系统和软件包

    bash 复制代码
    apt update && apt upgrade -y
  2. 配置防火墙:只开放必要的端口

  3. 修改默认密码:数据库密码、管理员密码等

  4. 启用 SSL/TLS:强制使用 HTTPS

  5. 定期备份:备份数据库和上传的文件

  6. 监控日志:定期检查访问日志和错误日志

  7. 限制 SSH 访问

    bash 复制代码
    # 禁用密码登录,只允许密钥登录
    # 编辑 /etc/ssh/sshd_config
    PasswordAuthentication no

部署指南版本: 1.0
最后更新: 2026-01-20

相关推荐
仗剑恬雅人2 小时前
LINUX数据库高频常用命令
linux·运维·服务器·数据库·ssh·运维开发
CAAS_IFR_zp2 小时前
PICRUSt2-SC:16s功能注释的更新
服务器
LetsonH2 小时前
服务器配置(开机自启+XRDP远程)
运维·服务器
Getgit3 小时前
Linux系统的特点有哪些
java·linux·运维·网络·sql
壮哥_icon3 小时前
Ubuntu 虚拟机中编译 Android 源码完整指南(含分卷合并、虚拟内存配置、复制粘贴设置及依赖库安装)
linux·运维·ubuntu
weixin_395448913 小时前
tidl_import_mul_rmfsd_psd_u8_3x480x544_bise_raw_dynamic.txt
java·服务器·前端
Maggie_ssss_supp3 小时前
Linux-Percona XtraDB Cluster (PXC)集群部署实战
linux·运维·服务器
十月南城3 小时前
压测方法论——目标、场景、指标与容量评估的闭环
运维·web安全·ci/cd·微服务·云计算
txinyu的博客3 小时前
std::function
服务器·开发语言·c++