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广告的公众号

也可以掏出手机扫一下二维码。

posted on 2021-06-15 09:12  快乐海盗  阅读(983)  评论(0编辑  收藏  举报