How to Debug PHP Script From the CLI with PHP-XDEBUG
TL;DR
- Set your IDE to listen for incoming connections
- On the CLI set the session key,
export XDEBUG_CONFIG="idekey=netbeans-xdebug" # or export XDEBUG_CONFIG="idekey=PHPSTORM"
- Set XDEBUG configuration (can be set as environment variables or in-line parameters)
# in-line php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 \ -dxdebug.remote_host=127.0.0.1 -dxdebug.remote_connect_back=0 /path/to/script.php # Using an environment variable export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0" # Next, start our script like we would normally do: php /path/to/script.php
PS. set
remote_host
IP to the machine which run the IDE which listen for incoming connections. You can learn how to use ssh to tunnel back the connection from a remote machine back to your local machine. - with PHPSTORM set the IDE config to use
export PHP_IDE_CONFIG="serverName=SomeName"
Resources
- Starting a Debugging Session from the Command Line | Start the Script with Debugger Options
- https://stackoverflow.com/questions/2944880/debugging-php-cli-scripts-with-xdebug-and-netbeans