#!/bin/bash # 完成部署脚本 set -e SERVER="156.225.27.52" PORT="5204" USER="root" PASSWORD="Jsjj@8888" ssh_cmd() { sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -p $PORT $USER@$SERVER "$1" } echo "完成部署..." # 运行迁移和构建 echo "运行数据库迁移和构建..." ssh_cmd " cd /var/www/ai-chatbot export NVM_DIR=\"\$HOME/.nvm\" [ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" if [ -f /var/www/ai-chatbot/scripts/security/install-security-stack.sh ]; then echo '安装 security-stack...' bash /var/www/ai-chatbot/scripts/security/install-security-stack.sh || echo 'security-stack 安装失败,继续部署' elif [ -f /var/www/ai-chatbot/scripts/security/install-perf-guard.sh ]; then echo '安装 perf-guard...' bash /var/www/ai-chatbot/scripts/security/install-perf-guard.sh || echo 'perf-guard 安装失败,继续部署' fi echo '运行数据库迁移...' pnpm db:migrate || echo '迁移完成或已存在' echo '构建项目...' pnpm build " # 启动应用 echo "启动应用..." ssh_cmd " cd /var/www/ai-chatbot export NVM_DIR=\"\$HOME/.nvm\" [ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # 停止旧进程 pm2 delete ai-chatbot 2>/dev/null || true # 启动应用 pm2 start ecosystem.config.js pm2 save " # 配置 Nginx echo "配置 Nginx..." ssh_cmd " cat > /etc/nginx/sites-available/ai-chatbot << 'NGINX' server { listen 80; server_name $SERVER; location / { proxy_pass http://localhost:3000; 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; proxy_cache_bypass \$http_upgrade; proxy_read_timeout 300s; proxy_connect_timeout 75s; } } NGINX ln -sf /etc/nginx/sites-available/ai-chatbot /etc/nginx/sites-enabled/ rm -f /etc/nginx/sites-enabled/default nginx -t systemctl restart nginx " # 检查状态 echo "检查部署状态..." ssh_cmd " echo '=== PM2 状态 ===' pm2 status echo '' echo '=== 应用日志(最后10行)===' pm2 logs ai-chatbot --lines 10 --nostream || echo '暂无日志' echo '' echo '=== Nginx 状态 ===' systemctl status nginx --no-pager -l | head -10 " echo "部署完成!" echo "访问地址: http://$SERVER"