Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Starting a Debugging Session from the Command Line

Let's see how we can start debugging a PHP CLI script from the command line, having PhpStorm listen for incoming debugger connectionsWhen running CLI scripts one of the requirements below should be met to start a debugging session with IDE:

  • Xdebug's remote_autostart setting is enabled
  • XDEBUG_CONFIG environment variable exists 

Let's go ahead and configure the environment, starting with IDE.

1. Listen for Incoming Debugger Connections

...

Expand
titleStarting the script with Xdebug

Xdebug has various configuration options which we can use to let the PHP interpreter reach out to PhpStorm. These parameters have to be passed to the PHP interpreter using the -d command line switch. A more convenient way will be to set an environment variable so we don't have to provide the -d switches all the time.

Using PHP command line switches

To start our script with debugging , we can launch we have to:

  1. Set an environment variable that would tell XDebug to connect to IDE:

    Code Block
    Windows:
    set XDEBUG_CONFIG="idekey=123"
     
    Linux / Mac OS X:
    export XDEBUG_CONFIG="idekey=123"

    Here idekey should have a random value.

  2. Launch PHP with several switches:

    Code Block
    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
  1.  

    You might use 10.0.2.2 instead of 127.0.0.1 with Vagrant (see related SO question).

     

Using an environment variable

To start our script with debugging, we can set an environment variable that configures Xdebug:

Code Block
Windows:
set XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0"

Linux / Mac OS X:
export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0"

Next, we can start our script like we would normally do:

Code Block
php path/to/script.php

 

 

 

Tip

Optionally, we can use Xdebug's remote_autostart setting to always start a debugging session for every script that is run.

Expand
titleStarting the script with Zend Debugger

Zend Debugger has various configuration options which we can use to let the PHP interpreter reach out to PhpStorm. These parameters have to be passed to the PHP interpreter using an environment variable.

First, we have to set the QUERY_STRING environment variable:

Code Block
Windows:
set QUERY_STRING="start_debug=1&debug_host=127.0.0.1&no_remote=1&debug_port=10137&debug_stop=1"

Linux / Mac OS X:
export QUERY_STRING="start_debug=1&debug_host=127.0.0.1&no_remote=1&debug_port=10137&debug_stop=1"

Next, we can start our script like we would normally do:

Code Block
php path/to/script.php

To  

Optionally: to tell the PhpStorm which path mapping configuration should be used for a connection from certain machine, the value of the PHP_IDE_CONFIG environment variable should be set to serverName=SomeName, where SomeName is the name of the server configured in Settings / Preferences | Languages & Frameworks | PHP | Servers:

Code Block
Windows:
set PHP_IDE_CONFIG="serverName=SomeName"

Linux / Mac OS X:
export PHP_IDE_CONFIG="serverName=SomeName"

If this environment variable is not set - you'll be prompted to specify path mappings manually once IDE detects an incoming XDebug connection.

3. Debug!

Once the script is started, PhpStorm will open the debug tool window and break at the first breakpoint that was set in the script. Next, we can continue debugging our PHP CLI script using PhpStorm.

...