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.

XDebug Config

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"

PHP.ini Config

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 Apache

Restart your Apache service.

Debugging Configuration

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.

Click OK.

Debug

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!