本文介紹Mac系統下Xampp+VSCode+xdebug 進行php調試
篇幅較長,閱讀需要較長時間,如有疑問請下方留言
下載安裝Xampp
找到 XAMPP for OS X 7.1.29, 7.2.18, 7.3.5, 7.1.29, 7.2.18 & 7.3.5 這個部分
選擇 More Downloads
選擇 XAMPP Mac OS X
根據需要 選擇 PHP 版本 我這里選擇7.1.23
注意
這里要注意 不要選后面帶VM的版本,這個版本是在Mac上運行Linux版本的Xampp虛擬機(雖然官方推薦但是在安裝xdebug時我是裝不成功的)
OS X native modules have been deprecated. Please use XAMPP-VM for OS X and the Linux version of the add-ons. Check our FAQs for more info.
生成xdebug.so文件
獲取量身定制的xdebug.so生成步驟
此頁面可幫助您查找要下載的文件,以及如何配置PHP以使Xdebug運行。請粘貼phpinfo()的完整輸出(HTML版本,HTML源代碼或php -i輸出的復制和粘貼)并提交表單以接收定制的下載和安裝說明。
打開先前安裝好的 Xampp
選擇 Manage Servers 點擊下面的 Start All 等待紅色圓圈變為綠色圓圈后,即啟動成功
選擇 Welcome 右下角點擊 Go to Application 會打開網頁
網頁打開后 選擇網頁菜單欄的 PHPInfo
打開 PHPInfo 頁面后 按command + a 全選網頁并復制
打開 點我去官網生成一個xdebug.so 將剛才復制的內容黏貼到文本框中,并點擊 Analyse my phpinfo() output 按鈕
之后將會顯示出您本地的PHP信息以及專門問您定制的xdebug安裝步驟(每個人顯示的可能不一樣,請按照網站顯示步驟進行安裝)
生成xdebug.so文件
下載 xdebug-2.7.2.tgz (xdebug版本與php版本相關聯,不可亂安裝)
xdebug版本對應的PHP版本,僅做參考,該鏈接下載的是Windows版本的
打開命令行工具 cd到xdebug下載目錄 解壓剛才下載的xdebug
tar -xvzf xdebug-2.7.2.tgz
cd 到 xdebug目錄 這里的xdebug-2.7.2 不同版本,版本號會不一樣
cd xdebug-2.7.2
在命令行輸入 phpize (這里的phpize是XAMPP帶的)
/Applications/XAMPP/xamppfiles/bin/phpize
命令行輸入 ./configure
./configure --with-php-config=/Applications/XAMPP/xamppfiles/bin/php-config
** 特別注意 **
這里要指定 Xampp 帶的 php-config 否則最后生成的 xdebug.so 無法在 PHP7.1.23 使用!!!!
命令行輸入 make
make
這里出現了 ‘php.h’ file not found 的錯誤,如果沒遇到請忽視這里
/Users/mac/Downloads/xdebug-2.7.2/xdebug.c:25:10: fatal error: 'php.h' file not found
#include "php.h"
解決辦法:
命令行輸入 xcode-select –install 如果提示沒有命令,請安裝 Xcode
xcode-select --install
如果提示以下信息,不用管
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
接著命令行輸入
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
當看到 The install was successful. 即成功
參考資料:
https://stackoverflow.com/questions/52623108/installing-xdebug-on-macos-mojave-php-h-file-not-found
接著重復第6步,命令行輸入 make
make
當看到以下內容即 xdebug 編譯成功,我們可以在剛才解壓的 xdebug 目錄的 modules 文件夾里看到 xdebug.so 文件
Build complete.
Don't forget to run 'make test'.
注意:
1、我的系統版本是10.14的所以才有 macOS_SDK_headers_for_macOS_10.14.pkg 這個包
具體叫什么名字請去以下目錄中查看
/Library/Developer/CommandLineTools/Packages/
2、-target / -target 后面有個空格接著才是 /
將 xdebug.so 拷到 /Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20180731 目錄下
no-debug-non-zts-20180731 這個目錄不同版本的 XAMPP 會不一樣,請注意
cp modules/xdebug.so /Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20180731
這時可能命令行會提示 Permission denied 錯誤
使用 sudo 命令即可
sudo cp modules/xdebug.so /Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20180731
接著 編輯 php.ini 文件 路徑如下(建議使用 Sublime Text 等工具打開)
/Applications/XAMPP/xamppfiles/etc/php.ini
在最底部添加
[xdebug]
zend_extension=/Applications/XAMPP/xamppfiles/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so
xdebug.remote_autostart=on
xdebug.remote_enable=on
xdebug.remote_mode="req"
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
xdebug.idekey="PhpStorm"
xdebug.profiler_enable = Off
接著重啟 Xampp 服務
VSCODE 配置 xdebug
打開 VSCODE 的插件安裝頁面
- 安裝 PHP Intelephense
-
安裝 PHP Debug
image.png
最后斷點就能調試代碼
注意: 文檔路徑要放在apache的應用根目錄,通過訪問本地localhost:80來進行觸發
錯誤解決方案
1、執行命令 /Applications/XAMPP/xamppfiles/bin/phpize 報 Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script. 錯誤的解決辦法
安裝 autoconf
brew install autoconf