Xdebug For Phpstorm

  1. Xdebug Phpstorm Not Working
  2. Phpstorm Xdebug Waiting For Connection
  3. Xdebug Phpstorm Postman
  4. Phpstorm Xdebug Ssh

This section describes on how to install Xdebug.

In this video I show you how to install Xdebug on Ubuntu and configure it to work with PhpStorm's Xdebug tools. NEVER type vardump again!For Magento 2 devel. Turn on XDebug inside the VM; Start listening for connections in PhpStorm by clicking the small telephone icon; Load a VM page in your browser PhpStorm will detect a request to connect, but will complain that path mappings are not set up. This is fine, we’re about to do that.

Xdebug is an extension for PHP to assist with debugging and development. Makes development easier by allowing the developer to debug his code in a simple and quick way. How to install Xdebug. In this tutorial, we will be installing Xdebug on Windows 10, the steps are similar for any Windows version. Phpxdebug: 1 phpxdebugdefaultenable: 1 phpxdebugremoteconnectback: 0 phpideconfig: servername=phpstorm phpxdebugidekey: 'phpstorm' phpxdebugremotehost: 172.17.0.1 # linux If you enable these after you've created the PHP container, remember to restart the container so that xdebug is enabled and configured.

How you install Xdebug depends on your system. There are the following possibilities:

  • Linux with a package manager such as apt, yum, or something else.
  • Linux without an Xdebug package with PECL.
  • macOSX with homebrew, through PECL.
  • Windows, with help from a wizard.
  • Unix-like operating systems, from source.

Installing on Linux #

Installing Xdebug with a package manager is often the fastest way. Depending on your distribution, run the following command:

  • Alpinelinux:
    sudo apk add php7-pecl-xdebug
  • Arch Linux:
    sudo pacman -S xdebug
  • CentOS:
    sudo yum install php-xdebug
  • CentOS (Remi Repro):
    sudo yum install php74-php-xdebug
  • Debian (9/stretch, testing/buster/bullseye/sid):
    sudo apt-get install php-xdebug
  • Fedora (32):
    sudo yum install php-xdebug
  • Fedora (Remi Repro):
    sudo yum install php74-php-xdebug
  • Gentoo:
    emerge dev-php/xdebug
  • Manjaro (20.1/Mikah):
    sudo pacman -S xdebug
  • RHEL:
    sudo yum install php-xdebug
  • RHEL (Remi Repro):
    sudo yum install php74-php-xdebug
  • SUSE (openSUSE, Enterprise):
    sudo zypper in php7-xdebug
  • Ubuntu (18.04 LTS/Bionic, 20.04 LTS/Focal):
    sudo apt-get install php-xdebug
  • Ubuntu (Ondřej Surý's PPA):
    sudo apt-get install php7.4-xdebug

Linux distributions might be providing an old and/or outdated version.If the package managerinstalls a version that is no longer supported (see Supported Versions), please installXdebug with PECL, or from sourceinstead.

Xdebug's latest version is 3.0.4.

Installing with PECL #

You can install Xdebug through PECL on Linux & macOS with Homebrew.Run:

On Apple M1 hardware, you might instead need to use:

Xdebug Phpstorm Not Working

Warning: You should ignore any prompts to add'extension=xdebug.so' tophp.ini — this will cause problems.

In some cases pecl will change the php.ini file toadd a configuration line to load Xdebug. You can check whether it did byrunning php -v. If Xdebug shows up with a version number, thanyou're all set and you can configure Xdebug's other functions, such asStep Debugging, or Profiling.

If pecl did not add the right line, skip to the Configure PHP section.

1 On macOS, you should have PHP installed with Homebrew.

Installing on Windows #

There are a few precompiled modules for Windows, they are all for the non-debugversion of PHP. You can get those at the downloadpage. Follow these instructions to get Xdebuginstalled.

Installation From Source #

Obtain #

You can download the source of the latest stable release 3.0.4.

Alternatively you can obtain Xdebug from GIT:

This will checkout the latest development version which is currently3.1.0-dev. This development branch might not always work asexpected, and may have bugs.

You can also browse the source on GitHub at https://github.com/xdebug/xdebug.

Compile #

There is a wizard available that provides youwith the correct file to download, and which paths to use.

You compile Xdebug separately from the rest of PHP. You need access to thescripts phpize and php-config. If your systemdoes not have phpize and php-config, you willneed to install the PHP development headers.

Debian users can do that with:

And RedHat and Fedora users with:

It is important that the source version matches the installed version as thereare slight, but important, differences between PHP versions. Once you haveaccess to phpize and php-config, take thefollowing steps:

  1. Unpack the tarball:

    tar -xzf xdebug-3.0.4.tgz

    You should notunpack the tarball inside the PHP source code tree.Xdebug is compiled separately, all by itself, as stated above.

  2. cd xdebug-3.0.4

  3. phpize

    If phpize is not in your path, please make surethat it is by expanding the PATH environment variable. Make sureyou use the phpize that belongs to the PHP version that you want to use Xdebugwith. See this FAQ entry if you're having someissues with finding which phpize to use.

  4. ./configure --enable-xdebug

  5. make

  6. make install

Configure PHP #

  1. Add the following line to php.ini:

    zend_extension=/wherever/you/put/it/xdebug

    To find out which php.ini file to modify, run a script with thefollowing:

    Alternatively, you can run php --ini on the command line.

    Note: There could be more than onephp.ini file. In many set-ups there is a different one for thecommand line (often cli/php.ini) and the web server (oftenfpm/php.ini).

    Note: If you want to use Xdebug andOPCache together, you must have the zend_extension line for Xdebugbelow the line for OPCache. Otherwise, they won't work properly together.

  2. Restart your webserver, or PHP-FPM, depending on what you areusing.

  3. Verify that Xdebug is now loaded.

    Create a PHP page that calls xdebug_info(). If you request thepage through the browser, it should show you an overview of Xdebug's settingsand log messages.

    On the command line, you can also run php -v. Xdebug and itsversion number should be present as in:

With Xdebug loaded, you can now enable individual features, such asStep Debugging, or Profiling.

Related Settings and Functions

  • stringxdebug.log =
  • integerxdebug.log_level = 7
  • stringxdebug.mode = develop
  • xdebug_info() : void

Settings

string xdebug.log = #

Configures Xdebug's log file.

Xdebug will log to this file all file creations issues, Step Debuggingconnection attempts, failures, and debug communication.

Enable this functionality by setting the value to a absolute path. Make surethat the system user that PHP runs at (such as www-data if you arerunning with Apache) can create and write to the file.

The file is opened in append-mode,and will therefore not be overwritten by default. There is no concurrencyprotection available.

The log file will include any attempt that Xdebugmakes to connect to an IDE:

It includes the opening time (2020-09-02 07:19:09.616195), theIP/Hostname and port Xdebug is trying to connect to(localhost:9003), and whether it succeeded (Connected toclient :-)). The number in brackets ([2693358]) is theProcess ID.

