# 测试环境部署指南

## 测试环境架构

| 服务器 | IP | 服务 |
|--------|-----|------|
| 测试机1 | 47.103.29.111 | 前端 (Nginx) |
| 测试机2 | 47.103.9.192 | 后端 (FastAPI) + PostgreSQL |

## 部署步骤

### 1. 测试机1 - 前端部署

```bash
# 拉取代码
cd /root/jiachenlong
git fetch --all
git checkout v1.1.21

# 安装依赖并构建
cd frontend
npm install
npm run build

# 复制静态文件到可访问目录
mkdir -p /var/www/html
cp -r dist/* /var/www/html/
cp -r ../static /var/www/html/
chmod -R 755 /var/www/html

# 配置Nginx
cat > /etc/nginx/conf.d/jiachenlong-test.conf << 'EOF'
server {
    listen 80;
    server_name _;
    
    root /var/www/html;
    index index.html;
    
    client_max_body_size 20M;
    
    # 静态资源（logo、图片等）
    location /static {
        alias /var/www/html/static;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }
    
    # SPA路由
    location / {
        try_files $uri $uri/ /index.html;
    }
    
    # API代理到后端
    location /api {
        proxy_pass http://47.103.9.192:3000/api;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
EOF

nginx -t && systemctl enable nginx && systemctl restart nginx
```

### 2. 测试机2 - 后端部署

```bash
# 拉取代码
cd /root/jiachenlong
git fetch --all
git checkout v1.1.21

# 安装Python依赖
pip3 install fastapi uvicorn sqlalchemy psycopg2-binary pydantic python-jose bcrypt python-multipart pillow dashscope alibabacloud-dysmsapi20170525 oss2 email-validator httpx python-dotenv

# 创建环境变量文件
cat > backend/.env << 'EOF'
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/zodiac
SECRET_KEY=test-secret-key-for-sms
ACCESS_TOKEN_EXPIRE_MINUTES=60
OSS_ACCESS_KEY_ID=LTAI5t6HUnpFBLEK9194kPVG
OSS_ACCESS_KEY_SECRET=LEr4Q8yRxb8D5b24cKfCwlt4MMoke1
OSS_BUCKET=jiachenlong-oss
OSS_ENDPOINT=oss-cn-hangzhou.aliyuncs.com
SMS_ACCESS_KEY_ID=LTAI5tQAx5niD7JQVqGE5acE
SMS_ACCESS_KEY_SECRET=QsQFAEKBkaNynIoKyvdIi3BUyWVZu1
SMS_SIGN_NAME=苏州算力
SMS_TEMPLATE_CODE=SMS_501590956
EOF

# 启动后端
cd backend
nohup python3 -m uvicorn app.main:app --host 0.0.0.0 --port 3000 > /tmp/backend.log 2>&1 &

# 复制静态文件
mkdir -p /var/www/html/static
cp -r ../static/* /var/www/html/static/
chmod -R 755 /var/www/html/static

# 配置Nginx
cat > /etc/nginx/conf.d/jiachenlong-test.conf << 'EOF'
server {
    listen 80;
    server_name _;
    
    root /var/www/html;
    index index.html;
    
    client_max_body_size 20M;
    
    location /static {
        alias /var/www/html/static;
    }
    
    location / {
        try_files $uri $uri/ /index.html;
    }
    
    location /api {
        proxy_pass http://127.0.0.1:3000/api;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
EOF

nginx -t && systemctl enable nginx && systemctl restart nginx
```

## 常见问题

### 问题1: Logo不显示

**原因:**
1. Nginx未配置`/static`路径，SPA路由捕获了请求
2. 静态文件在`/root`目录下，nginx无权限读取

**解决:**
1. 添加`location /static`配置
2. 将静态文件复制到`/var/www/html/static`

### 问题2: 短信发送失败 "找不到模板"

**原因:** sms.py中的默认配置未更新

**解决:** 修改`backend/app/services/sms.py`:
```python
SMS_CONFIG = {
    "access_key_id": os.getenv("SMS_ACCESS_KEY_ID", "LTAI5tQAx5niD7JQVqGE5acE"),
    "access_key_secret": os.getenv("SMS_ACCESS_KEY_SECRET", "QsQFAEKBkaNynIoKyvdIi3BUyWVZu1"),
    "sign_name": os.getenv("SMS_SIGN_NAME", "苏州算力"),
    "template_code": os.getenv("SMS_TEMPLATE_CODE", "SMS_501590956"),
}
```

### 问题3: 数据库连接失败 "Ident authentication failed"

**原因:** PostgreSQL默认使用ident认证

**解决:**
```bash
sed -i 's/ident/trust/g' /var/lib/pgsql/data/pg_hba.conf
systemctl restart postgresql
```

## 验证命令

```bash
# 测试机1验证
curl -s -o /dev/null -w "%{http_code}" http://47.103.29.111/
curl -s -o /dev/null -w "%{http_code}" http://47.103.29.111/static/images/jiachenlong-logo.png

# 测试机2验证
curl -s http://47.103.9.192:3000/
curl -s -o /dev/null -w "%{http_code}" http://47.103.9.192/static/images/jiachenlong-logo.png
```

## 短信配置

| 项目 | 值 |
|------|-----|
| 模板Code | SMS_501590956 |
| 签名 | 苏州算力 |
| AccessKey ID | LTAI5tQAx5niD7JQVqGE5acE |
| AccessKey Secret | QsQFAEKBkaNynIoKyvdIi3BUyWVZu1 |
