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

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

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

环境要求

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

一、环境准备

1. 宝塔面板安装

# 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. 软件安装清单

软件版本要求安装路径
Nginx1.20+/www/server/nginx
MySQL5.7+/www/server/mysql
JDK11/17/www/server/java
Node.js16.x LTS/www/server/nodejs

二、后端部署

目录结构

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

Java项目管理器配置

关键参数:

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

三、前端部署

构建流程

# 安装依赖(使用淘宝镜像加速)
npm install --registry=https://registry.npm.taobao.org
# 构建生产包
npm run build

目录结构

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

四、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自动跳转:
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}

常见问题排查表

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

进阶配置

1. 日志切割配置

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

2. 监控配置

建议监控:

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

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

posted @ 2025-08-23 11:44  yjbjingcha  阅读(28)  评论(0)    收藏  举报