xdebug3+php8.0+vscode断点调试php程序
今天帮朋友弄一个php写的小系统。因为逻辑稍微复杂,于是肉眼实在是无法看懂逻辑。
于是掏出Xdebug这个php调试神器。
本地环境
我的机器环境是mac OS 11.4 + php8.0.7 + vscode
安装php
这里使用brew安装,如果是其他的Linux系统话的,使用对应的命令即可。
brew install php
安装后,通过nginx反向代理到php-fpm上。
server {
listen 9845;
server_name localhost;
location / {
root /Users/qingcheng/php;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /Users/qingcheng/php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
安装xdebug
很多老教程都是用直接下载dll或者so包的方式来安装xdebug。后面我发现php本身也提供了快速安装某个扩展包的工具。那就是pecl
使用pecl可以瞬间就安装好当前php程序版本对应的扩展包
pecl install xdebug
然后使用php -v
看到输出有xdebug字样,那就说明xdebug安装成功了。
vscode配置。
安装php-debug插件
在扩展商店直接安装即可。也可以在vscode中,按f1 输入 ext install php-debug
修改php.ini
在php.ini中加入配置项
[xdebug]
zend_extension="xdebug.so"
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003 #xdebug3 监听的端口
调试运行,进入断点
在vscode的debug选项中,添加配置。然后选择php
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 0,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "Launch Built-in web server",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:0"
],
"program": "",
"cwd": "${workspaceRoot}",
"port": 9003,
"serverReadyAction": {
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
}
}
]
}
然后按F5 运行。通过浏览器访问nginx端口,就可以看到进入断点了。
关注我
有需要一起交流学习的同学,可以关注我的公众号【青城同学】,只和你分享干货,一个0广告的公众号
也可以掏出手机扫一下二维码。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端