发布于 

PHP调试工具-Xdebug

刚刚过完了三天的小长假,假期没事的时候整理了一下Xdebug的使用. 在测试服务器上装上也方便大家调试,提高大家的编码效率.

先解释下xdebug等工作原理:

进行xdebug调试,我们需要xdebug客户端和xdebug服务端,编译到php的就是服务端,客户端一般需要我们自己安装(像sublime text),或者一般的IDE都已经集成.

当我们进行xdebug调试时,首先客户端会监听一个端口,然后等待xdebug服务端连接,连接成功则进行通信.如下图所示:

原理图

本机的IP(也就是IDE的IP)是10.0.1.42,监听的是本机的9000端口;
服务器的IP是10.0.1.2,连接的是80端口(web服务器默认端口);

所以在php.ini里我们的配置应该是这样的:

1
2
xdebug.remote_host=10.0.1.42
xdebug.remote_port=9000

(PS:如果本机开着php并运行在9000端口上,应该换一个端口)

其中remote_host 就是xdebug服务器回调客户端的IP,也就是你进行xdebug调试的机器的IP

但是当我们有很多人进行远程调试的时候,这个remote_host不能动态变化,这样就没法满足所有人的调试需求了?

解决方法:在xdebug服务端有一个配置xdebug.remote_connect_back,这个值一旦设定为1就可以解决IP不是remote_host中值无法调试的问题,xdebug服务端可以从http请求的头部$_SERVER['REMOTE_ADDR']获取远程客户端的IP地址,如下图:

原理图

  • 好了,讲完原理,我们看下怎么编译服务端的:

从官网wget编译文件,解压安装包

1
2
[root@web4 software]# tar -zvxf xdebug-2.3.3.tgz
[root@web4 software]# cd xdebug-2.3.3

其中 README 中有关于安装的详细步骤,我的php安装路径为:/data/app/php

1
2
3
[root@web4 xdebug-2.3.3]# /data/app/php/bin/phpize
[root@web4 xdebug-2.3.3]# ./configure --enable-xdebug --with-php-config=/data/app/php/bin/php-config
make && make install

配置php.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[xdebug]
zend_extension="xdebug.so"
xdebug.auto_trace=true
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_handler=dbgp
xdebug.remote_host=192.168.0.116
xdebug.remote_connect_back=1
xdebug.remote_port=9090
xdebug.idekey=web
xdebug.remote_log="/data/app/php/var/log/xdebug/xdebug.log"
xdebug.profiler_enable=1
xdebug.profiler_output_dir="/data/app/php/var/log/xdebug/xdebug-profiler"
;xdebug.dump_once = On
;xdebug.dump_globals = On
;xdebug.dump_undefined = On
;xdebug.dump.SERVER = REQUEST_METHOD,REQUEST_URI,HTTP_USER_AGENT
;xdebug.dump.REQUEST=*
;xdebug.show_exception_trace = On
;xdebug.show_local_vars = 1
;xdebug.var_display_max_depth = 6

  • 现在用sublime text3来说下怎么配置客户端

安装 package Xdebug Client

新建一个 project 并且执行 项目=>项目另存为,并且已经成功设置 sftp

编辑存储后的project文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"folders":
[
{
"path": "/MyLocalProjectPath/xdebug"
}
],
"settings": {
"xdebug": {
"url": "http://RemoteAddr/xdebug/index.php",
"path_mapping": {
"/MyLocalProjectPath/xdebug/" : "/RemoteProjectLocation/xdebug/"
},
"ide_key": "web",
"super_globals": true,
"close_on_stop": true,
"port": 9090,
"debug": true
}
}

}

新加入的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
"settings": {
"xdebug": {
"url": "http://RemoteAddr/xdebug/index.php",
"path_mapping": {
"/MyLocalProjectPath/xdebug/" : "/RemoteProjectLocation/xdebug/"
},
"ide_key": "web",
"super_globals": true,
"close_on_stop": true,
"port": 9090,
"debug": true
}
}

如果是在本机进行调试,可以这样设置:

1
2
3
4
5
"settings": {
"xdebug": {
"url": "http://localhost/xdebug"
}
}

记得重启php-fpm

配置完毕!可以设置断点调试了~


更新:

sublime text不支持多人调试的模式,经测试发现设置了remote_connect_back=1并注释掉remote_host后,sublime text无法与xdebug服务器进行通信,后来发现是需要 DBGp代理的.

一般的IDE是集成的,并且sublime text xdebug界面也不是很友好,如果需要debug的时候感觉phpstorm比较顺手.

对于 调试 Zendframework 也有了比较好的方法,由于Zendframework是做过路由的,因此没法进行 map 对应,之前看过一篇文章说的是在debug的时候做路由映射,我可能比较懒吧,想到了另一种方法:

我们只设置一个 debug 文件,需要对 某个action进行debug的时候只需要引入这个controller,new 一个 controller 类,然后调用对应的action即可,经过测试,在action里的变量可以在debug文件中进行debug时完全可以捕获!

[参考资料]

  1. 官方文档

  2. SublimeTextXdebug

  3. PhpStorm 9.0.0 Help/Configuring Xdebug

  4. 使用netbeans进行PHP团队开发和基于xdebug进行多人远程调试

  5. Debugging with Xdebug and Sublime Text 3