详解SpringBoot+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. 软件安装清单
软件 | 版本要求 | 安装路径 |
---|---|---|
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版本:需与本地开发环境一致
- 启动参数示例:
-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配置
- 宝塔面板 → 网站 → SSL → Let’s Encrypt
- 勾选"强制HTTPS"
- 修改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. 日志切割配置
# 在宝塔计划任务中添加 0 0 * * * /usr/sbin/logrotate -f /etc/logrotate.d/java-app
2. 监控配置
建议监控:
- Java进程内存占用
- MySQL连接数
- 磁盘空间使用率
提示:部署完成后建议进行压力测试,可使用ab -n 1000 -c 50 http://example.com/api/test测试接口性能
到此这篇关于详解SpringBoot+Vue项目用宝塔面板部署指南的文章就介绍到这了,更多相关SpringBoot Vue宝塔面板部署内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!