云端部署指南
方案一:云服务器 + Docker Compose(推荐)
适用于:阿里云、腾讯云、AWS EC2、DigitalOcean 等
1. 准备工作
1 2 3 4 5 6 7
| # 1. 在云服务器上安装 Docker 和 Docker Compose curl -fsSL https://get.docker.com -o get-docker.sh sh get-docker.sh
# 安装 Docker Compose sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
|
2. 配置环境变量
创建 .env 文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| # 数据库配置(生产环境建议使用云数据库) DB_HOST=postgres DB_PORT=5432 DB_USERNAME=postgres DB_PASSWORD=你的强密码 DB_DATABASE=todolist
# JWT 密钥(必须修改为强随机字符串) JWT_SECRET=生成一个至少32位的随机字符串 JWT_EXPIRES_IN=7d
# 智谱AI API Key ZHIPU_API_KEY=你的智谱API密钥
# 服务器端口 PORT=3000
|
3. 修改 docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| version: '3.8'
services: postgres: image: postgres:15-alpine container_name: todolist-postgres restart: unless-stopped environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: ${DB_PASSWORD} # 从环境变量读取 POSTGRES_DB: todolist volumes: - postgres_data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 10s timeout: 5s retries: 5 # 生产环境建议不暴露端口,或使用内网IP # ports: # - "5432:5432"
backend: build: context: ./backend dockerfile: Dockerfile container_name: todolist-backend restart: unless-stopped environment: - DB_HOST=postgres - DB_PORT=5432 - DB_USERNAME=postgres - DB_PASSWORD=${DB_PASSWORD} - DB_DATABASE=todolist - JWT_SECRET=${JWT_SECRET} - JWT_EXPIRES_IN=7d - ZHIPU_API_KEY=${ZHIPU_API_KEY} - PORT=3000 depends_on: postgres: condition: service_healthy # 生产环境建议不直接暴露端口,通过 Nginx 反向代理 # ports: # - "3000:3000"
frontend: build: context: ./frontend dockerfile: Dockerfile container_name: todolist-frontend restart: unless-stopped ports: - "80:80" - "443:443" # HTTPS depends_on: - backend
volumes: postgres_data:
|
4. 部署步骤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| # 1. 上传项目到服务器 git clone <your-repo-url> cd full_web_demo_cursor
# 2. 创建 .env 文件并配置 cp backend/env.example .env nano .env # 编辑环境变量
# 3. 构建并启动服务 docker-compose up -d --build
# 4. 初始化数据库(如果需要) docker-compose exec backend npm run migration:run # 如果有迁移脚本 # 或者手动导入 docker-compose exec -T postgres psql -U postgres -d todolist < database/schema.sql docker-compose exec -T postgres psql -U postgres -d todolist < database/seed.sql
# 5. 查看日志 docker-compose logs -f
|
5. 配置 HTTPS(使用 Nginx + Let’s Encrypt)
1 2 3 4 5 6 7 8 9
| # 安装 Certbot sudo apt-get update sudo apt-get install certbot python3-certbot-nginx
# 申请证书 sudo certbot --nginx -d yourdomain.com
# 自动续期 sudo certbot renew --dry-run
|
方案二:Serverless 部署(Vercel + Railway/Render)
前端部署到 Vercel
1 2 3 4 5 6 7 8 9
| # 1. 安装 Vercel CLI npm i -g vercel
# 2. 在 frontend 目录下部署 cd frontend vercel
# 3. 配置环境变量(在 Vercel 控制台) # API_BASE_URL=https://your-backend-url.com
|
修改 frontend/src/api/index.ts:
1 2
| const api = axios.create({ baseURL: import.meta.env
|
后端部署到 Railway/Render
- Railway:
连接 GitHub 仓库
选择 backend 目录
配置环境变量
自动部署
- Render:
安全建议
- 环境变量:
- 数据库:
使用云数据库并配置白名单
启用 SSL 连接
定期备份
- 网络安全:
配置防火墙规则
使用 HTTPS
限制数据库端口访问
- 监控和日志:
配置日志收集(如 ELK、CloudWatch)
设置监控告警
定期检查日志
快速部署脚本
创建 deploy.sh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #!/bin/bash
# 配置变量 DOMAIN="yourdomain.com" EMAIL="your@email.com"
# 拉取最新代码 git pull origin main
# 构建并启动 docker-compose down docker-compose up -d --build
# 等待服务启动 sleep 10
# 检查服务状态 docker-compose ps
# 配置 HTTPS(首次运行) # certbot --nginx -d $DOMAIN --email $EMAIL --agree-tos --non-interactive
echo "部署完成!访问 https://$DOMAIN"
|
配置数据库
1 2 3 4 5 6 7 8
| # 设置编码 $env:PGCLIENTENCODING="UTF8"
# 使用公网地址连接并导入 schema psql -h monorail.proxy.rlwy.net -p 12345 -U postgres -d railway --set=client_encoding=UTF8 -f database\schema.sql
# 导入测试数据(如果有) psql -h monorail.proxy.rlwy.net -p 12345 -U postgres -d railway --set=client_encoding=UTF8 -f database\seed.sql
|