温馨提示×

如何用debian调试laravel应用

小樊
46
2026-09-26 11:26:27
栏目: 智能运维

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


一、基础环境准备(Debian)

1. 安装必要组件

sudo apt update
sudo apt install -y php php-cli php-common php-mbstring php-xml php-curl php-mysql php-zip unzip git

如果使用 Nginx/Apache:

sudo apt install -y nginx php-fpm
# 或
sudo apt install -y apache2 libapache2-mod-php

2. 安装 Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

二、Laravel 自带调试手段(最常用)

1. 开启调试模式

编辑 .env:

APP_DEBUG=true
APP_ENV=local

⚠️ 生产环境务必关闭 APP_DEBUG


2. 使用 dump() / dd()

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

3. 查看日志

tail -f storage/logs/laravel.log

常见错误:

  • 权限问题
  • 数据库连接失败
  • 路由未定义

4. Artisan 调试命令

php artisan route:list
php artisan migrate:status
php artisan config:clear
php artisan cache:clear
php artisan serve

三、在 Debian 上配置 Xdebug(推荐)

1. 安装 Xdebug

sudo apt install php-xdebug

确认:

php -m | grep xdebug

2. 配置 Xdebug(PHP 8+ 示例)

编辑:

sudo nano /etc/php/8.2/cli/conf.d/20-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
xdebug.idekey=PHPSTORM

重启 PHP:

sudo systemctl restart php8.2-fpm

3. IDE 配置(以 PhpStorm 为例)

  • 开启 Xdebug
  • 设置:
    • Port: 9003
    • IDE Key: PHPSTORM
  • 设置断点
  • 浏览器访问或 CLI 运行:
php artisan tinker

四、常见 Debian 特定问题

1. 权限问题

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

2. PHP 版本不一致

php -v
which php

确保 CLI 和 Web 使用同一 PHP 版本


3. 数据库无法连接

sudo systemctl status mysql
mysql -u root -p

五、推荐调试组合

✅ 开发环境:

  • APP_DEBUG=true
  • Xdebug + PhpStorm
  • tail -f laravel.log

✅ 生产环境:

  • APP_DEBUG=false
  • Sentry / Flare
  • 日志监控

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

  • 用的是 Nginx 还是 Apache
  • PHP 版本
  • 是 Web 请求还是 CLI / 队列调试

我可以给你更精确的 Debian + Laravel 调试方案。

0 踩