AI全栈工程师成长指南:Ubuntu云服务器部署Spring+Vue+MySQL实战
2025.09.12 10:21浏览量:1简介:本文详解在Ubuntu云服务器上部署Spring Boot后端、Vue前端与MySQL数据库的全流程,涵盖环境配置、项目构建、安全优化及自动化部署技巧,助力开发者快速搭建全栈AI应用开发环境。
一、环境准备与云服务器配置
1.1 云服务器选型与Ubuntu系统安装
选择云服务器时需考虑CPU核心数、内存容量及网络带宽。推荐配置:2核4G内存起步,搭配50GB SSD存储。Ubuntu 22.04 LTS因其长期支持特性成为首选系统。安装时注意:
- 使用SSH密钥认证替代密码登录
- 配置防火墙仅开放22(SSH)、80(HTTP)、443(HTTPS)端口
- 创建专用部署用户并加入sudo组
1.2 基础环境搭建
# 更新系统包sudo apt update && sudo apt upgrade -y# 安装必要工具sudo apt install -y git curl wget unzip nginx# 配置Java环境(OpenJDK 17)sudo apt install -y openjdk-17-jdkecho "export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64" >> ~/.bashrcsource ~/.bashrc# 安装Node.js与npm(LTS版本)curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -sudo apt install -y nodejs
二、MySQL数据库部署与优化
2.1 数据库安装与安全配置
# 安装MySQL 8.0sudo apt install -y mysql-server# 运行安全脚本sudo mysql_secure_installation# 配置项:# - 设置root密码# - 移除匿名用户# - 禁止root远程登录# - 移除测试数据库
2.2 数据库优化配置
修改/etc/mysql/mysql.conf.d/mysqld.cnf:
[mysqld]bind-address = 127.0.0.1 # 仅本地访问max_connections = 200innodb_buffer_pool_size = 1G # 内存的50%-70%character-set-server = utf8mb4collation-server = utf8mb4_unicode_ci
2.3 创建应用数据库
CREATE DATABASE ai_app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;CREATE USER 'ai_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';GRANT ALL PRIVILEGES ON ai_app.* TO 'ai_user'@'localhost';FLUSH PRIVILEGES;
三、Spring Boot后端部署
3.1 项目构建与依赖管理
推荐使用Maven构建项目,关键依赖配置:
<dependencies><!-- Spring Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- MySQL驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- JPA --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency></dependencies>
3.2 应用配置文件
application.properties示例:
# 服务器配置server.port=8080# 数据库配置spring.datasource.url=jdbc:mysql://localhost:3306/ai_app?useSSL=false&serverTimezone=UTCspring.datasource.username=ai_userspring.datasource.password=StrongPassword123!spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# JPA配置spring.jpa.hibernate.ddl-auto=updatespring.jpa.show-sql=truespring.jpa.properties.hibernate.format_sql=true
3.3 部署方式选择
方案一:Jar包直接运行
# 构建项目mvn clean package# 后台运行(使用nohup或systemd)nohup java -jar target/ai-app-0.0.1-SNAPSHOT.jar > app.log 2>&1 &
方案二:Systemd服务管理
创建/etc/systemd/system/ai-app.service:
[Unit]Description=AI Application BackendAfter=syslog.target network.target mysql.service[Service]User=deployWorkingDirectory=/home/deploy/ai-appExecStart=/usr/bin/java -jar /home/deploy/ai-app/target/ai-app.jarSuccessExitStatus=143Restart=always[Install]WantedBy=multi-user.target
四、Vue前端部署方案
4.1 项目构建配置
修改vue.config.js:
module.exports = {publicPath: process.env.NODE_ENV === 'production' ? '/dist/' : '/',outputDir: 'dist',devServer: {proxy: {'/api': {target: 'http://localhost:8080',changeOrigin: true}}}}
4.2 生产环境构建
# 安装依赖npm install# 构建生产版本npm run build# 构建结果将生成在dist目录
4.3 Nginx反向代理配置
修改/etc/nginx/sites-available/ai-app:
server {listen 80;server_name your-domain.com;root /home/deploy/ai-app/dist;index index.html;location / {try_files $uri $uri/ /index.html;}location /api {proxy_pass http://127.0.0.1:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;}# 静态资源缓存location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {expires 1y;add_header Cache-Control "public";}}
五、安全加固与性能优化
5.1 安全防护措施
- 配置Fail2Ban防止暴力破解
- 启用UFW防火墙限制访问
- 定期更新系统补丁
- 使用HTTPS加密通信(Let’s Encrypt免费证书)
5.2 性能监控方案
# 安装监控工具sudo apt install -y htop nmon# Spring Boot Actuator配置# 在application.properties中添加:management.endpoints.web.exposure.include=health,metrics,infomanagement.endpoint.health.show-details=always
5.3 日志管理策略
- 配置Logback实现日志分级
- 使用
logrotate进行日志轮转 - 集中式日志管理(ELK Stack可选)
六、自动化部署实践
6.1 Git Hook自动部署
创建.git/hooks/post-receive:
#!/bin/bashTARGET="/home/deploy/ai-app"GIT_DIR="$TARGET/.git"cd $TARGETgit pull origin mainmvn clean packagesystemctl restart ai-app
6.2 CI/CD集成方案
推荐使用GitHub Actions或Jenkins实现:
# GitHub Actions示例name: Deploy AI Appon:push:branches: [ main ]jobs:deploy:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v2- name: Set up JDKuses: actions/setup-java@v1with:java-version: '17'- name: Build with Mavenrun: mvn -B package --file pom.xml- name: Deploy to Serveruses: appleboy/ssh-action@masterwith:host: ${{ secrets.SERVER_IP }}username: ${{ secrets.SERVER_USER }}key: ${{ secrets.SSH_PRIVATE_KEY }}script: |cd /home/deploy/ai-appgit pull origin mainmvn clean packagesystemctl restart ai-app
七、常见问题解决方案
7.1 数据库连接问题
- 检查MySQL服务状态:
systemctl status mysql - 验证连接参数是否正确
- 查看MySQL错误日志:
/var/log/mysql/error.log
7.2 端口冲突处理
# 查找占用端口的进程sudo lsof -i :8080# 终止冲突进程sudo kill -9 <PID>
7.3 前端资源加载失败
- 检查Nginx配置中的root路径
- 验证构建后的静态文件是否存在
- 清除浏览器缓存后重试
通过以上系统化的部署方案,开发者可以在Ubuntu云服务器上高效构建稳定的Spring+Vue+MySQL全栈环境。实际部署时建议先在测试环境验证,再逐步迁移到生产环境,同时建立完善的监控和备份机制确保系统可靠性。

发表评论
登录后可评论,请前往 登录 或 注册