温馨提示×

debian如何调试laravel

小樊
37
2026-08-17 00:21:07
栏目: 智能运维

在 Debian 上调试 Laravel,通常可以从 环境、日志、调试工具、IDE、请求流程 几个层面来做。下面按“从基础到进阶”给你一套实用方案。


一、基础环境检查(先确认不是环境问题)

1. PHP 版本与扩展

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)


2. Composer

composer --version

如果没装:

sudo apt install composer

二、Laravel 日志调试(最常用)

1. 查看 Laravel 日志

tail -f storage/logs/laravel.log

常见错误:

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

2. 临时打印调试

logger('调试信息', ['data' => $data]);

或:

info('调试信息');

三、开启调试模式(非常重要)

1. 修改 .env

APP_DEBUG=true
APP_ENV=local

⚠️ 生产环境一定要关闭


2. 清除配置缓存

php artisan config:clear
php artisan cache:clear

四、使用 Laravel 自带调试工具

1. dd() / dump()

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

2. ray()(强烈推荐)

安装:

composer require spatie/ray

使用:

ray($data);

需要配合 Ray 应用(桌面端)


五、调试数据库(SQL)

1. 查看执行的 SQL

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

2. 使用 toSql()

dump(User::where('id', 1)->toSql());

六、调试路由与请求

1. 查看所有路由

php artisan route:list

2. 查看当前请求信息

dump(request()->all());
dump(request()->headers->all());

七、权限问题(Debian 常见坑)

1. storage / bootstrap 权限

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

八、使用 Xdebug(进阶,推荐)

1. 安装 Xdebug

sudo apt install php8.2-xdebug

确认:

php -m | grep xdebug

2. 配置 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

3. 在 PHPStorm / VS Code 中配置

  • PHPStorm:
    Settings → PHP → Debug → Xdebug
  • VS Code:
    安装 PHP Debug 插件

打断点 → 访问页面 → 单步调试 ✅


九、Artisan 调试技巧

1. 测试命令

php artisan tinker
User::first();

2. 检查配置

php artisan config:show
php artisan route:clear

十、常见 Debian + Laravel 错误速查

问题 解决
500 错误 storage/logs/laravel.log
权限 denied chown www-data
路由 404 php artisan route:list
白屏 APP_DEBUG=true
开启缓存、关闭 debug

如果你愿意,我可以:

  • ✅ 针对 Debian + Nginx / Apache 给你具体配置
  • ✅ 帮你 逐行分析一个报错
  • ✅ 给你一套 生产环境调试规范

你可以直接贴:

  • Laravel 版本
  • PHP 版本
  • 报错信息或截图

0