温馨提示×

ubuntu phpstorm断点调试怎么设

小樊
44
2026-09-10 22:24:35
栏目: 编程语言

在 Ubuntu 下用 PhpStorm 进行 PHP 断点调试,通常是配合 Xdebug 来做的。下面给你一套从零开始的完整流程(以 PHP 7.x / 8.x + Xdebug 3 为例,最常用)。


一、安装 Xdebug

1. 查看 PHP 版本

php -v

2. 安装 Xdebug

Ubuntu 一般可以直接用 apt:

sudo apt update
sudo apt install php-xdebug

如果是 PHP 8:

sudo apt install php8.1-xdebug

安装完成后确认:

php -m | grep xdebug

二、配置 Xdebug(关键)

1. 找到 php.ini

php --ini

通常路径类似:

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

Web 调试改 apache / fpm 的 ini 命令行调试改 cli 的 ini


2. Xdebug 3 推荐配置

在 php.ini 末尾追加:

[xdebug]
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

Xdebug 2 是 xdebug.remote_*,现在基本不用了

重启服务:

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

三、配置 PhpStorm

1. 设置 PHP 解释器

File → Settings → PHP
  • CLI Interpreter:选择你的 PHP
  • 确认 Xdebug 已识别(绿色对勾)

2. 配置 Debug 端口

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

3. 配置 Servers

Settings → PHP → Servers
  • Name: localhost
  • Host: localhost
  • Port: 80
  • Debugger: Xdebug
  • 勾选 Use path mappings
    • 本地路径 ↔ 服务器路径(必须一致或正确映射)

四、浏览器调试(Web 项目)

方式一:浏览器插件(推荐)

安装:

  • Chrome: Xdebug Helper
  • Firefox: Xdebug Ext

设置 IDE Key 为:

PHPSTORM

点击图标 → Debug


方式二:URL 参数

http://localhost/index.php?XDEBUG_SESSION=PHPSTORM

五、PhpStorm 中开始调试

  1. 在代码行号左侧 点红点(打断点)
  2. 点击右上角 电话图标(Start Listening for PHP Debug Connections)
  3. 浏览器访问页面
  4. PhpStorm 自动弹出调试窗口

六、命令行调试(CLI)

方式一:环境变量

export XDEBUG_MODE=debug
export XDEBUG_CONFIG="idekey=PHPSTORM"
php test.php

方式二:PhpStorm 直接 Run → Debug


七、常见问题

1. 断点不生效

  • 没开 Listening
  • path mapping 不对
  • xdebug.mode 没设 debug

2. 端口被占用

netstat -tunlp | grep 9003

3. 查看 Xdebug 是否生效

php -i | grep xdebug

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

  • PHP 版本
  • Apache / Nginx
  • Web 还是 CLI 调试

我可以直接给你 定制版配置。

0 踩