基于本次实际部署经验总结,适用于 ubuntu 24.04 服务器
一、服务器环境准备
1.1 安装基础软件
# 更新包列表 sudo apt update # 安装 jdk 17 sudo apt install openjdk-17-jdk -y java -version # 验证安装 # 安装 mysql 8.0 sudo apt install mysql-server -y sudo systemctl start mysql sudo systemctl enable mysql # 安装 redis sudo apt install redis-server -y sudo systemctl start redis sudo systemctl enable redis # 安装 nginx sudo apt install nginx -y sudo systemctl start nginx sudo systemctl enable nginx
1.2 创建项目目录结构
# 创建项目根目录 mkdir -p /opt/ipam # 创建子目录 mkdir -p /opt/ipam/backend # 后端 jar 包 mkdir -p /opt/ipam/frontend # 前端 dist 文件 mkdir -p /opt/ipam/config # 配置文件 mkdir -p /opt/ipam/logs # 日志文件 mkdir -p /opt/ipam/uploadpath # 文件上传目录(根据项目需要) mkdir -p /opt/ipam/sql # sql 脚本
二、数据库部署
2.1 登录 mysql
# 首次安装无密码,直接登录 sudo mysql
2.2 创建数据库和用户
-- 创建数据库(注意:带短横线的名称需要反引号) create database if not exists `zy-ipam` character set utf8mb4 collate utf8mb4_unicode_ci; -- 创建用户(用户名用下划线,密码自定义) create user 'zy_ipam'@'localhost' identified by '123456'; -- 授权 grant all privileges on `zy-ipam`.* to 'zy_ipam'@'localhost'; -- 刷新权限 flush privileges; -- 退出 exit;
2.3 导入 sql 数据
# 将 sql 文件上传到 /opt/ipam/sql/ 后执行 cd /opt/ipam/sql mysql -u zy_ipam -p123456 `zy-ipam` < zy-ipam.sql
2.4 验证数据库
mysql -u zy_ipam -p123456 -e "use \`zy-ipam\`; show tables;"
三、后端部署
3.1 上传 jar 包
# 将 jar 包上传到 /opt/ipam/backend/ # 示例文件:zy-admin.jar
3.2 创建配置文件application-prod.yml
文件位置: /opt/ipam/config/application-prod.yml
# 生产环境配置
# 服务器配置
server:
port: 8080
servlet:
context-path: /
tomcat:
uri-encoding: utf-8
accept-count: 1000
threads:
max: 800
min-spare: 100
# 日志配置
logging:
level:
com.zy: info
org.springframework: warn
# 数据源配置
spring:
datasource:
type: com.alibaba.druid.pool.druiddatasource
driverclassname: com.mysql.cj.jdbc.driver
druid:
master:
# 数据库名带短横线,url 中直接写
url: jdbc:mysql://localhost:3306/zy-ipam?useunicode=true&characterencoding=utf8&zerodatetimebehavior=converttonull&usessl=true&servertimezone=gmt%2b8
username: zy_ipam
password: 123456
slave:
enabled: false
initialsize: 5
minidle: 10
maxactive: 20
maxwait: 60000
connecttimeout: 30000
sockettimeout: 60000
timebetweenevictionrunsmillis: 60000
minevictableidletimemillis: 300000
maxevictableidletimemillis: 900000
validationquery: select 1 from dual
testwhileidle: true
testonborrow: false
testonreturn: false
webstatfilter:
enabled: true
statviewservlet:
enabled: true
allow:
url-pattern: /druid/*
login-username: ruoyi
login-password: 123456
filter:
stat:
enabled: true
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
# redis 配置
data:
redis:
host: localhost
port: 6379
database: 0
password:
timeout: 10s
lettuce:
pool:
min-idle: 0
max-idle: 8
max-active: 8
max-wait: -1ms
# token 配置
token:
header: authorization
secret: abcdefghijklmnopqrstuvwxyz
expiretime: 30
# mybatis 配置
mybatis:
typealiasespackage: com.zy.**.domain
mapperlocations: classpath*:mapper/**/*mapper.xml
configlocation: classpath:mybatis/mybatis-config.xml
# pagehelper 分页插件
pagehelper:
helperdialect: mysql
supportmethodsarguments: true
params: count=countsql
# 项目相关配置
ipam:
name: zy-ipam
version: 3.9.2
copyrightyear: 2026
profile: /opt/ipam/uploadpath # 文件上传路径
addressenabled: false
captchatype: math3.3 后端启动脚本start.sh
文件位置: /opt/ipam/backend/start.sh
#!/bin/bash
# 项目根目录
project_dir="/opt/ipam"
# 进入后端目录
cd $project_dir/backend
# 停止旧进程
pkill -f zy-admin.jar
# 清理旧日志(可选)
> $project_dir/logs/backend.log
# 启动后端
nohup java -jar zy-admin.jar \
--spring.profiles.active=prod \
--spring.config.location=file:$project_dir/config/ \
> $project_dir/logs/backend.log 2>&1 &
# 等待启动
sleep 3
# 检查进程
if ps aux | grep -v grep | grep zy-admin.jar > /dev/null; then
echo "✅ 后端启动成功!pid: $(pgrep -f zy-admin.jar)"
echo "📋 查看日志: tail -f $project_dir/logs/backend.log"
else
echo "❌ 后端启动失败,请查看日志"
tail -20 $project_dir/logs/backend.log
fi
3.4 添加执行权限并启动
chmod +x /opt/ipam/backend/start.sh /opt/ipam/backend/start.sh
3.5 配置 systemd 服务(开机自启)
文件位置: /etc/systemd/system/zy-ipam.service
[unit] description=zy ipam backend service after=network.target mysql.service redis.service [service] type=simple user=root workingdirectory=/opt/ipam/backend execstart=/usr/bin/java -jar /opt/ipam/backend/zy-admin.jar --spring.profiles.active=prod --spring.config.location=file:/opt/ipam/config/ restart=on-failure restartsec=10 standardoutput=append:/opt/ipam/logs/backend.log standarderror=append:/opt/ipam/logs/backend-error.log [install] wantedby=multi-user.target
# 启用服务 sudo systemctl daemon-reload sudo systemctl enable zy-ipam sudo systemctl start zy-ipam sudo systemctl status zy-ipam # 查看日志 sudo journalctl -u zy-ipam -f
3.6 检查后端状态
# 检查进程 ps aux | grep zy-admin.jar # 检查端口 sudo netstat -tlnp | grep 8080 # 测试接口 curl http://localhost:8080
四、前端部署
4.1 打包前端
在本地执行:
# vue 项目打包 npm run build:prod # 或 yarn build:prod # 打包后生成 dist 目录
4.2 上传前端文件
# 将 dist 目录内容上传到 /opt/ipam/frontend/ # 确保 index.html 在 /opt/ipam/frontend/ 根目录
4.3 设置文件权限
sudo chown -r www-data:www-data /opt/ipam/frontend sudo chmod -r 755 /opt/ipam/frontend
五、nginx 配置
5.1 创建 nginx 配置文件
文件位置: /etc/nginx/sites-available/zy-ipam
server {
listen 80;
server_name scycx.xyz www.scycx.xyz; # 替换为你的域名
# 前端静态文件
location / {
root /opt/ipam/frontend;
index index.html;
try_files $uri $uri/ /index.html;
expires 1h;
add_header cache-control "public, immutable";
}
# 后端 api 代理
location /prod-api/ {
rewrite ^/prod-api/(.*)$ /$1 break;
proxy_pass http://localhost:8080;
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_connect_timeout 180s;
proxy_send_timeout 180s;
proxy_read_timeout 180s;
}
# 文件上传大小限制
client_max_body_size 50m;
# 静态资源缓存(可选)
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
root /opt/ipam/frontend;
expires 1y;
add_header cache-control "public, immutable";
}
}5.2 启用配置
# 创建软链接 sudo ln -s /etc/nginx/sites-available/zy-ipam /etc/nginx/sites-enabled/ # 测试配置 sudo nginx -t # 重载 nginx sudo systemctl reload nginx
5.3 检查 nginx 状态
sudo systemctl status nginx sudo netstat -tlnp | grep :80
六、最终启动顺序
# 1. 启动 mysql sudo systemctl start mysql # 2. 启动 redis sudo systemctl start redis # 3. 启动后端 cd /opt/ipam/backend && ./start.sh # 或使用 systemd sudo systemctl start zy-ipam # 4. 重载 nginx sudo systemctl reload nginx
七、验证部署
7.1 本地测试
# 测试前端 curl http://localhost # 测试后端 curl http://localhost:8080 # 测试 api curl http://localhost/prod-api/xxx
7.2 浏览器访问
http://scycx.xyz
八、chrome 浏览器 https 强制跳转问题解决
如果 chrome 自动跳转 https 导致无法访问:
方法一:关闭 https-only mode
- 打开
chrome://settings/security - 找到 "始终使用安全连接"
- 关闭该开关
方法二:修改 chrome flag
- 打开
chrome://flags/#https-upgrades-typed-schemes-navigation-no-timeout-fallback - 设置为 disabled
- 重启 chrome
方法三:使用其他浏览器
- edge、firefox 默认不强制 https
九、常用管理命令
# 查看后端日志 tail -f /opt/ipam/logs/backend.log # 查看 nginx 日志 sudo tail -f /var/log/nginx/access.log sudo tail -f /var/log/nginx/error.log # 重启后端 pkill -f zy-admin.jar && /opt/ipam/backend/start.sh # 重载 nginx sudo nginx -t && sudo systemctl reload nginx # 查看所有服务状态 sudo systemctl status mysql redis nginx zy-ipam # 查看端口 sudo netstat -tlnp | grep -e ":(80|8080|3306|6379)"
十、项目目录结构最终版
/opt/ipam/ ├── backend/ │ ├── zy-admin.jar # 后端 jar 包 │ └── start.sh # 启动脚本 ├── frontend/ │ ├── index.html # 前端入口 │ ├── favicon.ico # 图标 │ └── static/ # 静态资源 ├── config/ │ └── application-prod.yml # 生产环境配置 ├── logs/ │ └── backend.log # 后端日志 ├── sql/ │ └── zy-ipam.sql # sql 初始化脚本 └── uploadpath/ # 文件上传目录
十一、配置文件汇总
| 文件 | 位置 | 用途 |
|---|---|---|
application-prod.yml | /opt/ipam/config/ | 后端生产环境配置 |
zy-ipam | /etc/nginx/sites-available/ | nginx 站点配置 |
zy-ipam.service | /etc/systemd/system/ | systemd 服务配置 |
start.sh | /opt/ipam/backend/ | 后端启动脚本 |
十二、常见问题排查
12.1 后端启动失败
# 查看日志 tail -100 /opt/ipam/logs/backend.log # 常见原因: # - 数据库密码错误 # - redis 未启动 # - 端口被占用 # - 配置文件格式错误
12.2 前端访问 404
# 检查文件 ls -la /opt/ipam/frontend/index.html # 检查 nginx sudo nginx -t sudo systemctl status nginx
12.3 api 请求超时
# 检查后端 curl http://localhost:8080 # 检查 nginx 超时配置 # 在 location /prod-api/ 中添加 proxy_read_timeout 180s;
部署完成!
以上就是springboot+vue项目生产环境部署完整指南的详细内容,更多关于springboot+vue项目生产环境部署的资料请关注代码网其它相关文章!
发表评论