Phpstorm Xdebug



Xdebug

Pass xdebug cookie as one of the headers (the one which is set by bookmarklet or browser extension, for example) For this to work - make sure that 'phone handle' icon is activated (Run Start Listen for PHP Debug Connection) in advance. PhpStorm will automatically add the XDEBUGSESSION cookie to the request, execute it, and stop at the specified breakpoint. When a request is executed, PhpStorm automatically creates a dedicated temporary HTTP Request run/debug configuration for it. You can save it as a permanent run/debug configuration if necessary. Xdebug.remoteenable = 1 We keep the other default parameters to keep the configuration as minimal as possible. So, with this setup, the port used by Xdebug is 9000 and the default IP address is 127.0.0.1. Check out the xdebug.remotehost and xdebug.remoteport parameters in the documentation.

I use PHPStorm along with Xdebug to step through my code. PHPStorm is running on a Windows 7 machine, and my local webserver is running on a separate CentOS 6.4 machine. I've done a phpinfo; to verify that Xdebug is being loaded by PHP, and it is. The following are the settings for my Xdebug in the PHP.ini.

This tutorial describes how to use an SSH tunnel to setup a secure connection between the development machine and a remote server. This can be useful for debugging code on a remote machine when there are firewalls in between, or a NAT router prevents direct connection, or the ISP or network infrastructure does not allow incoming TCP connections to the developer machine.

Prepare the debugging engine

Before you start debugging, make sure that you have a debugging engine installed and configured properly. PhpStorm supports debugging with two most popular tools: Xdebug and Zend Debugger. These tools cannot be used simultaneously because they block each other. To avoid this problem, you need to update the corresponding sections in the php.ini file as described in Configure Xdebug and Configure Zend Debugger.

Open the active php.ini file in the editor:

Phpstorm xdebug settings
  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.

When using Xdebug, make sure at least the following settings are specified:

[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>'

Listening for incoming debugger connections

In PhpStorm, enable listening to incoming debug connections by either clicking on the toolbar or selecting Run | Start Listening for PHP Debug Connections in the main menu. This will ensure PhpStorm reacts when a debugging session is started and opens the Debug tool window automatically. Before launching the script, make sure that either a breakpoint is set or the Break at first line in PHP scripts option is enabled on the Debug page of the Settings/Preferences dialog Ctrl+Alt+S.

Docker

Set up an SSH tunnel to the remote machine

What we want to do is connect to the remote machine over SSH and set up port forwarding for port 9000 (for Xdebug 2), 9003 (for Xdebug 3), or 10137 (Zend Debugger). The idea is to create a 'virtual' TCP port on the remote server that sends its traffic to a TCP port on our own machine, tunneling traffic over SSH.

The SSH tunnel is used for connecting through a firewall and establishing a secure connection between the remote server and the developer machine. When the remote server can connect to the developer machine directly (for example, with a Vagrant machine), an SSH tunnel may not be needed.

In this case, we need to make the debugger connect back to the developer machine by setting xdebug.remote_host=ip_address (for Xdebug 2), xdebug.client_host=ip_address (for Xdebug 3) or making sure the debug host is the IP address of the developer machine (for Zend Debugger). This can be done using the PhpStorm bookmarklets, a Browser Debugging Extension, or the techniques outlined in Debugging PHP CLI scripts with PhpStorm.

The setup process depends on the operating system and the debugger being used.

  • Run the following command on the command line:

  1. Download and install Putty.

  2. Configure the connection to the remote machine by providing the hostname and port.

  3. Expand the Connection | SSH | Tunnels node on the left and add a new forwarded port.

    • The source port will be 9000 (for Xdebug 2), 9003 (for Xdebug 3), or 10137 (for Zend Debugger).

    • For destination, enter localhost:9000 (for Xdebug 2), localhost:9003 (for Xdebug 3) or localhost :10137 (for Zend Debugger).

    Make sure to select the 'Remote' option and then click Add.

    To save the connection parameters for further use, switch to the Session node, select the session in the Saved Sessions list, and click Save.

    Click Open to connect to the remote server and setup the SSH tunnel.

Make sure that you are running a command to create an SSH tunnel from the developer's machine to the server (not vice versa).

When a machine is rebooted or the connection is lost, the SSH tunnel has to be reestablished.

Debug

Once the SSH tunnel is set up, we can start debugging using zero-configuration debugging with Xdebug or Zend Debugger. When the debugger is started, PhpStorm will prompt to accept the incoming connection.

Once we accept it, we will be able to debug using the techniques outlined in Examining a Suspended Program.

Debug PHP CLI scripts with remote PHP interpreters or via SSH tunnel

When a remote PHP interpreter is properly configured, it's possible to create a run/debug configuration taking advantage of the remote PHP interpreter for debugging.

When the SSH tunnel is up and running, we can also debug PHP CLI scripts. Since the debugger runs on a remote machine, starting a CLI debugging session can be done by using PHP command line switches or using environment variables (on the remote machine). This workflow is not the officially recommended way of debugging, though it might be useful in some cases when the debugging session is needed to be established from the remote server side.

Troubleshooting

The debugger never connects or refuses the connection

When the debugger never connects or refuses the connection, check the following:

  • Make sure Xdebug is configured to connect to 127.0.0.1 and port 9000 (for Xdebug 2) or port 9003 (for Xdebug 3).

  • When using Zend Debugger, make sure the PhpStorm bookmarklets or Browser Debugging Extension is configured to connect to 127.0.0.1.

  • Make sure PhpStorm is listening for incoming debugger connections prior to setting up the SSH tunnel.

  • When a machine is rebooted or the connection is lost, the SSH tunnel has to be reestablished.

Phpstorm

Remote file path is not mapped to any file path in project

In some cases, the debugger can connect, but we get the error messages indicating that no mapping between the remote and project files is defined. This means that PhpStorm cannot determine which local file corresponds to the file being debugged.

We can solve this problem by clicking Click to set up path mappings and providing the necessary path mappings.

Additionally, we can configure these mappings using the techniques outlined in Configure synchronization with a Web server.

Postman started out as an API development tool, but has developed more into, as they put it, an 'API Development Environment'.

Xdebug

An over-simplified description is it allows you to create and save requests to test your API. You can then save your requests as a *collection* and share or publish them for others to use.

It supports API calls for all request types, fine control over headers, common authentication methods, and long list of other features that could fill an article... but I wanted to write specifically about using Xdebug with Postman.

How to trigger Xdebug when working with Postman

Recently, I was using Postman to troubleshoot an [API Platform](https://api-platform.com/) project and needed to trigger a Xdebug session in my IDE, [PhpStorm](https://www.jetbrains.com/phpstorm/).

Phpstorm Xdebug Setup

Typically, I rely on a browser extension to trigger Xdebug, however Postman is its own application, so I could not rely on browser extensions.

Fortunately, there's an easy solution: Add `XDEBUG_SESSION_START=PHPSTORM` to the query string and PHPStorm will catch the debug session as it would if you were using a browser extension.

For example, if you are working on `http://localhost:8000/api/users`, simply append the URL with the query string mentioned above to initiate a debug session:

Phpstorm Xdebug Firefox

Now you can set breakpoints and debug as you would if you were working in a browser.