Files
WPSBot/deploy/README.md
2025-10-28 13:00:35 +08:00

346 lines
5.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# WPS Bot Game 部署指南
## 📋 前置要求
- Ubuntu 20.04+ 服务器
- Python 3.10+建议使用conda
- 1GB内存 + 单核CPU
- sudo权限
## 🚀 快速部署
### 1. 上传项目到服务器
```bash
# 方式1: 使用scp
scp -r WPSBotGame/ user@server:/opt/wps-bot
# 方式2: 使用git
cd /opt
git clone <your-repo-url> wps-bot
```
### 2. 运行安装脚本
```bash
cd /opt/wps-bot
chmod +x deploy/install.sh
sudo bash deploy/install.sh
```
安装脚本会自动完成:
- ✅ 检查环境
- ✅ 安装依赖
- ✅ 创建数据目录
- ✅ 配置systemd服务
### 3. 配置环境变量
```bash
# 编辑配置文件
sudo nano /opt/wps-bot/.env
# 修改Webhook URL
WEBHOOK_URL=https://xz.wps.cn/api/v1/webhook/send?key=你的密钥
```
### 4. 启动服务
```bash
# 使用管理脚本(推荐)
chmod +x deploy/manage.sh
./deploy/manage.sh start
# 或直接使用systemctl
sudo systemctl start wps-bot
sudo systemctl status wps-bot
```
### 5. 配置开机自启
```bash
./deploy/manage.sh enable
# 或
sudo systemctl enable wps-bot
```
## 🛠️ 服务管理
### 使用管理脚本(推荐)
```bash
cd /opt/wps-bot
# 启动服务
./deploy/manage.sh start
# 停止服务
./deploy/manage.sh stop
# 重启服务
./deploy/manage.sh restart
# 查看状态
./deploy/manage.sh status
# 查看实时日志
./deploy/manage.sh logs
# 启用开机自启
./deploy/manage.sh enable
# 禁用开机自启
./deploy/manage.sh disable
# 更新代码并重启
./deploy/manage.sh update
```
### 使用systemctl命令
```bash
# 启动服务
sudo systemctl start wps-bot
# 停止服务
sudo systemctl stop wps-bot
# 重启服务
sudo systemctl restart wps-bot
# 查看状态
sudo systemctl status wps-bot
# 启用开机自启
sudo systemctl enable wps-bot
# 禁用开机自启
sudo systemctl disable wps-bot
# 查看日志
sudo journalctl -u wps-bot -f
# 查看最近100行日志
sudo journalctl -u wps-bot -n 100
# 查看今天的日志
sudo journalctl -u wps-bot --since today
```
## 📊 监控和调试
### 查看系统状态
```bash
# 方式1: 通过API
curl http://localhost:8000/stats
# 方式2: 通过日志
sudo journalctl -u wps-bot | grep "memory_mb"
```
### 常用调试命令
```bash
# 查看服务是否运行
sudo systemctl is-active wps-bot
# 查看服务是否开机自启
sudo systemctl is-enabled wps-bot
# 查看端口占用
sudo netstat -tlnp | grep 8000
# 查看进程
ps aux | grep python
# 查看数据库
sqlite3 /opt/wps-bot/data/bot.db "SELECT * FROM users;"
```
### 日志分析
```bash
# 查看错误日志
sudo journalctl -u wps-bot -p err
# 查看特定时间段的日志
sudo journalctl -u wps-bot --since "2025-10-28 10:00:00" --until "2025-10-28 11:00:00"
# 导出日志到文件
sudo journalctl -u wps-bot > /tmp/wps-bot.log
```
## 🔧 配置WPS Callback
### 获取服务器地址
```bash
# 查看服务器公网IP
curl ifconfig.me
# 或
curl icanhazip.com
```
### 在WPS中配置
1. 进入WPS群聊
2. 找到webhook机器人设置
3. 配置Callback URL
```
http://你的服务器IP:8000/api/callback
```
4. 保存并验证WPS会发送GET请求
### 测试Callback
```bash
# 从服务器测试
curl http://localhost:8000/api/callback
# 从外部测试
curl http://你的服务器IP:8000/api/callback
```
## 🔒 安全建议
### 防火墙配置
```bash
# 允许8000端口
sudo ufw allow 8000/tcp
# 查看防火墙状态
sudo ufw status
```
### 使用Nginx反向代理可选
```bash
# 安装Nginx
sudo apt update
sudo apt install nginx
# 配置Nginx
sudo nano /etc/nginx/sites-available/wps-bot
# 添加配置(见下方)
```
Nginx配置示例
```nginx
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
```
## 📈 性能优化
### 定期清理数据库
```bash
# 创建清理脚本
cat > /opt/wps-bot/cleanup.sh << 'EOF'
#!/bin/bash
sqlite3 /opt/wps-bot/data/bot.db "DELETE FROM game_states WHERE updated_at < strftime('%s', 'now', '-7 days');"
echo "数据库清理完成"
EOF
chmod +x /opt/wps-bot/cleanup.sh
# 添加定时任务
sudo crontab -e
# 添加每天凌晨2点清理
0 2 * * * /opt/wps-bot/cleanup.sh
```
### 监控内存使用
```bash
# 创建监控脚本
cat > /opt/wps-bot/monitor.sh << 'EOF'
#!/bin/bash
MEMORY=$(curl -s http://localhost:8000/stats | jq -r '.system.memory_mb')
echo "$(date): 内存使用 ${MEMORY}MB"
if (( $(echo "$MEMORY > 200" | bc -l) )); then
echo "警告:内存使用过高!"
fi
EOF
chmod +x /opt/wps-bot/monitor.sh
# 添加定时任务:每小时检查一次
0 * * * * /opt/wps-bot/monitor.sh >> /var/log/wps-bot-monitor.log
```
## 🆘 故障排除
### 服务启动失败
```bash
# 查看详细错误
sudo systemctl status wps-bot -l
# 查看最新日志
sudo journalctl -u wps-bot -n 50
# 检查配置文件
sudo systemctl cat wps-bot
# 手动测试启动
cd /opt/wps-bot
sudo -u ubuntu /home/ubuntu/miniconda3/envs/liubai/bin/python app.py
```
### 端口被占用
```bash
# 查看占用进程
sudo lsof -i :8000
# 杀死占用进程
sudo kill -9 <PID>
```
### 内存不足
```bash
# 查看内存使用
free -h
# 清理缓存
sudo sync && sudo sysctl -w vm.drop_caches=3
# 重启服务
./deploy/manage.sh restart
```
### 数据库锁定
```bash
# 检查数据库
sqlite3 /opt/wps-bot/data/bot.db "PRAGMA integrity_check;"
# 如果损坏,恢复数据库
mv /opt/wps-bot/data/bot.db /opt/wps-bot/data/bot.db.backup
# 重启服务会自动创建新数据库
./deploy/manage.sh restart
```
## 📞 技术支持
如有问题,请查看:
- 应用日志:`sudo journalctl -u wps-bot -f`
- 系统状态:`curl http://localhost:8000/stats`
- README`/opt/wps-bot/README.md`