温馨提示×

ubuntu下phpstorm怎么用xdebug

小樊
40
2026-09-29 03:34:29
栏目: 编程语言

在 Ubuntu 下使用 PhpStorm + Xdebug 进行 PHP 调试,一般步骤如下(以 PHP 7.x / 8.x + Xdebug 3 为例):


一、安装 Xdebug

1. 查看 PHP 版本

php -v

2. 安装 Xdebug

如果是通过 apt 安装的 PHP:

sudo apt install php-xdebug

如果你用的是 PHP 8+:

sudo apt install php8.1-xdebug

(版本号按实际)

3. 确认是否安装成功

php -m | grep xdebug

能看到 xdebug 即成功。


二、配置 Xdebug(php.ini)

编辑 php.ini(位置可能是):

php --ini

常见路径:

/etc/php/8.1/cli/php.ini
/etc/php/8.1/apache2/php.ini
/etc/php/8.1/fpm/php.ini

在文件末尾添加(Xdebug 3 配置方式):

[xdebug]
zend_extension=xdebug
xdebug.mode=debug
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
xdebug.start_with_request=trigger

如果是 Xdebug 2(旧版本),配置不同,需要我也可以给你。

重启服务:

sudo systemctl restart apache2
# 或
sudo systemctl restart php8.1-fpm

三、配置 PhpStorm

1. 设置 PHP 解释器

  • File → Settings → PHP
  • CLI Interpreter → 选择你的 PHP
  • 确认 Xdebug 已被识别(会显示 Debugger: Xdebug)

2. 配置 Debug 端口

  • Settings → PHP → Debug
  • Debug port: 9003
  • 勾选 Can accept external connections

3. 配置 Server

  • Settings → PHP → Servers
  • 新增:
    • Name: localhost
    • Host: localhost
    • Port: 80 或 8080
    • Debugger: Xdebug
    • 勾选 Use path mappings(本地项目路径 ↔ 服务器路径)

四、启动调试

方式一:浏览器触发(推荐)

  1. 在 PhpStorm 点 电话图标(Start Listening for PHP Debug Connections)
  2. 浏览器安装:
    • Xdebug Helper(Chrome)
    • 或 FireFox Xdebug
  3. 开启调试 → 访问页面
  4. PhpStorm 自动断点

方式二:PHP CLI 调试

export XDEBUG_MODE=debug
php your_script.php

五、常见坑

✅ 端口被占用(9000 常见于 PHP-FPM) → 用 9003

✅ path mapping 不对 → 断点不生效,必须配置

✅ 使用 Docker / Vagrant → client_host 要写宿主机 IP


如果你告诉我:

  • PHP 版本
  • 是否用 Apache / Nginx
  • 是否 Docker

我可以给你完全对应的配置示例。

0 踩