> 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的时候，需要配置一些信息：

![](/files/-MJH5-wI9lfk1c_d0kA6)

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

![](/files/-MJH6c9SHd1z1bPhHXnv)

![](/files/-MJH6c9TbhRoSFOdvSMh)

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

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

![](/files/-MJH6c9UxgsW8ATMszhk)

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

![](/files/-MJH6c9VGrvMaeif1BM2)

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

![](/files/-MJH6c9WYjmEOyazxi0B)

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tech.banchengyun.com/archives/backend/hyperf/hyperf-xdebug.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
