in Debug, Environments, PHP

How to XDebug PHP on production or a remote application

Does you ever had an issue that only appears on production environment? Have you ever felt how easy could be to solve an issue if you can debug the live system instead of the localhost that just works fine?

Debugging a live system can be scary, but it is not if you follow the right steps. Follow this How to XDebug PHP on production and please comment.

Server setup

Check if your SSH Daemon have tcp forwarding enabled:

user@yourserver.com:~$ grep AllowTcpForwarding /etc/ssh/sshd_config
AllowTcpForwarding yes

Please double check that this line it is not commented with an # character, if so edit /etc/ssh/sshd_config  and uncomment it.

Locate where to add xdebug configuration:

root@server1:~# php --ini
Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File:         /etc/php5/cli/php.ini
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed:      /etc/php5/cli/conf.d/05-opcache.ini,
/etc/php5/cli/conf.d/10-pdo.ini,
/etc/php5/cli/conf.d/20-curl.ini,
/etc/php5/cli/conf.d/20-gd.ini,
/etc/php5/cli/conf.d/20-intl.ini,
/etc/php5/cli/conf.d/20-json.ini,
/etc/php5/cli/conf.d/20-mysql.ini,
/etc/php5/cli/conf.d/20-mysqli.ini,
/etc/php5/cli/conf.d/20-pdo_mysql.ini,
/etc/php5/cli/conf.d/20-pdo_sqlite.ini,
/etc/php5/cli/conf.d/20-readline.ini,
/etc/php5/cli/conf.d/20-sqlite3.ini,
/etc/php5/cli/conf.d/20-xdebug.ini

Edit /etc/php5/cli/conf.d/20-xdebug.ini  and add the following content:

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_host="127.0.0.1"
xdebug.remote_port="9000"

As you noticed you are editing the php5 cli configuration, but at least in Ubuntu 14.04 LTS the apache2 configuration is linked to the clil using soft links.

Developer host setup

On the developer machine we are going to setup a SSH tunnel to the server. This tunnel will redirect the remote (server) port 9000 to the localhost (developer host) port 9000 in order to connect to the local debugger/IDE.

We can do that with a normal ssh command:

ssh user@www.yourserver.com -p 22 -R 9000:127.0.0.1:9000

Another option is to configure it on your ssh config file ~/.ssh/config  :

Host yourservername
        HostName www.yourserver.com
        port 22
        User your-user-name
        RemoteForward 127.0.0.1:9000 127.0.0.1:9000

Once you have everything setup you just have to enable your debugger listener, crete the ssh tunnel and enable your XDebug browser helper.

If you enjoyed How to XDebug PHP on production you may also be interested on How to setup XDebug on OSX.

Please share your thoughts and tricks on the comments.

Write a Comment

Comment

  1. Hi

    Thanks for a great tutorial.
    Initially it didn’t work for me, as after changing xdebug settings I forgot to do
    sudo service apache2 restart

    I hope this helps.