8.3 KiB
8.3 KiB
🚀 DesignerCEP 部署配置手册(子域名方案)
📐 架构概览
app.aidg168.uk → 前端应用(Caddy 静态文件)
backend.aidg168.uk → 后端 API(FastAPI)
🌐 第 1 步:配置 DNS(Cloudflare)
登录 Cloudflare,添加以下 DNS 记录:
| 类型 | 名称 | 内容 | 代理状态 | TTL |
|---|---|---|---|---|
| A | app | 103.97.201.136 | ☁️ 已代理 | 自动 |
| A | backend | 103.97.201.136 | ☁️ 已代理 | 自动 |
| A | @ | 103.97.201.136 | ☁️ 已代理 | 自动 |
注:103.97.201.136 是您的服务器 IP
📁 第 2 步:服务器目录准备
SSH 登录服务器:
ssh root@103.97.201.136
创建目录结构:
# 创建静态文件目录
sudo mkdir -p /var/www/DesignerCEP/Server/static/app
# 设置权限
sudo chown -R www-data:www-data /var/www/DesignerCEP
sudo chmod -R 755 /var/www/DesignerCEP
⚙️ 第 3 步:配置 Caddy
方法 A:直接使用提供的配置文件
# 1. 上传 Caddyfile 到服务器
# 在本地执行:
scp Caddyfile root@103.97.201.136:/etc/caddy/Caddyfile
# 2. SSH 登录服务器
ssh root@103.97.201.136
# 3. 验证配置
sudo caddy validate --config /etc/caddy/Caddyfile
# 4. 重启 Caddy
sudo systemctl restart caddy
# 5. 查看状态
sudo systemctl status caddy
方法 B:手动编辑
# 编辑 Caddy 配置
sudo nano /etc/caddy/Caddyfile
# 粘贴 Caddyfile 的内容(已在项目根目录)
# 保存后验证
sudo caddy validate --config /etc/caddy/Caddyfile
# 重启
sudo systemctl restart caddy
📤 第 4 步:上传前端文件
构建前端
在本地执行:
cd Designer
# 构建主应用
npm run build:core
# 输出在 dist_core/ 目录
上传到服务器
方法 A:手动上传(临时测试)
# 在本地 Designer 目录执行
cd dist_core
# 上传所有文件到服务器
scp -r * root@103.97.201.136:/var/www/DesignerCEP/Server/static/app/
方法 B:使用自动化脚本(推荐)
修改 AdminTool/deploy_config.json:
{
"host": "103.97.201.136",
"port": "22",
"username": "root",
"password": "tuEj-jkaw-8mFe",
"remote_path": "/var/www/DesignerCEP/Server/static"
}
然后执行:
cd AdminTool
python auto_deploy_core.py --version 1.0.0 --deploy --update-db
🔍 第 5 步:验证部署
5.1 检查文件是否上传成功
SSH 登录服务器:
ssh root@103.97.201.136
# 检查文件
ls -la /var/www/DesignerCEP/Server/static/app/
# 应该看到:
# index-core.html
# assets/
# CSInterface.js
# vite.svg
5.2 测试静态文件访问
在浏览器访问:
https://app.aidg168.uk/index-core.html
期望结果:能看到页面(即使可能显示错误,至少文件能加载)
5.3 测试 API
# 测试健康检查
curl https://backend.aidg168.uk/health
# 期望输出:
# {"status":"healthy","timestamp":"...","env":"production"}
5.4 测试完整流程
- 在浏览器打开:
https://app.aidg168.uk/ - 应该能看到登录页面(如果已合并)
- 或者 404(如果还没合并 Shell 和 Core)
⚠️ 当前问题
问题 1:文件名不匹配
您的 Core 构建输出是 index-core.html,但 Caddy 和浏览器默认访问 index.html。
解决方法 A:修改 Caddy 配置
app.aidg168.uk {
root * /var/www/DesignerCEP/Server/static/app
# 指定默认文件
try_files {path} /index-core.html
file_server
}
解决方法 B(推荐):修改构建配置
// Designer/vite.config.ts
build: {
outDir: 'dist_core',
rollupOptions: {
input: 'index-core.html' // ← 改成 'index.html'
}
}
问题 2:Shell 和 Core 还没合并
目前 Shell(登录)和 Core(主功能)还是分开的。
需要:
- 把登录页面路由加到 Core 应用
- 或者先测试用 Core 的主页
🎯 快速测试方案
临时方案:先让 Core 能访问
- 修改 Caddy 配置(访问 index-core.html):
app.aidg168.uk {
root * /var/www/DesignerCEP/Server/static/app
# 默认访问 index-core.html
@root {
path /
}
rewrite @root /index-core.html
try_files {path} /index-core.html
file_server
encode gzip
}
- 上传文件:
cd Designer/dist_core
scp -r * root@103.97.201.136:/var/www/DesignerCEP/Server/static/app/
- 访问测试:
https://app.aidg168.uk/
📋 完整 Caddyfile 配置
复制这个内容到服务器的 /etc/caddy/Caddyfile:
# ==================== DesignerCEP Caddy 配置 ====================
{
# 邮箱用于 HTTPS 证书申请
email admin@aidg168.uk
# 如果使用 Cloudflare 橙云代理,取消下面这行注释
# auto_https off
}
# ==================== 前端应用 ====================
app.aidg168.uk {
# 静态文件根目录
root * /var/www/DesignerCEP/Server/static/app
# SPA 路由支持(所有请求都返回 index-core.html)
try_files {path} /index-core.html
# 提供静态文件
file_server
# ========== 缓存策略 ==========
# HTML 不缓存
@html path *.html
header @html Cache-Control "no-cache, no-store, must-revalidate"
# JS/CSS 长期缓存
@assets path *.js *.css *.woff *.woff2
header @assets Cache-Control "public, max-age=31536000, immutable"
# 图片缓存
@images path *.png *.jpg *.jpeg *.gif *.svg *.ico
header @images Cache-Control "public, max-age=2592000"
# ========== 安全头 ==========
header {
X-Frame-Options "SAMEORIGIN"
X-Content-Type-Options "nosniff"
X-XSS-Protection "1; mode=block"
-Server
}
# ========== 压缩 ==========
encode gzip
# ========== 日志 ==========
log {
output file /var/log/caddy/app.log {
roll_size 50mb
roll_keep 5
}
}
}
# ==================== 后端 API ====================
backend.aidg168.uk {
# 反向代理到 FastAPI (端口 8000)
reverse_proxy localhost:8000 {
# 传递真实 IP
header_up X-Real-IP {remote_host}
header_up X-Forwarded-For {remote_host}
header_up X-Forwarded-Proto {scheme}
header_up X-Forwarded-Host {host}
}
# ========== 压缩 ==========
encode gzip
# ========== 日志 ==========
log {
output file /var/log/caddy/backend.log {
roll_size 50mb
roll_keep 5
}
}
}
# ==================== 主域名重定向(可选)====================
aidg168.uk, www.aidg168.uk {
# 重定向到应用
redir https://app.aidg168.uk{uri} permanent
}
✅ 部署清单
执行以下命令:
# ========== 在本地 ==========
# 1. 构建前端
cd Designer
npm run build:core
# 2. 上传 Caddyfile
scp Caddyfile root@103.97.201.136:/etc/caddy/Caddyfile
# 3. 上传前端文件
cd dist_core
scp -r * root@103.97.201.136:/var/www/DesignerCEP/Server/static/app/
# ========== 在服务器 ==========
# 4. SSH 登录
ssh root@103.97.201.136
# 5. 验证 Caddy 配置
sudo caddy validate --config /etc/caddy/Caddyfile
# 6. 重启 Caddy
sudo systemctl restart caddy
# 7. 查看状态
sudo systemctl status caddy
# 8. 查看日志(如果有错误)
sudo journalctl -u caddy -n 50
🧪 测试
测试 1:静态文件
curl -I https://app.aidg168.uk/
# 期望:HTTP/2 200
测试 2:API
curl https://backend.aidg168.uk/health
# 期望:{"status":"healthy"}
测试 3:浏览器访问
https://app.aidg168.uk/
🔧 常见问题
Q1: 访问 app.aidg168.uk 显示 404
检查:
ls -la /var/www/DesignerCEP/Server/static/app/index-core.html
Q2: API 请求 CORS 错误
检查:FastAPI 的 ALLOWED_ORIGINS 是否包含 https://app.aidg168.uk
# 编辑 .env
sudo nano /var/www/DesignerCEP/Server/.env
# 确保有:
# ALLOWED_ORIGINS=https://app.aidg168.uk,https://backend.aidg168.uk
Q3: Caddy 证书申请失败
如果使用 Cloudflare 橙云代理,修改 Caddyfile:
{
auto_https off # ← 添加这行
}
然后重启 Caddy。
部署完成后,访问 https://app.aidg168.uk/ 测试! 🎉