Magento APC cache

Many times we have heard how Magento is slow and bulky. Page load times are high. This in turn puts off customers and affects SEO values. By default Magento uses its file system to save cache. This can become restrictive on larger sites with all the reads and writes to the file system.

One quick and easy way to speed up Magento is to install APC – Alternative PHP Cache. On systems we have installed this we have seen up to 50% performance improvement.

What is APC – Alternative PHP Cache

APC is an opcode cache for PHP. It is a framework for caching and optimizing PHP code. APC also serves as a user cache. APC cache is stored in memory where performance gains over file system cache are huge.

Installation

CentOS

Install php-pecl-apc package

yum install php-pecl-apc

The location of the APC configuration file – apc.ini.

ls /etc/php.d/apc.ini

UBUNTU based systems

Install php5-apc package

sudo apt-get install php5-apc

The location of the APC configuration file – apc.ini.

ls /etc/php5/conf.d/apc.ini

Configuration

There are many options in here that can be configured to fine tune performance but we will focus on shared memory. Ideally you would want a minimum of 512MB but that is dependent on your total available memory. For detailed description of settings go to http://php.net/manual/en/apc.configuration.php

Edit the apc.ini file:

apc.shm_size=512M ; The size of each shared memory segment, with M/G suffix

Example of main configurable items. There are many more:

apc.enabled=1 ; This can be set to 0 to disable APC.
apc.shm_segments=1 ; The number of shared memory segments to allocate for the compiler cache.
apc.shm_size=512M ; The size of each shared memory segment, with M/G suffix
apc.num_files_hint=1024
apc.user_entries_hint=4096
apc.ttl=7200 ; Seconds a cache entry is allowed to idle in a slot
apc.use_request_time=1
apc.user_ttl=7200 ; Seconds a user cache entry is allowed to idle in a slot
apc.gc_ttl=3600 ; Seconds that a cache entry may remain on the garbage-collection list
apc.cache_by_default=1 ; On by default, can be set to off and used with apc.filters
;apc.filters ; A comma-separated list of POSIX extended regular expressions.
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.file_update_protection=2 ; Puts a delay on caching brand new files.
apc.enable_cli=0 ; Enables APC for the CLI version of PHP
apc.max_file_size=1M ; Prevents large files from being cached
apc.stat=1 ; Defaults to on, forcing APC to stat (check) the script on each request to determine if it has been modified. May want to use 0 for production
apc.include_once_override=0 ; Optimize include_once and require_once calls and avoid the expensive system calls used.

Setup Magento to use APC

In Magento’s app/etc directory you will find a local.xml.additional file.
This file contains additional parameters that can be setup within Magento’s local.xml file which is located in the same directory and contains the database information.

To add APC caching to Magento with user caching add the following between the global tags. The prefix tag is the custom namespace used for user cache per store so make this unique especially if you are running multiple stores.

<global>
.
.
        <cache>
            <backend>apc</backend>
            <prefix>mystore_</prefix>
        </cache>
.
.
</global>

That’s it. That’s all it takes to have Magento fully utilize APC cache.

APC UI

APC comes with a UI page and admin functionality.
In your magento home directory create a apc directory.

mkdir apc

Centos

copy file /usr/share/doc/php-pecl-apc-*/apc.php to magento home apc directory
cd to magento home apc directory

Ubuntu based systems

copy file /usr/share/doc/php-apc/apc.php.gz to magento home apc directory
cd to magento home apc directory

gunzip apc.php.gz

For both CentOS and Ubuntu based systems:

Create a conf file in your apc directory

vi /var/www/html/apc/apc.conf.php

Add the following. Change username and password to your own:

<?php
  defaults('ADMIN_USERNAME','apcadminuser'); # admin username
  defaults('ADMIN_PASSWORD','apcadminpassword'); # admin password
?>

Block access to the apc directory.
In your apache config file find your virtual host for your magento store and add similar to following. You can also do this with the apache Directory directive. We will not going into details about apache configuration here so change accordingly if required. The following allows only localhost access.

<Location /apc/>
 Order deny,allow
 Deny from all
 Allow from 127.0.0.1
</Location>

Restart apache
CentOS – service httpd restart
Ubuntu based systems –  service apache2 restart

Clear Cache
Make sure to clear your store file cache from within Magento admin -> System -> Cache Management.

Now go to your store url eg. http://localhost.store/apc/apc.php to view your installation.

Clearing APC
To clear APC, you can reload apache:

CentOS – service httpd reload
Ubuntu – service apache2 reload

You can also clear APC through the APC UI.

Let us know how your installation went and what performance improvements you gained.

*** Update
If you plan on using Magento command line utilities – such as cron job, etc… – you may need to change the following setting:

apc.enable_cli=1 ;

This will prevent errors such as:

PHP Fatal error:  Uncaught exception 'Zend_Cache_Exception' with message 'can't get apc memory size' in /var/www/svn/yourstore/trunk/magento/lib/Zend/Cache.php:209