Categories
App linux PHP

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

By Swatantra Kumar

Swatantra is an engineering leader with a successful record in building, nurturing, managing, and leading a multi-disciplinary, diverse, and distributed team of engineers and managers developing and delivering solutions. Professionally, he oversees solution design-development-delivery, cloud transition, IT strategies, technical and organizational leadership, TOM, IT governance, digital transformation, Innovation, stakeholder management, management consulting, and technology vision & strategy. When he's not working, he enjoys reading about and working with new technologies, and trying to get his friends to make the move to new web trends. He has written, co-written, and published many articles in international journals, on various domains/topics including Open Source, Networks, Low-Code, Mobile Technologies, and Business Intelligence. He made a proposal for an information management system at the University level during his graduation days.

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.