Spring Boot + Vue 项目用宝塔面板部署指南

Spring Boot + Vue 宝塔面板部署指南

(示意图:用户请求 → Nginx → 静态资源/Vue → 反向代理/SpringBoot API)

环境要求

  • 服务器配置建议
  • CPU: 2核+
  • 内存: 4GB+
  • 系统: CentOS 7+/Ubuntu 20.04+

一、环境准备

1. 宝塔面板安装

bash 复制代码
# CentOS
yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh

# Ubuntu
wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh

2. 软件安装清单

软件 版本要求 安装路径
Nginx 1.20+ /www/server/nginx
MySQL 5.7+ /www/server/mysql
JDK 11/17 /www/server/java
Node.js 16.x LTS /www/server/nodejs

二、后端部署

目录结构

复制代码
/www/wwwroot/backend/
├── your-project.jar# 主程序
├── config/# 配置文件目录
│└── application.yml# 生产环境配置
└── logs/# 日志目录

Java项目管理器配置

关键参数:

  • 项目路径:/www/wwwroot/backend
  • JDK版本:需与本地开发环境一致
  • 启动参数示例:
ini 复制代码
-Xms256m -Xmx512m -Dspring.profiles.active=prod

三、前端部署

构建流程

bash 复制代码
# 安装依赖(使用淘宝镜像加速)
npm install --registry=https://registry.npm.taobao.org

# 构建生产包
npm run build

目录结构

复制代码
/www/wwwroot/frontend/
├── dist/# 构建产物
│├── static/
│└── index.html
├── node_modules/
└── vue.config.js# 生产环境代理配置

四、Nginx核心配置

完整配置示例

nginx 复制代码
server {
listen 80;
server_name example.com;

# 前端路由配置
location / {
root /www/wwwroot/frontend/dist;
try_files $uri $uri/ /index.html;
add_header Cache-Control "no-cache";
}

# 后端API配置
location /api/ {
proxy_pass http://127.0.0.1:8080;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_read_timeout 300s;
}

# 静态资源缓存
location ~* \.(js|css|png|jpg)$ {
expires 365d;
access_log off;
}
}

五、HTTPS配置

  1. 宝塔面板 → 网站 → SSL → Let's Encrypt
  2. 勾选"强制HTTPS"
  3. 修改Nginx自动跳转:
nginx 复制代码
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}

常见问题排查表

问题现象 可能原因 解决方案
502 Bad Gateway Java服务未启动 检查journalctl -u java_project日志
接口404 Nginx路由配置错误 检查proxy_pass地址末尾是否带/
静态资源加载失败 Vue publicPath配置错误 确保设置为/./
数据库连接失败 MySQL用户权限不足 执行GRANT ALL ON db.* TO 'user'@'%'

进阶配置

1. 日志切割配置

bash 复制代码
# 在宝塔计划任务中添加
0 0 * * * /usr/sbin/logrotate -f /etc/logrotate.d/java-app

2. 监控配置

建议监控:

  • Java进程内存占用
  • MySQL连接数
  • 磁盘空间使用率

提示 :部署完成后建议进行压力测试,可使用ab -n 1000 -c 50 http://example.com/api/test测试接口性能

相关推荐
我认不到你1 天前
自定义注解实现 Redis Stream 消息监听
spring boot·redis
martin10171 天前
基于Spring Boot + Thymeleaf + Flying Saucer实现PDF导出功能
后端
程序员爱钓鱼1 天前
Node.js 编程实战:路由与中间件
前端·后端·node.js
程序员爱钓鱼1 天前
Node.js 编程实战:Express 基础
前端·后端·node.js
CosMr1 天前
【QT】【FFmpeg】 Qt 中FFmpeg环境搭建以及D__STDC_FORMAT_MACROS、PRId64解答
后端
周万宁.FoBJ1 天前
在vite+Vue3项目中使用 自定义svg 图标,借助vite-plugin-svg-icons 封装SvgIcon组件
vue.js
BD_Marathon1 天前
Vue3_文本渲染命令
vue.js
Knight_AL1 天前
Spring Boot 的主要特性与传统 Spring 项目的区别
spring boot·后端·spring
回家路上绕了弯1 天前
一文读懂分布式事务:核心原理、解决方案与实践思考
分布式·后端
踏浪无痕1 天前
JobFlow 背后:五个让我豁然开朗的设计瞬间
分布式·后端·架构