CentOS 8 部署
本文档适用于 CentOS 8 系统部署卷王问卷系统。CentOS 8 使用 DNF 包管理器,systemd 服务管理,是目前主流的企业级 Linux 发行版。
同样适用于基于 CentOS 8 的其他发行版,如 Alibaba Cloud Linux 3、Rocky Linux 8、AlmaLinux 8 等。
准备工作
部署文件准备
surveyking-xxx.zip
,前端文件surveyking-server.jar
,后端 jar 包surveyking-pro.sql
,数据库脚本
系统要求
- CPU: 建议 2 核以上
- 内存: 建议 2GB 以上(生产环境推荐 4GB+)
- 磁盘: 建议 20GB 以上可用空间
- 操作系统: CentOS 8.x / Rocky Linux 8 / AlmaLinux 8 / Alibaba Cloud Linux 3
- 网络: 确保服务器可以访问外网(用于下载依赖包)
- 权限: 需要 root 权限进行系统配置
创建部署目录
# 创建部署目录
mkdir -p /opt/surveyking/{server,client,logs,config}
cd /opt/surveyking
# 设置目录权限
chmod 755 /opt/surveyking
## 基础软件环境安装
> **注意**: CentOS 8 已于 2021 年底停止维护,如果使用原版 CentOS 8,需要先更新软件源。推荐使用 Rocky Linux 8 或 AlmaLinux 8 作为替代方案。
### 更新软件源(仅原版 CentOS 8 需要)
```bash
# 如果使用原版 CentOS 8,需要更新软件源
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
# 更新系统
dnf update -y
安装 MySQL
1、安装 MySQL
# 1、安装 MySQL 服务器
dnf install -y mysql-server
# 2、查看配置文件位置
mysql --help | grep my.cnf # 显示配置文件加载优先级
2、配置 MySQL
vim /etc/my.cnf
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
#数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character-set-server = utf8mb4
#数据库字符集对应一些排序等规则,注意要和character-set-server对应
collation-server = utf8mb4_general_ci
#设置client连接mysql时的字符集,防止乱码
init_connect='SET NAMES utf8mb4'
#是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names = 1
#最大连接数
max_connections = 400
#最大错误连接数
max_connect_errors = 1000
#MySQL连接闲置超过一定时间后(单位:秒)将会被强行关闭
#MySQL默认的wait_timeout 值为8个小时, interactive_timeout参数需要同时配置才能生效
interactive_timeout = 1800
wait_timeout = 1800
3、设置启动数据库服务同时设置开机自启动
systemctl enable --now mysqld
4、初始化数据库 mysql_secure_installation
依次需要判断的内容:
配置验证密码组件,输入 y
配置密码验证等级,可以输入 0 、1 、2 ,分别对应三个等级。
输入密码,需要输入两次,页面上不会显示出你输入的密码,输入完之后按回车即可
确认使用该密码,输入 y
是否移除匿名用户,输入 y
是否禁止root用户远程登录 如果需要远程登录,请输入 n
是否移除测试数据库,输入 y
是否重载权限表,输入 y
5、配置数据库的连接信息
# 通过新密码再次登陆
mysql -uroot -p
# 授予远程访问权限
create user 'root'@'%' identified by '新密码';
# 授予远程访问权限
grant all privileges on *.* to 'root'@'%';
flush privileges;
# 授权mysql_native客户端工具
alter user 'root' @'%' identified with mysql_native_password by '新密码';
6、创建数据库
mysql -u root -p 新密码
CREATE DATABASE survey CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
7、导入数据库
mysql --user="root" --database="survey" --password="新密码" < "/opt/setups/surveyking-pro.sql"
安装 Redis
1、安装 Redis
# 安装 Redis
dnf install -y redis
2、修改配置文件
vim /etc/redis.conf
# 后台运行 redis
daemonize yes
# 设置密码,非必须
requirepass xxxxx
# 允许远程访问,如果和服务部署在一台机器就不用配置
protected-mode no
bind 0.0.0.0
3、启动并设置开机自启
# 启动 Redis 服务
systemctl start redis
# 设置开机自启
systemctl enable redis
# 检查服务状态
systemctl status redis
# 测试连接(如果设置了密码,需要加上 -a 参数)
redis-cli ping
安装 JDK
1、安装 OpenJDK 8
# 安装 OpenJDK 8(推荐,兼容性最好)
dnf install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
# 或者安装 OpenJDK 11(可选)
# dnf install -y java-11-openjdk java-11-openjdk-devel
2、验证安装
# 检查 Java 版本
java -version
# 检查 javac 编译器
javac -version
# 查看 JAVA_HOME 环境变量
echo $JAVA_HOME
3、设置环境变量(可选)
# 如果需要手动设置 JAVA_HOME
echo 'export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk' >> /etc/profile
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
source /etc/profile
安装 Nginx
1、安装 Nginx
# 安装 Nginx
dnf install -y nginx
# 如果默认仓库没有 Nginx,可以安装 EPEL 仓库
# dnf install -y epel-release
# dnf install -y nginx
2、修改 nginx.conf 配置文件
server {
listen 65500;
server_name localhost;
root /opt/surveyking/client;
# 开启 gzip 压缩
gzip on;
# 设置 gzip 压缩级别,范围是 1-9,推荐设置为 5
gzip_comp_level 5;
# 启用对以下 MIME 类型的压缩
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 启用对响应头中的 "Vary: Accept-Encoding" 字段的压缩
gzip_vary on;
location / {
try_files $uri $uri/ /index.html;
}
# 单独处理 HTML 文件的缓存策略
location ~* \.(html|htm)$ {
# 禁用缓存
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires "0";
# 可选:添加版本标识
add_header X-Version $date_gmt;
}
# 二维码代理
location /captcha {
proxy_pass http://localhost:48080/captcha;
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 30m;
proxy_http_version 1.1;
proxy_set_header Connection close;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_send_timeout 1800;
proxy_read_timeout 1800;
proxy_connect_timeout 1800;
}
# 后端接口代理
location /admin-api {
proxy_pass http://localhost:48080/admin-api;
set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 30m;
proxy_http_version 1.1;
proxy_set_header Connection close;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_send_timeout 1800;
proxy_read_timeout 1800;
proxy_connect_timeout 1800;
}
}
3、启动并设置开机自启
# 检测配置文件语法
nginx -t
# 启动 Nginx 服务
systemctl start nginx
# 设置开机自启
systemctl enable nginx
# 检查服务状态
systemctl status nginx
# 重载配置(配置文件修改后)
systemctl reload nginx
配置防火墙
# 开启防火墙端口
sudo firewall-cmd --zone=public --add-port=65500/tcp --permanent
sudo firewall-cmd --zone=public --add-port=48080/tcp --permanent
# 重载防火墙配置
sudo firewall-cmd --reload
# 查看已开放的端口
sudo firewall-cmd --zone=public --list-ports
部署应用文件
部署前端文件
# 解压前端文件到指定目录
cd /opt/surveyking/client
unzip /path/to/surveyking-xxx.zip
部署后端文件
# 复制 jar 包到服务目录
cp /path/to/surveyking-server.jar /opt/surveyking/server/
# 创建应用配置文件
cat > /opt/surveyking/config/application.yml << 'EOF'
server:
port: 48080
spring:
datasource:
dynamic:
datasource:
master:
url: jdbc:mysql://127.0.0.1:3306/survey?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
username: root
password: 你的数据库密码
driver-class-name: com.mysql.cj.jdbc.Driver
redis:
host: 127.0.0.1
port: 6379
password: 你的Redis密码(如果设置了)
database: 0
logging:
level:
cn.surveyking: INFO
file:
name: /opt/surveyking/logs/application.log
logback:
rollingpolicy:
max-file-size: 10MB
max-history: 30
EOF