README.md: - 补充环境要求(Go 1.26 / PG 16 / Redis 7) - 添加数据库初始化迁移步骤 - 说明 .env 敏感信息注入 - 链接到部署文档 docs/deployment.md (新增): - 编译 + systemd 服务 - Nginx HTTPS 反向代理配置 - 生产环境 config.yaml 关键调整 - 首次注册站长 + 备份建议
156 lines
2.9 KiB
Markdown
156 lines
2.9 KiB
Markdown
# 生产环境部署指南
|
||
|
||
## 环境要求
|
||
|
||
| 依赖 | 最低版本 | 用途 |
|
||
|------|----------|------|
|
||
| Go | 1.26 | 编译 |
|
||
| PostgreSQL | 16 | 主数据库(用户、帖子、通知等) |
|
||
| Redis | 7 | 会话存储(推荐) |
|
||
| Nginx | 1.24+ | 反向代理 + 静态资源 |
|
||
|
||
## 1. 编译
|
||
|
||
```bash
|
||
CGO_ENABLED=0 go build -ldflags="-s -w" -o /usr/local/bin/mce cmd/server/main.go
|
||
```
|
||
|
||
## 2. 数据库初始化
|
||
|
||
```bash
|
||
# 创建数据库
|
||
sudo -u postgres createuser metazone -P
|
||
sudo -u postgres createdb metalab_prod -O metazone
|
||
|
||
# 执行迁移
|
||
for f in docs/migrations/0*.sql; do
|
||
psql -U metazone -d metalab_prod -f "$f"
|
||
done
|
||
```
|
||
|
||
## 3. 配置
|
||
|
||
复制并修改生产配置:
|
||
|
||
```bash
|
||
cp config.yaml /etc/mce/config.yaml
|
||
```
|
||
|
||
生产环境关键调整:
|
||
|
||
```yaml
|
||
server:
|
||
mode: release # 关闭 debug 日志
|
||
cookie_secure: true # HTTPS 下启用
|
||
|
||
session:
|
||
idle_timeout: 120 # 2 小时
|
||
remember_timeout: 43200 # 30 天
|
||
|
||
redis:
|
||
enabled: true # 生产环境建议启用
|
||
```
|
||
|
||
敏感信息通过环境变量注入(不写进配置文件):
|
||
|
||
```bash
|
||
export DB_PASSWORD="..."
|
||
export REDIS_PASSWORD="..."
|
||
```
|
||
|
||
## 4. systemd 服务
|
||
|
||
创建 `/etc/systemd/system/mce.service`:
|
||
|
||
```ini
|
||
[Unit]
|
||
Description=MetaZone Community Engine
|
||
After=network.target postgresql.service redis.service
|
||
|
||
[Service]
|
||
Type=simple
|
||
User=mce
|
||
WorkingDirectory=/opt/mce
|
||
EnvironmentFile=-/etc/mce/env
|
||
ExecStart=/usr/local/bin/mce
|
||
Restart=on-failure
|
||
RestartSec=5
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target
|
||
```
|
||
|
||
`/etc/mce/env`:
|
||
|
||
```bash
|
||
DB_PASSWORD=your_db_password
|
||
REDIS_PASSWORD=your_redis_password
|
||
```
|
||
|
||
启动:
|
||
|
||
```bash
|
||
sudo systemctl daemon-reload
|
||
sudo systemctl enable --now mce
|
||
```
|
||
|
||
## 5. Nginx 反向代理
|
||
|
||
```nginx
|
||
server {
|
||
listen 443 ssl http2;
|
||
server_name your-domain.com;
|
||
|
||
ssl_certificate /etc/ssl/certs/your-domain.pem;
|
||
ssl_certificate_key /etc/ssl/private/your-domain.key;
|
||
|
||
# 静态资源(Gin 不处理)
|
||
location /static/ {
|
||
root /opt/mce/templates/MetaLab-2026;
|
||
expires 30d;
|
||
add_header Cache-Control "public, immutable";
|
||
}
|
||
|
||
location / {
|
||
proxy_pass http://127.0.0.1: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;
|
||
}
|
||
}
|
||
|
||
server {
|
||
listen 80;
|
||
server_name your-domain.com;
|
||
return 301 https://$host$request_uri;
|
||
}
|
||
```
|
||
|
||
## 6. 验证
|
||
|
||
```bash
|
||
# 服务状态
|
||
sudo systemctl status mce
|
||
|
||
# 日志
|
||
sudo journalctl -u mce -f
|
||
|
||
# 健康检查
|
||
curl -I https://your-domain.com
|
||
```
|
||
|
||
## 首次注册站长
|
||
|
||
部署后首个注册用户自动获得 `owner`(站长)权限,可访问 `/admin` 管理后台。
|
||
|
||
## 备份建议
|
||
|
||
```bash
|
||
# 数据库备份
|
||
pg_dump -U metazone metalab_prod | gzip > backup_$(date +%Y%m%d).sql.gz
|
||
|
||
# 上传文件备份
|
||
rsync -av /opt/mce/storage/ /backup/storage/
|
||
```
|