Wednesday, December 5, 2012

Debugging PHP

Debugging PHP without an IDE (Integrated Development Environment) can be difficult at first. But it's actually pretty easy using the Apache error log. Of course, a full blown IDE will probably offer many more debugging functionalities but for a quick debug the logs can be a great help. This short tutorial assumes you already have a VPS running with Apache and PHP. If you need help to setup a LAMP server, see the previous articles about creating a fully functional VPS webserver:
http://vpstips.blogspot.nl/2010/09/setting-up-lamp-vps-part-1.html
http://vpstips.blogspot.nl/2010/10/setting-up-lamp-vps-part-2.html

To start debugging create a simple PHP script with an error:
echo "<? echo phpinf(); ?>" > /var/www/vps.error.php
This simple script mistakes phpinfo for phpinf (without the letter "o" at the end). This results in a fatal PHP error. The phpinfo functions dipslays some general information about your server and PHP environment. Details: http://php.net/manual/en/function.phpinfo.php

To debug this script look at the Apache error log.
tail -f /var/log/apache2/error.log
Depending on your distribution the log location may vary and you might need root permissions to open the file. The error log will show you an error similar to this one:
PHP Fatal error: Unknown function: phpinf() in...
The error log is a quick way to debug PHP errors. Default PHP installations will supress error messages so if you get a blank page or a half loaded page in your browser it might be worth checking your Apache error logs!

VPS upgrades

Most VPS providers offer various packages allowing you to upgrade your server as you see fit. Because a VPS can be relocated to a new server and the provider often takes care of backups or snapshots, a VPS is easy to maintain when sustainig sudden growth. However, there comes a time when outgrowing a VPS can become reality. As a general rule of thumb 50-100 USD/EUR a month can hire a budget dedicated server providing at least 4GB ram with 250GB disk. When your VPS provider starts asking double this amount (remember they take probably care of backups) it's time to ask for a custom quote, relocate the VPS or go for a dedicated server.

SSD VPS

I've got some questions about VPS performance and mainly about disk performance. It's not uncommon for some budget VPS providers to offer an environment stored on a RAID array. RAID is great for data redundancy and increased disk speed but on a busy server your VPS performance can degrade to suboptimal levels. Detecting an oversold or overused VPS with disk problems is generally easy:
  • Saving a simple text file takes over 2 seconds
  • Starting disk intensive applications is extremely slow, at least two times slower than on a dedicated machine

Many VPS providers now offer SSD variants to overcome these problems. I'm currently testing a VPS from DirectVPS (https://www.directvps.nl/vps_ssd.plp) where disk activity was often the bottleneck on their regular line. So far, the results have been good but concrete benchmarks are missing. However, problems with extremely long disk writes are gone!