Remote Debugging via Reverse Port Forwarding with PHP SSH

PHP
2017-04-28 12:25 (7 years ago) ytyng

When remote debugging PHP on a production server or another distant environment:

Assume that the development PC is within an internal corporate LAN and cannot be accessed directly from the outside.

In this case, create a reverse SSH tunnel to enable communication between the production server and the local PC.

PHP xdebug configuration file on the production server

Example: /etc/php/5.6/apache2/conf.d/20-xdebug.ini

[xdebug]
zend_extension=xdebug.so

xdebug.remote_enable=1
xdebug.remote_port="9000"
xdebug.profiler_enable=0
xdebug.profiler_output_dir="/tmp"
xdebug.max_nesting_level=1000
xdebug.idekey = "PHPSTORM"

When you run phpinfo(), you will see something like "Scan this dir for additional .ini files /etc/php/5.6/apache2/conf.d" which indicates where to find the configuration file.

With the above settings, during debugging (when the URL’s GET parameter includes something like ?XDEBUG_SESSION_START=12345), it will connect to port 9000 on the localhost.

On the development local PC, execute:

$ ssh -R 127.0.0.1:9000:127.0.0.1:9000 user@example.com

This will tunnel the production server's port 9000 to the local port 9000.

Then, when you run the debugger in PHPStorm, it will break at the breakpoints and allow you to debug.

Please ensure that you properly set up the server path mappings, otherwise it will not break properly.

Incidentally, since I use Python's Fabric for deployment, I create the tunnel with a command like this:

@runs_once
def xdebug():
    """
    Creates an SSH tunnel for xdebug
    """
    local('ssh -R 127.0.0.1:9000:127.0.0.1:9000 {}@{}'.format(env.user, env.hosts[0]))

Current rating: 1

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011