在 Ubuntu 上调试 ThinkPHP,一般可以从**环境准备、框架调试模式、日志、断点调试(Xdebug)**几个方面入手。下面按实用程度给你一套完整方案。
编辑项目根目录的 .env(推荐)或 config/app.php
.env(推荐)APP_DEBUG = true
'app_debug' => true,
开启后:
确保:
APP_DEBUG=true// index.php 或入口文件
error_reporting(E_ALL);
ini_set('display_errors', '1');
日志路径:
runtime/log/
查看最新日志:
tail -f runtime/log/*/*.log
常见日志级别:
Log::error('错误信息');
Log::debug('调试信息');
dump($data);
或:
halt($data); // 输出并终止
sudo apt update
sudo apt install php-xdebug
确认安装:
php -m | grep xdebug
找到 php.ini(通常在 /etc/php/8.x/cli/php.ini 或 fpm)
[xdebug]
zend_extension=xdebug
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
xdebug.idekey=VSCODE
重启服务:
sudo service php8.1-fpm restart
# 或
sudo service apache2 restart
安装插件:PHP Debug
launch.json:
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003
}
在代码中打断点 → F5 启动调试
如果是命令行运行:
php think run
调试 CLI:
php -d xdebug.mode=debug think your:command
runtime 目录权限sudo chmod -R 777 runtime
php think route:list
php think clear
✅ APP_DEBUG=true
✅ tail -f runtime/log
✅ dump() / halt()
✅ Xdebug + VS Code
如果你愿意,可以告诉我:
我可以给你更精确的配置示例。