|
| 1 | +# 生产环境部署指南 |
| 2 | + |
| 3 | +## 🚀 快速部署 |
| 4 | + |
| 5 | +### 前置条件 |
| 6 | +- 服务器:199.68.217.152 |
| 7 | +- 操作系统:Linux (Ubuntu/CentOS) |
| 8 | +- 内存:至少2GB |
| 9 | +- 磁盘:至少10GB可用空间 |
| 10 | + |
| 11 | +### 1. 服务器环境准备 |
| 12 | + |
| 13 | +```bash |
| 14 | +# 连接服务器 |
| 15 | + |
| 16 | + |
| 17 | +# 更新系统 |
| 18 | +apt update && apt upgrade -y # Ubuntu |
| 19 | +# 或 |
| 20 | +yum update -y # CentOS |
| 21 | + |
| 22 | +# 安装Docker |
| 23 | +curl -fsSL https://get.docker.com -o get-docker.sh |
| 24 | +sh get-docker.sh |
| 25 | + |
| 26 | +# 安装Docker Compose |
| 27 | +curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose |
| 28 | +chmod +x /usr/local/bin/docker-compose |
| 29 | + |
| 30 | +# 启动Docker服务 |
| 31 | +systemctl start docker |
| 32 | +systemctl enable docker |
| 33 | + |
| 34 | +# 验证安装 |
| 35 | +docker --version |
| 36 | +docker-compose --version |
| 37 | +``` |
| 38 | + |
| 39 | +### 2. 代码部署 |
| 40 | + |
| 41 | +```bash |
| 42 | +# 创建项目目录 |
| 43 | +mkdir -p /opt/quit-journey-backend |
| 44 | +cd /opt/quit-journey-backend |
| 45 | + |
| 46 | +# 方式1: 使用Git (推荐) |
| 47 | +git clone <your-repository-url> . |
| 48 | + |
| 49 | +# 方式2: 使用SCP上传 |
| 50 | +# 在本地执行: |
| 51 | +# scp -r . [email protected]:/opt/quit-journey-backend/ |
| 52 | +``` |
| 53 | + |
| 54 | +### 3. 环境配置 |
| 55 | + |
| 56 | +```bash |
| 57 | +# 复制环境变量模板 |
| 58 | +cp .env.example .env |
| 59 | + |
| 60 | +# 编辑环境变量 |
| 61 | +nano .env |
| 62 | +``` |
| 63 | + |
| 64 | +#### 🔐 重要:必须修改的安全配置 |
| 65 | + |
| 66 | +```bash |
| 67 | +# 生成强JWT密钥 |
| 68 | +openssl rand -base64 64 |
| 69 | + |
| 70 | +# 生成安全的数据库密码 |
| 71 | +openssl rand -base64 32 |
| 72 | + |
| 73 | +# 生成Redis密码 |
| 74 | +openssl rand -base64 16 |
| 75 | +``` |
| 76 | + |
| 77 | +#### 📝 .env 配置示例 |
| 78 | + |
| 79 | +```env |
| 80 | +# 数据库配置 |
| 81 | +DB_USERNAME=quitjourney_user |
| 82 | +DB_PASSWORD=your_generated_secure_password_here |
| 83 | +POSTGRES_DB=quitjourney |
| 84 | +POSTGRES_USER=quitjourney_user |
| 85 | +POSTGRES_PASSWORD=your_generated_secure_password_here |
| 86 | +
|
| 87 | +# Redis配置 |
| 88 | +REDIS_PASSWORD=your_redis_password_here |
| 89 | +
|
| 90 | +# JWT配置 (使用上面生成的密钥) |
| 91 | +JWT_SECRET=your_generated_jwt_secret_here |
| 92 | +JWT_EXPIRATION=900000 |
| 93 | +JWT_REFRESH_EXPIRATION=2592000000 |
| 94 | +
|
| 95 | +# API文档 (生产环境建议关闭) |
| 96 | +API_DOCS_ENABLED=false |
| 97 | +SWAGGER_UI_ENABLED=false |
| 98 | +``` |
| 99 | + |
| 100 | +### 4. 部署服务 |
| 101 | + |
| 102 | +```bash |
| 103 | +# 使用部署脚本 (推荐) |
| 104 | +./scripts/deploy.sh prod |
| 105 | + |
| 106 | +# 或手动部署 |
| 107 | +docker-compose -f docker-compose.prod.yml up -d --build |
| 108 | +``` |
| 109 | + |
| 110 | +### 5. 验证部署 |
| 111 | + |
| 112 | +```bash |
| 113 | +# 检查服务状态 |
| 114 | +docker-compose -f docker-compose.prod.yml ps |
| 115 | + |
| 116 | +# 检查健康状态 |
| 117 | +curl http://localhost:8080/api/v1/actuator/health |
| 118 | + |
| 119 | +# 查看日志 |
| 120 | +docker-compose -f docker-compose.prod.yml logs -f api |
| 121 | +``` |
| 122 | + |
| 123 | +## 🔧 高级配置 |
| 124 | + |
| 125 | +### SSL证书配置 |
| 126 | + |
| 127 | +#### 使用Let's Encrypt (推荐) |
| 128 | + |
| 129 | +```bash |
| 130 | +# 安装Certbot |
| 131 | +apt install certbot # Ubuntu |
| 132 | +# 或 |
| 133 | +yum install certbot # CentOS |
| 134 | + |
| 135 | +# 获取证书 (替换your-domain.com) |
| 136 | +certbot certonly --standalone -d your-domain.com |
| 137 | + |
| 138 | +# 复制证书到项目目录 |
| 139 | +mkdir -p nginx/ssl |
| 140 | +cp /etc/letsencrypt/live/your-domain.com/fullchain.pem nginx/ssl/cert.pem |
| 141 | +cp /etc/letsencrypt/live/your-domain.com/privkey.pem nginx/ssl/key.pem |
| 142 | + |
| 143 | +# 设置自动续期 |
| 144 | +echo "0 12 * * * /usr/bin/certbot renew --quiet" | crontab - |
| 145 | +``` |
| 146 | + |
| 147 | +#### 使用自签名证书 (测试用) |
| 148 | + |
| 149 | +```bash |
| 150 | +mkdir -p nginx/ssl |
| 151 | +openssl req -x509 -nodes -days 365 -newkey rsa:2048 \ |
| 152 | + -keyout nginx/ssl/key.pem \ |
| 153 | + -out nginx/ssl/cert.pem \ |
| 154 | + -subj "/C=CN/ST=State/L=City/O=Organization/CN=localhost" |
| 155 | +``` |
| 156 | + |
| 157 | +### 域名配置 |
| 158 | + |
| 159 | +1. **DNS解析**:将域名A记录指向 199.68.217.152 |
| 160 | +2. **Nginx配置**:修改 `nginx/nginx.conf` 中的 `server_name` |
| 161 | +3. **重启服务**:`docker-compose -f docker-compose.prod.yml restart nginx` |
| 162 | + |
| 163 | +### 防火墙配置 |
| 164 | + |
| 165 | +```bash |
| 166 | +# Ubuntu (ufw) |
| 167 | +ufw allow 22 # SSH |
| 168 | +ufw allow 80 # HTTP |
| 169 | +ufw allow 443 # HTTPS |
| 170 | +ufw enable |
| 171 | + |
| 172 | +# CentOS (firewalld) |
| 173 | +firewall-cmd --permanent --add-service=ssh |
| 174 | +firewall-cmd --permanent --add-service=http |
| 175 | +firewall-cmd --permanent --add-service=https |
| 176 | +firewall-cmd --reload |
| 177 | +``` |
| 178 | + |
| 179 | +## 📊 监控和维护 |
| 180 | + |
| 181 | +### 日志管理 |
| 182 | + |
| 183 | +```bash |
| 184 | +# 查看应用日志 |
| 185 | +docker-compose -f docker-compose.prod.yml logs -f api |
| 186 | + |
| 187 | +# 查看数据库日志 |
| 188 | +docker-compose -f docker-compose.prod.yml logs -f db |
| 189 | + |
| 190 | +# 查看Nginx日志 |
| 191 | +docker-compose -f docker-compose.prod.yml logs -f nginx |
| 192 | +``` |
| 193 | + |
| 194 | +### 数据备份 |
| 195 | + |
| 196 | +```bash |
| 197 | +# 手动备份数据库 |
| 198 | +docker-compose -f docker-compose.prod.yml exec db pg_dump -U quitjourney_user quitjourney > backup_$(date +%Y%m%d_%H%M%S).sql |
| 199 | + |
| 200 | +# 恢复数据库 |
| 201 | +docker-compose -f docker-compose.prod.yml exec -T db psql -U quitjourney_user quitjourney < backup_file.sql |
| 202 | +``` |
| 203 | + |
| 204 | +### 性能监控 |
| 205 | + |
| 206 | +```bash |
| 207 | +# 查看资源使用情况 |
| 208 | +docker stats |
| 209 | + |
| 210 | +# 查看服务状态 |
| 211 | +docker-compose -f docker-compose.prod.yml ps |
| 212 | + |
| 213 | +# 健康检查 |
| 214 | +curl http://localhost:8080/api/v1/actuator/health |
| 215 | +``` |
| 216 | + |
| 217 | +## 🔄 更新部署 |
| 218 | + |
| 219 | +### 代码更新 |
| 220 | + |
| 221 | +```bash |
| 222 | +# 拉取最新代码 |
| 223 | +git pull origin main |
| 224 | + |
| 225 | +# 重新部署 |
| 226 | +./scripts/deploy.sh prod |
| 227 | +``` |
| 228 | + |
| 229 | +### 数据库迁移 |
| 230 | + |
| 231 | +```bash |
| 232 | +# 查看迁移状态 |
| 233 | +docker-compose -f docker-compose.prod.yml exec api ./gradlew flywayInfo |
| 234 | + |
| 235 | +# 执行迁移 |
| 236 | +docker-compose -f docker-compose.prod.yml exec api ./gradlew flywayMigrate |
| 237 | +``` |
| 238 | + |
| 239 | +## 🚨 故障排除 |
| 240 | + |
| 241 | +### 常见问题 |
| 242 | + |
| 243 | +#### 1. 服务启动失败 |
| 244 | +```bash |
| 245 | +# 查看详细日志 |
| 246 | +docker-compose -f docker-compose.prod.yml logs api |
| 247 | + |
| 248 | +# 检查配置文件 |
| 249 | +docker-compose -f docker-compose.prod.yml config |
| 250 | +``` |
| 251 | + |
| 252 | +#### 2. 数据库连接失败 |
| 253 | +```bash |
| 254 | +# 检查数据库状态 |
| 255 | +docker-compose -f docker-compose.prod.yml exec db pg_isready |
| 256 | + |
| 257 | +# 检查网络连接 |
| 258 | +docker-compose -f docker-compose.prod.yml exec api ping db |
| 259 | +``` |
| 260 | + |
| 261 | +#### 3. 内存不足 |
| 262 | +```bash |
| 263 | +# 查看内存使用 |
| 264 | +free -h |
| 265 | +docker stats |
| 266 | + |
| 267 | +# 调整Docker资源限制 |
| 268 | +# 编辑 docker-compose.prod.yml 中的 deploy.resources 部分 |
| 269 | +``` |
| 270 | + |
| 271 | +### 紧急恢复 |
| 272 | + |
| 273 | +```bash |
| 274 | +# 停止所有服务 |
| 275 | +docker-compose -f docker-compose.prod.yml down |
| 276 | + |
| 277 | +# 清理容器和网络 |
| 278 | +docker system prune -f |
| 279 | + |
| 280 | +# 重新启动 |
| 281 | +docker-compose -f docker-compose.prod.yml up -d --build |
| 282 | +``` |
| 283 | + |
| 284 | +## 📞 支持 |
| 285 | + |
| 286 | +如果遇到问题,请检查: |
| 287 | +1. 服务器资源是否充足 |
| 288 | +2. 网络连接是否正常 |
| 289 | +3. 配置文件是否正确 |
| 290 | +4. 日志中的错误信息 |
| 291 | + |
| 292 | +更多帮助请联系技术支持。 |
0 commit comments