DEV Community

Krixnaas
Krixnaas

Posted on • Updated on

Debug in PHP/Laravel Docker with xDebug

Install xDebug

sudo apt-get install php-xdebug;

or specify the version

sudo apt-get install php7.4-xdebug;

Initialize PHP with xDebug

In those same 2 directories named 7.4 and 8.0, you will find a php.ini file. Open them.

cd /etc/php/7.4

We need ton configure xdebug. Just add the following in the php.ini file:

[XDebug]
zend_extension = xdebug.so
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.discover_client_host = true
xdebug.idekey = VSC
xdebug.client_host = host.docker.internal
xdebug.client_port = 9003

Install PHP Debug Extension

Image description

Click on the “Extensions” icon (or Ctrl + Shift + X),
then use the search field to find the extension (type xdebug),
and finally install it (click on the little green “Install” button).

Configuration of a listener

launch.json

{
    // 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 on Docker App",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "pathMappings": {
                "/var/www/api": "${workspaceFolder}"
            },
            "hostname": "localhost",
            "xdebugSettings": {
                "max_data": 65535,
                "show_hidden": 1,
                "max_children": 100,
                "max_depth": 5
            },
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Note: pathMappings should be your project path.

Top comments (1)

Collapse
 
bearveloper profile image
Bearveloper 🐻

Setting xdebug.start_with_request = yes will slow your server. You don't need to be in debug mode ALL the time. My recommendation is to update it to trigger; this will have a side effect though, PHP will not start a debug session unless you specifically ask for it, so you will need a browser extension to toggle the debug mode on and off.

Aside from the trigger mode, I also recommend you to add an extra mode, develop to get better error messages.

xdebug.mode=debug,develop
xdebug.start_with_request=trigger
Enter fullscreen mode Exit fullscreen mode

Sources: