| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- #!/bin/bash
- # Tolan 生产环境部署脚本
- # 用于生产环境的部署和管理
- set -e
- # 颜色定义
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[1;33m'
- BLUE='\033[0;34m'
- NC='\033[0m'
- log_info() {
- echo -e "${BLUE}[PROD]${NC} $1"
- }
- log_success() {
- echo -e "${GREEN}[PROD]${NC} $1"
- }
- log_warning() {
- echo -e "${YELLOW}[PROD]${NC} $1"
- }
- log_error() {
- echo -e "${RED}[PROD]${NC} $1"
- }
- # 配置变量
- DEPLOY_DIR="/opt/tolan"
- SERVICE_USER="tolan"
- NGINX_CONFIG="/etc/nginx/sites-available/tolan"
- SYSTEMD_SERVICE="/etc/systemd/system/tolan-backend.service"
- # 检查权限
- check_permissions() {
- if [[ $EUID -ne 0 ]]; then
- log_error "此脚本需要 root 权限运行"
- log_info "请使用: sudo $0 $@"
- exit 1
- fi
- }
- # 安装系统依赖
- install_dependencies() {
- log_info "安装系统依赖..."
-
- # 更新包管理器
- apt update
-
- # 安装基础依赖
- apt install -y \
- python3 \
- python3-pip \
- python3-venv \
- nodejs \
- npm \
- nginx \
- ffmpeg \
- libsndfile1 \
- supervisor \
- certbot \
- python3-certbot-nginx
-
- log_success "系统依赖安装完成"
- }
- # 创建部署用户
- create_user() {
- if ! id "$SERVICE_USER" &>/dev/null; then
- log_info "创建部署用户: $SERVICE_USER"
- useradd --system --create-home --shell /bin/bash $SERVICE_USER
- usermod -aG www-data $SERVICE_USER
- log_success "用户 $SERVICE_USER 创建完成"
- else
- log_info "用户 $SERVICE_USER 已存在"
- fi
- }
- # 部署应用代码
- deploy_application() {
- log_info "部署应用代码到 $DEPLOY_DIR"
-
- # 创建部署目录
- mkdir -p $DEPLOY_DIR
-
- # 复制代码
- log_info "复制应用代码..."
- cp -r frontend $DEPLOY_DIR/
- cp -r backend $DEPLOY_DIR/
- cp -r scripts $DEPLOY_DIR/
- cp start.sh $DEPLOY_DIR/
-
- # 设置权限
- chown -R $SERVICE_USER:$SERVICE_USER $DEPLOY_DIR
- chmod +x $DEPLOY_DIR/start.sh
- chmod +x $DEPLOY_DIR/scripts/*.sh
-
- log_success "应用代码部署完成"
- }
- # 构建前端
- build_frontend() {
- log_info "构建前端应用..."
-
- cd $DEPLOY_DIR/frontend
-
- # 切换到部署用户
- sudo -u $SERVICE_USER bash << 'EOF'
- # 安装依赖
- npm ci --production
- # 设置生产环境变量
- export REACT_APP_WS_URL=wss://your-domain.com/ws
- export REACT_APP_ENV=production
- # 构建应用
- npm run build
- EOF
-
- log_success "前端构建完成"
- }
- # 设置后端环境
- setup_backend() {
- log_info "设置后端环境..."
-
- cd $DEPLOY_DIR/backend
-
- # 切换到部署用户
- sudo -u $SERVICE_USER bash << 'EOF'
- # 创建虚拟环境
- python3 -m venv venv
- source venv/bin/activate
- # 安装依赖
- pip install --upgrade pip
- pip install -r requirements.txt
- # 复制环境配置
- if [ ! -f .env ]; then
- cp .env.example .env
- fi
- EOF
-
- log_warning "请编辑 $DEPLOY_DIR/backend/.env 配置生产环境参数"
-
- log_success "后端环境设置完成"
- }
- # 配置 Nginx
- setup_nginx() {
- log_info "配置 Nginx..."
-
- cat > $NGINX_CONFIG << 'EOF'
- server {
- listen 80;
- server_name your-domain.com; # 替换为你的域名
-
- # 前端静态文件
- location / {
- root /opt/tolan/frontend/build;
- index index.html index.htm;
- try_files $uri $uri/ /index.html;
-
- # 缓存静态资源
- location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
- expires 1y;
- add_header Cache-Control "public, immutable";
- }
- }
-
- # WebSocket 代理
- location /ws {
- proxy_pass http://127.0.0.1:8765;
- proxy_http_version 1.1;
- proxy_set_header Upgrade $http_upgrade;
- proxy_set_header Connection "upgrade";
- 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;
-
- # WebSocket 超时设置
- proxy_read_timeout 86400;
- proxy_send_timeout 86400;
- }
-
- # 健康检查
- location /health {
- proxy_pass http://127.0.0.1:8765/health;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- }
-
- # 安全头
- add_header X-Frame-Options "SAMEORIGIN" always;
- add_header X-XSS-Protection "1; mode=block" always;
- add_header X-Content-Type-Options "nosniff" always;
- add_header Referrer-Policy "no-referrer-when-downgrade" always;
- add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
- }
- EOF
-
- # 启用站点
- ln -sf $NGINX_CONFIG /etc/nginx/sites-enabled/
- rm -f /etc/nginx/sites-enabled/default
-
- # 测试配置
- nginx -t
-
- log_success "Nginx 配置完成"
- }
- # 配置 Systemd 服务
- setup_systemd() {
- log_info "配置 Systemd 服务..."
-
- cat > $SYSTEMD_SERVICE << EOF
- [Unit]
- Description=Tolan Backend Server
- After=network.target
- [Service]
- Type=exec
- User=$SERVICE_USER
- Group=$SERVICE_USER
- WorkingDirectory=$DEPLOY_DIR/backend
- Environment=PATH=$DEPLOY_DIR/backend/venv/bin
- EnvironmentFile=$DEPLOY_DIR/backend/.env
- ExecStart=$DEPLOY_DIR/backend/venv/bin/python run.py
- Restart=always
- RestartSec=5
- StandardOutput=journal
- StandardError=journal
- # 安全设置
- NoNewPrivileges=yes
- PrivateTmp=yes
- ProtectSystem=strict
- ProtectHome=yes
- ReadWritePaths=$DEPLOY_DIR
- [Install]
- WantedBy=multi-user.target
- EOF
-
- # 重载 systemd
- systemctl daemon-reload
-
- log_success "Systemd 服务配置完成"
- }
- # 启动服务
- start_services() {
- log_info "启动服务..."
-
- # 启动后端服务
- systemctl enable tolan-backend
- systemctl start tolan-backend
-
- # 启动 Nginx
- systemctl enable nginx
- systemctl restart nginx
-
- log_success "服务启动完成"
- }
- # 设置 SSL 证书
- setup_ssl() {
- local domain=$1
-
- if [ -z "$domain" ]; then
- log_warning "跳过 SSL 设置,未提供域名"
- return
- fi
-
- log_info "为域名 $domain 设置 SSL 证书..."
-
- # 使用 Certbot 获取证书
- certbot --nginx -d $domain --non-interactive --agree-tos --email admin@$domain
-
- log_success "SSL 证书设置完成"
- }
- # 检查服务状态
- check_status() {
- log_info "检查服务状态..."
-
- # 检查后端服务
- if systemctl is-active --quiet tolan-backend; then
- log_success "后端服务运行正常"
- else
- log_error "后端服务异常"
- systemctl status tolan-backend --no-pager
- fi
-
- # 检查 Nginx
- if systemctl is-active --quiet nginx; then
- log_success "Nginx 运行正常"
- else
- log_error "Nginx 异常"
- systemctl status nginx --no-pager
- fi
-
- # 检查端口
- log_info "端口监听状态:"
- netstat -tlnp | grep -E "(80|443|8765)"
- }
- # 显示部署信息
- show_deploy_info() {
- log_success "🎉 Tolan 生产环境部署完成!"
- echo ""
- log_info "📁 部署目录: $DEPLOY_DIR"
- log_info "👤 运行用户: $SERVICE_USER"
- log_info "🌐 Web 服务: http://your-domain.com"
- log_info "🔗 WebSocket: ws://your-domain.com/ws"
- echo ""
- log_info "📋 管理命令:"
- log_info " systemctl status tolan-backend # 查看后端状态"
- log_info " systemctl restart tolan-backend # 重启后端"
- log_info " systemctl status nginx # 查看 Nginx 状态"
- log_info " journalctl -u tolan-backend -f # 查看后端日志"
- echo ""
- log_warning "⚠️ 重要提醒:"
- log_warning "1. 请编辑 $DEPLOY_DIR/backend/.env 配置 API 密钥"
- log_warning "2. 请在 Nginx 配置中替换域名"
- log_warning "3. 建议设置防火墙规则"
- log_warning "4. 定期备份数据和配置"
- }
- # 显示帮助
- show_help() {
- echo "Tolan 生产环境部署脚本"
- echo ""
- echo "用法: $0 [命令] [选项]"
- echo ""
- echo "命令:"
- echo " install 完整安装部署"
- echo " deploy 部署应用代码"
- echo " start 启动服务"
- echo " stop 停止服务"
- echo " restart 重启服务"
- echo " status 查看状态"
- echo " ssl <domain> 设置 SSL 证书"
- echo " help 显示帮助"
- echo ""
- echo "示例:"
- echo " $0 install # 完整部署"
- echo " $0 ssl example.com # 设置 SSL"
- echo " $0 status # 查看状态"
- }
- # 主函数
- main() {
- case "${1:-install}" in
- "install")
- check_permissions
- log_info "开始 Tolan 生产环境部署..."
- install_dependencies
- create_user
- deploy_application
- build_frontend
- setup_backend
- setup_nginx
- setup_systemd
- start_services
- show_deploy_info
- ;;
- "deploy")
- check_permissions
- deploy_application
- build_frontend
- setup_backend
- systemctl restart tolan-backend
- systemctl reload nginx
- log_success "应用重新部署完成"
- ;;
- "start")
- check_permissions
- start_services
- ;;
- "stop")
- check_permissions
- systemctl stop tolan-backend
- systemctl stop nginx
- log_success "服务已停止"
- ;;
- "restart")
- check_permissions
- systemctl restart tolan-backend
- systemctl restart nginx
- log_success "服务已重启"
- ;;
- "status")
- check_status
- ;;
- "ssl")
- check_permissions
- setup_ssl "$2"
- ;;
- "help"|"-h"|"--help")
- show_help
- ;;
- *)
- log_error "未知命令: $1"
- show_help
- exit 1
- ;;
- esac
- }
- main "$@"
|