Alternative PHP Cache (APC) is a neat little tool, which optimises the bytecode PHP compiles and stores the results in a pool of shared memory. Normally PHP will load your site’s PHP code from the disk, compile some bytecode from the PHP code which not only takes time (resulting in a slower “time to first byte” when measuring site speed) but also reducing the capacity of your site to field large volumes of requests.
In this post, I’ll walk you through setting up and initial APC installation with minimal configuration.
The first step is setting up APC, in my case using ubuntu the command was:
apt-get install php-apc
The next step, is to edit (for ubuntu) /etc/php5/conf.d/apc.ini.
You’ll fine that it only contains a line to load APC. You also need to tell php to enable it.
Replace it’s contents with the following:
extension = apc.so
apc.enabled = 1
apc.optimization = 0
apc.shm_segments = 1
apc.shm_size = 128
apc.enable_cli = 1
Don’t comment any lines out in this file, as it will cause all kinds of havoc later on (it still tries to interpret parameters after a #).
Now restart Apache:
service apache2 restart
If you run a php file that returns php_info(), you’ll now see:
If you don’t see this, APC isn’t installed and running. You should go through each step, and check that they are correct. If you omit the “extension = apc.so”, for instance, you won’t see this block in your php_info().
Now that you have APC working, it’s time to configure magento to use it.
Before we do, we are going to set up a php file we can use to monitor APC.
Download apc.php from here.
To start with, put it in your magento folder. You’ll want to protect this page in the future or remove it altogether. When you access apc.php on your magento store URL, you should see something like the following:
Now that this is working, all we need to do is tell Magento to use apc.
You’ll need to edit magento/app/etc/local.xml
Add something like the following:
INSIDE your tags. I put this after . This is all that’s required to get APC to work with magento.
It’s important to dump the cache files in your magento/var folder. I removed all the files in magento/var (they will be recreated as needed). If you don’t do this, you will get all kinds of magento errors.
Hopefully I’ll follow this article up with one discussing the performance benefits.