Ubuntu云服务器部署Spring Boot + Vue + MySQL的完整指南
一、引言
现代全栈应用部署需要前后端分离、数据库高效协同。通过Ubuntu云服务器部署Spring Boot(后端)、Vue(前端)和MySQL(数据库),可实现高可用、易扩展的Web服务。典型应用场景包括:
企业级SaaS平台
电商系统
数据可视化仪表盘
二、技术架构
graph TD
A[用户] --> B[Nginx]
–>静态资源
C[Vue]
–>API请求
D[Spring Boot]
–> E[MySQL]
组件分工:
Nginx:反向代理/静态资源托管(80/443端口)
Vue:前端构建产物(dist目录)
Spring Boot:提供REST API(默认8080端口)
MySQL:数据持久化(3306端口)
三、环境准备(Ubuntu 20.04+)
基础环境
更新系统
sudo apt update && sudo apt upgrade -y
安装必要工具
sudo apt install -y git curl wget unzip
安装核心组件
Java (Spring Boot)
sudo apt install -y openjdk-17-jdk
Node.js (Vue)
curl -fsSL deb.nodesourcecom/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
MySQL
sudo apt install -y mysql-server
sudo mysql_secure_installation
配置防火墙
sudo ufw allow 22
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
四、详细部署步骤
MySQL配置
– 创建数据库用户
CREATE DATABASE app_db;
CREATE USER ‘app_user’@‘localhost’ IDENTIFIED BY ‘StrongPassword123!’;
GRANT ALL PRIVILEGES ON app_db.* TO ‘app_user’@‘localhost’;
FLUSH PRIVILEGES;
Spring Boot部署
克隆项目
git clone githubcom/your-repo/spring-backendgit
cd spring-backend
配置文件修改
nano src/main/resources/application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/app_db
spring.datasource.username=app_user
spring.datasource.password=StrongPassword123!
构建并运行
./mvnw clean package
java -jar target/your-app.jar --spring.profiles.active=prod
Vue部署
构建生产版本
npm install
npm run build
复制到Nginx目录
sudo cp -r dist/* /var/www/html/
Nginx配置
sudo nano /etc/nginx/sites-available/your-domain.conf
server {
listen 80;
server_name your-domaincom;
location / {
root /var/www/html;
try_files uri uri/ /index.html;
location /api {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
}
sudo ln -s /etc/nginx/sites-available/your-domain.conf /etc/nginx/sites-enabled/
sudo systemctl restart nginx
五、测试验证
接口测试
curl -X GET localhost:8080/api/health
预期输出:{“status”:“UP”}
前端访问
浏览器打开http://your-server-ip应显示Vue应用界面
数据库监控
sudo mysql -u root -p -e “SHOW STATUS LIKE ‘Threads_connected’;”
六、生产优化
服务守护
创建Spring Boot系统服务
sudo nano /etc/systemd/system/spring.service
[Unit]
Description=Spring Boot Service
After=syslog.target
[Service]
User=ubuntu
ExecStart=/usr/bin/java -jar /home/ubuntu/spring-backend/target/your-app.jar
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start spring
HTTPS配置(Let’s Encrypt)
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your-domaincom
七、疑难解答
问题现象 解决方案
502 Bad Gateway 检查Spring是否运行sudo systemctl status spring
数据库连接失败 验证MySQL用户权限SHOW GRANTS FOR ‘app_user’@‘localhost’;
静态资源加载404 确认Nginx的root路径是否正确
八、未来扩展
容器化部署:使用Docker Compose整合服务
version: '3'
services:
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: rootpass
backend:
build: ./spring-backend
ports:
“8080:8080”
负载均衡:多节点部署 + Nginx upstream
监控系统:集成Prometheus + Grafana
九、总结
关键成功因素:
权限最小化原则(数据库用户权限)
服务进程守护(systemd)
自动化HTTPS(Certbot)
分层监控(应用/数据库/服务器)
- 点赞
- 收藏
- 关注作者
评论(0)