Configure Xdebug with Lando and VS Code

Configure Xdebug with Lando and VS Code

I am documenting this to avoid wasting time on this issue. You can find all the details from Lando's official documentation in the following link.

I have been using Ubuntu for the last six years or so. As you may have guessed, this tutorial will assume you are using the same system or something similar, like WSL in Windows.
To use Xdebug in Lando, as mentioned in the documentation, you must include this line under the configuration of your app server:

config:
    xdebug: true

In the documentation after this step, you have to set up your launch.json file in VS code.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for XDebug",
      "type": "php",
      "request": "launch",
      "port": 9003,
      "pathMappings": {
        "/app/": "${workspaceFolder}/",
      }
    }
  ]
}

Also, in the documentation, you have to run. lando rebuild -y So the changes take place in your Lando image. However, most of the time, you will run to issue regarding the listening port. This was mentioned at the end of the documentation under the known issues.

To fix it, you have to add a new rule to the iptables and allow the target port.

sudo iptables -A INPUT -p tcp -d 0/0 -s 0/0 --dport 9003 -j ACCEPT

You have to restart your Lando workspace to get things working.