Cli Xdebug



Xdebug can be used to debug both web requests as well as cli scripts (e.g., Drush commands).

  1. Copying the xdebug config lines from the /etc/php5/apache2/php.ini file into /etc/php5/cli/php.ini setting an environment variable with the name of the debug session (you can get this from the query string in the url of the page netbeans launches when you start debugging) so the command is: export XDEBUGCONFIG='idekey=netbeans-xdebug'.
  2. Copying the xdebug config lines from the /etc/php5/apache2/php.ini file into /etc/php5/cli/php.ini setting an environment variable with the name of the debug session (you can get this from the query string in the url of the page netbeans launches when you start debugging) the command is: export XDEBUGCONFIG='idekey=netbeans-xdebug'.

GIBBY 126 West 1570 South, Lehi, Utah 84043. (801) 706-6971. cgibby@gmail.com EXPERIENCE Owner/Developer Match The Memory, Lehi, Utah. October 2009-Present. Xdebug.remoteenable = 1 xdebug.remoteautostart = 1 Tip! If your PHP project runs in a Docker container, you need also to add the following line to the xdebug.ini file, where 172.17.0.1 is the IP address of the docker0 interface on your computer (on Mac OS you have to put host.docker.internal instead). Using the CLI DDEV-Local Command-Line Usage¶ Type ddev or ddev -hin a terminal window to see the available ddev commands. There are commands to configure a project, start, stop, describe, etc. Each command also has help. For example, ddev stop -h shows that ddev rm is an alias, and shows all the many flags that can be used with ddev stop.

Stack Setup

Xdebug integration is disabled by default as it causes a roughly 20% performance hit. To enable it:

Cli Xdebug

To verify that Xdebug was enabled:

Next, follow the IDE specific setup steps:

Debugging with PHPStorm

First, follow the setup instructions to enable the Xdebug integration.

Automatic Setup

PHPStorm makes debugging setup very easy using the Incoming Connection Dialog.

  1. Open a project in PHPStorm and set a breakpoint wherever you like
  2. Click on the Start Listening for PHP Debug Connections button in PHPStorm

  3. Open the project in a browser

    A debugging session will start and Xdebug will initialize a connection to PHPStorm.

  4. Click on Accept in the Incoming Connection From Xdebug dialogue in PHPStorm

    PHPStorm automatically configures a server and directory mappings between the host and the server.

Directory mappings are very important, as that’s how PHPStorm knows how to map sources on the server to those onthe host. By default, you will not be able to debug anything above the project’s docroot folder.

If you don’t get the Incoming Connection From Xdebug dialogue or you need to debug scripts above the docrootdirectory, see the manual setup steps.

Xdebug

Manual Setup

  1. Under Preferences > Languages & Frameworks > PHP > Servers add a new server
  2. Set Name and Hostname to project’s virtual host (e.g., myproject.docksal)
  3. Configure host to server directory mappings

    Map the project directory on the host to /var/www/ on the server:

With this manual setup you will be able to debug scripts within your project’s root (/var/www/ on the server).

CLI Scripts

First, follow automatic or manual instructions to configure server and pathmapping settings in PHPStorm.

To debug PHP CLI scripts, we have to tell PHPStorm which existing server configuration to use via thePHP_IDE_CONFIG variable. This can be done using the following commands:

Cli Xdebug

The script you are trying to debug must reside within the project root directory (/var/www/on the server) orPHPStorm won’t be able to access the scripts’s source code and debug it.

CLI Scripts: Drush

To debug custom Drush commands, make the following additional adjustments in PHPStorm settings:

  1. Go to Preferences > Languages & Frameworks > PHP > Debug
  2. Uncheck Force break at the first line when no path mapping specified
  3. Uncheck Force break at the first line when a script is outside the project

You can run your scripts in console and debug them in the same way as browser requests. For example, you can runfin drush fl and debug this Drush command from the Features module.

Debugging with Visual Studio Code

  1. Follow the setup instructions to enable the Xdebug integration
  2. Install PHP_Debug plugin by Felix Becker
  3. Configure PHP Debug Settings

    • Click Debug Icon in left sidebar
    • Click Configuration Gear in top bar, select PHP from the dropdown list
    • Configure pathMappings

    Here is an an example of what launch.json should look like:

  4. Set a breakpoint

  5. Select the Listen for XDebug configuration from the dropdown and click Start Debugging

Cli

You can debug both web requests and cli scripts using this configuration.

CLI Scripts: Drush

To debug Drush commands using Xdebug and VSCode, add the following to your path mappings under the configuration that begins with 'name': 'Listen for XDebug',

Drush 8.x

  • Add '/usr/local/bin/drush': '${workspaceFolder}/bin/drush' to pathMappings in your launch.json file.

Drush 9.x

Xdebug Cli Not Working

Cli
  • Add '/usr/local/bin/drush': '${workspaceFolder}/vendor/bin/drush' to pathMappings in your launch.json file.

Debugging with NetBeans

  1. Follow the setup instructions to enable the Xdebug integration
  2. Open NetBeans Debugging configuration (“Tools> Options > PHP > Debugging”) and set “DebuggerPort” to 9000
  3. Open your project in NetBeans
  4. Configure project properties:

    • Right mouse click on project name, then “Properties” from the dropdown menu
    • Under “Sources”, set correct Web Root folder by clicking “Browse” button (usually it’s docroot)
    • Under “Run Configuration”, use project’s virtual host (e.g., myproject.docksal) to set the Project URL
    • Click “OK” to save project properties
  5. Set a breakpoint wherever you like

  6. In NetBeans, with the whole project selected or one of the project files opened and active, press <CTRL> + <F5> on your keyboard to start the debugger

Xdebug Cli Config

Debugging with Atom

Command Line Xdebug

  1. Follow the setup instructions to enable the Xdebug integration
  2. Install PHP-Debug Plugin
  3. Configure PHP Debug Settings (Preferences > Packages)

    • Search for “php-debug”
    • Click “Settings” button below plugin
    • “Server” can be set to *
    • “Server Listen Port” should be set to 9000
    • Make sure “Continue to listen for debug sessions even if the debugger windows are all closed” is checked. This will make the debugger window open automatically.