温馨提示×

怎么在debian调试laravel

小樊
43
2026-09-23 13:01:34
栏目: 智能运维

在 Debian 上调试 Laravel,通常可以从**环境准备、日志与错误显示、调试工具、IDE 调试(Xdebug)**几个方面入手。下面给你一套比较实用的做法。


一、基础环境确认(Debian)

确保你已经安装并正确配置:

php -v
composer -V
mysql -V   # 或 mariadb
nginx -v  # 或 apache2 -v

Laravel 所需 PHP 扩展(常见):

sudo apt install php-cli php-mbstring php-xml php-curl php-mysql php-zip php-bcmath php-gd

二、开启错误显示(最快调试方式)

1. 修改 .env

APP_DEBUG=true
APP_ENV=local

2. 清除配置缓存

php artisan config:clear
php artisan cache:clear

3. Web 服务器错误日志

  • Nginx + PHP-FPM
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/php8.2-fpm.log
  • Apache
sudo tail -f /var/log/apache2/error.log

三、使用 Laravel 自带调试工具

1. dump() / dd()

dump($user);
dd($request->all());

2. Laravel Log

\Log::error('调试信息', ['data' => $data]);

查看日志:

tail -f storage/logs/laravel.log

四、使用 Tinker(命令行调试)

php artisan tinker

示例:

User::first();
App\Models\Order::where('status', 'paid')->count();

非常适合调试 Model / 查询。


五、使用 Xdebug(专业级调试)

1. 安装 Xdebug

sudo apt install php-xdebug

查看版本:

php -m | grep xdebug

2. 配置 Xdebug(PHP 8.2 示例)

编辑:

sudo nano /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
xdebug.idekey=PHPSTORM

重启服务:

sudo systemctl restart php8.2-fpm

六、在 IDE 中调试(以 VS Code 为例)

1. 安装扩展

  • PHP Debug

2. launch.json

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

然后:

  • 打断点
  • 访问 Laravel 页面
  • 开始调试

七、常见问题排查清单

✅ .env 是否存在
✅ storage 和 bootstrap/cache 权限

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

✅ 路由是否存在

php artisan route:list

✅ 数据库是否连接

php artisan tinker
DB::connection()->getPdo();

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

  • 新手:
    APP_DEBUG=true + dd() + laravel.log

  • 进阶:
    tinker + Log::

  • 专业:
    Xdebug + VS Code / PHPStorm


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

  • Debian 版本(11 / 12)
  • PHP 版本
  • Web 服务器(Nginx / Apache)
  • 是 Web 请求调试 还是 命令行 / 队列调试

我可以直接给你一份可用的配置文件。

0 踩