首先推薦三個在vscode上開發PHP的插件
PHPDebug 用於調試php,這里主要指打斷點、F5等操作。xdebug是跟蹤、調試和分析PHP程序的運行狀況
PHP IntelliSense 是php的函數智能提示功能
php cs fixer 可以對代碼進行格式化,支持PSR規范
下載XDebug https://xdebug.org/download.php
windows上
下載xdebug要結合php的版本、32/64位 、nts/非nts、VC14/VC15
把xdebug.dll放到php下的ext目錄
pnp.ini
[XDebug]
zend_extension=php_xdebug.dll
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_handler = dbgp
xdebug.remote_host= 127.0.0.1
xdebug.remote_port = 9001
vscode中
setting.json 配置php的執行路徑
"php.validate.executablePath": "D:/phpstudy_pro/Extensions/php/php7.3.4nts/php.exe"
launch.json 配置xdebug port端口即為xdebug設置的端口
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9001
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9001
}
]
到此即可在windows上使用vscode加斷點的方式調試php代碼
linux上
使用vscode的Remote-SSH 插件遠程打開linux上的文件,然后在linux的php上安裝xdebug設置端口,即可使用vscode遠程打斷點的方式調試php代碼
首先在vscode上安裝Remote-SSH 插件
配置ssh-config
Host 192.168.xx.xx
HostName 192.168.xx.xx
User xuebing
Port 10088
#配置下面兩項可免密登錄
#PreferredAuthentications publickey
#IdentityFile ~/.ssh/id_rsa
關於免密登錄,需要把本地的id_rsa.pub發送到遠程服務器上 ssh-copy-id -i ~/.ssh/id_rsa.pub <romte_ip>
在linux上安裝xdebug,這個需要下載編譯
wget https://xdebug.org/files/xdebug-2.5.5.tgz tar -zxvf xdebug-2.5.5.tgz cd xdebug-2.5.5 phpize ./configure --with-php-config=/usr/local/php7/bin/php-config make make install
然后配置php.ini
zend_extension="xdebug.so"
xdebug.remote_port= 8888
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_handler = dbgp
xdebug.remote_host= 192.168.x.x
xdebug.remote_port = 8888
remote_host就是你的linux服務器的ip
vscode同windows
launch.json
port改成當前linux上xdebug監聽的port即可
setting.json
配置php的執行路徑為當前linux服務器上的php可執行文件的路徑
寫在最后:在開發時遇到一個項目需要去調用另一個項目,即業務層調api層這樣的,windows上需要在配置nginx的時候,用不同的fast-cgi端口,因此往往需要提前啟動一個fast-cgi端口(php-cgi.exe -b 127.0.0.1:9002),很麻煩。linux有fpm因此比windows上方便很多,如果配了xdebug,又用了php-fpm,你就可以調式所有訪問php的請求了