How to Debug PHP Script From the CLI with PHP-XDEBUG

TL;DR

  1. Set your IDE to listen for incoming connections
  2. On the CLI set the session key,
    export XDEBUG_CONFIG="idekey=netbeans-xdebug"
    # or
    export XDEBUG_CONFIG="idekey=PHPSTORM"
    
  3. 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.

  4. with PHPSTORM set the IDE config to use
    export PHP_IDE_CONFIG="serverName=SomeName"

Resources

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *