对于简单的项目或仅仅想知道某一位置的某个变量是什么值,直接使用var_dump配置exit来打印和中断就能够了,方便又快捷,php
而对于大型项目的调试,或想了解某个系统的整个运行过程,xdebug可能会是更好的选择。html
网上大多数xdebug教程中的项目代码和运行环境是配置在本地,IDE也是在本地,web
而我所使用的环境是运行于远程服务器中,因此xdebug配置起来稍有不一样。json
环境介绍:
本地:win10 + vscode
远程:CentOS + LNMP + xdebug
服务器
即PHP的运行环境在远程服务器中,项目代码放在本地,使用nfs共享映射到虚拟机中运行。ssh
1.ssh到虚拟机,检查并安装php的xdebug扩展php-fpm
2.配置php.ini中的xdebugspa
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环境vagrant
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通讯时就是使用这个端口。
以上设置完毕后就能够开始断点调试!!!
参考连接: