# 服务器迁移手动步骤指南 由于新服务器 (114.66.31.51) 目前无法通过SSH连接,请按照以下步骤进行迁移: ## 前置条件检查 ### 1. 检查新服务器SSH配置 在新服务器的控制台或通过其他方式登录后,运行: ```bash # 上传并运行检查脚本 chmod +x check-server-ssh.sh ./check-server-ssh.sh ``` ### 2. 配置服务器允许SSH连接 #### 如果使用云服务器(阿里云/腾讯云等): 1. 登录云控制台 2. 找到安全组设置 3. 添加入站规则: - 协议:TCP - 端口:22 - 源:0.0.0.0/0(或你的IP地址) - 动作:允许 #### 如果服务器有防火墙: ```bash # Ubuntu/Debian (UFW) sudo ufw allow 22/tcp sudo ufw reload # CentOS/RHEL (firewalld) sudo firewall-cmd --permanent --add-service=ssh sudo firewall-cmd --reload # 或使用iptables sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT sudo iptables-save ``` #### 确保SSH服务运行: ```bash # 检查SSH服务状态 sudo systemctl status sshd # 或 systemctl status ssh # 如果未运行,启动服务 sudo systemctl start sshd sudo systemctl enable sshd ``` ## 迁移步骤 ### 步骤1: 准备备份文件 备份文件已保存在本地: - 数据库备份:`./migration-backup-20251227_102258/database.dump` (624MB) - 项目文件:`./migration-backup-20251227_102258/project.tar.gz` (11MB) - 环境变量:`./migration-backup-20251227_102258/.env.local` ### 步骤2: 在新服务器上初始化环境 一旦SSH连接成功,运行迁移脚本: ```bash # 在本地执行 cd /Users/lanmu/Desktop/ai-chatbot-main ./migrate-server.sh ``` 或者手动执行以下步骤: #### 2.1 在新服务器上安装必要软件 ```bash # 更新系统 apt-get update # 安装基础工具 apt-get install -y curl wget git build-essential postgresql-client postgresql nginx # 安装Node.js 20.x curl -fsSL https://deb.nodesource.com/setup_20.x | bash - apt-get install -y nodejs # 安装PM2和pnpm npm install -g pm2 pnpm ``` #### 2.2 创建数据库 ```bash # 启动PostgreSQL systemctl start postgresql systemctl enable postgresql # 创建数据库和用户 sudo -u postgres psql << 'EOF' CREATE DATABASE ai_chatbot; CREATE USER chatbot_user WITH PASSWORD 'your_secure_password_here'; GRANT ALL PRIVILEGES ON DATABASE ai_chatbot TO chatbot_user; ALTER DATABASE ai_chatbot OWNER TO chatbot_user; \q EOF ``` #### 2.3 上传备份文件 ```bash # 在本地执行,上传备份文件到新服务器 cd /Users/lanmu/Desktop/ai-chatbot-main scp -P 22 ./migration-backup-20251227_102258/database.dump root@114.66.31.51:/tmp/ scp -P 22 ./migration-backup-20251227_102258/project.tar.gz root@114.66.31.51:/tmp/ scp -P 22 ./migration-backup-20251227_102258/.env.local root@114.66.31.51:/tmp/ ``` #### 2.4 在新服务器上解压和配置 ```bash # 在新服务器上执行 mkdir -p /var/www/ai-chatbot cd /var/www/ai-chatbot tar -xzf /tmp/project.tar.gz # 配置环境变量 cp /tmp/.env.local .env.local # 编辑 .env.local,更新数据库连接信息 nano .env.local # 修改 POSTGRES_URL 为新服务器的数据库连接 # POSTGRES_URL=postgresql://chatbot_user:your_secure_password_here@localhost:5432/ai_chatbot # AUTH_URL=http://114.66.31.51 ``` #### 2.5 导入数据库 ```bash # 在新服务器上执行 export PGPASSWORD="your_secure_password_here" pg_restore -h localhost -p 5432 -U chatbot_user -d ai_chatbot -c --no-owner --no-privileges /tmp/database.dump ``` #### 2.6 安装依赖和构建 ```bash # 在新服务器上执行 cd /var/www/ai-chatbot pnpm install --frozen-lockfile pnpm db:migrate pnpm build ``` #### 2.7 配置PM2 ```bash # 在新服务器上执行 cd /var/www/ai-chatbot cat > ecosystem.config.js << 'EOF' module.exports = { apps: [{ name: 'ai-chatbot', script: 'node_modules/next/dist/bin/next', args: 'start', cwd: '/var/www/ai-chatbot', instances: 1, exec_mode: 'fork', env: { NODE_ENV: 'production', PORT: 3000 }, error_file: '/var/log/pm2/ai-chatbot-error.log', out_file: '/var/log/pm2/ai-chatbot-out.log', log_date_format: 'YYYY-MM-DD HH:mm:ss Z', merge_logs: true, autorestart: true, max_memory_restart: '1G' }] }; EOF pm2 start ecosystem.config.js pm2 save pm2 startup ``` #### 2.8 配置Nginx ```bash # 在新服务器上执行 cat > /etc/nginx/sites-available/ai-chatbot << 'EOF' server { listen 80; server_name 114.66.31.51; 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; } } EOF ln -sf /etc/nginx/sites-available/ai-chatbot /etc/nginx/sites-enabled/ rm -f /etc/nginx/sites-enabled/default nginx -t && systemctl reload nginx ``` ## 验证迁移 1. 检查PM2状态:`pm2 status` 2. 检查日志:`pm2 logs ai-chatbot` 3. 访问网站:http://114.66.31.51 4. 测试登录和基本功能 ## 注意事项 1. **数据库密码**:请使用强密码替换 `your_secure_password_here` 2. **环境变量**:检查 `.env.local` 中的所有配置是否正确 3. **防火墙**:确保开放80和443端口(如果使用HTTPS) 4. **域名**:如果使用域名,更新 `AUTH_URL` 和Nginx配置 ## 如果SSH连接成功 直接运行自动化迁移脚本: ```bash ./migrate-server.sh ``` 脚本会自动完成所有步骤。