It includes:

[2693358]
process ID in brackets
2020-09-02 07:19:09.616195
opening time

For Step Debugging:

For Profiling:

For Function Trace:

All warnings and errors are described on the Description of errors page, withdetailed instructions on how to resolve the problem, if possible. All errors are always logged throughPHP's internal logging mechanism (configured with error_login php.ini). All warnings and errors also show up in thediagnostics log that you can view by calling xdebug_info().

Step Debugger Communication

The debugging log can also log the communication between Xdebug and an IDE.This communication is in XML, and starts with the <init XMLelement:

The fileuri attribute lists the entry point of yourapplication, which can be useful to compare to breakpoint_setcommands to see if path mappings are set-up correctly.

Beyond the <init element, you will find the configuration offeatures:

And continuation commands:

You can read about DBGP - A common debugger protocol specification at its dedicated documation page.

The xdebug.log_level setting controls how much information islogged.

Note: Many Linux distributions now use systemd, whichimplements private tmp directories. This means that when PHPis run through a web server or as PHP-FPM, the /tmp directory isprefixed with something akin to:

This setting can additionally be configured through theXDEBUG_CONFIGenvironment variable.

integer xdebug.log_level = 7#

Configures which logging messages should be added to the log file.

The log file is configured with the xdebug.log setting.

The following levels are supported:

LevelNameExample
0CriticalsErrors in the configuration
1ErrorsConnection errors
3WarningsConnection warnings
5CommunicationProtocol messages
7InformationInformation while connecting
10DebugBreakpoint resolving information

Criticals, errors, and warnings always show up in thediagnostics log that you can view by calling xdebug_info().

Criticals and errors are additionally logged throughPHP's internal logging mechanism (configured with error_login php.ini).

This setting can additionally be configured through theXDEBUG_CONFIGenvironment variable.

string xdebug.mode = develop#

This setting controls which Xdebug features are enabled.

This setting can only be set in php.ini orfiles like 99-xdebug.ini that are read when a PHP process starts(directly, or through php-fpm), but not in .htaccess and.user.ini files, which are read per-request.

