Step-debugging with ddev and xdebug

Every ddev project is automatically configured with xdebug so that popular IDEs can do step-debugging of PHP code. It is disabled by default for performance reasons, so you'll need to enable it in your config.yaml.

xdebug is a server-side tool: It is installed automatically on the container and you do not need to install or configure it on your workstation.

All IDEs basically work the same: They listen on a port and react when they're contacted there. IDEs other than those listed here work fine, if they listen on the default xdebug port 9000.

Key facts:

  • Enable xdebug with ddev xdebug or ddev xdebug on. It will remain enabled until you start or restart the project.
  • Disable xdebug for better performance when not debugging with ddev xdebug off
  • ddev xdebug status will show current status.
  • The debug server port on the IDE must be set to port 9000, which is the default and is probably already set in most IDEs. (If you need to change the xdebug port due to a port conflict on your host computer, you can do it with a PHP override, explained below.)

For more background on XDebug see XDebug documentation. The intention here is that one won't have to understand XDebug to do debugging.

For each IDE the link to their documentation is provided, and the skeleton steps required are listed here.

Setup for Various IDEs

PHPStorm Debugging Setup

PHPStorm is a leading PHP development IDE with extensive built-in debugging support. It provides two different ways to do debugging. One requires very little effort in the PHPStorm IDE (they call it zero-configuration debugging) and the other requires you to set up a "run configuration", and is basically identical to the Netbeans or Eclipse setup.

Please note that PHPStorm 2018 and before are not compatible with current versions of XDebug.

PHPStorm Zero-Configuration Debugging

PHPStorm zero-configuration debugging means you only have to:

  1. Toggle the “Start Listening for PHP Debug Connections” button: Start listening for debug connections button
  2. Set a breakpoint.
  3. Visit a page that should stop in the breakpoint you set.

PHPStorm "Run/Debug configuration" Debugging

PHPStorm run/debug configurations require slightly more up-front work but can offer more flexibility and may be easier for some people.

  1. Under the "Run" menu select "Edit configurations"
  2. Click the "+" in the upper left and choose "PHP Web Application" to create a configuration. Give it a reasonable name.
  3. Create a "server" for the project. (Screenshot below)
  4. Add file mappings for the files on the server. Click on the local repo path and add "/var/www/html" as the "Absolute path on the server" and your repository root as the path on the host.
  5. Set an appropriate breakpoint.
  6. Start debugging by clicking the "debug" button, which will launch a page in your browser.

PHPStorm debug start

Server creation:

PHPStorm server creation

PHPStorm and Command-Line Debugging

If you need to debug command-line PHP processes, especially code that is outside the docroot (as in /vendor) there's a little extra work to be done:

  • If you have used PHPStorm with xdebug you already have a PHPStorm "server" ("Languages and Frameworks" -> "PHP" -> "Servers"). The key job of the "server" is to map filesystem locations on the workstation (your computer) to filesystem locations on the remote server (in this case the ddev-webserver container). Often, PHPStorm has automatically set up a mapping that doesn't include the entire project. So map the top-level directory of your project to /var/www/html in the container, as in this image: PHPStorm mapping
  • When debugging inside the container, you'll want to export PHP_IDE_CONFIG=<phpstorm_project_name>, for example, export for the server in the image above. You could also set PHP_IDE_CONFIG automatically with a docker-composer.environment.yaml as in example.
  • If debugging Drupal's drush command, you'll probably want to use direct access to the site-installed drush rather than using the built-in drush launcher, so run it as vendor/drush/drush/drush or whatever.

Atom Debugging Setup

Atom is an extensible developers' editor promoted by GitHub. The available extensions include php-debug which you can use to conduct PHP debugging with the Xdebug PHP extension. This project is currently an alpha release.

  1. Under Preferences->+Install install the php-debug add-on: php-debug installation
  2. Add configuration to the Atom config.cson by choosing "Config..." under the "Atom" menu. A "php-debug" stanza must be added, with file mappings that relate to your project. (Example config.cson snippet Atom cson config
  3. Open a project/folder and open a PHP file you'd like to debug.
  4. Set a breakpoint. (Right-click->PHP Debug->Toggle breakpoint)
  5. Open the debug view and enable debugging by choosing Packages->PHP-Debug->Toggle Debugging. You should see "Listening on address:port".
  6. Visit a page that should trigger your breakpoint.

An example configuration:

    PathMaps: [

Visual Studio Code (vscode) Debugging Setup

  1. Install the php-debug extension.
  2. Add to the launch.json the stanza defining "Listen for xdebug" (see config snippet)
  3. Set a breakpoint in your index.php. If it isn't solid red, restart.
  4. Click the vscode debug button: vscode debug button.
  5. Run the "Listen for XDebug" job: Listen for XDebug
  6. Go to a page in your project, you should hit your breakpoint.

Using xdebug on a Port Other than the Default

By default, ddev is set up to contact the default port, port 9000 on your IDE. However, if you have something else listening on that port, you'll need to change the port. (The most likely conflict is php-fpm, which also has 9000 as a default port.)

  • To override the port, add an override file in the project's .ddev/php directory. For example, a file .ddev/php/xdebug_remote_port.ini:
  • Then change your IDE's configuration to listen on the new port.