Xdebug in PhpStorm with Docker
Tags: php, xdebug, phpstorm, docker
Published: 2021-12-17 10:45:14

In my last post, I talked about how to configure [Xdebug in VSCode with Docker](https://dev.to/jackmiras/xdebug-in-vscode-with-docker-379l). Now, I would like to share how we can build upon our previous Dockerfile in such a way that Xdebug can run directly from Docker and also connect it with PhpStorm.

By choosing this approach, we substantially reduce the amount of setup that each team member has to do on their machine to get the project up and running, which means that we can start writing code faster.

So, why is this so important? Recent [research from JetBrains](https://www.jetbrains.com/lp/devecosystem-2022/php/#how-do-you-usually-debug-php-code-) shows that 62% of PHP developers debug their code using var_dump(), die(), dd(), and dump().  From my perspective, there is nothing wrong with that. Even if you do it by choice and not because you lack knowledge.

I'm included in the 62% of developers who debug their code with auxiliary functions instead of using a full-featured debug solution such as Xdebug. I'm a heavy Neovim user, and I didn't adapt quite well to using Neovim with Xdebug; to me, it is just easier and faster to use my code snippets around the dd() function.

But occasionally, I catch myself in situations where it would be faster to jump into PhpStorm and just use Xdebug, especially when I'm working with other people who aren't familiar with Vim or Neovim.

### Content
- [Xdebug config file](#xdebug-config-file)
    - [Explaining xdebug.ini](#explaining-xdebug.ini)
- [PhpStorm](#phpstorm)

### Xdebug config file

Before jumping into PhpStorm, first we have to clear up a few things about Xdebug to fully grasp the changes we’re going to make to the IDE. The information was first introduced on the topic of the [command directive](https://dev.to/jackmiras/docker-compose-for-a-laravel-app-ie7#app-command-directive) in a previous post. You will notice that at some point a **xdebug.ini** file gets copied from a local **.docker** folder into **/etc/php8/conf.d/50_xdebug.ini** at the container.

Even though the content of the file got shown, I intentionally didn't explain its content so that we could explore the debugging topic all at once, going all the way from configuring Xdebug to using it with an IDE.

Down below, we have the same Xdebug config file from the previous post, placed at **.docker/xdebug.ini** on the root of our Laravel project. Each line of code will be explained further, but in case you want to know every configuration that you can add in this file, check the [Xdebug documentation](https://xdebug.org/docs/all_settings).

```bash
zend_extension=xdebug.so
xdebug.mode=develop,coverage,debug,profile
xdebug.idekey=docker
xdebug.start_with_request=yes
xdebug.log=/dev/stdout
xdebug.log_level=0
xdebug.client_port=9003
xdebug.client_host=<YOUR_COMPUTER_IP>
```

#### Explaining xdebug.ini

- zend_extension=xdebug.so

A Zend extension hooks into “lower-level” languages; a single extension can be both a PHP and a Zend extension; despite being very uncommon, it's possible, and Xdebug is a good example of it.

- xdebug.mode=develop,coverage,debug,profile

This setting controls which Xdebug features get enabled; according to the documentation, the following values will get accepted:

    - develop
        Enables Development Helpers, including the overloaded var_dump().
    - coverage
        Enables Code Coverage Analysis to generate code coverage reports, mainly with PHPUnit.
    - debug
        Enables step-debugging. This can be used to step through your code while it is running, and analyze the values of variables.
    - profile
        Enables Profiling, with which you can analyze performance bottlenecks with tools like CacheGrind.

- xdebug.idekey=docker

Controls which IDE key Xdebug should pass on to the debugging client or proxy. The IDE Key is only important for use with the DBGp Proxy Tool, although some IDEs are incorrectly picky as to what its value is. The default is based on the DBGP_IDEKEY environment setting. If it is not present, the default falls back to an empty string.

- xdebug.start_with_request=yes

The functionality starts when the PHP request starts, and before any PHP code gets executed. For example, xdebug.mode=trace and xdebug.start_with_request=yes, start a function trace for the whole request.

- xdebug.log=/dev/stdout

Configure Xdebug's log file, but in this case, we are redirecting the log content to the default stdout of our container.

> In case you don't want to see these logs you can comment out this line of your **.docker/xdebug.ini** file by changing the line to `;xdebug.log=/dev/stdout`.

- xdebug.log_level=0

Configures which logging messages should be added to the log file. In this case, we are instructing Xdebug to log only errors in the configuration. If you want to see more information, you can use level 7 for log info or level 10 for log debug.

- xdebug.client_port=9003

The port to which Xdebug tries to connect on the remote host. Port 9003 is the default for both Xdebug and the Command Line Debug Client. As many clients use this port number, it is best to leave this setting unchanged.

- xdebug.client_host=\<YOUR_COMPUTER_IP\>

Configures the IP address or hostname to which Xdebug will attempt to connect when initiating a debugging connection. This address should be the address of the machine where your IDE or debugging client is listening for incoming debugging connections.

Down below, you can see how to get your IP address correctly in the main OS the developers use. In case you are using a different OS, the commands may serve as a base to try to extrapolate a solution for your use case.

macOS:

```bash
ipconfig getifaddr en0
```

Windows with WSL:

```bash
grep nameserver /etc/resolv.conf | cut -d ' ' -f2
```

Linux (Debian-based distros):

```bash
hostname -I | cut -d ' ' -f1
```

Once you have correctly found your IP address, you can place it into xdebug.client_host as mentioned before, and that will leave you with a directive looking similar to this: xdebug.client_host=192.168.0.158.

In summary, you've instructed Xdebug to start with a request and try to send the debug events to the host with the IP 192.168.0.158 on port 9003. Since the IP represents your computer, when configuring PhpStorm to connect to Xdebug, the configuration will be extremely similar to when connecting to localhost.

### PhpStorm

As you may already know, PhpStorm is a proprietary, cross-platform IDE for PHP, built by the Czech Republic-based company JetBrains. PhpStorm provides an editor for PHP, HTML, and JavaScript with on-the-fly code analysis, error prevention, and automated refactorings for PHP and JavaScript code.

With that being said, you may be wondering: “What do we need to have PhpStorm with all the aspects of an IDE with full-featured debugging”?

For starters, we need to check if the IDE can properly connect with Docker:

![check-docker-connection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35shori540bxaxw6b2e1.png)

Thereafter, we have to configure a PHP server. This can be done by going to Settings > PHP > Servers, then clicking on the plus sign, as the following screenshot shows:

![php-servers00](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3fbulglz8o783xkpsqk.png)

Once you click the plus sign, a form will open, where the Name, Host, Port, and Absolute path of the server have to be filled in:

![php-servers01](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5klac4fq6l9wzf8n5eg6.png)

> Notice that the _Port_ matches the port from our previsouly built [docker-compose.yml](https://dev.to/jackmiras/docker-compose-for-a-laravel-app-ie7) and the _Absolute path on the server_ matches the workdir from our previously built [Dockerfile](https://dev.to/jackmiras/laravel-with-php7-4-in-an-alpine-container-3jk6).

Now, that the server has been created, we can move to the debug configuration. Let's start by clicking on the _More Actions_ button in the main window, as the next screenshot shows:

![run-debug-configurations00](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z1tfici0i17yew5lkjlb.png)

Then, click on the _Edit…_ option to open the _Run/Debug Configurations_ window:

![run-debug-configurations01](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eau58mdbcamuxqhxc57s.png)

Subsequently, click into _Add New Configuration  > PHP Remote Debug_ as the screenshot down below shows:

![run-debug-configurations00](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4swboc5uhbc16y18nt9.png)

Next, a form will open, and there, fill the _Name_ with your Remote Debug configuration; next, check the _Filter debug connection by IDE key_ option and then select the _Server_ previously created; and finally, fill the _IDE key (session id)_ with the same value that got used at the xdebug.idekey directive at our **.docker/xdebug.ini**. More details are in the illustration below:

![run-debug-configurations03](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kantjy4st2lmz2mjhy5e.png)

As a result of the previous steps, the remote debug configuration got finished, and PhpStorm can now start listening for PHP debug connections. Click on the indicated button, as shown:

![start-listening-to-php-debug-connections](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxnsr0w4jyvp3t0jo94d.png)

And then, as a final step, click the debug button. Once the debug is running, you can trigger a request through Postman or your tests, and PhpStorm will intercept the event and stop at the first breaking point found.

![debug-docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6r2oljgf1529zezykm90.png)

___

Now, Xdebug is finally configured in your PhpStorm, and you can enjoy a more robust debugging tool with the potential to speed up your entire workflow.


Happy coding!