單步斷點、變量監視、調用棧回溯,是提升開發效率的利器。本文記錄 Windows + Apache + PHP 7.x + PhpStorm 2017+ + Xdebug 2.5+ 的完整配置流程,避開常見坑點。
https://xdebug.org/wizard.phpphp -i 或瀏覽 phpinfo(),將完整輸出複製到向導文本框php_xdebug-2.5.5-7.1-vc14-x86_64.dll)php.ini 配置片段php/ext 目錄[Xdebug] zend_extension="php_xdebug-2.5.5-7.1-vc14-x86_64.dll" xdebug.remote_enable=1 xdebug.remote_handler=dbgp xdebug.remote_host=localhost xdebug.remote_port=9000 xdebug.idekey=PHPSTORM xdebug.remote_autostart=1 ; 強制每請求嘗試連接 IDE,免去瀏覽器插件 xdebug.remote_connect_back=0 ; 若為 1 則自動連回 $_SERVER['REMOTE_ADDR'],Docker/遠程調試常用 xdebug.profiler_enable=0 ; 性能分析按需開啟 xdebug.profiler_output_dir="D:/tmp/xdebug_profiler"
重啟 Apache 後執行 php -m 確認 xdebug 出現在模組列表。
xdebug.idekey 一致)XDEBUG_SESSION=PHPSTORM Cookie/參數localhost | Host: localhost | Port: 80 | Debugger: XdebugD:/www/project)9000 | Can accept external connections: ✓PHPSTORM | Host: localhost | Port: 80| 現象 | 原因 | 解決 |
|---|---|---|
| 斷點不生效、提示「No path mapping」 | Server 路徑映射缺失或錯誤 | Servers 設定正確的 Path Mappings |
| PhpStorm 無彈窗、端口無連接 | 防火牆阻擋 9000 / xdebug.remote_host 錯誤 | 放行 9000;遠程調試設 xdebug.remote_connect_back=1 |
| 「Waiting for connection」超時 | xdebug.remote_autostart=0 且瀏覽器未帶 Session | 開啟 autostart 或確認 Helper 狀態 |
| 變量顯示「Cannot get property」 | Opcache 優化導致變量被優化掉 | 開發環境關閉 Opcache 或 opcache.optimization_level=0 |
| Docker/虛擬機調試不通 | 容器內 localhost ≠ 宿主機 | xdebug.remote_host=host.docker.internal (Mac/Win) 或宿主機 IP |
$user->id === 123Exception、Errorxdebug.profiler_enable=1 生成 cachegrind.out.*,用 PhpStorm → Tools → Analyze Xdebug Profiler Snapshot 或 KCacheGrind/QCacheGrind 可視化