跳到主要内容

RHEL 8 系部署

本页适用于 Rocky Linux 8、AlmaLinux 8、RHEL 8、Alibaba Cloud Linux 3,以及仍需迁移的存量 CentOS 8 环境。

不要新建 CentOS Linux 8 生产环境

CentOS Linux 8 已于 2021 年 12 月 31 日停止维护,不再获得安全更新。新服务器请选择仍受支持的 Rocky Linux、AlmaLinux、RHEL 或其他企业 Linux 发行版。

准备工作

交付文件

  • surveyking-server.jar
  • surveyking-pro.sql
  • 包含 dist/publicdist/admin 的前端压缩包。
  • 版本配置说明和 License。

服务器要求

  • 建议至少 2 核 CPU、4 GB 内存和 20 GB 可用磁盘。
  • 需要具有 sudo 权限的运维账号。
  • 公网部署提前准备域名和 HTTPS 证书。

1. 安装基础软件

sudo dnf install -y \
java-1.8.0-openjdk-headless \
mysql-server redis nginx unzip curl \
policycoreutils-python-utils

sudo systemctl enable --now mysqld redis nginx

验证版本和服务:

java -version
mysql --version
redis-server --version
nginx -v

systemctl status mysqld redis nginx --no-pager

如果仍使用 CentOS Linux 8 Vault 软件源,只应把它作为迁移期间的临时措施,不要将归档软件源视为持续安全更新。

2. 初始化 MySQL 和 Redis

先运行 MySQL 安全初始化:

sudo mysql_secure_installation

创建独立数据库和账号:

CREATE DATABASE survey
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

CREATE USER 'surveyking'@'127.0.0.1' IDENTIFIED BY '请替换为强密码';
GRANT ALL PRIVILEGES ON survey.* TO 'surveyking'@'127.0.0.1';
FLUSH PRIVILEGES;

导入初始化脚本:

mysql -h 127.0.0.1 -u surveyking -p survey < surveyking-pro.sql

单机部署保持 Redis 只监听本机:

/etc/redis.conf
bind 127.0.0.1 ::1
protected-mode yes

修改 Redis 配置后重启并验证:

sudo systemctl restart redis
redis-cli ping

如启用 Redis 密码,需要同时配置 spring.redis.password

3. 创建目录和运行用户

sudo useradd --system --home-dir /opt/surveyking --shell /sbin/nologin surveyking

sudo mkdir -p /opt/surveyking/{server,client,config,files,logs}
sudo install -o surveyking -g surveyking -m 0644 \
surveyking-server.jar /opt/surveyking/server/surveyking-server.jar

sudo unzip surveyking-fe.zip -d /opt/surveyking/client
sudo chown -R surveyking:surveyking /opt/surveyking

确认前端目录:

test -f /opt/surveyking/client/dist/public/index.html
test -f /opt/surveyking/client/dist/admin/index.html

如果压缩包包含额外顶层目录,移动文件后再继续,最终路径必须与上面一致。

4. 配置后端

创建 /opt/surveyking/config/application.yml

server:
port: 48080

spring:
datasource:
dynamic:
datasource:
master:
name: survey
url: jdbc:mysql://127.0.0.1:3306/survey?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
username: surveyking
password: '请替换为数据库密码'
redis:
host: 127.0.0.1
port: 6379
database: 0
# Redis 未设置密码时删除下一行。
password: '请替换为 Redis 密码'

logging:
file:
name: /opt/surveyking/logs/surveyking-server.log

设置文件权限,避免其他系统用户读取密码:

sudo chown surveyking:surveyking /opt/surveyking/config/application.yml
sudo chmod 600 /opt/surveyking/config/application.yml

5. 创建 systemd 服务

创建 /etc/systemd/system/surveyking.service

[Unit]
Description=SurveyKing Pro
After=network-online.target mysqld.service redis.service
Wants=network-online.target

[Service]
Type=simple
User=surveyking
Group=surveyking
WorkingDirectory=/opt/surveyking/files
ExecStart=/usr/bin/java -Xms512m -Xmx1536m -Dfile.encoding=UTF-8 -jar /opt/surveyking/server/surveyking-server.jar --spring.profiles.active=prod --spring.config.additional-location=optional:file:/opt/surveyking/config/
SuccessExitStatus=143
Restart=on-failure
RestartSec=10

NoNewPrivileges=true
PrivateTmp=true
ProtectHome=true
ProtectSystem=strict
ReadWritePaths=/opt/surveyking/files /opt/surveyking/logs

[Install]
WantedBy=multi-user.target

启动服务:

sudo systemctl daemon-reload
sudo systemctl enable --now surveyking
sudo systemctl status surveyking --no-pager
sudo journalctl -u surveyking -n 100 --no-pager

完成标志: 48080 端口已监听,日志中没有数据库、Redis、目录权限或 License 错误。

6. 配置 Nginx

参考标准 Nginx 配置,保持以下值:

  • 前端目录:/opt/surveyking/client/dist/public/opt/surveyking/client/dist/admin
  • 后端地址:127.0.0.1:48080
  • map $http_upgrade ... 放在 http {} 中。
  • /admin-api/captcha 原样转发,不额外删除路径。

检查并重载:

sudo nginx -t
sudo systemctl reload nginx

SELinux

启用 SELinux 时,允许 Nginx 连接后端,并标记前端静态目录:

sudo setsebool -P httpd_can_network_connect 1
sudo semanage fcontext -a -t httpd_sys_content_t '/opt/surveyking/client(/.*)?'
sudo restorecon -Rv /opt/surveyking/client

不要通过永久关闭 SELinux 解决路径或代理配置问题。

防火墙

只开放 HTTP/HTTPS,不对公网开放 3306637948080

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

7. 验证部署

systemctl status mysqld redis surveyking nginx --no-pager
ss -lntp | grep -E ':(3306|6379|48080|80|443)\b'

curl -I http://127.0.0.1/pub/index.html
curl -I http://127.0.0.1/adm/index.html
curl http://127.0.0.1/health

再使用普通账号完成登录、创建测试项目、提交答卷和上传文件。

常见问题

SurveyKing 服务无法启动

sudo journalctl -u surveyking -n 200 --no-pager
sudo -u surveyking test -r /opt/surveyking/server/surveyking-server.jar
sudo -u surveyking test -r /opt/surveyking/config/application.yml

重点检查 Java 版本、YAML 缩进、数据库连接、Redis 密码和目录权限。

Nginx 返回 502

ss -lntp | grep 48080
curl -I http://127.0.0.1:48080/
sudo ausearch -m AVC -ts recent

如果后端正常但 Nginx 仍无法连接,检查 SELinux 审计日志和 httpd_can_network_connect

页面刷新后 404

确认前端路由使用 try_files ... /index.html,并且 public 和 admin 分别指向正确目录。

升级与备份

升级前至少备份:

  • MySQL 数据库。
  • /opt/surveyking/files
  • /opt/surveyking/config 和 License。
  • 当前 JAR 与前端 dist 目录。

替换文件后重启后端,并在 Nginx 配置未变化时直接刷新前端文件:

sudo systemctl restart surveyking
sudo journalctl -u surveyking -f

先在测试环境验证数据库升级脚本,不要对正式库重复执行初始化 SQL。