logo

AI全栈工程师成长指南:Ubuntu云服务器部署Spring+Vue+MySQL实战

作者:狼烟四起2025.09.12 10:21浏览量:0

简介:本文详解在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 基础环境搭建

  1. # 更新系统包
  2. sudo apt update && sudo apt upgrade -y
  3. # 安装必要工具
  4. sudo apt install -y git curl wget unzip nginx
  5. # 配置Java环境(OpenJDK 17)
  6. sudo apt install -y openjdk-17-jdk
  7. echo "export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64" >> ~/.bashrc
  8. source ~/.bashrc
  9. # 安装Node.js与npm(LTS版本)
  10. curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
  11. sudo apt install -y nodejs

二、MySQL数据库部署与优化

2.1 数据库安装与安全配置

  1. # 安装MySQL 8.0
  2. sudo apt install -y mysql-server
  3. # 运行安全脚本
  4. sudo mysql_secure_installation
  5. # 配置项:
  6. # - 设置root密码
  7. # - 移除匿名用户
  8. # - 禁止root远程登录
  9. # - 移除测试数据库

2.2 数据库优化配置

修改/etc/mysql/mysql.conf.d/mysqld.cnf

  1. [mysqld]
  2. bind-address = 127.0.0.1 # 仅本地访问
  3. max_connections = 200
  4. innodb_buffer_pool_size = 1G # 内存的50%-70%
  5. character-set-server = utf8mb4
  6. collation-server = utf8mb4_unicode_ci

2.3 创建应用数据库

  1. CREATE DATABASE ai_app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
  2. CREATE USER 'ai_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';
  3. GRANT ALL PRIVILEGES ON ai_app.* TO 'ai_user'@'localhost';
  4. FLUSH PRIVILEGES;

三、Spring Boot后端部署

3.1 项目构建与依赖管理

推荐使用Maven构建项目,关键依赖配置:

  1. <dependencies>
  2. <!-- Spring Web -->
  3. <dependency>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-web</artifactId>
  6. </dependency>
  7. <!-- MySQL驱动 -->
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. <scope>runtime</scope>
  12. </dependency>
  13. <!-- JPA -->
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-data-jpa</artifactId>
  17. </dependency>
  18. </dependencies>

3.2 应用配置文件

application.properties示例:

  1. # 服务器配置
  2. server.port=8080
  3. # 数据库配置
  4. spring.datasource.url=jdbc:mysql://localhost:3306/ai_app?useSSL=false&serverTimezone=UTC
  5. spring.datasource.username=ai_user
  6. spring.datasource.password=StrongPassword123!
  7. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  8. # JPA配置
  9. spring.jpa.hibernate.ddl-auto=update
  10. spring.jpa.show-sql=true
  11. spring.jpa.properties.hibernate.format_sql=true

3.3 部署方式选择

方案一:Jar包直接运行

  1. # 构建项目
  2. mvn clean package
  3. # 后台运行(使用nohup或systemd)
  4. nohup java -jar target/ai-app-0.0.1-SNAPSHOT.jar > app.log 2>&1 &

方案二:Systemd服务管理

创建/etc/systemd/system/ai-app.service

  1. [Unit]
  2. Description=AI Application Backend
  3. After=syslog.target network.target mysql.service
  4. [Service]
  5. User=deploy
  6. WorkingDirectory=/home/deploy/ai-app
  7. ExecStart=/usr/bin/java -jar /home/deploy/ai-app/target/ai-app.jar
  8. SuccessExitStatus=143
  9. Restart=always
  10. [Install]
  11. WantedBy=multi-user.target

四、Vue前端部署方案

4.1 项目构建配置

修改vue.config.js

  1. module.exports = {
  2. publicPath: process.env.NODE_ENV === 'production' ? '/dist/' : '/',
  3. outputDir: 'dist',
  4. devServer: {
  5. proxy: {
  6. '/api': {
  7. target: 'http://localhost:8080',
  8. changeOrigin: true
  9. }
  10. }
  11. }
  12. }

