Reduce CPU load with Alternative PHP Cache

alternative php cachePHP is by far the most popular scripting language for dynamic web pages but it is not the most efficient language. Each time a PHP page is accessed it has to be:

  • Parsed by the interpreter
  • Converted into intermediary format called opcode
  • Converted into machine code for your server’s CPU to execute it

If the same script is accessed 100 times these steps are repeated a 100 times. To avoid this repetition and waste of server resources you can use the Alternative PHP Cache or APC.

APC is a PECL extension that caches frequently accessed PHP files code in opcode format for faster execution thereby reducing CPU load on your web server. In this guide I write about how to install and configure APC.

Installing APC

Installing APC is easy if you have PHP 5. These instructions are for a centos 5 system.  First su into your root account:

su -

Enter the root password at the prompt and then type this:

pecl install apc

If everything goes to plan you should see a successful APC installation.

If not then you may need to install some additonal packages first:

yum install httpd-devel make pcre-devel php-devel

Configuration

Once APC is installed you need to configure it. Most centos installations have a /etc/php.d directory for per-extension PHP configuration files. If you find such a directory on your system then add the following code in a new file apc.ini in that directory. If not then add it into the php.ini file that is likely in /etc/ or /usr/local/lib/.

extension=apc.so
apc.enabled=1

APC uses what is called a shared memory segment to cache php scripts. This allows multiple processes to access the cache. You can configure how large that segment is in megabytes:

apc.shm_size=100M

Check that apc is loaded by typing this in at the shell:

php -m

If you see apc in the list of php modules then you know apc is working. If you don’t then make sure that your extension_dir option in php.ini is set correctly.

To make APC cache web scripts you will need to reload apache:

service httpd reload

Copy the file apc.php from the php directory (either /usr/local/lib/php or /usr/share/php) to a web accessible directory. Accessing this file from the web will tell you some interesting statistics about apc.


Fine tuning APC

apc.php statistics display.

By default APC will cache every php file requested by visitors to your website. On a typical server you will have many thousands of source files. So if you monitor the output of apc.php you will find that with time even 100MB of memory is not sufficient for APC. It will keep running out of space as it tries to cache all the files.

To deal with this APC will swap out old files from the cache according to this setting which specifies the time to live in seconds of files that have not been accessed recently:

apc.ttl=1300 
Selective caching

You may prefer to cache only some of the files such as those used by the most popular websites on the server. To do that you must first set this option:

apc.cache_by_default=0

Then specify a regular expression of the files you do want to cache:

apc.filters="+(example|verypopularsite|favourite\.php)"
 

The advantage of selective caching of only popular sites is that you can specify a smaller shared memory size and still reduce CPU load significantly.

APC is a great PECL extension written by some of the core developers of PHP. Its easy to install and configure and can reduce CPU load dramatically. In my experience it reduced peak CPU load from ~5 to 1.4 on a dual core system! I hope this guide helps you achieve similar CPU resource savings.


3 thoughts on “Reduce CPU load with Alternative PHP Cache

  1. To display the statistics you can also use the [b]apc_cache_info()[/b] command (less graphical but all the info is accessible from the array returned)

    http://blog.bottomlessinc.com/2010/03/display-apc-cache-statistics/

Leave a Reply

Your email address will not be published. Required fields are marked *