vscode 安装 php-debug 扩展

介绍

PHP debug(PHP调试)是指在PHP程序开发过程中,通过使用调试工具和技术来查找、定位和修复程序错误(bug)的过程。调试的目的是确保代码按预期工作,提高程序的稳定性和效率。

安装

1. 下载phpdebug扩展

查看自己的php版本下载对应的debug版本
我的环境是版本:8.3.6,nts,x64

根据自己的版本去这个下载xdebug,选择对应的系统和对应的版本。有windows版本和linux版本,我的环境是linux版本

本地系统是Ubuntu,使用apt-get安装

1
2
3
sudo apt-get install php-xdebug
# 查看是否安装成功
php -v

2. 配置php.ini

把php xdebug的配置信息添加到.ini文件中

  • 查看系统中的model扩展地址
    php --ini | grep xdebug

    打开这个.ini文件,把这个扩展复制到php.ini文件中就行了

  • php.ini问价增加配置参数

1
2
3
4
5
zend_extension=xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9000

放到最下面就行了

  • 重启fpm和nginx服务
1
2
systemctl restart php8.3-fpm.service
systemctl restart nginx.service
  • 查看端口是否开启服务
1
2
netstat -lnt | grep 9000

3. 安装vscode xdebug插件

  • 直接安装

  • 配置launch.json
    点击小瓢虫的图标,再点击齿轮图标就看到launch.json文件了

根据自己的环境修改对应的配置参数,根据自己的需要就行修改。
修改一下端口和地址,其他配置默认。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9000,
},
{
"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"
}
}
]
}
  • 调试效果