在 Debian 上调试 Laravel,通常可以从 环境、日志、调试工具、IDE、请求流程 几个层面来做。下面按“从基础到进阶”给你一套实用方案。
Laravel 对 PHP 版本有要求(以 Laravel 10 为例,需要 PHP ≥ 8.1):
php -v
常见必需扩展:
sudo apt install php8.2-cli php8.2-common php8.2-curl php8.2-mbstring \
php8.2-xml php8.2-zip php8.2-mysql php8.2-bcmath php8.2-tokenizer
根据你的 PHP 版本调整(8.1 / 8.2 / 8.3)
composer --version
如果没装:
sudo apt install composer
tail -f storage/logs/laravel.log
常见错误:
logger('调试信息', ['data' => $data]);
或:
info('调试信息');
.envAPP_DEBUG=true
APP_ENV=local
⚠️ 生产环境一定要关闭
php artisan config:clear
php artisan cache:clear
dd() / dump()dd($user);
dump($request->all());
dd():打印并终止dump():打印继续运行ray()(强烈推荐)安装:
composer require spatie/ray
使用:
ray($data);
需要配合 Ray 应用(桌面端)
\DB::enableQueryLog();
// 执行查询
dump(\DB::getQueryLog());
toSql()dump(User::where('id', 1)->toSql());
php artisan route:list
dump(request()->all());
dump(request()->headers->all());
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
sudo apt install php8.2-xdebug
确认:
php -m | grep xdebug
/etc/php/8.2/cli/conf.d/20-xdebug.ini
zend_extension=xdebug
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
Settings → PHP → Debug → Xdebug打断点 → 访问页面 → 单步调试 ✅
php artisan tinker
User::first();
php artisan config:show
php artisan route:clear
| 问题 | 解决 |
|---|---|
| 500 错误 | 看 storage/logs/laravel.log |
| 权限 denied | chown www-data |
| 路由 404 | php artisan route:list |
| 白屏 | APP_DEBUG=true |
| 慢 | 开启缓存、关闭 debug |
你可以直接贴: