目录

  1. 环境准备与基础工具安装
  2. MySQL数据库安装与配置
  3. Spring Boot后端项目部署
  4. Vue前端项目部署
  5. Nginx配置反向代理与HTTPS证书申请
  6. systemd服务管理与后台运行
  7. 跨域(CORS)配置
  8. 安全加固及防火墙设置
  9. 日志管理与性能优化
  10. 自动化部署与持续集成建议

1. 环境准备与基础工具安装

1.1 系统更新

sudo apt update && sudo apt upgrade -y

1.2 安装Java(建议使用OpenJDK 17)

sudo apt install openjdk-17-jdk -y
java -version

确保输出类似:

openjdk version "17.0.x"

1.3 安装Git、curl、Nginx等工具

sudo apt install git curl nginx -y

1.4 安装Maven(编译Spring Boot项目)

sudo apt install maven -y
mvn -version

1.5 安装Node.js和npm(Vue项目需要)

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
node -v
npm -v

2. MySQL数据库安装与配置

2.1 安装MySQL

sudo apt install mysql-server -y
sudo systemctl start mysql
sudo systemctl enable mysql

2.2 安全配置

sudo mysql_secure_installation
  • 设置root密码
  • 删除匿名用户
  • 禁止远程root登录
  • 删除测试数据库
  • 重新加载权限

2.3 创建数据库和用户

登录MySQL:

sudo mysql -u root -p

执行SQL:

CREATE DATABASE ai_fullstack_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'ai_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON ai_fullstack_db.* TO 'ai_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

3. Spring Boot后端项目部署

3.1 获取源码或上传Jar包

git clone https://github.com/your_repo/your_springboot_project.git
cd your_springboot_project

3.2 配置数据库连接(application.properties或application.yml)

spring.datasource.url=jdbc:mysql://localhost:3306/ai_fullstack_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=ai_user
spring.datasource.password=your_password

3.3 编译打包

mvn clean package -DskipTests

3.4 运行Jar包

nohup java -jar target/your_springboot_project.jar > springboot.log 2>&1 &

4. Vue前端项目部署

4.1 获取源码

git clone https://github.com/your_repo/your_vue_project.git
cd your_vue_project

4.2 安装依赖并打包

npm install
npm run build

4.3 拷贝构建文件到Nginx目录

sudo cp -r dist/* /var/www/html/

5. Nginx反向代理与HTTPS证书申请

5.1 配置Nginx代理Spring Boot接口和静态资源

编辑 /etc/nginx/sites-available/default

server {
    listen 80;
    server_name your_domain_or_ip;

    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /api/ {
        proxy_pass http://localhost:8080/api/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

5.2 测试配置并重启Nginx

sudo nginx -t
sudo systemctl restart nginx

5.3 使用Let’s Encrypt申请免费SSL证书

安装Certbot:

sudo apt install certbot python3-certbot-nginx -y

申请证书:

sudo certbot --nginx -d your_domain

完成后,自动配置HTTPS。


6. systemd服务管理与后台运行

6.1 创建Spring Boot服务文件 /etc/systemd/system/springboot.service

[Unit]
Description=Spring Boot Application
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/your_springboot_project
ExecStart=/usr/bin/java -jar /home/ubuntu/your_springboot_project/target/your_springboot_project.jar
SuccessExitStatus=143
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

6.2 启动并设置开机自启

sudo systemctl daemon-reload
sudo systemctl start springboot
sudo systemctl enable springboot

7. 跨域(CORS)配置

在Spring Boot的后端项目里,添加CORS配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/api/**")
                        .allowedOrigins("http://your_domain_or_ip")
                        .allowedMethods("GET", "POST", "PUT", "DELETE")
                        .allowCredentials(true);
            }
        };
    }
}

8. 安全加固及防火墙设置

8.1 配置UFW防火墙

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'  # 允许80和443端口
sudo ufw enable
sudo ufw status

9. 日志管理与性能优化

  • 定期查看Spring Boot日志:
journalctl -u springboot -f
  • Nginx访问日志:/var/log/nginx/access.log
  • 开启数据库慢查询日志,优化SQL语句
  • 利用缓存(Redis或内存缓存)减少数据库压力

10. 自动化部署与持续集成建议

  • 使用GitHub Actions/GitLab CI实现代码自动构建和部署
  • 结合Docker容器化部署,提高环境一致性
  • 结合Jenkins实现更复杂流水线
  • 使用PM2管理Node进程(Vue开发时)

总结

  • 本教程涵盖了从基础环境搭建、数据库配置、后端与前端部署、反向代理、HTTPS配置,到安全加固、日志管理及自动化部署的全流程方案。
  • 你可以根据项目需求灵活调整和扩展。