Aids

The following values are accepted:

off
Nothing is enabled. Xdebug does no work besides checking whetherfunctionality is enabled. Use this setting if you want close to 0overhead.
develop
Enables Development Aids including the overloaded var_dump().
coverage
Enables Code Coverage Analysis to generate code coverage reports, mainly incombination withPHPUnit.
debug
Enables Step Debugging. This can be used to step through your code while itis running, and analyse values of variables.
gcstats
Enables Garbage Collection Statistics to collect statistics about PHP's GarbageCollection Mechanism.
profile
Enables Profiling, with which you can analyse performance bottleneckswith tools like KCacheGrind.
trace
Enables the Function Trace feature, which allows you record every functioncall, including arguments, variable assignment, and return value that is madeduring a request to a file.

You can enable multiple modes at the same time by comma separating theiridentifiers as value to xdebug.mode: xdebug.mode=develop,trace.

Xdebug For Phpstorm

You can also set the mode by setting the XDEBUG_MODE environmentvariable on the command-line; this will take precedence over the xdebug.mode setting, but will no change the value of the xdebug.mode setting.

Functions

xdebug_info() : void#

This function returns an HTML page which shows diagnostic information. It is analogous to PHP's phpinfo() function.

The HTML output includes which mode is active, what the settings are, and diagnostic information in case there are problems with debugging connections, opening of files, etc.

Each warning and error in the diagnostics log also links through to the Description of errors documentation page.

Download Xdebug

  • Download the Xdebug extension compatible with your PHP version and install it as described in the installation guide.

    Xdebug 3 brings performance improvements, simplified configuration, and PHP 8 support. To learn more on upgrading to Xdebug 3, see the Upgrade guide.

    If you are using an AMP package, the Xdebug extension may be already installed. Refer to the instructions specific for your package.

Integrate Xdebug with the PHP interpreter

  1. Open the active php.ini file in the editor:

    1. In the Settings/Preferences dialog Ctrl+Alt+S, click PHP.

    2. On the PHP page that opens, click next to the CLI Interpreter field.

    3. In the CLI Interpreters dialog that opens, the Configuration file read-only field shows the path to the active php.ini file. Click Open in Editor.

  2. To disable the Zend Debugger and Zend Optimizer tools, which block Xdebug, remove or comment out the following lines in the php.ini file:

    zend_extension=<path_to_zend_debugger> zend_extension=<path_to_zend_optimizer>
  3. To enable Xdebug, locate or create the [xdebug] section in the php.ini file and update it as follows:

    [xdebug] zend_extension='<path to xdebug extension>' xdebug.remote_enable=1 xdebug.remote_host=127.0.0.1 xdebug.remote_port='<the port (9000 by default) to which Xdebug connects>'
    [xdebug] zend_extension='<path to xdebug extension>' xdebug.mode=debug xdebug.client_host=127.0.0.1 xdebug.client_port='<the port (9003 by default) to which Xdebug connects>'

    In PHP 5.3 and later, you need to use only zend_extension, not zend_extension_ts, zend_extension_debug, or extension.

    To enable multi-user debugging via Xdebug proxies, locate the xdebug.idekey setting and assign it a value of your choice. This value will be used to register your IDE on Xdebug proxy servers.

  4. Save and close the php.ini file.

  5. Verify Xdebug installation by doing any of the following:

    • In the command line, run the following command:

      The output should list Xdebug among the installed extensions:

    • Create a php file containing the following code:

      <?php phpinfo();

      Open the file in the browser. The phpinfo output should contain the Xdebug section:

