Xdebug
Flight Deck may be configured to support Xdebug, an open source PHP debugger. Using a debugger makes a world of difference if you’ve been using print
statements to get your code working.
Enabling or Disabling Xdebug
Xdebug is enabled out of the box in Flight Deck. If you need to disable or re-enable it, you only need to edit the .env
file:
- Using the
docker
ordocker-compose
commands, kill your currently running instance of Flight Deck. - Open the
.env
file in your favorite text editor. - Scroll to the Xdebug section, and follow instructions in the file.
- Note that Docker for macOS and Docker for Windows will not work without some additional configuration. See the
.env
file for details. - (Re)start Flight Deck using
docker-compose up -d
.
Verification
To verify that Xdebug is working, you will need to visit a page displaying phpinfo()
. If using Flight Deck with Drupal, you can do the following:
- Login to the drupal site.
- Navigate to
admin/reports/status/php
. - Validate that the
xdebug
module is loaded in the page output.
Setting the IDE Key
Xdebug uses an “IDE key” to identify if you are attempting to debug a page. To make setting this parameter easier, you can install a web browser extension:
- Google Chrome: Xdebug helper
- Mozilla Firefox: The easiest Xdebug
By default, Flight Deck expects the IDE key to be PHPSTORM
. Be sure to set it in the extension preferences.
Configuring PHPStorm
Path mapping
Even though Flight Deck is running on your local machine, you need to treat it as if it was a completely separate, physical machine elsewhere on the network. For this reason, you need to configure PHPStorm to map file paths on your local machine to those inside the containers.
First we need to create a new Debug configuration. You only have to do that once per project.
- Using the Run menu, select Edit configurations.
- The Run/Debug Configurations window appears. Click the + icon in the upper left corner, and select PHP Remote Debug.
- Enter a unique Name.
- Click Apply to save your changes.
Next, we need to configure the remote server.
- Open the Run/Debug Configurations window, and the debug configuration you created earlier.
- Click the … button next to Servers.
- The Servers window appears. Click the + icon in the upper left corner to create a new server.
- Enter a unique Name for the server. Usually this is the same as your URL alias (
docker.test
). - Enter the Host as the same as your URL alias.
- Select the Debugger as Xdebug.
- Select Use path mappings.
- In the path mappings table, locate the
src
directory. Enter the Absolute path on the server as/var/www/html
. - Click OK to save your changes.
Linux hosts
Linux hosts have two ways they may configure Xdebug support.
- If your
.env
isn’t shared between multiple, mixed-OS team members, follow the instructions to usePHP_XDEBUG_REMOTE_CONNECT_BACK
. - If your
.env
is shared, you can define a loopback alias (see the.env
file for an example). Once finished, continue to the next section.
macOS and Windows hosts
In order for PHPStorm to pick up the remote debugging session, you need to configure the DBGp Proxy:
- Open the PHPStorm preferences.
- Navigate to Languages & Frameworks > PHP > Debug > DBG Proxy.
- Set the IDE key as
PHPSTORM
. - Set the Host to the value of
PHP_XDEBUG_REMOTE_HOST
in your.env
file. - Set the Port to the Xdebug default,
9000
. - Click OK to save your changes.
Logging and Troubleshooting
By default, Flight Deck stores Xdebug logs at /var/log/apache2/xdebug.log
. If you ever need to troubleshoot the Xdebug configuration is may be useful to redirect the logs to STDOUT, this makes the logs available with a docker-compose logs
command.
To enable Xdebug logging to STDOUT:
- Open the
.env
file for editing. - Uncomment the
PHP_XDEBUG_STDOUT_LOGS
setting, be sure it’s set to a value of1
. - Stop and restart Flight Deck.
- Use
docker-compose logs
to view the container logs.
When interacting with the web container using docker-compose exec
, you may see the output of Xdebug in your terminal window. This is normal, if annoying, when PHP_XDEBUG_STDOUT_LOGS
is enabled.
Shell alias for loopback
You may wish to set up an alias for setting the loopback address. This command differs slightly on macOS and Linux:
# For macOS
alias docdebug='sudo ifconfig lo0 alias 10.254.254.254'
# For Linux
alias docdebug='sudo ip -4 addr add 10.254.254.254/32 dev lo'