Files
trade-assistant/deploy/README.md
T
TradeMate Dev 5a1af9f82f feat: production branch with deploy config for baota panel
- Add deploy/ directory with production env, supervisor, nginx, migration configs
- Include all latest features: admin management, feedback, footer with ICP/beian
- Database: foreign_trade (PostgreSQL), user: foreign_trade
- Frontend: trade.yuzhiran.com, backend proxy via Nginx
2026-05-14 09:19:30 +08:00

106 lines
2.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 生产部署文档(宝塔面板)
## 前提条件
宝塔面板已安装:
- Nginx
- PostgreSQL(已有数据库 `foreign_trade`
- Redis
- Python 3.11+
## 目录结构(宝塔站点根目录)
```
/www/wwwroot/trade.yuzhiran.com/
├── backend/ # FastAPI 后端代码
│ ├── .env # 生产环境变量
│ ├── app/
│ ├── alembic/
│ └── ...
├── frontend/ # 前端静态文件
│ └── dist/ # uni-app 构建产物
└── deploy/ # 部署配置(此目录)
```
## 部署步骤
### 1. 上传代码到服务器
将项目代码(production 分支)上传到 `/www/wwwroot/trade.yuzhiran.com/`
### 2. 配置后端环境变量
```bash
cp deploy/backend/.env.production backend/.env
# 编辑 backend/.env,填入:
# - SECRET_KEY(随机字符串,用于 JWT 签名)
# - AI API KeyOPENAI_API_KEY 或 SENSENOVA_API_KEY 等)
vim backend/.env
```
### 3. 安装后端依赖 & 运行迁移
```bash
cd backend
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
alembic upgrade head
```
### 4. 构建前端
```bash
cd uni-app
npm install
npm run build:h5
# 产物在 dist/build/h5/ 目录
# 将 dist/build/h5/ 下的内容复制到 /www/wwwroot/trade.yuzhiran.com/frontend/dist/
```
### 5. 配置 Nginx(宝塔面板操作)
在宝塔面板中:
1. 添加站点 `trade.yuzhiran.com`
2. 站点根目录设为 `/www/wwwroot/trade.yuzhiran.com/frontend/dist`
3. 修改 Nginx 配置,参考 `deploy/frontend/nginx.conf`
关键配置点:
- 根目录指向前端 dist
- `/api/` 反向代理到 `http://127.0.0.1:8000`
- SPA fallback: `try_files $uri $uri/ /index.html`
### 6. 启动后端(宝塔 Python 项目管理器)
在宝塔面板中使用 **Python项目管理器**
- 项目路径:`/www/wwwroot/trade.yuzhiran.com/backend`
- Python 版本:3.11
- 启动命令:`uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 2`
- 项目名称:`trademate-backend`
或者使用 Supervisor 命令行部署:
```bash
pip install supervisor
supervisord -c deploy/backend/supervisord.conf
```
### 7. 验证
- 访问 https://trade.yuzhiran.com → 前端正常加载
- 访问 https://trade.yuzhiran.com/api/health → 返回 `{"status": "ok"}`
## 常见问题
**Q: 数据库迁移失败?**
A: 确认 PostgreSQL 中 `foreign_trade` 数据库已创建,用户有权限。
```sql
CREATE DATABASE foreign_trade;
GRANT ALL PRIVILEGES ON DATABASE foreign_trade TO foreign_trade;
```
**Q: 前端 API 请求 502**
A: 检查后端是否启动,以及 Nginx 中 `/api/` 的 proxy_pass 地址是否正确。
**Q: CORS 报错?**
A: 确认 `backend/.env` 中的 `FRONTEND_URL` 与实际前端域名一致。