In this recipe we will learn how to improve performance of any application written in PHP by creating a brand new caching system. This recipe is relevant only if your server stands on a dedicated server and you have a root access to it.
We will install Alternative PHP Cache (APC) on our Unix-based server. In order to install this caching system on our server, we will need to import several packages, as done in the following command line:
sudo apt-get install php-pear php5-dev apache2-threaded-dev make
When all these packages are successfully installed, we can go ahead and install APC by using the following command:
sudo pecl install apc-3.1.4
Then you have to create a file named
apc.ini
under/etc/php5/conf.d/
. Add the following content in the newly created file:extension=apc.so
Create a file named
info.php
that contains the following code and upload it:<?php phpinfo(); ?|
Open
local.xml
under/app/etc/
and locate the following line:<global>
Then add the following lines after the previous line:
<cache> <backend>apc</backend> <prefix>yourStoreName</prefix> </cache>
Restart your web server.
The Alternative PHP cache should now be installed on your server; you can check if it's working correctly by browsing to the info.php
file. If there is an APC section, it means that everything is done correctly.
Using the same web stressor, the Apache benchmarking tool, we will compare the performance when our PHP accelerator is on and when it is off.
Type |
Min (milliseconds) |
Max (milliseconds) |
Median (milliseconds) |
Requests/second |
---|---|---|---|---|
APC off |
432 |
744 |
452 |
2.17 |
APC on |
231 |
686 |
268 |
3.54 |
Another relevant improvement! Indeed, median time to complete a request was reduced by almost 40 percent. So your server can now handle 3.54 requests per second, which is almost a 60 percent improvement!
PHP accelerators are extensions made for improving the performance of any application written with PHP. The main aim is to cache the PHP byte code in order to skip all the parsing and compiling processes when a processor executes PHP scripts. The cached code uses the shared memory so that it can be executed from there. On an average, the improvements that come with a PHP accelerator vary by two or seven.