Hackernoon logoHow To Debug PHP Container With Xdebug And PhpStorm by@ifomin

How To Debug PHP Container With Xdebug And PhpStorm

Author profile picture

@ifominIgor Fomin

Full stack web developer, tech lead, project manager

I will setup a very simple php page and debug it using xdebug and PhpStorm.
Source files can be found here:

1. Create "Dockerfile" file in "docker" folder:

FROM php:7.2-fpm

#Install xdebug
RUN pecl install xdebug-2.6.1 && docker-php-ext-enable xdebug

CMD ["php-fpm"]
Execute this command from docker folder to build image:
docker build -t php-xdebug-custom -f Dockerfile .

2. Create docker-compose.yml file in "docker" folder:

version: "3.7"

services:

  web:
    image: nginx:1.17
    ports:
      - 80:80
    volumes:
      - /var/www/docker-study.loc/recipe-09/php:/var/www/myapp
      - /var/www/docker-study.loc/recipe-09/docker/site.conf:/etc/nginx/conf.d/site.conf
    depends_on:
      - php

  php:
    image: php-xdebug-custom
    volumes:
      - /var/www/docker-study.loc/recipe-09/php:/var/www/myapp
      - /var/www/docker-study.loc/recipe-09/docker/php.ini:/usr/local/etc/php/php.ini
Here I use "php-xdebug-custom" image instead of "php:7.2-fpm"

3. Modify php.ini file with xdebug settings:

[xdebug]
zend_extension=xdebug.so
xdebug.profiler_enable=1
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=host.docker.internal
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.remote_connect_back=1
xdebug.idekey=PHPSTORM

4. Configure server in PhpStorm:

File -> Settings -> Languages & Frameworks -> PHP -> Servers
Add a new server using + icon, and configure like on the following screenshot:
Make sure to check "Use path mappings" and map php folder to "/var/www/myapp"

5. Configure PHP remote debugger in PhpStorm:

Run -> Edit configurations -> PHP Remote Debug
Add a new configuration and give it values like on the following screenshot:

6. Select debug configuration in PhpStorm debug panel

7. Go to /var/www/docker-study.loc/recipe-09/docker/ and execute:

docker-compose up -d
If I now try
myapp.loc/
in browser, I will see results from index.php file.
I can set a breakpoint, start listening for connections in PhpStorm debug panel, and reload page.
Happy debugging!

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!