温馨提示×

linux laravel怎么调试错误

小樊
49
2026-09-19 08:33:45
栏目: 智能运维

在 Linux 下调试 Laravel 错误,常见且实用的方法如下(从基础到进阶):


一、开启错误显示(最基础)

1. 修改 .env

APP_DEBUG=true
APP_ENV=local

修改后清缓存:

php artisan config:clear

二、查看日志(最常用)

日志位置

storage/logs/laravel.log

实时查看错误

tail -f storage/logs/laravel.log

查看最近 50 行

tail -n 50 storage/logs/laravel.log

三、在代码中调试

1. 使用 dd() / dump()

dd($user);
// 或
dump($user);

2. 使用 Log 输出

\Log::error('用户信息', ['user' => $user]);
\Log::info('调试信息');

四、命令行调试(Artisan)

查看路由错误

php artisan route:list

查看配置

php artisan config:show

清缓存(很重要)

php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear

五、HTTP / 页面错误调试

1. 打开调试页面

APP_DEBUG=true 时,页面会显示:

  • 错误堆栈
  • SQL 语句
  • 请求参数

2. 常见 HTTP 错误

错误 原因
500 代码异常
404 路由不存在
419 CSRF 验证失败
403 权限不足

六、数据库调试

打印 SQL

DB::enableQueryLog();
// 执行查询
dd(DB::getQueryLog());

或使用:

\Log::info(DB::getQueryLog());

七、使用 Xdebug(高级)

安装 Xdebug(Ubuntu 示例)

sudo apt install php-xdebug

配置

xdebug.mode=debug
xdebug.start_with_request=yes

配合:

  • VS Code
  • PHPStorm

打断点调试


八、权限问题(Linux 特有)

常见错误:

The stream or file could not be opened

解决:

sudo chown -R www-data:www-data storage
sudo chmod -R 775 storage

九、推荐调试组合(实战)

✅ APP_DEBUG=true
✅ tail -f storage/logs/laravel.log
✅ dd() / Log::error()
✅ php artisan clear 系列命令


如果你有具体报错信息(比如 500、SQL 错误、路由错误),可以直接贴出来,我可以帮你精准分析。

0 踩