还记得刚学PHP的时候使用var_dump()
、echo
、die()
、exit()
等函数进行断点调试,这种方法不但效率低、不直观、并且很难发现错误出现的具体位置。自从使用了PHPStorm这款IDE后,开始使用Xdebug进行调试,效率很是高。我在网上没有找到比较适合我开发环境的Xdebug配置教程,因而将具体的配置写成了一篇博客,但愿能帮到你们。php
PHP:5.5
Xdebug:2.2.3
PHPStorm:2016.2.2
Vagrant:1.8.6
OS:Ubuntu 14.04 LTSubuntu
首先须要检查Xdebug是否已经安装,若是已安装则跳过此步骤。方法很是简单,能够直接打开phpinfo()页面,搜索xdebug
。若是搜索结果以下图的话,说明已经安装了Xdebug:浏览器
在Ubuntu中安装PHP的扩展很简单,只须要一个命令:服务器
sudo apt-get install php5-xdebug
安装成功命令行会有以下提示:app
Reading package lists... Done Building dependency tree Reading state information... Done The following package was automatically installed and is no longer required: libaio1 Use 'apt-get autoremove' to remove it. The following NEW packages will be installed: php5-xdebug 0 upgraded, 1 newly installed, 0 to remove and 66 not upgraded. Need to get 253 kB of archives. After this operation, 982 kB of additional disk space will be used. Get:1 http://cn.archive.ubuntu.com/ubuntu/ trusty/universe php5-xdebug amd64 2.2.3-2build1 [253 kB] Fetched 253 kB in 2s (101 kB/s) Selecting previously unselected package php5-xdebug. (Reading database ... 69439 files and directories currently installed.) Preparing to unpack .../php5-xdebug_2.2.3-2build1_amd64.deb ... Unpacking php5-xdebug (2.2.3-2build1) ... Setting up php5-xdebug (2.2.3-2build1) ... php5_invoke: Enable module xdebug for fpm SAPI php5_invoke: Enable module xdebug for cli SAPI
这样,咱们就安装好了Xdebug。此时安装程序应该自动帮咱们生成了Xdebug的配置文件,pathinfo()中也应该能够搜索到Xdebug的结果。ide
咱们在pathinfo()里能够看到xdebug的配置路径:函数
个人Xdebug配置文件路径在“/etc/php5/fpm/conf.d/20-xdebug.ini”。若是你的pathinfo()中的Additional .ini
没有任何配置的话,那么你的Xdebug配置应该在php.ini中。ui
接着,编辑Xdebug配置文件,在“zend_extension=xdebug.so”下面添加一些配置,最终以下:this
zend_extension=xdebug.so xdebug.idekey = "vagrant" xdebug.default_enable = 1 xdebug.remote_connect_back = 1 xdebug.remote_port = 9001 xdebug.remote_enable = 1 xdebug.remote_autostart = 1 xdebug.remote_handler="dbgp"
到此,Xdebug配置部分已经好了。spa
在Languages & Frameworks -> PHP -> Debug 中找到配置,Debug port
改为Xdebug配置文件中的xdebug.remote_port
同样的端口号:
在Languages & Frameworks -> PHP -> Debug -> DBGp Proxy,IDE key
改为Xdebug配置文件中对应的xdebug.idekey
。
在Languages & Frameworks -> PHP -> Servers 中找到配置,点击+
添加一个Server,配置相似下图:
根据本身开发环境修改配置:
Name
是Server名称,能够随便填写;Host
为你调试项目是访问的IP
或者域名
.
192.168.1.102
。www.abc.com
;Port
为你Web服务器的端口,通常为80
。
除此以外还须要配置项目路径的映射(path mapping),将项目的根目录
以及public目录
映射到Vagrant服务器中的绝对路径。
在菜单中找到 Run -> Edit Configurations,点击窗口左上角的+
添加PHP Web Application
(2018.2以上版本选择PHP Web Page
):
根据本身开发环境修改配置:
Name
是配置名称,能够随便填写;Server
选择刚添加的那个Server;Start URL
无需改变;Browser
也无需改变。
全部步骤都配置完后,咱们能够点击菜单中的 Run -> Start Listening for PHP Debug Connections 进行调试监听。
这个操做能够用IDE右上角的图标代替:
这时咱们在代码中添加断点,而后在浏览器中访问,调试效果以下:
经过Debug咱们能够清晰的看到各参数、对象的值,还能够深刻的跟踪,很是方便。本教程到此就结束了,但愿帮到了你们。
若是在添加Server时未配置映射,断点调试时会提示以下错误:
解决方法就是按照添加一个Server
步骤配置Mapping。