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.
- Enable xdebug with
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 statuswill 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:
- Toggle the “Start Listening for PHP Debug Connections” button:
- Set a breakpoint.
- 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.
- Under the "Run" menu select "Edit configurations"
- Click the "+" in the upper left and choose "PHP Web Application" to create a configuration. Give it a reasonable name.
- Create a "server" for the project. (Screenshot below)
- 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.
- Set an appropriate breakpoint.
- Start debugging by clicking the "debug" button, which will launch a page in your browser.
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:
- When debugging inside the container, you'll want to
export PHP_IDE_CONFIG=<phpstorm_project_name>, for example,
export PHP_IDE_CONFIG=d8composer.ddev.sitefor the server in the image above. You could also set PHP_IDE_CONFIG automatically with a
docker-composer.environment.yamlas 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
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.
- Under Preferences->+Install install the php-debug add-on:
- 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
- Open a project/folder and open a PHP file you'd like to debug.
- Set a breakpoint. (Right-click->PHP Debug->Toggle breakpoint)
- Open the debug view and enable debugging by choosing Packages->PHP-Debug->Toggle Debugging. You should see "Listening on address:port 127.0.0.1:9000".
- Visit a page that should trigger your breakpoint.
An example configuration:
"php-debug": PathMaps: [ "remotepath;localpath" "/var/www/html;/Users/rfay/workspace/d8git" ]
Visual Studio Code (vscode) Debugging Setup
- Install the php-debug extension.
- Add to the launch.json the stanza defining "Listen for xdebug" (see config snippet)
- Set a breakpoint in your index.php. If it isn't solid red, restart.
- Click the vscode debug button: .
- Run the "Listen for XDebug" job:
- 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.