前言

本人的电脑是 M1 Pro ,所使用的 LNMP 环境为集成开发环境 EServer(继 MxSrvs 比较不错的 Mac 下的集成开发环境),下面将演示 PhpStorm 调试 PHP7.4 所需要的配置(其他版本同理,经验证通过该方法配置了 PHP 7.0 和 PHP 5.6)。

EServer 下载链接:https://github.com/xianyunleo/EServer/

image-20250813160146720

环境配置

下载合适的 xdebug

首先运行 phpinfo 获取 php 的相关信息

image-20250813160615700

在 phpinfo 页面中右键查看页面源代码,将源码全部复制到 xdebug 页面进行分析,推荐兼容的 xdebug 版本:

image-20250813160744453

image-20250813160830338

编译 xdebug

点击推荐的版本进行下载,解压之后进入到相关目录:

image-20250813160932584

在该 xdebug 目录下运行 php7.4 版本的 phpize 工具,我这里使用的是 EServer 集成开发环境 phpize 路径为:

1
/Applications/EServer/childApp/php/php-7.4/bin/phpize

image-20250813161049760

接下来编译 xdebug 程序,由于我使用的是 EServer 集成开发环境自带的 PHP 程序是 x86_64 架构,非 ARM 架构,因此编译的 xdebug 也要是 x86_64。

1
2
3
4
5
6
7
./configure \
--build=x86_64-apple-darwin \
--with-php-config=/Applications/EServer/childApp/php/php-7.4/bin/php-config \
CC="clang -arch x86_64" \
CXX="clang++ -arch x86_64" \
CFLAGS="-arch x86_64" \
LDFLAGS="-arch x86_64"

其中的 --with-php-config 参数需要指定为 php-7.4 版本的php-config 程序,这里路径与 phpize 路径一致。

image-20250813161325834

接下来执行 make 命令即可:

1
make

在当前目录的 modules 目录下即可看到编译好的 xdebug.so 文件。

image-20250813161410989

并且该文件是 x86_64 位的。

image-20250813161507891

将该文件复制到 php 的扩展目录,我这里的目录为:

1
/Applications/EServer/childApp/php/php-7.4/lib/php/extensions/no-debug-non-zts-20190902 # 最后一级路径可能不要一样

image-20250813161827263

配置 php

接着在 php.ini 文件启用 xdebug 并进行配置:

image-20250813161931396

php.ini 中添加如下配置:

1
2
3
4
5
6
zend_extension=xdebug
[XDebug]
xdebug.client_host=127.0.0.1
xdebug.client_port=9900
xdebug.mode=debug
xdebug.idekey ="PHPSTORM"

配置 phpstorm

首先 phpstorm 配置解释器的路径:

image-20250813162253537

配置 debug 的端口:

这里配置的端口要与 php.ini 配置的 xdebug.client_port 端口一致!!

image-20250813162415308

接着配置 DBGp Proxy。

这里配置的信息要与 php.ini 配置的一致!!!

image-20250813162322073

最后配置 Server 信息:

image-20250813162754065

下拉选择 PHP Web Page ,然后根据实际情况配置 Servers

image-20250813162819164

image-20250813162856508

最后点击小虫开启监听,然后点击 Vaildate 进行验证:

image-20250813163105931

如果后面出现全对即可:

image-20250813163145922

效果验证

在需要断点的位置打上断点,然后访问页面,或点击绿色的小虫子即可进入到断点。

image-20250813163354727

注意事项

最后需要注意的是,PHP 7.2 + 版本使用的 xdebug 是 3.0 语法与 2.0 配置不太一样。

例如:

Xdebug 3.1 的 php.ini 配置事例:

1
2
3
4
5
6
zend_extension=xdebug
[XDebug]
xdebug.client_host=127.0.0.1
xdebug.client_port=9900
xdebug.mode=debug
xdebug.idekey ="PHPSTORM"

Xdebug 2.x 的 php.ini 配置事例:

1
2
3
4
5
6
7
8
9
zend_extension=xdebug.so
[XDebug]
xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9900
xdebug.remote_autostart=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.idekey="PHPSTORM"