在phpStorm中使用xdebug除錯

魚兒小貓發表於2017-01-11

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


相關文章