Configure Xdebug in PhpStorm

  1. In the Settings/Preferences dialog Ctrl+Alt+S, select PHP.

  2. Check the Xdebug installation associated with the selected PHP interpreter:

    1. On the PHP page, choose the relevant PHP installation from the CLI Interpreter list and click next to the field. The list shows all the PHP installations available in PhpStorm, see Configure local PHP interpreters and Configure remote PHP interpreters.

    2. The CLI Interpreters dialog that opens shows the following:
      • The version of the selected PHP installation.

      • The name and version of the debugging engine associated with the selected PHP installation (Xdebug or Zend Debugger). If no debugger is configured, PhpStorm shows the corresponding message:

    Alternatively, open the Installation Wizard, paste the output of the phpinfo(), and click Analyze my phpinfo() output. Learn more about checking the Xdebug installation in Validate the Configuration of a Debugging Engine.

  3. Define the Xdebug behaviour. Click Debug under the PHP node. On the Debug page that opens, specify the following settings in the Xdebug area:

    • In the Debug port field, appoint the port through which the tool will communicate with PhpStorm.

      This must be the same port number as specified in the php.ini file:

      xdebug.remote_port='<the port (9000 by default) to which Xdebug connects>'
      xdebug.client_port='<the port (9003 by default) to which Xdebug connects>'

      By default, Xdebug 2 listens on port 9000. For Xdebug 3, the default port has changed from 9000 to 9003. You can specify several ports by separating them with a comma. By default, the Debug port value is set to 9001,9003 to have PhpStorm listen on both ports simultaneously.

    • To have PhpStorm accept any incoming connections from Xdebug engine through the port specified in the Debug port field, select the Can accept external connections checkbox.

    • Select the Force break at first line when no path mapping specified checkbox to have the debugger stop as soon as it reaches and opens a file that is not mapped to any file in the project on the Servers page. The debugger stops at the first line of this file and Examine/update variables shows the following error message: Cannot find a local copy of the file on server <path to the file on the server> and a link Click to set up mappings. Click the link to open the Resolve Path Mappings Problem dialog and map the problem file to its local copy.

      When this checkbox cleared, the debugger does not stop upon reaching and opening an unmapped file, the file is just processed, and no error messages are displayed.

    • Select the Force break at first line when a script is outside the project checkbox to have the debugger stop at the first line as soon as it reaches and opens a file outside the current project. With this checkbox cleared, the debugger continues upon opening a file outside the current project.

  4. In the External connections area, specify how you want PhpStorm to treat connections received from hosts and through ports that are not registered as deployment server configurations.

    • Ignore external connections through unregistered server configurations: Select this checkbox to have PhpStorm ignore connections received from hosts and through ports that are not registered as deployment server configurations. When this checkbox is selected, PhpStorm does not attempt to create a deployment server configuration automatically.

    • Break at first line in PHP scripts: Select this checkbox to have the debugger stop as soon as connection between it and PhpStorm is established (instead of running automatically until the first breakpoint is reached). Alternatively turn on the Run Break at first line in PHP scripts option from the main menu.

    • Max. simultaneous connections Use this spin box to limit the number of external connections that can be processed simultaneously.

By default, PhpStorm only listens for incoming IPv4 connections. To enable IPv6 support, you need to make adjustments in PhpStorm JVM options:

  1. Select Help Edit Custom VM Options from the main menu.

  2. In the .vmoptions file that opens, delete the -Djava.net.preferIPv4Stack=true line.

  3. Restart PhpStorm.

Xdebug

Configure Xdebug for using in the On-Demand mode

PhpStorm supports the On-Demand mode, where you can disable Xdebug for your global PHP installation and have it enabled automatically on demand only when you are debugging your command-line scripts or when you need code coverage reports. This lets your command line scripts (including Composer and unit tests) run much faster.

  1. Disable Xdebug for command-line scripts:

    1. In the Settings/Preferences dialog Ctrl+Alt+S, go to PHP.

    2. From the PHP executable list, choose the relevant PHP interpreter and click next to it. In the CLI Interpreters dialog that opens, click the Open in Editor link next to the Configuration file: <path to php.ini> file. Close all the dialogs and switch to the tab where the php.ini file is opened.

    3. In the php.ini file, find the [xdebug] section and comment the following line in it by adding ; in preposition:

      ;[xdebug] ;zend_extension = '<path to xdebug extension>'
    4. Open the CLI Interpreters dialog and click next to the PHP executable field. PhpStorm informs you that debugger is not installed:

  2. To enable PhpStorm to activate Xdebug when it is necessary, specify the path to it in the Debugger extension field, in the Additional area. Type the path manually or click and select the location in the dialog that opens.

Configure Xdebug for using in the Just-In-Time mode

PhpStorm supports the use of Xdebug in the Just-In-Time (JIT) mode so it is not attached to your code all the time but connects to PhpStorm only when an error occurs or an exception is thrown. Depending on the Xdebug version used, this operation mode is toggled through the following settings:

  • Xdebug 2 uses the xdebug .remote_mode setting, which has to be set to jit.

  • Xdebug 3 uses the xdebug.start_upon_error setting, which has to be set to yes.

The mode is available both for debugging command-line scripts and for web server debugging.

Depending on whether you are going to debug command-line scripts or use a Web server, use one of the scenarios below.

Command-line scripts

