docs: 快速开始(含 DB 迁移步骤) + 生产部署指南
README.md: - 补充环境要求(Go 1.26 / PG 16 / Redis 7) - 添加数据库初始化迁移步骤 - 说明 .env 敏感信息注入 - 链接到部署文档 docs/deployment.md (新增): - 编译 + systemd 服务 - Nginx HTTPS 反向代理配置 - 生产环境 config.yaml 关键调整 - 首次注册站长 + 备份建议
This commit is contained in:
60
README.md
60
README.md
@ -70,21 +70,71 @@ MCE 是 **MetaZone Community Engine** 的缩写,是 MetaZone 品牌下的社
|
|||||||
|
|
||||||
## 快速开始
|
## 快速开始
|
||||||
|
|
||||||
|
### 环境要求
|
||||||
|
|
||||||
|
| 依赖 | 最低版本 | 说明 |
|
||||||
|
|------|----------|------|
|
||||||
|
| Go | 1.26 | 编译运行 |
|
||||||
|
| PostgreSQL | 16 | 主数据库 |
|
||||||
|
| Redis | 7 | 会话存储(可选,关闭后使用内存存储) |
|
||||||
|
|
||||||
|
### 1. 克隆并配置
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# 克隆仓库
|
|
||||||
git clone git@git.metazone.cc:MetaZone/mce.git
|
git clone git@git.metazone.cc:MetaZone/mce.git
|
||||||
cd mce
|
cd mce
|
||||||
|
```
|
||||||
|
|
||||||
# 配置数据库和 Redis
|
编辑 `config.yaml`,修改数据库和 Redis 连接信息:
|
||||||
cp .env.example .env
|
|
||||||
# 编辑 .env 填入 PostgreSQL 和 Redis 连接信息
|
|
||||||
|
|
||||||
# 运行
|
```yaml
|
||||||
|
database:
|
||||||
|
host: 127.0.0.1
|
||||||
|
port: 5432
|
||||||
|
user: metazone
|
||||||
|
password: "your_password" # 或用 .env 注入
|
||||||
|
dbname: metalab_dev # 提前创建此数据库
|
||||||
|
|
||||||
|
redis:
|
||||||
|
enabled: true # false 则使用内存存储,无需 Redis
|
||||||
|
host: 127.0.0.1
|
||||||
|
port: 6379
|
||||||
|
password: ""
|
||||||
|
```
|
||||||
|
|
||||||
|
敏感信息(数据库密码、Redis 密码)可通过 `.env` 注入:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# .env(git 不追踪)
|
||||||
|
DB_PASSWORD=your_password
|
||||||
|
REDIS_PASSWORD=your_redis_password
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 初始化数据库
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 按序执行迁移 SQL
|
||||||
|
psql -U metazone -d metalab_dev -f docs/migrations/001_uid_to_id.sql
|
||||||
|
psql -U metazone -d metalab_dev -f docs/migrations/002_announcements.sql
|
||||||
|
psql -U metazone -d metalab_dev -f docs/migrations/003_post_attributes.sql
|
||||||
|
psql -U metazone -d metalab_dev -f docs/migrations/004_categories_tags.sql
|
||||||
|
psql -U metazone -d metalab_dev -f docs/migrations/005_scheduled_posts.sql
|
||||||
|
```
|
||||||
|
|
||||||
|
首次部署后注册的第一个用户自动获得站长(owner)权限。
|
||||||
|
|
||||||
|
### 3. 启动
|
||||||
|
|
||||||
|
```bash
|
||||||
go run cmd/server/main.go
|
go run cmd/server/main.go
|
||||||
|
# 或编译后运行
|
||||||
|
go build -o mce cmd/server/main.go && ./mce
|
||||||
```
|
```
|
||||||
|
|
||||||
启动后访问 `http://localhost:8080`。
|
启动后访问 `http://localhost:8080`。
|
||||||
|
|
||||||
|
> 生产环境部署详见 [`docs/deployment.md`](docs/deployment.md)。
|
||||||
|
|
||||||
## 项目结构
|
## 项目结构
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|||||||
155
docs/deployment.md
Normal file
155
docs/deployment.md
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
# 生产环境部署指南
|
||||||
|
|
||||||
|
## 环境要求
|
||||||
|
|
||||||
|
| 依赖 | 最低版本 | 用途 |
|
||||||
|
|------|----------|------|
|
||||||
|
| 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/
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user