Files
mce/docs/deployment.md
Victor_Jay e7185f58fb refactor: 连接配置统一迁移至 .env,config.yaml 仅作默认值
- 新增 .env.example 模板(DB/Redis/Server 全部连接信息)
- bindEnvOverride 补全 11 个环境变量绑定(原仅 2 个密码)
- README 和 deployment.md 改为 .env 优先流程
- 移除 systemd EnvironmentFile,统一读 WorkingDirectory/.env
2026-06-22 02:49:24 +08:00

148 lines
2.8 KiB
Markdown
Raw Permalink 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.

# 生产环境部署指南
## 环境要求
| 依赖 | 最低版本 | 用途 |
|------|----------|------|
| 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 .env.example .env
```
编辑 `.env`,填入生产环境值:
```bash
# 服务
SERVER_MODE=release
# 数据库(生产实例地址)
DATABASE_HOST=10.0.0.1
DATABASE_PORT=5432
DATABASE_USER=metazone
DATABASE_PASSWORD=your_secure_password
DATABASE_DBNAME=metalab_prod
DATABASE_SSLMODE=require
# Redis
REDIS_ENABLED=true
REDIS_HOST=10.0.0.2
REDIS_PORT=6379
REDIS_PASSWORD=your_redis_password
REDIS_DB=0
```
> `config.yaml` 中的默认值会被 `.env` 覆盖,通常无需修改。
## 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
ExecStart=/usr/local/bin/mce
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
```
启动:
```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/
```