A quick guide to how to do this, since I always forget and have to look it up. This is done with docker4drupal, but should work equally well with any Docker Compose setup.
XDebug Remote Debugging So the idea is to write PHP code in PHPStorm on Windows and run and debug PHP code on Ubuntu over WSL2. To do that, we need to setup remote debugging for XDebug so that PHPStorm can connect to it and set breakpoints, show variable values and step through the code. Xdebug is a PHP extension which provides debugging, profiling, code coverage, stack traces and many other capabilities. In this succinct tutorial, I will show us how to set up Xdebug that ships with XAMPP in PhpStorm. See the steps below. Open up php.ini (located at C:path-to-xamppphp) for editing. How do you map an incoming xdebug connection when your directory structure is split between a public folder which is the entry point for CodeIgniter and the rest of the CodeIgniter framework code in the server folder? It looks like this: I was able to successfully create and run a docker deployment in PhpStorm: Deploying 'Compose: docker'.
- Phpxdebug: 1 phpxdebugdefaultenable: 1 phpxdebugremoteconnectback: 0 phpideconfig: servername=phpstorm phpxdebugidekey: 'phpstorm' phpxdebugremotehost: 172.17.0.1 # linux If you enable these after you've created the PHP container, remember to restart the container so that xdebug is enabled and configured.
- Create an xdebug.ini file (must end by.ini): # Navigate to the Devilbox git directory host cd path/to/devilbox # Navigate to PHP 5.6 ini configuration directory host cd cfg/php-ini-5.6/ # Create and open debug.ini file host vi xdebug.ini.
Enable xdebug on the relevant Docker container. For docker4drupal this means uncommenting these lines in the docker-compose.yml
file:
If you enable these after you've created the PHP container, remember to restart the container so that xdebug is enabled and configured.
Now go to Settings > Languages & Frameworks > PHP and set your language level as required. Add a new CLI interpreter by clicking ...
. Click the +
on the top left and add a new Docker configuration. Select Docker Compose and the php service, then use the default options for everything else. Save this and apply.
Next click the 'Add configuration' button on the taskbar in the top right. Pick the PHP Remote Debug template and click the +
on the top left to add a new configuration (don't make the mistake of editing the template). Check 'Filter debug connection by IDE key' and set the IDE key to match the one in your XDebug config (for the above we'd use PHPSTORM
). Add a server (the name doesn't matter). Give it the appropriate host and port—for docker4drupal the host will be something like http://myproject.docker.drupal.localhost, and the port will be 8000 unless you've changed it. Check 'Use path mappings', and set a path mapping for the root directory to /var/www/html
(or wherever it's mounted to on the container).
It's important to note that for this to work with Drush, or anything that exists both in Composer and globally, you need to invoke Drush from the binary in the vendor directory vendor/bin/drush
rather than using the system Drush, so that PHPStorm can use the path mappings. You could do this with a make command or Composer script. A make command I've used in the past:
This is a modified version of the default docker4drupal command.
ddev
The process for ddev is largely the same. You can skip the step for setting up the CLI interpreter as this will work fine without it. An important point is that the name of your debug configuration must match your ddev site domain for this to work. See the ddev documentation for more details.
This section provides a handful selection of tutorials that will help you get started with PHP debugging in PhpStorm.
Quick Start
Before proceeding with any of the debugging scenarios, you need to choose a debugging engine and install it in your PHP environment. These tutorials will guide you through the installation process:
With the debugging engine installed, you can start debugging by following the zero-configuration debugging approach:
Choose and install the browser extension suitable for your browser.
On the PhpStorm toolbar, toggle to start listening for incoming PHP debug connections, or choose Run | Start Listening for PHP Debug Connections from the main menu.
Set a breakpoint in your code. Breakpoints can be set in the PHP context inside PHP, HTML, TWIG, BLADE, and files of other types. Line breakpoints can be set only on executable lines, but not on comments, declarations, or empty lines. For details on getting started with Twig and Blade debugging, refer to Debug Twig templates and Debug Blade templates.
Start the debugging session in the browser using the installed browser extension.
During a debugging session, examine the program state: see variable values, evaluate expressions, step through the program, and so on.
See Zero-configuration debugging for the detailed step-by-step instructions, and Advanced debugging scenarios for more debugging scenarios.
Debugging a PHP web application
Depending on your environment, you can debug your PHP Web application locally or remotely.
Local debugging | Remote debugging |
---|---|
The debugging engine and PhpStorm are running on the same machine. | The debugging engine is running on a remote environment (for example, a remote server or a virtual machine). |
Follow the Zero-configuration debugging procedure. |
|
To test direct connection, run the telnet host 9000
(for Xdebug) or the telnet host 10137
(for Zend Debugger) command on the remote server and ensure that connection is established. Here, host
is the IP address of the local machine PhpStorm is running on.
To check for opened inbound ports, you can use canyouseeme.org or a similar service.
Phpstorm Xdebug Ssh
Debugging a PHP CLI script
Depending on your environment, you can debug your PHP CLI script locally or remotely.
Local debugging | Remote debugging |
---|---|
The debugging engine and PhpStorm are running on the same machine. | The debugging engine is running on a remote environment (for example, a remote server or a virtual machine). |
Follow the Debug a PHP CLI script procedure. |
|
Besides running an entire application, you may want to run a PHPUnit or Behat test that covers a specific class, function or scenario, with the debugging engine attached. To learn how to do it, follow the Create a test configuration for a specific target procedure.
Troubleshooting
Xdebug Phpstorm Postman
When using Xdebug or Zend Debugger with PhpStorm, some misconfiguration can prevent the debugger from working. In the Troubleshooting guide, you will find the description of some common issues and learn how to troubleshoot them.
Advanced debugging scenarios
The following topics will assist you in exploring the advanced debugging scenarios in PhpStorm: