baodan/CLOUD_DEPLOY.md

202 lines
4.6 KiB
Markdown
Raw 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.

# 云服务器部署指南(精简版)
## 问题:代码太大怎么办?
**解决方案**Docker 镜像部署
- 本地构建好 Docker 镜像(包含所有代码)
- 推送到 Docker Hub免费
- 服务器只需要拉取镜像运行,**无需上传源码**
## 部署流程
### 第一步:本地构建并推送镜像
```bash
# 1. 登录 Docker Hub没有账号先注册https://hub.docker.com
docker login
# 2. 设置你的 Docker Hub 用户名
export DOCKER_USERNAME=yourusername
# 3. 构建并推送镜像
./build-and-push.sh
```
**镜像大小参考**
- API 镜像:约 500MB
- Web 镜像:约 200MB
- 前端镜像:约 50MB
### 第二步:服务器部署
```bash
# 1. 登录服务器
ssh user@your-server-ip
# 2. 创建目录
mkdir -p /opt/baodanagent && cd /opt/baodanagent
# 3. 下载部署脚本(或从本地上传)
# 方式一:从 Git 下载
curl -sSL https://raw.githubusercontent.com/yourusername/baodanagent/main/server-deploy.sh -o server-deploy.sh
# 方式二:从本地上传(只需要这一个文件)
scp server-deploy.sh user@your-server-ip:/opt/baodanagent/
# 4. 设置执行权限
chmod +x server-deploy.sh
# 5. 运行部署脚本
DOCKER_USERNAME=yourusername ./server-deploy.sh
```
### 第三步:配置环境变量(可选)
```bash
# 编辑 .env 文件
nano .env
# 修改以下配置:
DOCKER_USERNAME=yourusername # 必须修改
POSTGRES_PASSWORD=your_password # 建议修改
SECRET_KEY=your_secret_key # 建议修改
ADMIN_PASSWORD=your_password # 建议修改
```
### 第四步:访问系统
| 服务 | 地址 |
|------|------|
| 管理后台 | http://your-server-ip:3000 |
| API 服务 | http://your-server-ip:5001 |
| 默认账号 | taiyi@baodan.com / taiyi1224 |
## 文件清单
### 本地需要的文件(构建镜像时)
```
baodanagent/
├── Dockerfile.dify-custom # API 镜像构建文件
├── Dockerfile.web-custom # Web 镜像构建文件
├── Dockerfile.frontend # 前端镜像构建文件
├── build-and-push.sh # 构建并推送脚本
├── api/insurance/ # 自研代码
├── scripts/ # 启动脚本
├── deploy/ # 部署配置
│ ├── sql/init.sql
│ └── logo/
└── frontend/ # 前端代码
```
### 服务器需要的文件(部署时)
```
/opt/baodanagent/
├── server-deploy.sh # 部署脚本(唯一必需文件)
├── docker-compose.yml # 自动生成
├── .env # 自动生成
└── deploy/sql/init.sql # 自动生成
```
## 常用命令
```bash
# 查看服务状态
docker compose ps
# 查看日志
docker compose logs -f baodan-api
# 重启服务
docker compose restart
# 停止服务
docker compose down
# 更新镜像并重启
docker compose pull && docker compose up -d
# 备份数据库
docker compose exec db pg_dump -U postgres baodan > backup.sql
# 恢复数据库
docker compose exec -T db psql -U postgres baodan < backup.sql
```
## 更新部署
```bash
# 本地重新构建并推送镜像
./build-and-push.sh
# 服务器拉取新镜像并重启
docker compose pull
docker compose up -d
```
## 域名和 HTTPS 配置
如果需要配置域名和 HTTPS
```bash
# 安装 Nginx 和 Certbot
apt install nginx certbot python3-certbot-nginx -y
# 配置 Nginx 反向代理
cat > /etc/nginx/sites-available/baodanagent << 'EOF'
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /api/ {
proxy_pass http://127.0.0.1:5001/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
EOF
# 启用配置
ln -s /etc/nginx/sites-available/baodanagent /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx
# 申请 SSL 证书
certbot --nginx -d your-domain.com
```
## 故障排查
```bash
# 查看容器日志
docker compose logs baodan-api
# 进入容器调试
docker compose exec baodan-api bash
# 检查数据库连接
docker compose exec db psql -U postgres -d baodan
# 检查 Redis 连接
docker compose exec redis redis-cli ping
# 重启所有服务
docker compose down && docker compose up -d
```
## 总结
| 对比项 | 传统部署 | Docker 镜像部署 |
|--------|---------|----------------|
| 上传内容 | 整个项目代码数GB | 只需一个脚本几KB |
| 部署复杂度 | 需要安装各种依赖 | 一键部署 |
| 环境一致性 | 可能有差异 | 完全一致 |
| 更新方式 | 重新上传代码 | 拉取新镜像 |