在 Windows + Apache + MySQL + PHP (WAMP) 環境下配置 xdebug 進行效能分析,配合 WinCacheGrind 視覺化工具,能快速定位 PHP 程式瓶頸。本文記錄完整安裝與配置步驟(以 WAMP 2.5 / PHP 5.2.5 為例)。
php_xdebug.dll)。php_xdebug.dll 複製到 PHP 擴展目錄(例:D:\wamp\bin\php\php5.2.5\ext\)。php.ini,於末尾加入:[Xdebug]
zend_extension_ts="D:/wamp/bin/php/php5.2.5/ext/php_xdebug.dll"
; 必須用 zend_extension_ts (Thread Safe) 或 zend_extension (Non-TS),不可用 extension=
xdebug.auto_trace=On
xdebug.collect_params=On
xdebug.collect_return=On
xdebug.trace_output_dir="D:/wamp/bin/php/debuginfo"
xdebug.profiler_enable=On
xdebug.profiler_output_dir="D:/wamp/bin/php/debuginfo"
D:\wamp\bin\php\debuginfo 並確保 Apache 執行身分可寫入。| 設定 | 功能 |
|---|---|
zend_extension_ts | 載入 xdebug Zend 擴展(TS 版本必須用此指令) |
xdebug.auto_trace | 自動記錄函數呼叫追蹤(產生 trace.xxx.xt) |
xdebug.collect_params | 收集函數參數值 |
xdebug.collect_return | 收集函數回傳值 |
xdebug.trace_output_dir | 追蹤檔輸出目錄 |
xdebug.profiler_enable | 啟用效能分析器(產生 cachegrind.out.xxx) |
xdebug.profiler_output_dir | 分析檔輸出目錄 |
https://localhost:8080/demo/index.php)。D:\wamp\bin\php\debuginfo\,確認產生 cachegrind.out.xxxx 與 trace.xxxx.xt。cachegrind.out.xxxx。生產/開發環境建議改為觸發模式:
xdebug.profiler_enable=0
xdebug.profiler_enable_trigger=1
; 只有帶 ?XDEBUG_PROFILE=1 才會產生 profile
profiler_output_dir 權限、PHP 版本對應、zend_extension 路徑正確。auto_trace、collect_params、collect_return,僅啟用 profiler。配置完成後,即可針對慢速請求進行精準效能剖析,優化資料庫查詢、迴圈邏輯與函數呼叫結構。