PHP-Xdebug在PhpStorm中的使用

咱们在软件开发的时候,常常要进行调试,其中有一种调试方法就是断点,此时能够用PHP的Xdebug扩展来使用。php

1, 安装PHP-Xdebug扩展;ubuntu

sudo apt-get install php-xdebug

安装完成后,用 php -v的命令查看结果vim

php -v
PHP 5.6.30-12~ubuntu16.04.1+deb.sury.org+1 (cli) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
    with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans

能够看到已经有Xdebug v2.5.5安装好了.bash

2, 在php的配置文件中对xdebug扩展进行配置,这里有两种方式:session

a: 最直接的方式,是找到php.ini,在最后面加入如下配置项;dom

sudo vim /etc/php/5.6/fpm/php.ini
zend_extension=/usr/lib/php/20131226/xdebug.so
xdebug.remote_host=localhost
xdebug.remote_port=9100
xdebug.remote_enable=1
xdebug.remote_autostart=1

b: 另外一种是在Xdebug的配置文件中,加入相同的配置项;socket

咱们能够在/etc/php/5.6/fpm/conf.d中看到性能

20-xdebug.ini -> /etc/php/5.6/mods-available/xdebug.ini

能够进入这个文件中,看到这里已经讲zend_extension的路径添加进去了,将其余配置添加进去优化

zend_extension=xdebug.so

 

以上两种方法是等价的,由于php的配置文件会引入各个扩展的配置文件this

 

咱们逐项来看看这些配置分别表明什么意思

① xdebug.remote_host=localhost

Type: string, Default value: localhost

Selects the host where the debug client is running, you can either use a host name, IP address, or 'unix:///path/to/sock' for a Unix domain socket. This setting is ignored if xdebug.remote_connect_back is enabled.

Support for Unix domain sockets was introduced in Xdebug 2.6.

选择debug客户端在那个主机上运行,能够设置成一个host名称,IP地址,或者Unix域名的路径. 若是xdebug.remote_connect_back 被设置为 enabled时,此host设置将被忽略。

 

② xdebug.remote_port=9100

Type: integer, Default value: 9000

The port to which Xdebug tries to connect on the remote host. Port 9000 is the default for both the client and the bundled debugclient. As many clients use this port number, it is best to leave this setting unchanged.

这是Xdebug尝试链接的远程host的端口. 9000端口是客户端和随附的debug客户端的默认端口。因为不少客户端都使用这个端口号,这个配置一旦设好,最好保持不变。

 

③ xdebug.remote_enable=1
Type: boolean, Default value: 0
This switch controls whether Xdebug should try to contact a debug client which is listening on the host and port as set with the settings xdebug.remote_host and xdebug.remote_port. If a connection can not be established the script will just continue as if this setting was 0.

此开关控制着Xdebug是否应该尝试联系debug客户端,这个客户端正在监听配置好的host和port. 若是这项配置为0,则当没法链接时,脚本会啥都不作,继续执行。

 

④ xdebug.remote_autostart=1

Type: boolean, Default value: 0

Normally you need to use a specific HTTP GET/POST variable to start remote debugging (see Remote Debugging). When this setting is set to 1, Xdebug will always attempt to start a remote debugging session and try to connect to a client, even if the GET/POST/COOKIE variable was not present.

一般你须要使用一个特定的HTTP GET/POST 变量来启动远程bugging. 当此配置为1时, Xdebug将永远会试图远程debugging session,并尝试链接一个客户端,即使 GET/POST/COOKIE变量没有出现。

 

3, 重启php服务

sudo service php5.6-fpm restart

 

4, 在IDE中添加Debug的Host和Port,进入PhpStorm->File->Settings->Languages & Frameworks->Servers->+

保存后,此时能够在具体的代码文件页面看到debug的标识,

在这里单击一下,有一个红色的圆圈,表明在此处设置一个断点(break point)

用Postman请求到这个文件,能够看到在IDE下方,出现了不少信息

这些变量值直接展现出来了,而不要再另外去打断点来查看。

 

固然,以上只是xdebug很小的一部分功能。更重要的是用来作性能分析,代码逻辑优化等,这个过一段时间再来更新。

相关文章
相关标签/搜索