Debugging Web Applications In PHPStorm With Xdebug And Docker

From Littledamien Wiki
Jump to navigation Jump to search

Overview

How to debug a PHP web application in PHPStorm using Docker and XDebug. [1] [2]

PHP configuration

Docker server

Preferences > Language & Frameworks > PHP > Servers

Click the "plus" icon to create a new server.

  • Name: docker-server
  • Host: localhost
  • Port: 80
  • Debugger: Xdebug
  • Use path mappings: checked

Then under path mappings create associations between project files and their respective locations in the Docker container, e.g.:

The host and port reference host and port within the Docker container. This applies to the path mappings as well.

The container's HTML root is /var/www/html. This path may or may not exist on the local file system, but that doesn't impact anything here.

Two project file directories cannot point to the same path on the server (e.g. bfhhand_web and common_lib can't coexist in the same root directory as they do on production.)

Run configuration

Run > Edit Configurations...

Click the "plus" icon to create a new Debug Configuration.

Select PHP Remote Debug

  • Name: docker
  • Server: docker-server
  • IDE key: docker

docker-server is the server that was created in the previous step.

The value of the IDE Key must match the value that is put into the xdebug.ini file that is created later in the process.

Xdebug preferences

These are the defaults, they can be confirmed, but don't need to be changed

Preferences > Languages & Frameworks > PHP > Debug > Xdebug

  • Debug port: 9001
  • Can accept external connections: checked

Docker container configuration

The four files used in the Docker container configuration (Dockerfile, xdebug.ini, start-container, docker-compose.yml) are all located in the PHPStorm project directory. In the case of BFH Handwriting they are all located in the root of the project, which also happens to be the root of the website. It might be better to place the web files one directory down to separate them from these configuration files which should not be uploaded to production.

Dockerfile

The following files are placed in the PHPStorm project directory. They should not be uploaded to production.

FROM php:7.2-fpm
RUN pecl install redis-4.0.1 \
    && pecl install xdebug-2.6.0 \
    && docker-php-ext-enable redis xdebug

COPY xdebug.ini /usr/local/etc/php/conf.d/

COPY start-container /usr/local/bin/start-container
RUN chmod +x /usr/local/bin/start-container

EXPOSE "80"

CMD ["start-container"]
  • Installs xdebug
  • Applies local xdebug.ini to the container.
  • Makes start-container script available to the container.
  • Makes http port 80 available in the container.
  • Runs the start-container script.

xdebug.ini

zend.extension="/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_handler="dbgp"
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.idekey="docker"
xdebug.remote_host=docker.for.mac.localhost
xdebug.remote_log="/tmp/xdebug.log"
  • xdebug.idekey refers back to the IDE key entered in PHPStorm's run configuration.
  • xdebug.remote_host could be the IP address of the local machine, but this would have to be changed any time the IP address changes. See below for the work-around that explains the setting above.
    • In a Mac OS terminal find the current IP with ipconfig getifaddr en0
  • xdebug.remote_connect_back must be set to "0" or else it would override the xdebug.remote_host settting.
  • xdebug.remote_port must be the same as PHPStorm > Preferences > Languages & Frameworks > PHP > Debug > Xdebug > Debug Port.
  • If you're debugging more than one PHP web application, then you either remove all Docker images except the one for the current app, or make sure that they are using different ports.

start-container script

Create a bash script in the project directory named start-container (to match the directive in the Dockerfile).

#!/usr/bin/env bash

php -S 0.0.0.0:80 -t /var/www/html

This starts a PHP server listening at all addresses on port 80.

Its root directory is /var/www/html which matches the paths set in PHPStorm > Preferences > Language & Frameworks > PHP > Servers > docker > path mappings.

docker-compose.yml

version: '2'
services:
  app:
    build:
      context: .
    image: xdebug-bfhhand:latest
    volumes:
      - ./:/var/www/html
      - ~/develop/bfhhand/keys:/var/www/keys
      - ~/develop/common_lib:/var/www/html/common_lib
    ports:
      - "80:80"

This

  • The context directive specifies where the Dockerfile is located.
  • image specifies the name of the Docker container that will be created.
  • volumes maps local directories to directories in the Docker container. These mappings correspond to the mappings found in PHPStorm > Preferences > Language & Frameworks > PHP > Servers > docker > path mappings.
  • ports maps the port used to reach the container with the port used inside the container.

Starting the Docker container

$ docker-compose build
$ docker-compose up -d
$ curl localhost

The -d option is for "detached" to get the terminal prompt back after running the command.

The server is tested with curl localhost.

Stop the container with

$ docker-compose down

Remove the image with

$ docker rmi [IMAGE_NAME]

Connecting the the Docker container with PHPStorm

Run > Debug… > Select docker (PHP Remote…) from the dropdown options.

Or in the upper right corner select docker from the drop down and click the Debug icon.

It doesn't seem to be necessary to turn on Run > Start Listening for PHP Debug Connections, but it doesn't interfere either.

Then in a browser load http://localhost or in a terminal window enter curl localhost. The IDE will automatically catch the request and break at any breakpoints.

See also

External links

Notes