Debugging PHP in Ubuntu
A buddy, dlachasse, and I have been learning up on PHP. We’ve been working on making a simple PHP based PDM (engineering Product Data Management) system, which we’ve conveniently dubbed as ‘repchill.’ I hope to make formal BOM structures to the different breeds of repraps with it.
Both being newbies to PHP and web-apps, we needed to debug some sections of our scripts. Variable monitoring, watch expressions, the ability to step in and out of sections of code–these are standard tools here! For such a simple task, why was it so difficult? There are millions of videos and tutorials all over the web, yet it still took me a couple of hours to get this running successfully. When I learned to program, it was in Visual Studio or the Excel VBA editor, and the debugger is right there out-of-the-box! When I did C, bloodshed had the debugger built in too. Open source isn’t always the easiest. Of all of the information posted, the information over on the netbeans website seemed to work best. Fear not, you can be debugging in no time too!
For those users who just want to get their debugger up and running, whether that be in Eclipse or Netbeans, follow these steps!
- Make sure your server is already setup with PHP enabled. If you are running Ubuntu, installing the LAMP (linux apache mysql php) stack is easy as pie. Click the LAMP link. This will turn your std system into a functional dev server in minutes!
- Write some HTML/PHP, and put it into your /var/www directory. This assumes you are using the default server root directory when you install such a LAMP.
- Create a phpinfo.php file. The contents should be exactly:
- Browse to your_server/phpinfo.php (on my machine, localhost/phpinfo.php). I just opened it up in chrome.
- Open another browser window. Visit http://xdebug.org/wizard.php. Copy and paste the entirety of the phpinfo page into here.
- Follow all of the instructions on the xdebug page. You may need to run a ‘sudo apt-get install php5-dev’ and ‘sudo apt-get install make’ if your system is fresh. php5 dev is the most recent as of this post. If you’re on an older or newer system than mine, just open up synaptic package manager in ubuntu, and search for ‘php.’ look for php#-dev. It’s a common package in the repositories. ALSO, this step will have you add something like zend_extension=/path/to/xdebug.so
- Download netbeans package geared towards php. It’s a .sh file, so you’ll have to make it executable before installing it. do a ‘chmod +x [downloaded filename].sh’. Then run a ‘./[filename].sh’ to install it!
- Wooof. Not done yet. We need to update our PHP.ini files to enable the debugger to get dirty with the server. you’ve should have two php.ini files. not sure if you need to update them both, but the netbeans site seemed to hint you do. Open both php.ini files (1. /etc/php5/apache2/php.ini, 2. /etc/php5/cli/php.ini). If you get a permissions error, open then in terminal via a ‘gksudo gedit /etc/php5/apache2/php.ini’ etc.
- Way back yonder, the xdebug instructions had you add the zend_extensions =… stuff. Immediately below that in BOTH files, add the following (no this isn’t me doing something custom to make it work, it IS the standard process)
- xdebug.remote_host=localhost [OR WHATEVER YOUR SERVER ADDY IS, i.e. 127.0.0.1]
Now, just restart your apache server. ‘sudo /etc/init.d/apache2 restart’
- BOOM, debuggin time! Open netbeans. Tool->Options->PHP->Debugging is where you can configure the debugger. Check it out. Make sure all looks zen and chi.
- Open a .php file. Double left click to the left of the line numbers to toggle breakpoints to stop the debugger at those points, and you can monitor variable values. Yahoo! Use the Debug menu commands to navigate through your script.
Let me know if that helped! Good luck