Debug CLI Scripts with XDebug and PHPStorm
This is a quick how-to guide on setting up XDebug debugging for command line scripts. I felt it took me way too long to set it up, so I’m documenting it here. This guide assumes that you already have XDebug set up, and just need to enable CLI debugging.
Run this command to add XDebug config to your environment variables. Note that this is for Windows.
set XDEBUG_CONFIG="idekey=PHPStorm remote_host=127.0.0.1 remote_port=9000"
Add these options under the XDebug section in your php.ini file.
[xdebug] xdebug.remote_enable = 1 xdebug.remote_handler = "dbgp" xdebug.remote_host = "127.0.0.1"
Restart your Apache service.
Now add a new debugging configuration in PHPStorm. IMPORTANT: The debug config type must be PHP Remote Debug. Choose your local server as the from the servers drop down.
Your command line debugging should now work. Select your new debug config from the drop down and enable listening for debugging connections. Add a break point in your CLI script, and voila!