Troubleshooting

Xdebug: debug PHP code remotely with PhpStorm or NetBeans

Reviewed on 7 min read debugging PHP

Debugging with var_dump, print_r or dd() works in most cases, but some errors only make sense when you follow the code step by step. That is what Xdebug is for: a PHP extension that connects to your editor (PhpStorm, NetBeans, VS Code…) and lets you set breakpoints, step through line by line and see the value of every variable.

It is normally used locally, but sometimes the fault only happens on the server. In this guide we connect your IDE to the website on your hosting through an SSH tunnel, without opening any port to the Internet.

Xdebug slows PHP down while it is enabled, and the IDE will see the data the request processes (passwords, customer data). Only enable it while you are debugging and disable it when you finish.

1. Enable Xdebug in cPanel

  1. In cPanel, go to Select PHP Version.
  2. In the Extensions tab, tick xdebug.
  3. In the Options tab, find xdebug.mode and set it to debug. This value can only be changed here: it has no effect in .htaccess.

With PHP 7.2 or later the extension is Xdebug 3, which uses port 9003. With PHP 7.1 or earlier it is Xdebug 2, which uses 9000; in that case replace 9003 with 9000 in every step.

Then add the following at the top of the website's .htaccess:

php_value xdebug.start_with_request trigger
php_value xdebug.client_host 127.0.0.1
php_value xdebug.client_port 9003
php_value xdebug.trigger_value MI_CLAVE_DEBUG

Replace MI_CLAVE_DEBUG with a word only you know. With start_with_request trigger, Xdebug is only activated on requests that carry that key, not on normal visits.

2. Open the SSH tunnel

Xdebug calls the IDE, not the other way round: from the server it tries to connect to 127.0.0.1:9003. The SSH tunnel picks up that connection on the server and carries it to port 9003 on your computer, where the IDE is listening.

First you need SSH access enabled on your hosting. If you want to log in without a password, set up an SSH key.

Linux and macOS

Open the terminal and run:

ssh -N -p 93 -o ExitOnForwardFailure=yes -R 127.0.0.1:9003:127.0.0.1:9003 USUARIO_CPANEL@TU_DOMINIO.COM

Replace USUARIO_CPANEL and TU_DOMINIO.COM with your own. It will ask for your cPanel password if you are not using a key. Leave the terminal open while you debug: if you close it, the tunnel closes. If the command ends with a forwarding error, port 9003 is already in use on the server; use another one (for example 9010) in the tunnel and in xdebug.client_port.

Windows with PuTTY

Download and install PuTTY. When you open it, type your domain in Host Name and set Port to 93:

PuTTY start screen with the domain and port 93

In the side menu go to Connection > SSH > Tunnels and fill in:

  • Source port: 9003
  • Destination: 127.0.0.1:9003
  • Select Remote and Auto, and click Add.

Remote tunnel in PuTTY (the screenshot shows Xdebug 2's port 9000; with Xdebug 3 use 9003)

Leave Remote ports do the same unticked. Click Open, enter your cPanel username and password, and leave the window open while you debug.

3. Set up the IDE

The IDE needs the same files as the server and needs to know which server path each one corresponds to (path mapping). The easiest way is to keep the project synchronised with the hosting via FTP or SFTP.

PhpStorm

  1. Set up the project with the remote server (deployment via FTP/SFTP). In Settings > PHP > Servers, enter the domain and the mapping between your local folder and the server's (for example /home/USUARIO_CPANEL/public_html).
  2. In Settings > PHP > Debug, check that the Xdebug port is 9003.
  3. Click Run > Start Listening for PHP Debug Connections.
  4. Set a breakpoint and load the website with the key (see step 4).

PhpStorm will show an incoming connection notice; accept it and you will be in control:

PhpStorm stopped at a breakpoint with the remote Xdebug session

NetBeans

With the project already connected to the server (for example via FTP), go to Preferences > PHP > Debugging and fill in:

  • Debugger Port: 9003
  • Session ID: the same value you set in xdebug.trigger_value

PHP debugging options in NetBeans (the screenshot shows Xdebug 2's port 9000; with Xdebug 3 use 9003)

Save, add a breakpoint and run Debug Project. NetBeans will open the browser with the key and take control:

NetBeans debugging the remote code

4. Start debugging

Open the page you want to debug, adding the key to the URL:

https://tudominio.com/pagina?XDEBUG_SESSION=MI_CLAVE_DEBUG

Xdebug saves a cookie, and the following requests in that session are debugged too. You can also use the Xdebug Helper browser extension (Chrome or Firefox) with the key as the IDE key: you switch debugging on and off with one click.

If the IDE does not receive the connection:

  • Check that the tunnel is still open and that the IDE is listening.
  • Check with a phpinfo() that the xdebug section appears, that xdebug.mode is debug and that xdebug.client_port matches the tunnel's.
  • If the IDE stops but not at your breakpoint, check the path mapping.

5. When you finish

  1. Close the tunnel (the terminal or the PuTTY window).
  2. Remove the xdebug.* lines from .htaccess.
  3. In Select PHP Version, untick xdebug.

Simpler alternatives

If what you are looking for is why the website is slow, you do not need Xdebug: PHP X-Ray tells you which functions, queries and external calls take up the time, without setting anything up on your computer.

Another option is Ray, by Spatie (paid): it replaces dump() by showing the information in a desktop application, and it can receive data from a remote server via SSH.

You may also find useful

Something wrong or out of date? Tell us and we will review it.