Source: ivanblagojevic.com

APC is the Alternative PHP Cache. It’s a handy caching utility for use with PHP. We use it with W3 Total Cache on our WordPress installations.

It runs OK “out of the box.” But you likely need to tune the config a bit to get the most out of it. The most common tweak is to increase the shared memory allocation from the default 32M to something higher. But what’s the right number?

If you already have APC installed and running, the first step is to install the APC monitoring script. Assuming your web root is at /var/www/, you can easily install it like this:

$ sudo cp /usr/share/doc/php-apc/apc.php.gz /var/www
$ sudo gzip -d /var/www/apc.php.gz

NOTE that you likely want to lock this file down a bit so that it’s not accessible to the entire world!

To use the script, you need to edit it to change the default password:

$ sudo vim /var/www/apc.php

Once installed you can access the script in your browser at http://[your domain or ip address]/apc.php

Then take a look at the Cache Full Count stat in the the File Cache Information section. This shows how often APC is filling the cache. In other words, how often is APC running out of memory. You want this to be a low number, as close to zero as possible.

You can also look at the Memory Usage pie chart and the Hits & Misses bar graph. You want to see at least a small cushion of available (green) memory in the pie chart. You also want to minimize the fragmentation. On Hits & Misses, you want a very high hit rate and a very small miss rate. Note that if you just restarted Apache, the miss rate might be low until you have had some traffic. Over time, the hit rate should be increasing. Our production ht rate is in the high 90 percent.

If the Cache Full Count is high (or increasing), the memory pie chart shows little memory and high fragmentation, or the hit rate is low, you need to increase memory. To do this, edit the config file. Note that it’s likely almost empty:

$ sudo vim /etc/php5/apache2/conf.d/apc.ini
1

Increase the memory by adding (or changing) apc.shm configuration parameter.  Here is our config file, that sets the memory to 128M:

1
extension=apc.so
apc.shm_size=64
apc.stat=1

You’ll need to restart apache after you make this change:

$ sudo /etc/init.d/apache2 graceful

Once you make this change, check the APC monitor script. With a little tweaking, you’ll find the right value for your site that keeps the cache full count low and the hit rate high.