Xdebug是一個開放源代碼的PHP程序調(diào)試器(即一個Debug工具),可以用來跟蹤,調(diào)試和分析PHP程序的運行狀況,本文主要記錄一下在centos(linux)下xdebug的安裝和配置方法。
專注于為中小企業(yè)提供網(wǎng)站制作、網(wǎng)站設(shè)計服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)銅官免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
首先讓php錯誤顯示,只需要修改php.ini當(dāng)中的2條指令,把 displayerrors和htmlerrors都設(shè)置為On,如下所示:
html_errors = On
display_errors = On
當(dāng)然如果你要需要查看更多信息,比如說打印調(diào)用棧,哪就需要安裝xdebug,這個對于比較復(fù)雜的代碼系統(tǒng)特別有幫助。
xdebug是php的一個module,需要編譯安裝,我用lnmp安裝的php,php被默認(rèn)安裝到/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版本相對應(yīng),由于我的php是5.6,所以這里下載是:xdebug2.4
(2)編譯xdebug
然后開始編譯
tar xzf xdebug-2.4.0rc4.tgz
cd xdebug-2.4.0RC4/
phpize
注意這里如果你的phpize沒有加入到環(huán)境變量的話,需要帶該文件的全路徑
./configure --enable-xdebug --with-php-config=/usr/local/php/bin/php-config
這里的php-config參數(shù)要改為你實際的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代碼,刷新瀏覽器,就能看到錯誤提示。
1.在php.ini配置文件中添加如下內(nèi)容zend_extension="/wherever/you/put/it/xdebug.so"
2.如果是php5.3之前版本線程安全,添加如下內(nèi)容代替上面1的內(nèi)容zend_extension_ts="/wherever/you/put/it/xdebug.so"
3.自從php5.3之后zend_extension_ts, zend_extension_debug不再使用
4.xdebug的一些其他配置
;顯示錯誤信息 xdebug.default_enable = 1;函數(shù)調(diào)試 xdebug.auto_trace=on xdebug.trace_output_dir xdebug.trace_output_name ;Type: string, Default value: trace.%c xdebug.collect_params = 1|3|4 (參數(shù)長度,參數(shù)值,參數(shù)=值) xdebug.show_mem_delta=1 顯示內(nèi)存 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;默認(rèn)是如下格式,t時間,p進(jìn)程id ;xdebug.profiler_output_name = cachegrind.out.%t.%p ;代碼覆蓋率 xdebug.coverage_enable = 1;以下是遠(yuǎn)程調(diào)試配置 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
相關(guān)網(wǎng)址
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右側(cè)。xdebug的那一塊。 設(shè)置Debug port:9900(這里設(shè)置 的是,xdebug 吐出的debug信息,通過本機的什么端口傳輸。)
2.file->setings->php|Servers 右側(cè)。 host: 你的web服務(wù)器的域名或ip ,端口, 下面的 use path mapping 意的是,你的項目的目錄,對應(yīng)服務(wù)器上的,什么目錄? 這里一定要設(shè)置哦! 不然,會發(fā)生找不到文件而出錯,導(dǎo)至調(diào)試終止。
3.Run->Edit Configurations-> 增加一個 PHP WEB APPlication 的調(diào)試點。 右側(cè): server 選擇你上面建立的server. starturl 設(shè)置你的入口文件。
至此,配置完畢!
這樣的請求,可以注冊一個調(diào)試客戶端哦!
http://www.aihuxi.com/****.php?XDEBUG_SESSION_START=19192
點擊,小蟲子圖標(biāo),即可,開始調(diào)試!