For debugging command-line scripts, specify the custom -dxdebug.remote_mode=jit (for Xdebug 2) or -dxdebug.start_upon_error=yes (for Xdebug 3) directive as an additional configuration option:

  1. In the Settings/Preferences dialog Ctrl+Alt+S, navigate to PHP.

  2. From the PHP executable list, choose the relevant PHP interpreter and click next to it.

  3. In the CLI Interpreters dialog that opens, click next to the Configuration options field in the Additional area.

  4. In the Configuration Options dialog that opens, click to add a new entry.

    • For Xdebug 2, type xdebug.remote_mode in the Configuration directive field and jit in the Value field.

    • For Xdebug 3, type xdebug.start_upon_error in the Configuration directive field and yes in the Value field.

    When you click OK, you return to the CLI Interpreters dialog where the Configuration options field shows -dxdebug.remote_mode=jit (for Xdebug 2) or -dxdebug.start_upon_error=yes (for Xdebug 3).

Web server debugging

  1. From the main menu, choose Run Web Server Debug Validation.

  2. In the Validate Remote Environment that opens, choose the Web server to validate the debugger on.

    • Choose Local Web Server or Shared Folder to check a debugger associated with a local Web server.

      • Path to Create Validation Script: In this field, specify the absolute path to the folder under the server document root where the validation script will be created. For Web servers of the type Inplace, the folder is under the project root.

        The folder must be accessible through http.

      • URL to Validation Script: In this field, type the URL address of the folder where the validation script will be created. If the project root is mapped to a folder accessible through http, you can specify the project root or any other folder under it.

    • Choose Remote Web Server to check a debugger associated with a remote server.

      • Path to Create Validation Script: In this field, specify the absolute path to the folder under the server document root where the validation script will be created. The folder must be accessible through http.

      • Deployment Server: In this field, specify the server access configuration of the type Local Server or Remote Server to access the target environment. For details, see Configure synchronization with a Web server.

        Choose a configuration from the list or click Browse in the Deployment dialog.

  3. Click Validate to have PhpStorm create a validation script, deploy it to the target remote environment, and run it there.

  4. Open the php.ini file which is reported as loaded and associated with Xdebug.

  5. In the php.ini file, find the [xdebug] section.

    Change the value of the xdebug.remote_mode from the default req to jit.

    Change the value of the xdebug.start_upon_error from the default default to yes.

See also Just-In-Time debugging and PHP Exception Breakpoints with PhpStorm and Xdebug

Configure Xdebug running in a Docker container

To configure Xdebug running in a Docker container, provide the Xdebug-specific parameters in the Dockerfile, for example:

RUN pecl install xdebug && docker-php-ext-enable xdebug && echo 'xdebug.remote_enable=on' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && echo 'xdebug.remote_host = host.docker.internal' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

In this example, we're modifying /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini providing the remote_enable and remote_host Xdebug parameters.

Note that the xdebug.remote_host value should be replaced with the IP address of the machine where PhpStorm is running, which is accessible from the Docker container. If you are using Docker for Windows or Docker for Mac, you can set xdebug.remote_host to host.docker.internal, which automatically resolves to the internal address of the host, letting you easily connect to it from the container.

RUN pecl install xdebug && docker-php-ext-enable xdebug && echo 'xdebug.mode=debug' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && echo 'xdebug.client_host = host.docker.internal' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

In this example, we're modifying /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini providing the mode and client_host Xdebug parameters.

Note that the xdebug.client_host value should be replaced with the IP address of the machine where PhpStorm is running, which is accessible from the Docker container. If you are using Docker for Windows or Docker for Mac, you can set xdebug.client_host to host.docker.internal, which automatically resolves to the internal address of the host, letting you easily connect to it from the container.

Configure Xdebug running on a Vagrant instance

Phpstorm Xdebug Waiting For Connection

To configure Xdebug running on a Vagrant instance, connect to the Vagrant machine and provide the Xdebug-specific parameters in the php.ini file:

Xdebug Phpstorm Postman

[xdebug] zend_extension='<path to xdebug extension>' xdebug.remote_enable=1 xdebug.remote_host=10.0.2.2 xdebug.remote_port=9000

Note that the xdebug.remote_host value is 10.0.2.2. This is the gateway used in the default Vagrant setup, which allows connecting from the instance to host where PhpStorm is running.

[xdebug] zend_extension='<path to xdebug extension>' xdebug.mode=debug xdebug.client_host=10.0.2.2 xdebug.client_port=9003

Phpstorm Xdebug Ssh

Note that the xdebug.client_host value is 10.0.2.2. This is the gateway used in the default Vagrant setup, which allows connecting from the instance to host where PhpStorm is running.