inicio mail me! sindicaci;ón

remote debugging PHP for a single virtualhost with PhpStorm and xdebug

I struggled for several days to set up remote debugging. I wanted to be able to remotely debug a single virtualhost on my CentOS server using my local IDE (PhpStorm) and xdebug.

Since this took me quite a while to figure out, I thought I’d include the steps here for others.

Installing Xdebug

Over SSH, I ran the following:

$ pecl install xdebug

Configuring Xdebug in php.ini

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
zend_extension=/usr/lib/php/modules/xdebug.so
 
[xdebug]
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.idekey=PHPSTORM-XDEBUG
xdebug.remote_connect_back=1
 
[Date]

Note the use of zend_extension; as of PHP 5.3, this is the only one that works (extension=…) will appear to function, but never connect!

Also, I used xdebug.remote_connect_back=1 – this allows connections from anywhere. But since I’ve configured my server to load a separate php.ini for each site (sorry, this is outside the scope of this discussion), and the entire site is under development and protected by .htaccess, I didn’t have to worry about other people debugging remotely.

An alternative to using remote_connect_back is to specify the remote_host to your local ip address:

xdebug.remote_host=0.0.0.0 ;your ip address
;xdebug.remote_connect_back=0

 Alternative: Loading via .htaccess instead of php.ini

Alternately, I could have dropped a .htaccess file into my root directory for the virtualhost, if the php.ini for each site were too much trouble:

php_value  zend_extension=/usr/lib/php/modules/xdebug.so
php_flag   xdebug.remote_enable on
php_value  xdebug.remote_port 9000
php_value  xdebug.idekey PHPSTORM-XDEBUG
php_flag   xdebug.remote_connect_back on

Configuring PhpStorm

I followed these instructions to the letter. Make sure you set up the “PHP Remote Debug” and not the “PHP Server” option if you’re using my list here as a guide (both work, I just chose the remote)

Remember to get those IDEKEY values to match up in the editor and on the server!

Configure Firewalls

On CentOS, I use apf, so I configured my firewall by adding port 9000 into the following settings: IG_TCP_PORTS, EG_TCP_PORTS

Then I restarted apf:

$ /etc/init.d/apf restart

At home, I opened a connection in my firewall by adding a redirect for port 9000 to my personal PC.

Starting it Up

Remember to reload Apache’s config!

$ /etc/init.d/httpd reload

Make sure PhpStorm is set to break on first line, or you won’t know if it worked! (you need the debugger to get involved):

 

Start the PhpStorm Remote Debug:

 

Make sure to click the “listen for connections” icon!

A quick note on semantics here: It is not technically necessary to have the debug server running and “listen for connections” clicked. Listen for connections will, in fact, start the debugger when an incoming connection is received. However, for initial testing, having the debugger actually ON was very helpful in being sure PhpStorm wasn’t failing to start incoming connections.

 

Connect to your IP address from the web server to make sure it can talk to your IDE:

$ telnet 0.0.0.0 9000

 

Add XDEBUG_SESSION_START=PHPSTORM-XDEBUG in the URL of the site to initiate a connection:

 

Witness debugging in all its glory!

 

Browser Plugins

There are browser plugins for Chrome and Firefox that make remote connections a breeze; you no longer have to type the parms into the URL. I’ve used both of these with success and highly recommend this simplification.

WebKit Enhancements

For the web devs, Safari‘s WebKit has been updated, providing some great new tools for writing the web and debugging Safari issues. They also wrote up a great overview of the features.

Together with Firebug the Venkman Toolbar for Firefox, that’s a great set of tools.

Now if IE would just join the bandwagon and offer us more than “null is null or not an object”!