Install , Configure, Fine tune APC (Alternative PHP Cache) to Cut down CPU load on Server

APC performance

PHP has established itself well enough to be one of the most Popular Web programming language. PHP needs to be optimized sometimes in order to get better optimization and speed enhancements.

While getting accessed, every page in PHP needs to First get parsed by Interpreter, followed by a conversion to an intermediary format called opcode. At last, it finally gets converted into machine code which is executed by the server’s CPU.

This process is acceptable for Some cycles. But 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.

The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.

(Linux…)

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 additional packages first:
yum install httpd-devel make

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=100

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

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.

Some performance charts for APC.

apc performance boost

linuxapcphpinfo

Speak Your Mind

*