847 lines
17 KiB
Markdown
847 lines
17 KiB
Markdown
|
|
# 🐧 Linux 云端部署完整指南
|
|||
|
|
|
|||
|
|
本文档提供将项目打包并部署到Linux云服务器的完整步骤。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 📦 一、本地打包(Windows/Mac)
|
|||
|
|
|
|||
|
|
### 1.1 打包后端JAR文件
|
|||
|
|
|
|||
|
|
在项目根目录(`demo/`)执行:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# Windows (PowerShell)
|
|||
|
|
.\mvnw.cmd clean package -DskipTests
|
|||
|
|
|
|||
|
|
# Linux/Mac
|
|||
|
|
./mvnw clean package -DskipTests
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
打包成功后,JAR文件位置:
|
|||
|
|
```
|
|||
|
|
demo/target/demo-0.0.1-SNAPSHOT.jar
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 1.2 打包前端
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 进入前端目录
|
|||
|
|
cd frontend
|
|||
|
|
|
|||
|
|
# 安装依赖(首次需要)
|
|||
|
|
npm install
|
|||
|
|
|
|||
|
|
# 构建生产版本
|
|||
|
|
npm run build
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
构建成功后,前端文件位置:
|
|||
|
|
```
|
|||
|
|
demo/frontend/dist/
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 1.3 准备部署文件
|
|||
|
|
|
|||
|
|
创建部署包目录结构:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 在项目根目录创建部署包
|
|||
|
|
mkdir -p deploy-package
|
|||
|
|
mkdir -p deploy-package/backend
|
|||
|
|
mkdir -p deploy-package/frontend
|
|||
|
|
mkdir -p deploy-package/scripts
|
|||
|
|
mkdir -p deploy-package/config
|
|||
|
|
|
|||
|
|
# 复制文件
|
|||
|
|
cp target/demo-0.0.1-SNAPSHOT.jar deploy-package/backend/
|
|||
|
|
cp -r frontend/dist/* deploy-package/frontend/
|
|||
|
|
cp deploy.sh deploy-package/scripts/
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🚀 二、服务器环境准备
|
|||
|
|
|
|||
|
|
### 2.1 连接服务器
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 使用SSH连接服务器
|
|||
|
|
ssh username@your-server-ip
|
|||
|
|
|
|||
|
|
# 或使用密钥
|
|||
|
|
ssh -i ~/.ssh/your-key.pem username@your-server-ip
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2.2 安装必需软件
|
|||
|
|
|
|||
|
|
#### Ubuntu/Debian
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 更新系统
|
|||
|
|
sudo apt update && sudo apt upgrade -y
|
|||
|
|
|
|||
|
|
# 安装Java 21
|
|||
|
|
sudo apt install -y openjdk-21-jdk
|
|||
|
|
|
|||
|
|
# 验证Java安装
|
|||
|
|
java -version
|
|||
|
|
# 应该显示: openjdk version "21.x.x"
|
|||
|
|
|
|||
|
|
# 安装MySQL 8.0
|
|||
|
|
sudo apt install -y mysql-server
|
|||
|
|
|
|||
|
|
# 启动MySQL服务
|
|||
|
|
sudo systemctl start mysql
|
|||
|
|
sudo systemctl enable mysql
|
|||
|
|
|
|||
|
|
# 安装FFmpeg(视频处理需要)
|
|||
|
|
sudo apt install -y ffmpeg
|
|||
|
|
|
|||
|
|
# 验证FFmpeg安装
|
|||
|
|
ffmpeg -version
|
|||
|
|
|
|||
|
|
# 安装Nginx(反向代理)
|
|||
|
|
sudo apt install -y nginx
|
|||
|
|
|
|||
|
|
# 安装Maven(可选,用于重新构建)
|
|||
|
|
sudo apt install -y maven
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
#### CentOS/RHEL
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 更新系统
|
|||
|
|
sudo yum update -y
|
|||
|
|
|
|||
|
|
# 安装Java 21
|
|||
|
|
sudo yum install -y java-21-openjdk java-21-openjdk-devel
|
|||
|
|
|
|||
|
|
# 安装MySQL 8.0
|
|||
|
|
sudo yum install -y mysql-server
|
|||
|
|
|
|||
|
|
# 启动MySQL服务
|
|||
|
|
sudo systemctl start mysqld
|
|||
|
|
sudo systemctl enable mysqld
|
|||
|
|
|
|||
|
|
# 安装FFmpeg
|
|||
|
|
sudo yum install -y epel-release
|
|||
|
|
sudo yum install -y ffmpeg
|
|||
|
|
|
|||
|
|
# 安装Nginx
|
|||
|
|
sudo yum install -y nginx
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 2.3 创建应用目录
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 创建应用目录结构
|
|||
|
|
sudo mkdir -p /app/aigc-platform
|
|||
|
|
sudo mkdir -p /app/aigc-platform/{logs,uploads,temp,config,backend,frontend}
|
|||
|
|
|
|||
|
|
# 设置权限
|
|||
|
|
sudo chown -R $USER:$USER /app/aigc-platform
|
|||
|
|
chmod -R 755 /app/aigc-platform
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🗄️ 三、数据库配置
|
|||
|
|
|
|||
|
|
### 3.1 配置MySQL
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 安全配置MySQL(首次安装)
|
|||
|
|
sudo mysql_secure_installation
|
|||
|
|
|
|||
|
|
# 登录MySQL
|
|||
|
|
sudo mysql -u root -p
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3.2 创建数据库和用户
|
|||
|
|
|
|||
|
|
```sql
|
|||
|
|
-- 创建数据库
|
|||
|
|
CREATE DATABASE aigc_platform CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
|||
|
|
|
|||
|
|
-- 创建用户(替换'your_password'为强密码)
|
|||
|
|
CREATE USER 'aigc_user'@'localhost' IDENTIFIED BY 'your_password';
|
|||
|
|
|
|||
|
|
-- 授予权限
|
|||
|
|
GRANT ALL PRIVILEGES ON aigc_platform.* TO 'aigc_user'@'localhost';
|
|||
|
|
FLUSH PRIVILEGES;
|
|||
|
|
|
|||
|
|
-- 退出
|
|||
|
|
EXIT;
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 3.3 优化MySQL配置
|
|||
|
|
|
|||
|
|
编辑MySQL配置文件:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# Ubuntu/Debian
|
|||
|
|
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
|
|||
|
|
|
|||
|
|
# CentOS/RHEL
|
|||
|
|
sudo nano /etc/my.cnf
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
添加/修改以下配置:
|
|||
|
|
|
|||
|
|
```ini
|
|||
|
|
[mysqld]
|
|||
|
|
# 字符集
|
|||
|
|
character-set-server=utf8mb4
|
|||
|
|
collation-server=utf8mb4_unicode_ci
|
|||
|
|
|
|||
|
|
# 连接数(50人并发)
|
|||
|
|
max_connections=100
|
|||
|
|
max_user_connections=50
|
|||
|
|
|
|||
|
|
# 缓冲池(根据内存调整,8GB服务器建议2-4GB)
|
|||
|
|
innodb_buffer_pool_size=2G
|
|||
|
|
|
|||
|
|
# 日志
|
|||
|
|
slow_query_log=1
|
|||
|
|
slow_query_log_file=/var/log/mysql/slow-query.log
|
|||
|
|
long_query_time=2
|
|||
|
|
|
|||
|
|
# 时区
|
|||
|
|
default-time-zone='+08:00'
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
重启MySQL:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
sudo systemctl restart mysql
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 📤 四、上传文件到服务器
|
|||
|
|
|
|||
|
|
### 4.1 使用SCP上传(推荐)
|
|||
|
|
|
|||
|
|
在**本地**执行:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 上传JAR文件
|
|||
|
|
scp target/demo-0.0.1-SNAPSHOT.jar username@your-server-ip:/app/aigc-platform/backend/
|
|||
|
|
|
|||
|
|
# 上传前端文件
|
|||
|
|
scp -r frontend/dist/* username@your-server-ip:/app/aigc-platform/frontend/
|
|||
|
|
|
|||
|
|
# 或使用rsync(更高效)
|
|||
|
|
rsync -avz frontend/dist/ username@your-server-ip:/app/aigc-platform/frontend/
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 4.2 使用FTP/SFTP工具
|
|||
|
|
|
|||
|
|
使用FileZilla、WinSCP等工具上传文件到:
|
|||
|
|
- JAR文件 → `/app/aigc-platform/backend/`
|
|||
|
|
- 前端文件 → `/app/aigc-platform/frontend/`
|
|||
|
|
|
|||
|
|
### 4.3 使用Git(如果服务器有Git)
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 在服务器上克隆仓库
|
|||
|
|
cd /app/aigc-platform
|
|||
|
|
git clone ssh://git@your-git-server/path/to/repo.git
|
|||
|
|
|
|||
|
|
# 构建
|
|||
|
|
cd repo/demo
|
|||
|
|
./mvnw clean package -DskipTests
|
|||
|
|
cp target/demo-0.0.1-SNAPSHOT.jar /app/aigc-platform/backend/
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## ⚙️ 五、应用配置
|
|||
|
|
|
|||
|
|
### 5.1 创建配置文件
|
|||
|
|
|
|||
|
|
在服务器上创建配置文件:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 创建生产环境配置
|
|||
|
|
sudo nano /app/aigc-platform/config/application-prod.properties
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
配置文件内容:
|
|||
|
|
|
|||
|
|
```properties
|
|||
|
|
# 数据库配置
|
|||
|
|
spring.datasource.url=jdbc:mysql://localhost:3306/aigc_platform?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai
|
|||
|
|
spring.datasource.username=aigc_user
|
|||
|
|
spring.datasource.password=your_password
|
|||
|
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
|||
|
|
|
|||
|
|
# 连接池配置(50人并发)
|
|||
|
|
spring.datasource.hikari.maximum-pool-size=30
|
|||
|
|
spring.datasource.hikari.minimum-idle=10
|
|||
|
|
spring.datasource.hikari.connection-timeout=30000
|
|||
|
|
spring.datasource.hikari.idle-timeout=600000
|
|||
|
|
spring.datasource.hikari.max-lifetime=1800000
|
|||
|
|
|
|||
|
|
# JPA配置
|
|||
|
|
spring.jpa.hibernate.ddl-auto=update
|
|||
|
|
spring.jpa.show-sql=false
|
|||
|
|
spring.jpa.properties.hibernate.format_sql=true
|
|||
|
|
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
|
|||
|
|
|
|||
|
|
# 服务器配置
|
|||
|
|
server.port=8080
|
|||
|
|
server.tomcat.threads.max=150
|
|||
|
|
server.tomcat.threads.min-spare=20
|
|||
|
|
server.tomcat.max-connections=200
|
|||
|
|
server.tomcat.accept-count=100
|
|||
|
|
|
|||
|
|
# JWT配置
|
|||
|
|
jwt.secret=your-jwt-secret-key-change-this-to-random-string
|
|||
|
|
jwt.expiration=86400000
|
|||
|
|
|
|||
|
|
# AI API配置(从环境变量读取)
|
|||
|
|
ai.api.base-url=${AI_API_BASE_URL:https://api.openai.com}
|
|||
|
|
ai.api.key=${AI_API_KEY:your-api-key}
|
|||
|
|
ai.image.api.base-url=${AI_IMAGE_API_BASE_URL:https://api.openai.com}
|
|||
|
|
ai.image.api.key=${AI_IMAGE_API_KEY:your-api-key}
|
|||
|
|
|
|||
|
|
# 文件上传配置
|
|||
|
|
app.upload.path=${UPLOAD_PATH:/app/aigc-platform/uploads}
|
|||
|
|
spring.servlet.multipart.max-file-size=100MB
|
|||
|
|
spring.servlet.multipart.max-request-size=100MB
|
|||
|
|
|
|||
|
|
# 日志配置
|
|||
|
|
logging.level.root=INFO
|
|||
|
|
logging.level.com.example.demo=DEBUG
|
|||
|
|
logging.file.path=/app/aigc-platform/logs
|
|||
|
|
logging.file.name=${logging.file.path}/application.log
|
|||
|
|
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
|
|||
|
|
|
|||
|
|
# 时区
|
|||
|
|
spring.jackson.time-zone=Asia/Shanghai
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 5.2 创建环境变量文件(可选)
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
sudo nano /app/aigc-platform/config/.env
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 数据库配置
|
|||
|
|
export DB_URL="jdbc:mysql://localhost:3306/aigc_platform?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai"
|
|||
|
|
export DB_USERNAME="aigc_user"
|
|||
|
|
export DB_PASSWORD="your_password"
|
|||
|
|
|
|||
|
|
# JWT密钥(生成随机密钥)
|
|||
|
|
export JWT_SECRET="$(openssl rand -base64 64)"
|
|||
|
|
|
|||
|
|
# AI API密钥
|
|||
|
|
export AI_API_KEY="your-api-key"
|
|||
|
|
export AI_IMAGE_API_KEY="your-api-key"
|
|||
|
|
|
|||
|
|
# 文件路径
|
|||
|
|
export UPLOAD_PATH="/app/aigc-platform/uploads"
|
|||
|
|
export TEMP_DIR="/app/aigc-platform/temp"
|
|||
|
|
export FFMPEG_PATH="/usr/bin/ffmpeg"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🔧 六、创建Systemd服务
|
|||
|
|
|
|||
|
|
### 6.1 创建服务文件
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
sudo nano /etc/systemd/system/aigc-platform.service
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
服务文件内容:
|
|||
|
|
|
|||
|
|
```ini
|
|||
|
|
[Unit]
|
|||
|
|
Description=AIGC Platform Application
|
|||
|
|
After=network.target mysql.service
|
|||
|
|
Requires=mysql.service
|
|||
|
|
|
|||
|
|
[Service]
|
|||
|
|
Type=simple
|
|||
|
|
User=your-username
|
|||
|
|
Group=your-group
|
|||
|
|
WorkingDirectory=/app/aigc-platform/backend
|
|||
|
|
|
|||
|
|
# 环境变量(可选,如果使用.env文件)
|
|||
|
|
# EnvironmentFile=/app/aigc-platform/config/.env
|
|||
|
|
|
|||
|
|
# JVM参数(根据服务器内存调整)
|
|||
|
|
# 8GB内存服务器建议:-Xms1g -Xmx4g
|
|||
|
|
# 16GB内存服务器建议:-Xms2g -Xmx8g
|
|||
|
|
ExecStart=/usr/bin/java \
|
|||
|
|
-Xms1g \
|
|||
|
|
-Xmx4g \
|
|||
|
|
-XX:+UseG1GC \
|
|||
|
|
-XX:MaxGCPauseMillis=200 \
|
|||
|
|
-Dspring.profiles.active=prod \
|
|||
|
|
-Dspring.config.location=file:/app/aigc-platform/config/application-prod.properties \
|
|||
|
|
-jar /app/aigc-platform/backend/demo-0.0.1-SNAPSHOT.jar
|
|||
|
|
|
|||
|
|
# 重启策略
|
|||
|
|
Restart=always
|
|||
|
|
RestartSec=10
|
|||
|
|
|
|||
|
|
# 日志
|
|||
|
|
StandardOutput=journal
|
|||
|
|
StandardError=journal
|
|||
|
|
SyslogIdentifier=aigc-platform
|
|||
|
|
|
|||
|
|
# 安全设置
|
|||
|
|
NoNewPrivileges=true
|
|||
|
|
PrivateTmp=true
|
|||
|
|
|
|||
|
|
[Install]
|
|||
|
|
WantedBy=multi-user.target
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**重要:** 将 `your-username` 和 `your-group` 替换为实际的用户名和组。
|
|||
|
|
|
|||
|
|
### 6.2 启动服务
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 重新加载systemd配置
|
|||
|
|
sudo systemctl daemon-reload
|
|||
|
|
|
|||
|
|
# 启用服务(开机自启)
|
|||
|
|
sudo systemctl enable aigc-platform
|
|||
|
|
|
|||
|
|
# 启动服务
|
|||
|
|
sudo systemctl start aigc-platform
|
|||
|
|
|
|||
|
|
# 查看服务状态
|
|||
|
|
sudo systemctl status aigc-platform
|
|||
|
|
|
|||
|
|
# 查看日志
|
|||
|
|
sudo journalctl -u aigc-platform -f
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 6.3 常用服务管理命令
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 启动服务
|
|||
|
|
sudo systemctl start aigc-platform
|
|||
|
|
|
|||
|
|
# 停止服务
|
|||
|
|
sudo systemctl stop aigc-platform
|
|||
|
|
|
|||
|
|
# 重启服务
|
|||
|
|
sudo systemctl restart aigc-platform
|
|||
|
|
|
|||
|
|
# 查看状态
|
|||
|
|
sudo systemctl status aigc-platform
|
|||
|
|
|
|||
|
|
# 查看日志
|
|||
|
|
sudo journalctl -u aigc-platform -n 100 -f
|
|||
|
|
|
|||
|
|
# 查看最近错误
|
|||
|
|
sudo journalctl -u aigc-platform -p err -n 50
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🌐 七、Nginx反向代理配置
|
|||
|
|
|
|||
|
|
### 7.1 创建Nginx配置
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
sudo nano /etc/nginx/sites-available/aigc-platform
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
配置内容:
|
|||
|
|
|
|||
|
|
```nginx
|
|||
|
|
# HTTP服务器(重定向到HTTPS)
|
|||
|
|
server {
|
|||
|
|
listen 80;
|
|||
|
|
server_name your-domain.com www.your-domain.com;
|
|||
|
|
|
|||
|
|
# 重定向到HTTPS
|
|||
|
|
return 301 https://$server_name$request_uri;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# HTTPS服务器
|
|||
|
|
server {
|
|||
|
|
listen 443 ssl http2;
|
|||
|
|
server_name your-domain.com www.your-domain.com;
|
|||
|
|
|
|||
|
|
# SSL证书配置(使用Let's Encrypt)
|
|||
|
|
ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
|
|||
|
|
ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
|
|||
|
|
ssl_protocols TLSv1.2 TLSv1.3;
|
|||
|
|
ssl_ciphers HIGH:!aNULL:!MD5;
|
|||
|
|
ssl_prefer_server_ciphers on;
|
|||
|
|
|
|||
|
|
# 日志
|
|||
|
|
access_log /var/log/nginx/aigc-platform-access.log;
|
|||
|
|
error_log /var/log/nginx/aigc-platform-error.log;
|
|||
|
|
|
|||
|
|
# 客户端最大上传大小
|
|||
|
|
client_max_body_size 100M;
|
|||
|
|
|
|||
|
|
# API代理
|
|||
|
|
location /api/ {
|
|||
|
|
proxy_pass http://localhost:8080;
|
|||
|
|
proxy_set_header Host $host;
|
|||
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|||
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|||
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|||
|
|
|
|||
|
|
# WebSocket支持(如果需要)
|
|||
|
|
proxy_http_version 1.1;
|
|||
|
|
proxy_set_header Upgrade $http_upgrade;
|
|||
|
|
proxy_set_header Connection "upgrade";
|
|||
|
|
|
|||
|
|
# 超时设置
|
|||
|
|
proxy_connect_timeout 60s;
|
|||
|
|
proxy_send_timeout 60s;
|
|||
|
|
proxy_read_timeout 60s;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 上传文件代理
|
|||
|
|
location /uploads/ {
|
|||
|
|
proxy_pass http://localhost:8080;
|
|||
|
|
proxy_set_header Host $host;
|
|||
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|||
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|||
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|||
|
|
|
|||
|
|
# 缓存设置
|
|||
|
|
expires 7d;
|
|||
|
|
add_header Cache-Control "public, immutable";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 前端静态文件
|
|||
|
|
location / {
|
|||
|
|
root /app/aigc-platform/frontend;
|
|||
|
|
index index.html;
|
|||
|
|
try_files $uri $uri/ /index.html;
|
|||
|
|
|
|||
|
|
# 缓存设置
|
|||
|
|
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
|||
|
|
expires 1y;
|
|||
|
|
add_header Cache-Control "public, immutable";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 健康检查
|
|||
|
|
location /health {
|
|||
|
|
proxy_pass http://localhost:8080/actuator/health;
|
|||
|
|
access_log off;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 7.2 启用配置
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 创建符号链接
|
|||
|
|
sudo ln -s /etc/nginx/sites-available/aigc-platform /etc/nginx/sites-enabled/
|
|||
|
|
|
|||
|
|
# 测试配置
|
|||
|
|
sudo nginx -t
|
|||
|
|
|
|||
|
|
# 重新加载Nginx
|
|||
|
|
sudo systemctl reload nginx
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 7.3 配置SSL证书(Let's Encrypt)
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 安装Certbot
|
|||
|
|
sudo apt install -y certbot python3-certbot-nginx
|
|||
|
|
|
|||
|
|
# 获取证书(自动配置Nginx)
|
|||
|
|
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
|
|||
|
|
|
|||
|
|
# 测试自动续期
|
|||
|
|
sudo certbot renew --dry-run
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## ✅ 八、验证部署
|
|||
|
|
|
|||
|
|
### 8.1 检查服务状态
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 检查应用服务
|
|||
|
|
sudo systemctl status aigc-platform
|
|||
|
|
|
|||
|
|
# 检查MySQL服务
|
|||
|
|
sudo systemctl status mysql
|
|||
|
|
|
|||
|
|
# 检查Nginx服务
|
|||
|
|
sudo systemctl status nginx
|
|||
|
|
|
|||
|
|
# 检查端口占用
|
|||
|
|
sudo netstat -tlnp | grep -E '8080|80|443'
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 8.2 测试API
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 测试健康检查
|
|||
|
|
curl http://localhost:8080/actuator/health
|
|||
|
|
|
|||
|
|
# 测试API(需要认证)
|
|||
|
|
curl -X POST http://localhost:8080/api/auth/login \
|
|||
|
|
-H "Content-Type: application/json" \
|
|||
|
|
-d '{"username":"test","password":"test"}'
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 8.3 测试前端
|
|||
|
|
|
|||
|
|
在浏览器中访问:
|
|||
|
|
- HTTP: `http://your-server-ip`
|
|||
|
|
- HTTPS: `https://your-domain.com`
|
|||
|
|
|
|||
|
|
### 8.4 检查日志
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 应用日志
|
|||
|
|
tail -f /app/aigc-platform/logs/application.log
|
|||
|
|
|
|||
|
|
# 系统日志
|
|||
|
|
sudo journalctl -u aigc-platform -f
|
|||
|
|
|
|||
|
|
# Nginx日志
|
|||
|
|
sudo tail -f /var/log/nginx/aigc-platform-access.log
|
|||
|
|
sudo tail -f /var/log/nginx/aigc-platform-error.log
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🔄 九、更新部署
|
|||
|
|
|
|||
|
|
### 9.1 更新后端
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 1. 停止服务
|
|||
|
|
sudo systemctl stop aigc-platform
|
|||
|
|
|
|||
|
|
# 2. 备份旧版本
|
|||
|
|
cp /app/aigc-platform/backend/demo-0.0.1-SNAPSHOT.jar \
|
|||
|
|
/app/aigc-platform/backend/demo-0.0.1-SNAPSHOT.jar.backup
|
|||
|
|
|
|||
|
|
# 3. 上传新版本(从本地)
|
|||
|
|
# scp target/demo-0.0.1-SNAPSHOT.jar username@server:/app/aigc-platform/backend/
|
|||
|
|
|
|||
|
|
# 4. 启动服务
|
|||
|
|
sudo systemctl start aigc-platform
|
|||
|
|
|
|||
|
|
# 5. 检查状态
|
|||
|
|
sudo systemctl status aigc-platform
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 9.2 更新前端
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 1. 备份旧版本
|
|||
|
|
cp -r /app/aigc-platform/frontend /app/aigc-platform/frontend.backup
|
|||
|
|
|
|||
|
|
# 2. 上传新版本(从本地)
|
|||
|
|
# scp -r frontend/dist/* username@server:/app/aigc-platform/frontend/
|
|||
|
|
|
|||
|
|
# 3. 重新加载Nginx
|
|||
|
|
sudo systemctl reload nginx
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🛠️ 十、故障排查
|
|||
|
|
|
|||
|
|
### 10.1 应用无法启动
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 查看详细日志
|
|||
|
|
sudo journalctl -u aigc-platform -n 100
|
|||
|
|
|
|||
|
|
# 检查Java版本
|
|||
|
|
java -version
|
|||
|
|
|
|||
|
|
# 检查端口占用
|
|||
|
|
sudo lsof -i :8080
|
|||
|
|
|
|||
|
|
# 检查文件权限
|
|||
|
|
ls -la /app/aigc-platform/backend/
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 10.2 数据库连接失败
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 测试MySQL连接
|
|||
|
|
mysql -u aigc_user -p aigc_platform
|
|||
|
|
|
|||
|
|
# 检查MySQL服务
|
|||
|
|
sudo systemctl status mysql
|
|||
|
|
|
|||
|
|
# 查看MySQL日志
|
|||
|
|
sudo tail -f /var/log/mysql/error.log
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 10.3 Nginx配置错误
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 测试配置
|
|||
|
|
sudo nginx -t
|
|||
|
|
|
|||
|
|
# 查看错误日志
|
|||
|
|
sudo tail -f /var/log/nginx/error.log
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 10.4 内存不足
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 查看内存使用
|
|||
|
|
free -h
|
|||
|
|
|
|||
|
|
# 查看Java进程内存
|
|||
|
|
ps aux | grep java
|
|||
|
|
|
|||
|
|
# 调整JVM参数(在systemd服务文件中)
|
|||
|
|
# -Xms1g -Xmx4g # 减少内存使用
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 📊 十一、性能监控
|
|||
|
|
|
|||
|
|
### 11.1 系统监控
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# CPU和内存使用
|
|||
|
|
top
|
|||
|
|
htop # 如果安装了
|
|||
|
|
|
|||
|
|
# 磁盘使用
|
|||
|
|
df -h
|
|||
|
|
|
|||
|
|
# 网络连接
|
|||
|
|
netstat -an | grep ESTABLISHED | wc -l
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 11.2 应用监控
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 查看JVM内存使用
|
|||
|
|
jstat -gc <pid> 1000
|
|||
|
|
|
|||
|
|
# 查看线程
|
|||
|
|
jstack <pid>
|
|||
|
|
|
|||
|
|
# 查看应用日志
|
|||
|
|
tail -f /app/aigc-platform/logs/application.log | grep ERROR
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🔒 十二、安全建议
|
|||
|
|
|
|||
|
|
1. **防火墙配置**
|
|||
|
|
```bash
|
|||
|
|
# 只开放必要端口
|
|||
|
|
sudo ufw allow 22/tcp # SSH
|
|||
|
|
sudo ufw allow 80/tcp # HTTP
|
|||
|
|
sudo ufw allow 443/tcp # HTTPS
|
|||
|
|
sudo ufw enable
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
2. **定期备份**
|
|||
|
|
```bash
|
|||
|
|
# 备份数据库
|
|||
|
|
mysqldump -u aigc_user -p aigc_platform > backup_$(date +%Y%m%d).sql
|
|||
|
|
|
|||
|
|
# 备份上传文件
|
|||
|
|
tar -czf uploads_backup_$(date +%Y%m%d).tar.gz /app/aigc-platform/uploads
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
3. **更新系统**
|
|||
|
|
```bash
|
|||
|
|
sudo apt update && sudo apt upgrade -y
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 📝 快速部署脚本
|
|||
|
|
|
|||
|
|
创建一键部署脚本 `deploy-linux.sh`:
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
#!/bin/bash
|
|||
|
|
set -e
|
|||
|
|
|
|||
|
|
echo "🚀 开始部署 AIGC 平台..."
|
|||
|
|
|
|||
|
|
# 检查Java
|
|||
|
|
if ! command -v java &> /dev/null; then
|
|||
|
|
echo "❌ Java未安装,请先安装Java 21"
|
|||
|
|
exit 1
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
# 检查MySQL
|
|||
|
|
if ! systemctl is-active --quiet mysql; then
|
|||
|
|
echo "❌ MySQL服务未运行"
|
|||
|
|
exit 1
|
|||
|
|
fi
|
|||
|
|
|
|||
|
|
# 停止服务
|
|||
|
|
echo "🛑 停止服务..."
|
|||
|
|
sudo systemctl stop aigc-platform || true
|
|||
|
|
|
|||
|
|
# 备份
|
|||
|
|
echo "💾 备份旧版本..."
|
|||
|
|
BACKUP_DIR="/app/aigc-platform/backup/$(date +%Y%m%d_%H%M%S)"
|
|||
|
|
mkdir -p $BACKUP_DIR
|
|||
|
|
cp /app/aigc-platform/backend/demo-0.0.1-SNAPSHOT.jar $BACKUP_DIR/ || true
|
|||
|
|
|
|||
|
|
# 启动服务
|
|||
|
|
echo "▶️ 启动服务..."
|
|||
|
|
sudo systemctl start aigc-platform
|
|||
|
|
|
|||
|
|
# 等待启动
|
|||
|
|
sleep 5
|
|||
|
|
|
|||
|
|
# 检查状态
|
|||
|
|
if systemctl is-active --quiet aigc-platform; then
|
|||
|
|
echo "✅ 部署成功!"
|
|||
|
|
sudo systemctl status aigc-platform
|
|||
|
|
else
|
|||
|
|
echo "❌ 部署失败,查看日志:"
|
|||
|
|
sudo journalctl -u aigc-platform -n 50
|
|||
|
|
exit 1
|
|||
|
|
fi
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
使用:
|
|||
|
|
```bash
|
|||
|
|
chmod +x deploy-linux.sh
|
|||
|
|
./deploy-linux.sh
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 📞 支持
|
|||
|
|
|
|||
|
|
如遇问题,请检查:
|
|||
|
|
1. 日志文件:`/app/aigc-platform/logs/application.log`
|
|||
|
|
2. 系统日志:`sudo journalctl -u aigc-platform`
|
|||
|
|
3. Nginx日志:`/var/log/nginx/aigc-platform-error.log`
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
**部署完成后,访问:**
|
|||
|
|
- 前端:`https://your-domain.com`
|
|||
|
|
- API:`https://your-domain.com/api`
|
|||
|
|
- 健康检查:`https://your-domain.com/health`
|
|||
|
|
|