Files
whale-town/DEPLOYMENT_GUIDE.md
2025-12-05 19:00:14 +08:00

2.6 KiB

AI Town Game 部署和运维指南

🚀 生产环境部署

环境要求

  • 服务器: 2 核心 CPU, 4GB RAM, 10GB 存储
  • 软件: Node.js 18+, PM2, Nginx, Git
  • 系统: Ubuntu 20.04+ / CentOS 8+ / Windows Server 2019+

快速部署

1. 环境准备

# Ubuntu/Debian
sudo apt update && sudo apt install -y nodejs npm nginx git
npm install -g yarn pm2

# 克隆项目
git clone <repository-url> /opt/ai-town
cd /opt/ai-town/server
yarn install --production && yarn build

2. 启动服务

# 启动服务器
pm2 start dist/server.js --name ai-town-server
pm2 startup && pm2 save

# 配置 Nginx
sudo cp nginx.conf /etc/nginx/sites-available/ai-town
sudo ln -s /etc/nginx/sites-available/ai-town /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

3. Web 客户端

在 Godot 编辑器中导出 HTML5 版本到 /opt/ai-town/web/ 目录

监控和维护

日常检查

# 服务状态
pm2 status
pm2 logs ai-town-server

# 系统资源
htop && df -h

# 健康检查
curl -f http://localhost:8080/health || echo "Service down"

备份策略

# 自动备份脚本
#!/bin/bash
tar -czf /backup/ai-town-$(date +%Y%m%d).tar.gz /opt/ai-town/server/data/
find /backup -name "ai-town-*.tar.gz" -mtime +7 -delete

# 定时任务
echo "0 2 * * * /opt/ai-town/backup.sh" | crontab -

安全配置

SSL 和防火墙

# SSL 证书
sudo certbot --nginx -d your-domain.com

# 防火墙
sudo ufw allow ssh && sudo ufw allow 80 && sudo ufw allow 443
sudo ufw enable

故障排除

常见问题

  • 服务无法启动: 检查端口占用 sudo lsof -i :8080
  • 连接失败: 测试 WebSocket wscat -c ws://localhost:8080
  • 性能问题: 监控资源 pm2 monit

紧急恢复

# 重启所有服务
pm2 restart all && sudo systemctl restart nginx

# 数据恢复
tar -xzf /backup/ai-town-YYYYMMDD.tar.gz -C /opt/ai-town/server/

📊 监控告警

健康检查脚本

#!/bin/bash
# health-check.sh
pm2 describe ai-town-server | grep -q "online" || exit 1
nc -z localhost 8080 || exit 1
echo "OK: Service healthy"

自动告警

# 错误监控
tail -f /opt/ai-town/server/logs/error.log | while read line; do
    echo "$line" | grep -q "ERROR" && echo "Alert: $line" | mail admin@domain.com
done

部署检查清单:

  • 环境配置完成
  • 服务正常启动
  • Web 界面可访问
  • SSL 证书配置
  • 备份策略启用
  • 监控告警配置

详细配置请参考项目文档和 server/README.md