4.2 生产环境构建

  1. # 安装依赖
  2. npm install
  3. # 构建生产版本
  4. npm run build
  5. # 构建结果将生成在dist目录

4.3 Nginx反向代理配置

修改/etc/nginx/sites-available/ai-app

  1. server {
  2. listen 80;
  3. server_name your-domain.com;
  4. root /home/deploy/ai-app/dist;
  5. index index.html;
  6. location / {
  7. try_files $uri $uri/ /index.html;
  8. }
  9. location /api {
  10. proxy_pass http://127.0.0.1:8080;
  11. proxy_set_header Host $host;
  12. proxy_set_header X-Real-IP $remote_addr;
  13. }
  14. # 静态资源缓存
  15. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
  16. expires 1y;
  17. add_header Cache-Control "public";
  18. }
  19. }

五、安全加固与性能优化

5.1 安全防护措施

  • 配置Fail2Ban防止暴力破解
  • 启用UFW防火墙限制访问
  • 定期更新系统补丁
  • 使用HTTPS加密通信(Let’s Encrypt免费证书)

5.2 性能监控方案

  1. # 安装监控工具
  2. sudo apt install -y htop nmon
  3. # Spring Boot Actuator配置
  4. # 在application.properties中添加:
  5. management.endpoints.web.exposure.include=health,metrics,info
  6. management.endpoint.health.show-details=always

5.3 日志管理策略

  • 配置Logback实现日志分级
  • 使用logrotate进行日志轮转
  • 集中式日志管理(ELK Stack可选)

六、自动化部署实践

6.1 Git Hook自动部署

创建.git/hooks/post-receive

  1. #!/bin/bash
  2. TARGET="/home/deploy/ai-app"
  3. GIT_DIR="$TARGET/.git"
  4. cd $TARGET
  5. git pull origin main
  6. mvn clean package
  7. systemctl restart ai-app

6.2 CI/CD集成方案

推荐使用GitHub Actions或Jenkins实现:

  1. # GitHub Actions示例
  2. name: Deploy AI App
  3. on:
  4. push:
  5. branches: [ main ]
  6. jobs:
  7. deploy:
  8. runs-on: ubuntu-latest
  9. steps:
  10. - uses: actions/checkout@v2
  11. - name: Set up JDK
  12. uses: actions/setup-java@v1
  13. with:
  14. java-version: '17'
  15. - name: Build with Maven
  16. run: mvn -B package --file pom.xml
  17. - name: Deploy to Server
  18. uses: appleboy/ssh-action@master
  19. with:
  20. host: ${{ secrets.SERVER_IP }}
  21. username: ${{ secrets.SERVER_USER }}
  22. key: ${{ secrets.SSH_PRIVATE_KEY }}
  23. script: |
  24. cd /home/deploy/ai-app
  25. git pull origin main
  26. mvn clean package
  27. systemctl restart ai-app

七、常见问题解决方案

7.1 数据库连接问题

  • 检查MySQL服务状态:systemctl status mysql
  • 验证连接参数是否正确
  • 查看MySQL错误日志:/var/log/mysql/error.log

7.2 端口冲突处理

  1. # 查找占用端口的进程
  2. sudo lsof -i :8080
  3. # 终止冲突进程
  4. sudo kill -9 <PID>

7.3 前端资源加载失败

  • 检查Nginx配置中的root路径
  • 验证构建后的静态文件是否存在
  • 清除浏览器缓存后重试

通过以上系统化的部署方案,开发者可以在Ubuntu云服务器上高效构建稳定的Spring+Vue+MySQL全栈环境。实际部署时建议先在测试环境验证,再逐步迁移到生产环境,同时建立完善的监控和备份机制确保系统可靠性。

相关文章推荐

发表评论