CentOS(Linux)下安装Xdebug

Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况,本文主要记录一下在centos(linux)下xdebug的安装和配置方法。

首先让php错误显示,只需要修改php.ini当中的2条指令,把 displayerrors和htmlerrors都设置为On,如下所示:

html_errors = On
display_errors = On

当然如果你要需要查看更多信息,比如说打印调用栈,哪就需要安装xdebug,这个对于比较复杂的代码系统特别有帮助。

xdebug是php的一个module,需要编译安装,我用lnmp安装的php,php被默认安装到/usr/local/php,然后做一个硬链接到/usr/bin。

(1)下载xdebug

xdebug 下载地址页:https://xdebug.org/download.php

wget https://xdebug.org/files/xdebug-2.4.0rc4.tgz

注意:xdebug的版本需与您的php版本相对应,由于我的php是5.6,所以这里下载是:xdebug2.4

(2)编译xdebug

然后开始编译

tar xzf xdebug-2.4.0rc4.tgz

cd xdebug-2.4.0RC4/

phpize

注意这里如果你的phpize没有加入到环境变量的话,需要带该文件的全路径

./configure --enable-xdebug --with-php-config=/usr/local/php/bin/php-config

这里的php-config参数要改为你实际的php-config地址额,不知道的话自己搜吧。

make

make test

make install

(4)配置php.ini

编译完成之后可以去php的安装目录通过find搜索一下是否生成了xdebug.so文件。如果生成成功,就可以直接配置php.ini文件了。具体配置代码如下:

[Xdebug]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
xdebug.profiler_enable=on
xdebug.trace_output_dir="/usr/local/php5/xdebug/"
xdebug.profiler_output_dir="/usr/local/php5/xdebug/"
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
;xdebug.remote_host=localhost
xdebug.remote_port=9999

重启php-fpm,可以通过phpinfo()检查你的xdebug是否安装成功,也可以随便写段错误的php代码,刷新浏览器,就能看到错误提示。


xdebug配置

1.在php.ini配置文件中添加如下内容
zend_extension="/wherever/you/put/it/xdebug.so"
2.如果是php5.3之前版本线程安全,添加如下内容代替上面1的内容
zend_extension_ts="/wherever/you/put/it/xdebug.so"
3.自从php5.3之后zend_extension_ts, zend_extension_debug不再使用

4.xdebug的一些其他配置

;显示错误信息xdebug.default_enable=1;函数调试xdebug.auto_trace=onxdebug.trace_output_dirxdebug.trace_output_name;Type:string,Defaultvalue:trace.%cxdebug.collect_params=1|3|4(参数长度,参数值,参数=值)xdebug.show_mem_delta=1显示内存xdebug.collect_return=1显示返回值xdebug.trace_options=1追加日志xdebug.collect_params=1xdebug.collect_vars=1;开启性能分析xdebug.profiler_enable=1;性能分析日志保存目录xdebug.profiler_output_dir=/data/logs/xdebug/;性能分析日志文件名称xdebug.profiler_output_name=cachegrind.out.log;默认是如下格式,t时间,p进程id;xdebug.profiler_output_name=cachegrind.out.%t.%p;代码覆盖率xdebug.coverage_enable=1;以下是远程调试配置xdebug.remote_host=127.0.0.1xdebug.remote_connect_back=1xdebug.remote_port=9000xdebug.remote_log="/data/logs/xdebug/xdebug.log"

4.重启php使配置生效


性能分析日志工具

1.linux平台

工具KCacheGrind (Linux, KDE)https://kcachegrind.github.io/

2.win平台查看工具WinCacheGrind

相关网址

http://ceefour.github.io/wincachegrind/https://sourceforge.net/projects/wincachegrind/https://github.com/ceefour/wincachegrind/releases/tag/1.1

列名称含义

Self - Shows the execution time of the code in the current block

Cum. - Shows the total exuction time of functions that the current function (Self) calls

Avg vs. Total: Average is average time per call, Total is the total time spend in all calls.

3.Web方式查看webgrind

https://github.com/jokkedk/webgrind


PHPSTORM 配置:

1.file->setings->php|Debug右侧。xdebug的那一块。 设置Debug port:9900(这里设置 的是,xdebug 吐出的debug信息,通过本机的什么端口传输。)

2.file->setings->php|Servers 右侧。 host: 你的web服务器的域名或ip ,端口, 下面的 use path mapping 意的是,你的项目的目录,对应服务器上的,什么目录? 这里一定要设置哦! 不然,会发生找不到文件而出错,导至调试终止。

3.Run->Edit Configurations-> 增加一个 PHP WEB APPlication 的调试点。 右侧: server 选择你上面建立的server. starturl 设置你的入口文件。

至此,配置完毕!

这样的请求,可以注册一个调试客户端哦!

http://www.aihuxi.com/****.php?XDEBUG_SESSION_START=19192

点击,小虫子图标,即可,开始调试!