Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save artemrogov/c103e3410bf6686caf0ade9c8b02dacf to your computer and use it in GitHub Desktop.
Save artemrogov/c103e3410bf6686caf0ade9c8b02dacf to your computer and use it in GitHub Desktop.
Directions for debugging using Xdebug, VSCode, and Homestead.

How to Configure Visual Studio Code to Debug PHPUnit Tests with Xdebug on Laravel Homestead

Homestead Setup

  1. SSH into your homestead machine:
homestead ssh
  1. cd into the project directory

  2. Enable Xdebug:

xon
  1. Locate the xdebug.ini file that is loaded:
php --ini | grep 'xdebug'

My output shows the ini file at /etc/php/7.1/cli/conf.d/20-xdebug.ini

  1. Find your guest machine's gateway. This will be the IP Address that your guest machine can use to communicate with your host machine.
netstat -rn | grep "^0.0.0.0 " | cut -d " " -f10

The output I see on my Homestead machine is 10.0.2.2

  1. Configure Xdebug to use the gateway as the xdebug.remote_host value:
sudo vim /etc/php/7.1/cli/conf.d/20-xdebug.ini

xdebug.ini

zend_extension=xdebug.so
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host=10.0.2.2
  1. Restart the PHP-FPM service
sudo service php7.1-fpm restart

VSCode Setup

  1. Install the PHP Debug plugin.
  2. Open the debug panel.
  3. Click on the "config" button (the cogwheel) and select PHP.
  4. Modify the default launch.json file:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000
        },
        {
            "name": "Listen for XDebug on Homestead",
            "type": "php",
            "request": "launch",
            "serverSourceRoot": "/home/vagrant/Code/tighten-app-homestead",
            "localSourceRoot": "/Users/jose/Code/tighten-app-homestead",
            "port": 9000
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

Add a new configuration object (as shown above) that is the same as Listen for Xdebug, but add the key/values for serverSourceRoot and localSourceRoot.

serverSourceRoot is the absolute path to your project root as though you were logged into your Homestead machine.

localSourceRoot is the absolute path to your project root on your local machine.

  1. Select "Listen for XDebug on Homestead" on the debug drop-down menu.

  2. Start Debugging by clicking on the green arrow button.

Debug!

You're now ready to run your test from within your Homestead machine:

phpunit --filter some_test_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment