對于簡單的項目或僅僅想知道某一位置的某個變量是什麼值,直接使用var_dump配置exit來列印和中斷就可以了,友善又快捷,而對于大型項目的調試,或想了解某個系統的整個運作過程,xdebug可能會是更好的選擇。
網上大多數xdebug教程中的項目代碼和運作環境是配置在本地,IDE也是在本地,而我所使用的環境是運作于虛拟機中,是以xdebug配置起來稍有不同。
環境介紹:
本地:win7 + vscode
遠端:ubuntu14 + lnmp + xdebug
虛拟機平台:virtualbox + vagrant
即PHP的運作環境在遠端虛拟機中,項目代碼放在本地,使用nfs共享映射到虛拟機中運作,虛拟機使用NAT方式聯網。
1.ssh到虛拟機,檢查并安裝php的xdebug擴充
2.配置php.ini中的xdebug
zend_extension=xdebug.so
[XDebug]
xdebug.remote_enable = on
xdebug.remote_autostart = 1
;xdebug.remote_host = 192.168.10.1
xdebug.remote_port = 9000
xdebug.remote_connect_back = 1
xdebug.auto_trace = 1
xdebug.collect_includes = 1
xdebug.collect_params = 1
xdebug.remote_log = /tmp/xdebug.log
“remote_enable”是允許遠端調試
“remote_autostart”遠端調試自動啟動?
“remote_host”是指定通過哪個IP進行遠端調試,也就是你IDE所在的IP(這裡是192.168.10.1即是我本地,但當下面remote_connect_back設定了時,這個IP設定無效,是以我注釋了),
“remote_port”是在vscode中設定的監聽端口,是本地的端口哦~ 即當開始調試時,xdebug将與這個端口通訊
“remote_connect_back”不知道是什麼意思,隻是如果開啟此,将忽略上面的 xdebug.remote_host 的設定
其它的可自行搜尋xdebug配置說明。
3. 重新開機php-fpm,或web環境
4.vscode中安裝插件”PHP Debug”
5.配置launch.json
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"stopOnEntry":false,
"localSourceRoot": "${workspaceRoot}",
"serverSourceRoot": "/home/wwwroot/weiphp.dev",
"port": 9000
},
{
"name": "Launch currently open script",
"program": "${file}",
"cwd": "${fileDirname}",
}
以上,其中”localSourceRoot”是項目代碼在本地的路徑,設定的值是目前工作區根目錄,也就是我項目根目錄。”serverSourceRoot”是遠端虛拟機中的代碼路徑,”port”是本地IDE在debug時會監聽的端口,遠端xdebug與vscode通信時就是使用這個端口。
好像主要就是第一部分,第二部分目前不需要
6.結束