Ai_GirlFriend/PHP服务连接问题解决方案.md
2026-02-04 19:26:08 +08:00

200 lines
5.2 KiB
Markdown
Raw 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.

# PHP 服务连接问题解决方案
## 问题描述
Python 后端无法连接到 PHP 服务,报错:
```
HTTPConnectionPool(host='192.168.1.164', port=30100): Read timed out
```
## 已完成的修复
### 1. 配置修正
- ✅ 修改 `lover/deps.py`:移除硬编码 IP从配置读取
- ✅ 修改 `lover/config.py`:默认地址改为 `127.0.0.1:30100`
- ✅ 修改 `.env`:端口从 `8080` 改为 `30100`
- ✅ 减少超时时间:从 5 秒改为 3 秒
- ✅ 改进错误处理:区分超时和连接错误
### 2. 启动脚本优化
- ✅ 使用 `router.php` 而不是 `-t .`
- ✅ 添加端口清理逻辑,自动终止占用端口的旧进程
- ✅ 添加等待时间,确保服务完全启动
### 3. 测试工具
创建了 `xunifriend_RaeeC/public/test_api.php` 用于测试:
- `/test_api.php` - 测试 PHP 服务器基本响应
- `/test_db` - 测试数据库连接
## 当前问题分析
### PHP 服务器状态
```
端口 30100 已被监听(进程 31592, 16636
但是请求超时,无法获得响应
```
### 可能的原因
1. **数据库连接问题**
- PHP 应用可能在启动时尝试连接数据库
- 如果数据库连接失败或慢,会导致请求超时
- 检查 `xunifriend_RaeeC/application/database.php` 配置
2. **PHP 内置服务器限制**
- PHP 内置服务器是单线程的
- 如果有请求阻塞,后续请求会超时
- 建议使用 Apache 或 Nginx + PHP-FPM
3. **应用初始化问题**
- ThinkPHP 框架初始化可能有问题
- 检查 `xunifriend_RaeeC/application/admin/command/Install/install.lock` 是否存在
4. **路由配置问题**
- API 路由可能未正确配置
- 检查 `xunifriend_RaeeC/application/route.php`
## 解决步骤
### 步骤 1: 测试 PHP 服务器基本功能
```cmd
# 在浏览器或命令行测试
curl http://127.0.0.1:30100/test_api.php
```
预期响应:
```json
{
"code": 1,
"msg": "PHP 服务器运行正常",
"time": 1738665600,
"data": {
"php_version": "8.0.0",
"server_time": "2026-02-04 19:00:00"
}
}
```
### 步骤 2: 测试数据库连接
```cmd
curl http://127.0.0.1:30100/test_db
```
如果数据库连接失败,检查:
- MySQL 是否运行
- `xunifriend_RaeeC/application/database.php` 配置是否正确
- 数据库用户名密码是否正确
### 步骤 3: 测试实际 API
```cmd
# 使用有效的 token 测试
curl -H "token: YOUR_TOKEN_HERE" http://127.0.0.1:30100/api/user_basic/get_user_basic
```
### 步骤 4: 检查 PHP 错误日志
PHP 内置服务器的错误会显示在启动窗口中,查看是否有:
- 数据库连接错误
- 文件权限错误
- PHP 语法错误
- 缺少扩展
## 临时解决方案
### 方案 1: 使用开发环境兜底(已实现)
Python 后端在开发环境下,如果 PHP 连接失败,会自动使用测试用户:
```python
# 在 lover/deps.py 中
if settings.APP_ENV == "development" and settings.DEBUG:
logger.warning(f"开发环境token 验证失败({e.detail}),使用测试用户")
return AuthedUser(id=70, reg_step=2, gender=0, nickname="test-user", token="")
```
### 方案 2: 直接使用数据库认证
修改 Python 后端,不依赖 PHP API直接查询数据库
```python
# 在 lover/deps.py 中添加
def _fetch_user_from_db(token: str) -> Optional[dict]:
"""直接从数据库获取用户信息"""
from lover.db import get_db
db = next(get_db())
user = db.execute(
"SELECT * FROM fa_user WHERE token = :token",
{"token": token}
).fetchone()
return dict(user) if user else None
```
### 方案 3: 重启 PHP 服务
```cmd
# 使用更新后的启动脚本,会自动清理旧进程
启动项目.bat
```
## 长期解决方案
### 推荐:使用 Nginx + PHP-FPM
PHP 内置服务器不适合生产环境,建议:
1. 安装 Nginx
2. 配置 PHP-FPM
3. 配置 Nginx 反向代理
配置示例:
```nginx
server {
listen 30100;
server_name localhost;
root C:/Users/Administrator/Desktop/Project/AI_GirlFriend/xunifriend_RaeeC/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
```
## 调试命令
### 查看端口占用
```cmd
netstat -ano | findstr :30100
```
### 终止进程
```cmd
taskkill /F /PID <进程ID>
```
### 测试 PHP 配置
```cmd
D:\2_part\php-8.0.0-Win32-vs16-x64\php.exe -v
D:\2_part\php-8.0.0-Win32-vs16-x64\php.exe -m # 查看已安装的扩展
```
### 手动启动 PHP 服务器(用于调试)
```cmd
cd xunifriend_RaeeC\public
D:\2_part\php-8.0.0-Win32-vs16-x64\php.exe -S 0.0.0.0:30100 router.php
```
## 下一步行动
1. **立即测试**:运行 `curl http://127.0.0.1:30100/test_api.php`
2. **检查数据库**:确认 MySQL 正在运行
3. **查看日志**:检查 PHP 启动窗口的错误信息
4. **重启服务**:使用更新后的 `启动项目.bat`
## 文件修改记录
- `lover/deps.py` - 改进错误处理和超时设置
- `lover/config.py` - 修正默认地址
- `.env` - 修正端口配置
- `启动项目.bat` - 添加端口清理和 router.php
- `xunifriend_RaeeC/public/test_api.php` - 新增测试脚本