1. 程式人生 > 其它 >xdebug3+php8.0+vscode斷點除錯php程式

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廣告的公眾號

也可以掏出手機掃一下二維碼。