> For the complete documentation index, see [llms.txt](https://tech.banchengyun.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tech.banchengyun.com/archives/backend/hyperf/hyperf-xdebug.md).

# 在 phpstorm 中调试 hyperf 代码

php常用的调试器是xdebug，不过hyperf是基于swoole，而swoole不支持xdebug。swoole官方基于xdebug魔改了一个swoole调试插件-sdebug。

我的电脑是使用WSL的，因此本文的配置是基于WSL ubuntu的。理论上可以应用所有系统与虚拟机。

见地址：[sdebug](https://github.com/swoole/sdebug)

## 安装

```
# 拉取sdebug代码
git clone https://github.com/swoole/sdebug.git
# 进入目录
cd sdebug
# 编译
./rebuild.sh
```

以上编译完插件之后，需要将sdebug插件加入到php.ini中。

```
# 查看php的配置
php --ini
# 编辑php.ini配置文件
vim xxx/php.ini
# 添加一行
zend_extension="xdebug.so"
# 查看是否配置成功
php --ri sdebug
```

如果`php --ri sdebug`能够查到扩展的信息，说明扩展成功安装。

## 配置sdebug

```
# 开启xdebug扩展
zend_extension=xdebug.so
# 开启远程调试
xdebug.remote_enable =1
# 设置远程的ip
xdebug.remote_host=phpstorm所在机子的ip
# 自动启动调试
xdebug.remote_autostart=1
# 设置远程的调试端口
xdebug.remote_port=9000
xdebug.remote_connect_back=1
```

## phpstorm的配置

在phpstorm中，需要将php路径映射为远程的路径。因此在配置php cli的时候，需要配置一些信息：

![](https://1958377202-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFFhvK5IKCzwYDrIDCO%2Fuploads%2Fgit-blob-991d1d2ab6902986537a88b0d3060f9082cbdfac%2Fhyperf-xdebug1.png?alt=media)

红圈中的配置要取消掉，不然请求时swoole的子进程会卡死。

![](https://1958377202-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFFhvK5IKCzwYDrIDCO%2Fuploads%2Fgit-blob-2e97e584edf39e5f5a0e68c9ad565597d3905981%2Fhyperf-xdebug2.png?alt=media)

![](https://1958377202-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFFhvK5IKCzwYDrIDCO%2Fuploads%2Fgit-blob-3da7d577dedc8b3ea6ab897b6ee2ad39b5991b36%2Fhyperf-xdebug3.png?alt=media)

这里还需要配置php cli的。需要将php的cli配置正常，并且命令行的选项需要增加一个配置xdebug.remote\_host，该选项配置为phpstorm所在机子的ip地址。

配置完php cli之后，还需要配置以何命令启动php。

![](https://1958377202-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFFhvK5IKCzwYDrIDCO%2Fuploads%2Fgit-blob-beaa1fbb6b33532a5260c5a58cdf76ef0566d49a%2Fhyperf-xdebug4.png?alt=media)

我们之前是在命令行启动`php bin/hyperf.php start`，现在需要如同这么配置。配置完成之后，点击调试按钮启动，这个时候使用postman请求某个接口，就能够调试了：

![](https://1958377202-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFFhvK5IKCzwYDrIDCO%2Fuploads%2Fgit-blob-f3cff5074f9e3791430aed2bb61e7e55c0bf163a%2Fhyperf-xdebug6.png?alt=media)

除了调试请求外，也能够调试脚本。脚本的话，也需要配置多一个命令：

![](https://1958377202-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFFhvK5IKCzwYDrIDCO%2Fuploads%2Fgit-blob-371c352fea865bee921be94ce44b861947f383cf%2Fhyperf-xdebug5.png?alt=media)

这么配置之后，使用调试按钮启动之后就能够调试命令行。
