# Wagtail 部署指南

Wagtail 是一个基于 Django 的现代化 CMS 系统，以下是部署 Wagtail 的详细步骤，涵盖多种部署方式。

## 1. 本地开发环境部署

### 前置要求
- Python 3.7+
- PostgreSQL/MySQL/SQLite (推荐 PostgreSQL)
- Node.js (如需前端构建)

### 安装步骤

```bash
# 创建虚拟环境
python -m venv venv
source venv/bin/activate  # Linux/Mac
# venv\Scripts\activate   # Windows

# 安装 Wagtail
pip install wagtail

# 创建新项目
wagtail start mysite
cd mysite

# 安装依赖
pip install -r requirements.txt

# 初始化数据库
python manage.py migrate

# 创建超级用户
python manage.py createsuperuser

# 运行开发服务器
python manage.py runserver
```

访问 http://localhost:8000 查看站点，http://localhost:8000/admin 访问后台。

## 2. 生产环境部署 (Docker 方式)

### docker-compose.yml 示例

```yaml
version: '3.8'

services:
  db:
    image: postgres:13
    environment:
      POSTGRES_DB: wagtail
      POSTGRES_USER: wagtail
      POSTGRES_PASSWORD: wagtail
    volumes:
      - postgres_data:/var/lib/postgresql/data

  web:
    build: .
    command: gunicorn mysite.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db
    environment:
      DATABASE_URL: postgres://wagtail:wagtail@db:5432/wagtail
      SECRET_KEY: your-secret-key-here

volumes:
  postgres_data:
```

### Dockerfile 示例

```dockerfile
FROM python:3.9-slim

WORKDIR /code

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apt-get update && apt-get install -y \
    libpq-dev \
    gcc \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8000
```

### 部署步骤

```bash
# 构建并启动容器
docker-compose up -d --build

# 执行数据库迁移
docker-compose exec web python manage.py migrate

# 创建超级用户
docker-compose exec web python manage.py createsuperuser

# 收集静态文件
docker-compose exec web python manage.py collectstatic --no-input
```

## 3. 生产环境部署 (传统服务器方式)

### 使用 Gunicorn + Nginx

1. 安装 Gunicorn:
   ```bash
   pip install gunicorn
   ```

2. 创建 Gunicorn 服务文件 `/etc/systemd/system/gunicorn.service`:
   ```ini
   [Unit]
   Description=gunicorn daemon
   After=network.target

   [Service]
   User=youruser
   Group=www-data
   WorkingDirectory=/path/to/your/project
   ExecStart=/path/to/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock mysite.wsgi:application

   [Install]
   WantedBy=multi-user.target
   ```

3. Nginx 配置示例 (`/etc/nginx/sites-available/yourdomain`):
   ```nginx
   server {
       listen 80;
       server_name yourdomain.com;

       location = /favicon.ico { access_log off; log_not_found off; }
       
       location /static/ {
           root /path/to/your/project;
       }
       
       location /media/ {
           root /path/to/your/project;
       }
       
       location / {
           include proxy_params;
           proxy_pass http://unix:/run/gunicorn.sock;
       }
   }
   ```

4. 启用配置:
   ```bash
   sudo ln -s /etc/nginx/sites-available/yourdomain /etc/nginx/sites-enabled
   sudo systemctl restart nginx
   ```

## 4. 云平台部署

### Heroku 部署

1. 创建 `Procfile`:
   ```procfile
   web: gunicorn mysite.wsgi:application --bind 0.0.0.0:$PORT
   ```

2. 创建 `runtime.txt` 指定 Python 版本:
   ```text
   python-3.9.7
   ```

3. 部署命令:
   ```bash
   heroku create
   heroku addons:create heroku-postgresql:hobby-dev
   heroku config:set SECRET_KEY=your-secret-key
   heroku config:set DISABLE_COLLECTSTATIC=1
   git push heroku main
   heroku run python manage.py migrate
   heroku run python manage.py createsuperuser
   ```

### AWS Elastic Beanstalk 部署

1. 创建 `requirements.txt` 包含所有依赖
2. 创建 `.ebextensions/django.config`:
   ```yaml
   option_settings:
     aws:elasticbeanstalk:container:python:
       WSGIPath: mysite/wsgi.py
   ```

3. 部署命令:
   ```bash
   eb init -p python-3.9 wagtail-app
   eb create wagtail-env
   ```

## 5. 部署后配置

1. **安全配置**:
   ```python
   # settings.py
   DEBUG = False
   ALLOWED_HOSTS = ['yourdomain.com', 'localhost']
   SECURE_SSL_REDIRECT = True
   SESSION_COOKIE_SECURE = True
   CSRF_COOKIE_SECURE = True
   ```

2. **性能优化**:
   - 设置缓存
   - 启用数据库连接池
   - 配置静态文件 CDN

3. **备份策略**:
   ```bash
   # 数据库备份
   docker-compose exec db pg_dump -U wagtail wagtail > backup.sql
   
   # 媒体文件备份
   tar -czvf media_backup.tar.gz /path/to/media
   ```

## 6. 常见问题解决

1. **静态文件404错误**:
   - 确保执行了 `collectstatic`
   - 检查 Nginx/Apache 配置中的静态文件路径

2. **数据库连接问题**:
   - 检查数据库服务是否运行
   - 验证 `DATABASE_URL` 或 `settings.py` 中的配置

3. **性能问题**:
   - 增加 Gunicorn worker 数量 (通常 2-4 * CPU核心)
   - 添加缓存层 (Redis/Memcached)

通过以上步骤，您应该能够成功部署 Wagtail CMS 到各种环境中。根据您的具体需求选择最适合的部署方式。