I read a lot about the speed of programming languages that can be used for web apps lately. A lot of people think Ruby is cool because it’s so easy to build stuff. On the other hand, it will be probably be very slow. This is the same for everything that’s build in ASP and .NET. The last language can’t even be called a proper language, it’s just pure crap.
The main thing everyone is discussing about is how fast a language is once you’ve written a program. I still think you need to choose the right tool for the job, but I think PHP is great. It’s easy to program and it can do virtually anything. ( In comparison to other languages that are commonly used for web apps ). Maybe I will give Python a go in the future too. Anyway, one of the big reasons people would not use PHP for their web apps is the speed. PHP isn’t the fastest language there is, no. So I thought: how can I improve this speed?
I read a lot about people building OOP frameworks with caching mechanisms in it, but eventually this will be slower than just coding it without the framework. I heard about Xcache and APC. Xcache just was to damn hard to install the last time I checked it ( no Debian packages and at that moment no PHP5 support ). APC just worked different than I thought. You have to use APC in your code in order to cache things.
That wouldn’t be practical because then I have to change all of my code to use APC. I read a comparison on a other blog a while ago about the comparison between Xcache, APC and eAccelerator. I hadn’t heard of eAccelerator. The benchmarks proved that eAccelerator was 113% faster than APC in caching. For me it was obvious that eAccelerator would be a good choice. I tested it and my webapps really got a LOT faster.
eAccelerator works with PHP5 and PHP4.
Here’s how to install it. This works for Debian GNU/Linux and Ubuntu Feisty. It will probably work for Edgy too.
Open a terminal.
First we need to change the directory to /usr/src/local/ because this is a good place to compile binary’s and consequently keep your system clean.
Then we will download the latest and greatest eAccelerator. At the moment of writing this 0.9.5.1.
sudo wget http://bart.eaccelerator.net/source/0.9.5.1/eaccelerator-0.9.5.1.tar.bz2
Then we need to extract the package.
sudo tar xjvf eaccelerator-0.9.5.1.tar.bz2
Then we need to change the directory to what we just extracted.
Now we need to make sure we have to right stuff to compile eAccelerator. Note: If you use PHP5, use php5-dev, for 4, use php4-dev.
sudo apt-get install build-essential phpx-dev
Now we need to run phpize.
Now this is done we need to configure the compile process.
sudo ./configure -enable-eaccelerator=shared -with-php-config=/usr/bin/php-config
When this is successful ( the last line is config.status: creating config.h ), do this:
When the build is complete ( it will tell you this ), do this:
sudo make install
It will now tell you something like this:
Installing shared extensions: /usr/lib/phpx/20060613/
Where x is your php version. Note that the date can be somewhat different.
Now the extension is successfully installed. Now we can edit the php configuration file in order to configure the extension.
sudo gedit /etc/phpx/apache2/php.ini
Where x is your php version.
Now scroll all the way down to the end of the file and past this:
Please note that the date that is in the zend_extension path matches the one that is given to you after the make install!
If you’re having a high cpu load on your system, put the compress_level somewhat lower.
Save the php.ini file and close gedit.
Now do the following:
sudo mkdir /var/cache/eaccelerator/
sudo chown root:www-data /var/cache/eaccelerator/
sudo chmod 770 /var/cache/eaccelerator/
Now test your php.ini configuration file and check if there are no errors:
sudo apache2ctl configtest
If there aren’t any errors ( tells you “Syntax OK” ), do the following:
sudo /etc/init.d/apache2 restart
Now eAccelerator is installed successfully. The only thing we need to do is copy the eAccelerator Control Panel to the right place.
Make sure the apache2-default directory exists in /var/www/.
You can do that this way:
sudo cp control.php /var/www/apache2-default/
Don’t forget to change control.php! You can edit the file with nano or gedit ( sudo gedit /var/www/apache2-default/control.php ). Change the $user and $pw settings to a username and password chosen by you. If you do not do this there is a chance someone might mess around with the settings of your server.
Now you can access the Control Panel with:
If your source code of your files change often, set accelerator.check_mtime to 1 in php.ini.
Now you successfully optimized the performance of your php scripts on your server. Play around with the settings of eAccelerator. You can always read the whole documentation of eAccelerator to get the whole idea of what you change.