温馨提示×

Laravel在Debian如何调试

小樊
45
2026-07-06 06:26:23
栏目: 智能运维

在 Debian 上调试 Laravel,通常可以从 日志、调试模式、断点调试、性能分析、队列/任务 等几个方面入手。下面按“常用 → 进阶”给你一套完整方案。


一、基础调试(最常用)

1. 开启调试模式(最重要)

# .env
APP_DEBUG=true
APP_ENV=local

⚠️ 生产环境一定关闭


2. 查看 Laravel 日志

tail -f storage/logs/laravel.log

常见错误:

  • SQL 错误
  • 路由不存在
  • 中间件异常
  • 权限问题(Debian 常见)

3. 使用 dd() / dump()

dd($user);
dump($request->all());
  • dd():打印并终止
  • dump():打印继续运行

4. 查看 SQL 日志

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

或使用 barryvdh/laravel-debugbar(强烈推荐)。


二、Debian 系统级调试

1. 查看 PHP 错误日志

tail -f /var/log/php8.2-fpm.log
# 或
journalctl -xe

常见路径:

/var/log/php8.2-fpm.log
/var/log/nginx/error.log
/var/log/apache2/error.log

2. 权限问题(Debian 最常见)

sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache

3. PHP 配置检查

php -i | grep "display_errors"
php -i | grep "error_reporting"

建议:

display_errors = On
error_reporting = E_ALL

三、断点调试(专业级)

1. 安装 Xdebug(Debian)

sudo apt install php8.2-xdebug

确认:

php -v

2. 配置 Xdebug

# /etc/php/8.2/mods-available/xdebug.ini
zend_extension=xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003

重启 PHP:

sudo systemctl restart php8.2-fpm

3. VS Code 调试 Laravel

安装插件:PHP Debug

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for Xdebug",
      "type": "php",
      "request": "launch",
      "port": 9003
    }
  ]
}

然后:

  • 打断点
  • 访问页面
  • VS Code 自动停住

四、路由 & 请求调试

1. 查看所有路由

php artisan route:list

2. 查看请求数据

request()->all()
request()->headers->all()

五、队列 / 任务调试(Debian 常见)

1. 队列失败

php artisan queue:failed
php artisan queue:retry all

2. 队列 worker 日志

tail -f storage/logs/worker.log

六、性能 & 异常分析工具

1. Laravel Debugbar(强烈推荐)

composer require barryvdh/laravel-debugbar --dev

浏览器底部会显示:

  • SQL
  • 内存
  • 路由
  • 请求时间

2. Telescope(高级)

composer require laravel/telescope
php artisan telescope:install
php artisan migrate

可查看:

  • 请求
  • 异常
  • 队列
  • 邮件
  • 日志

七、常见 Debian + Laravel 错误速查

问题 解决
500 错误 laravel.log
权限拒绝 chown www-data
路由 404 php artisan route:clear
缓存异常 php artisan cache:clear
Composer 权限 使用非 root 用户

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

本地开发

  • APP_DEBUG=true
  • laravel-debugbar
  • Xdebug + VS Code

Debian 服务器

  • tail -f laravel.log
  • php-fpm 日志
  • queue:work --verbose

如果你愿意,可以告诉我:

  • Laravel 版本
  • Debian 版本
  • Nginx / Apache
  • 是 500 / 404 / 超时 / 队列问题

我可以直接帮你定位具体错误。

0