PHP vscode+XDebug 遠程斷點調試服務器上的代碼


參考鏈接

https://www.cnblogs.com/ryanzheng/p/10575790.html

斷斷續續做php五年了,前期只在開發機器上debug,中期從不debug,有什么問題var_dump一下,現在遇到一個fpdf的問題,無奈必須debug服務器。

我只是記錄一下自己遇到的問題,基本完全按照原文思路來。

 

環境介紹:
本地:win7 + vscode
遠程:CentOS + Apache + PHP5.6 + xdebug

 

PHP的運行環境在遠程服務器中,項目代碼放在本地,使用nfs共享映射到虛擬機中運行。

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配置說明。

這里由於我使用的是寶塔,所有直接打出phpinfo

QQ截圖20190613002648

發現真的有好幾個不符合要求,於是將參考鏈接中的配置全部放了進去

2

3. 重啟php-fpm,或web環境

4.vscode中安裝插件”PHP Debug”

5.配置launch.json

{
    "name": "Listen for XDebug",
    "type": "php",
    "request": "launch",
    "stopOnEntry":false,
    "localSourceRoot": "Z://php_project/",
    "serverSourceRoot": "/home/ryan/php_project/",
    "port": 9000
},
{
    "name": "Launch currently open script",
    "type": "php",
    "request": "launch",
    "program": "${file}",
    "cwd": "${fileDirname}",
    "port": 9000
}

以上,其中”localSourceRoot”是項目代碼在本地的路徑,設置的值是當前工作區根目錄,也就是我項目根目錄。

”serverSourceRoot”是遠程虛擬機中的代碼路徑,”port”是本地IDE在debug時會監聽的端口,遠程xdebug與vscode通信時就是使用這個端口。

以上設置完畢后就可以開始斷點調試!!!

 

然后我試了是,並沒有卵用,肯定是哪里配置錯了呀,於是好吧,一個一個來,先本地debug,因為之前從沒在vscode,而是曾經在phpstrom中debug

3

zend_extension              = "E:\Develop\php-5.6.29\ext\php_xdebug-2.5.0-5.6-vc11-x86_64.dll"
;允許遠程IDE調試
xdebug.remote_enable        = true
;遠程主機
xdebug.remote_host          = 127.0.0.1
;xdebug.remote_port         = 9000 ;默認端口 9000
xdebug.profiler_enable      = on
;臨時跟蹤信息輸出
xdebug.trace_output_dir     = "E:\Develop\ApacheServer\xdebug\trace"
xdebug.profiler_output_dir  = "E:\Develop\ApacheServer\xdebug\profiler"
;其余參數
;開啟自動跟蹤。自動打開"監測函數調用過程"的功模。該功能可以在你指定的目錄中將函數調用的監測信息以文件的形式輸出
xdebug.auto_trace           = On
;開啟異常跟蹤
xdebug.show_exception_trace = On
;開啟遠程調試自動啟動
xdebug.remote_autostart     = On
;收集變量
xdebug.collect_vars         = On
;收集返回值
xdebug.collect_return       = On
;收集參數
xdebug.collect_params       = On
;顯示局部變量
xdebug.show_local_vars      = On
;顯示默認的錯誤信息
xdebug.default_enable       = On
;用於zend studio遠程調試的應用層通信協議
xdebug.remote_handler       = dbgp
;如果設得太小,函數中有遞歸調用自身次數太多時會報超過最大嵌套數錯
xdebug.max_nesting_level    = 10000
xdebug.idekey = PHPSTORM

博主vscode不熟,偶然間發現這個launch.json配置文件是全局的

4

於是終於能在本地debug了,debug界面好漂亮,超級簡潔

5

 

 

 

結果我在本地debug解決了問題,遠程debug還是不行,留着這個帖子下次搞吧,別罵我


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM