在phpStorm中使用xdebug调试

1 环境说明

在mac下搭建的lnmp环境,可以参考:
Mac搭建lnmp环境
http://blog.csdn.net/alex_my/article/details/53818143

nginx中的网站配置:

fastcgi_pass   127.0.0.1:9000

环境均使用brew安装,其中xdebug被安装到:

/usr/local/opt/php56-xdebug/xdebug.so

php的配置中也有指向

/usr/local/etc/php/5.6/conf.d/ext-xdebug.ini

2 配置php.ini

/usr/local/etc/php/5.6/php.ini

[xdebug]
xdebug.remote_enable =1 
xdebug.remote_handler = "dbgp" 
xdebug.remote_host = "localhost"        # 调试的IDE所在的地址
xdebug.remote_port = 9001               # 调试的IDE所用的端口
xdebug.remote_mode = "req" 
xdebug.idekey="PHPSTORM"

xdebug.remote_mode:

req: 在PHP程序开始执行的时候,xdebug与IDE建立连接
jit: 在PHP程序执行到断点处或者遇到Error的时候,xdebug才与IDE建立连接

需要注意的是,这里不要再添加以下配置,会出现警告: 已经加载了xdebug.so

zend_extension="/usr/local/opt/php56-xdebug/xdebug.so" 

还有一个重要的是,如果你用的是nginx,并且是默认配置,一般9000端口都是被使用的。
按照网上其它教程做而xdebug无法断点的原因就是使用了以下配置:

xdebug.remote_port = 9000

重启php-fpm

killall php-fpm
php-fpm -D

3 配置phpStorm

打开 phpstorm–Preferences–Languages & Frameworks – PHP
点击Debug, 填写以下内容

Xdebug -- Debug port: 9001 # 和php.ini中的xdebug.remote_port保持一致

打开Debug–DBGp Proxy填写以下内容

IDE key: phpStorm
Host: localhost         # 要调试的网站地址, 如127.0.0.1, site.com
Port: 80                # 要调试的网站端口
  • 打开网站工程,IDE右上角,点击Edit Configurations..
  • 点击弹出框左侧的+号。
  • 选择PHP Web Application
  • 此时左侧多了一列PHP Web Application – Unnamed (改名为start)
  • 在右侧 – Configuration – Server 右侧的 …
  • 在弹出框Servers左侧点击+号,填写以下内容
Name: start2        # 随意名称
Host: localhost     # 网站地址,与Debug--DBGp Proxy相同
Port: 80            # 网站端口,与Debug--DBGp Proxy相同
Debugger: Xdebug

一些就绪后,在IDE的右上侧,绿色三角形右侧,有一个臭虫按钮,打好断点,就可以点击使用了

4 xdebug工作原理说明

  • IDE中安装了一个遵循BGDp协议的Xdebug插件, 称为xdebug-a
  • 调试模式下,IDE中的xdebug-a创建服务,监听端口: 9001(在phpStorm中设置的)
  • IDE在当前url后面加上了XDEBUG_SESSION_START参数
  • php服务器中的xdebug模块,称为xdebug-b, 接收到带有XDEBUG_SESSION_START的请求后,会进入到调试模式
  • xdebug-b会以协议(BGDp)向xdebug-a的服务建立连接,提供调试服务。
  • php.ini中配置的xdebug.remote_host:xdebug.remote_port是xdebug-a的地址和端口

    xdebug-a创建服务时,这个端口不能被其它进程占用了。

TOP

发布了118 篇原创文章 · 获赞 68 · 访问量 57万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章