If you want to debug PHP sites, you need to install a great deal of programs and you need a decent IDE. In this guide, I will show you exactly how to install XDebug on a CentOS server and what some of the most common problems are

Tip: Do Not Install XDebug Via PECL

It may be tempting to install xdebug via PECL, by simply entering pecl install xdebug but this will not work well on most systems, including CentOS WHM servers. You will get the error failed loading xdebug.so when you do so. It is better to download and compile xdebug manually and you can be sure it will properly install xdebug as a zend extension and not a regular PHP extension

Compiling From Source: How To

1. Download the source from here – as of right now the latest is 2.2.3 (MD5: e49cec9861b45dc0b36eae33bf8a14fa)

Xdebug Source Md5

2. Open up Putty and enter cd /usr/bin, then run the code below to compile xdebug from source

Instructions for 2.2.3 (December 2013)

wget http://xdebug.org/files/xdebug-2.2.3.tgz
tar -xzvf xdebug-2.2.3.tgz
cd xdebug-2.2.3
phpize
./configure
make
make install

Configure Xdebug via PHP.ini

Alright, if you’re on a WHM server, enter

cd /usr/local/lib/
pico php.ini

Now at the top, it’s likely that you will already see zend_extension. If not, this is the most important setting to add, verify that the path is correct, don’t be lazy and check if xdebug.so actually exists

Copy and paste this at the top and modify xdebug.remote_host to match your current IP address

zend_extension = /usr/local/lib/php/extensions/xxx/xdebug.so
xdebug.remote_log="/tmp/xdebug.log"
xdebug.profiler_enable = 1
xdebug.remote_enable=on
xdebug.remote_host=YOURIP!
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.idekey=phpstorm-xdebug

A setting you may consider adding, but be warned that this has security implications and once you add this any IP addresses that requests your PHP app will be able to access the debug information

xdebug.remote_connect_back = On

Explanation from Xdebug Docs

If enabled, the xdebug.remote_host setting is ignored and Xdebug will try to connect to the client that made the HTTP request. It checks the $_SERVER[‘REMOTE_ADDR’] variable to find out which IP address to use

Confirm Xdebug is loaded properly

Enter the following into Putty / Shell:

php -i | grep xdebug

If you see that it returns “Failed loading Xdebug.so” you need to go back to the top and re-compile from scratch

Download A Good IDE: PHPStorm!

IDE’s are a must-have if you are serious about developing PHP sites. Download PHPStorm which is one of the best IDE’s on the market aside from Eclipse and NetBeans

1. Open up PHPStorm and click on File->Settings

2. Select PHP, then Debug

3. Enter the Debug port 9000 and make sure the checkbox Can accept external connections is checked

Xdebug Port

4. Now it’s time to tell PHPStorm to start listening on port 9000 (or 9001 or whatever you chose).

5. Hit ALT+SHIFT+F10

6. If you haven’t added localhost as a connection yet, do so now. Simply enter 127.0.0.1 as the host and use 9000 as the port and save the connection

7. Then hit the “green-bug-button” and check the bottom for the debugger panel

Phpstorm Listen On Port 9000.Png

Debugging Php Applications.png

Verify That Port 9000 Is Open And Your Firewall Is Forwarding It Properly

Now it’s time to see if an outside service can actually connect to your local machine on port 9000.

1. Open up http://canyouseeme.org/

2. Enter port 9000 and hit OK

Verify That Port Is Being Forwarded.png

3. Should you use a fritz, Cisco or Netgear router, make sure the router is forwarding port 9000. For testing purposes you can add an exposed host – make sure to remove this as soon as possible because it leaves you vulnerable to outside attacks (all ports are being forwarded when adding an exposed host)

Download Firefox And Chrome Extensions

A good web developer usually tests in different browsers, so download the Firefox and Chrome extensions:

Specify IDE Key

Alright, now let’s do a quick test and load a site on the server. You may have to add the correct IDE key first if you did not add the option remote_connect_back!

Firefox:
Specify Ide Key In Firefox

Chrome:
The options for the Chrome extension can be accessed via: This Link

After you have specified the key, hit F5 and if you followed all steps above correctly you should now get your first debug variables. If you don’t verify that your ports are correctly forwarded, that you set up the IDE-keys. Also restart your apache server and possibly restart your IDE and browsers, just to be sure.

Debug Variables.png

Useful Sources And Links