How to debug remote PHP code?

Are you maintaining, developing, debugging PHP application? Been in a situation to debug remote php code when site is in production? Are you taking your site down or using immatured die() or exit() while site is live? Gone are the days, now you can enjoy debugging without any disruption in service. You need to install xdebug on the remote server (only once).

php debugging on local system

The process of Xdebug installation differs from OS to OS. In this article we are covering Amazon Linux. Let’s install it on Amazon Linux.

$ sudo yum install php-pear
$ sudo pecl install xdebug

Forward the local xdebug port to remote server.

$ ssh -R 9669:127.0.0.1:9669 ec2-swatantra@your.remote.host

Run an xdebug client, like macGDBp, or Netbeans debugger.

Make sure to match the port forwarded above and set an IDE key in the debugger you are using.

Local Port: 9669
Remote Port: 9669
IDE Key: xdebug-swatantra

Also you can configure local to remote path mapping on Netbeans or on macGDBp to use code navigation with your local checked out code.

Local Path: /Users/swatantra/workspace/php-code
Remote Path: /home/ec2-swatantra/workspace/php-code

Initiate debugging on your debugger of choice.

Run the php cli command on remote host, remember to use the IDE key you wrote above.

$ alias phpdr="php -d zend_extension=/usr/lib64/php/5.6/modules/xdebug.so -d xdebug.remote_enable=1 -d xdebug.remote_autostart=1 -d xdebug.remote_host=127.0.0.1 -d xdebug.remote_port=9669 -d xdebug.idekey=xdebug-swatantra"
$ phpdr /home/ec2-user/workspace/php-code/src/script.php --option=value
php debugging on remote system

You can save the above alias ‘phpdr’ in your .bashrc or .zshrc based on your choice of shell.

'Coz sharing is caring

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.