501adf7819
- 删除 .gitlab-ci.yml - 新增 .gitea/workflows/deploy.yml (Gitea Actions 工作流) - 新增 deploy/docker/docker-compose.gitea.yml (Gitea + Act Runner) - 更新 scripts/setup-server.sh 初始化脚本 - 更新 deploy/README.md 部署文档 - 更新 Makefile 新增 gitea-* 命令 - 更新 .env.example 新增 GITEA_RUNNER_TOKEN Gitea 内存占用约 100MB,适合 4GB 小内存服务器 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
6.3 KiB
6.3 KiB
部署指南
架构概览
Internet → Gateway Nginx (80/443)
├─ rent-prod 网络
│ ├─ rent-prod-mysql (3306)
│ ├─ rent-prod-redis (6379)
│ ├─ rent-prod-server (3000)
│ ├─ rent-prod-merchant (8081→80)
│ ├─ rent-prod-platform (8082→80)
│ └─ rent-prod-website (8083→80)
└─ rent-test 网络
├─ rent-test-mysql (3307→3306)
├─ rent-test-redis (6380→6379)
├─ rent-test-server (3001→3000)
├─ rent-test-merchant (9081→80)
├─ rent-test-platform (9082→80)
└─ rent-test-website (9083→80)
域名规划
| 环境 | 用途 | 域名 |
|---|---|---|
| 生产 | API | api.pinzhuhui.com |
| 生产 | 官网 | www.pinzhuhui.com |
| 生产 | 商家后台 | merchant.pinzhuhui.com |
| 生产 | 平台后台 | platform-admin.pinzhuhui.com |
| 测试 | API | api-test.pinzhuhui.com |
| 测试 | 官网 | test.pinzhuhui.com |
| 测试 | 商家后台 | merchant-test.pinzhuhui.com |
| 测试 | 平台后台 | platform-admin-test.pinzhuhui.com |
目录结构
deploy/
├── docker/
│ ├── docker-compose.prod.yml # 生产环境
│ ├── docker-compose.test.yml # 测试环境
│ ├── docker-compose.gateway.yml # Gateway 网关
│ ├── Dockerfile.server # 后端镜像
│ ├── Dockerfile.merchant # 商家后台镜像
│ ├── Dockerfile.platform # 平台后台镜像
│ ├── Dockerfile.website # 官网镜像
│ ├── .env.example # 环境变量模板
│ └── .env # 实际配置(不入仓库)
├── nginx/
│ ├── gateway/gateway.conf # Gateway 域名路由
│ ├── api.conf # 后端 Nginx 配置
│ ├── merchant.conf # 商家后台 Nginx 配置
│ └── platform.conf # 平台后台 Nginx 配置
scripts/
│ └── setup-server.sh # 首次初始化脚本
一、首次部署(服务器初始化)
第 1 步:运行初始化脚本
# 将代码克隆到服务器
git clone <你的GitLab仓库地址> ~/rent-platform
cd ~/rent-platform
# 运行初始化(安装 Docker、创建网络、启动网关)
bash scripts/setup-server.sh
脚本会自动完成:
- 安装 Docker、Docker Compose、Git、pnpm
- 创建
.env文件并提示你填写密码 - 创建 Docker 网络(rent-prod、rent-test)
- 启动 Gateway Nginx
第 2 步:配置 DNS
在你的域名管理面板添加 A 记录,将以下域名指向服务器 IP:
# 生产环境
api.pinzhuhui.com → 服务器IP
www.pinzhuhui.com → 服务器IP
merchant.pinzhuhui.com → 服务器IP
platform-admin.pinzhuhui.com → 服务器IP
# 测试环境
api-test.pinzhuhui.com → 服务器IP
test.pinzhuhui.com → 服务器IP
merchant-test.pinzhuhui.com → 服务器IP
platform-admin-test.pinzhuhui.com → 服务器IP
第 3 步:部署服务
# 部署生产环境
make prod-deploy
# 部署测试环境
make test-deploy
二、Gitea + Act Runner 自动化
Gitea 内存占用约 100MB,适合 4GB 小内存服务器。
前提条件
- 服务器已运行初始化脚本(Gitea 和 Act Runner 已自动启动)
- Gitea 已完成初始配置
第 1 步:配置 Gitea
首次初始化已自动启动 Gitea,访问 http://服务器IP:3002 完成:
- 设置管理员账号密码
- 创建仓库
rent-platform
第 2 步:注册 Act Runner
- Gitea → Settings → Actions → Runners → Create new Runner
- 复制 Registration Token
- 编辑
.env填入 token:
vi deploy/docker/.env
# 将 GITEA_RUNNER_TOKEN=change_me 改为真实 token
- 重启 Runner:
docker compose -f deploy/docker/docker-compose.gitea.yml restart act-runner
- 验证 Runner 状态:
docker logs rent-act-runner
在 Gitea → Settings → Actions → Runners 页面应能看到 rent-deploy-runner 已注册并在线。
第 3 步:推送代码到 Gitea
# 添加 Gitea 远程仓库
git remote add gitea http://服务器IP:3002/用户名/rent-platform.git
# 推送分支
git push gitea master
git push gitea test
git push gitea prod
第 4 步:自动部署触发
# 推送 test 分支 → 自动部署测试环境
git push gitea test
# 推送 prod 分支 → 自动部署生产环境
git push gitea prod
流水线:install → build → deploy
在 Gitea → 仓库 → Actions 页面查看实时日志。
三、常用命令
make help # 查看所有命令
# 生产环境
make prod-deploy # 完整部署(停止→构建→启动)
make prod-logs # 查看日志
make prod-logs-server # 查看后端日志
make prod-ps # 查看容器状态
make prod-restart # 重启
# 测试环境
make test-deploy # 完整部署
make test-logs # 查看日志
make test-ps # 查看容器状态
make test-restart # 重启
# 网关
make gateway-up # 启动网关
make gateway-reload # 重载网关配置
# 其他
make clean # 清理所有环境(包括数据)
四、故障排查
查看服务状态
make prod-ps # 生产环境
make test-ps # 测试环境
查看日志
make prod-logs # 生产环境所有日志
make prod-logs-server # 生产环境后端日志
make test-logs # 测试环境所有日志
# 直接查看某个容器
docker logs rent-prod-server
docker logs rent-test-mysql
数据库连接
# 生产环境
docker exec -it rent-prod-mysql mysql -u root -p rent_platform
# 测试环境
docker exec -it rent-test-mysql mysql -u root -p rent_platform
重建单个服务
# 只重建生产环境后端
docker-compose -f deploy/docker/docker-compose.prod.yml up -d --build server
# 只重建测试环境商家后台
docker-compose -f deploy/docker/docker-compose.test.yml up -d --build merchant-admin
网关问题
# 检查网关状态
docker logs rent-gateway
# 重载网关配置(修改了 gateway.conf 后)
make gateway-reload