Debug PHP shell scripts with Xdebug

November 2022Jonathan Gruber

Step debugging with Xdebug v3 is not enabled by default for PHP requests or scripts, because it degrades performance. You need to set one of the possible triggers to enable debugging. For PHP shell scripts you can use an environmental variable for this:

XDEBUG_SESSION=session_name php script.php

Provided that you have a suitable client like PhpStorm listening on the Xdebug port (9003 per default), you are then able to step debug your program. To make this easier, you can put the following script called xdebug in some directory that is in your $PATH:

#!/bin/sh
XDEBUG_SESSION=cli "$@"

Now you are able to enable Xdebug for a script like this:

xdebug php script.php