Just to give you brief introduction of APC Cache; Alternative PHP Cache (APC) is a free and open opcode cache which provides free, open, and robust framework for caching and optimizing PHP intermediate code. If you tweak APC well you can even cache application data (user cache).
Now, let me ask you this. Does your store despite all hard work still under performing? If the answer is yes then it’s a right time to induce Alternative PHP Cache (APC) to speed up your Magento store. Although, there are lots of opcode cache available like eaccelerator, Xcache etc.) still we are using APC just because of greater support and stability. APC cache is taken as default in PHP 6 so using APC makes more sense.
If your Magento store is running on a shared server then you must confirm with your server guys whether they will install it for you. Typically opcode cache is not installed in most of the shared hosting, but you can always confirm. In case you are using VPS, dedicated servers then read on how to install and configure APC for your Magento store.
Installing APC Cache For Magento Store
Installing APC is easy just do the followings:
sudo apt-get install php5-apc
If you are running some other versions of Linux distos you have to follow some other technique to install APC on your server. You can always Google how to install APC on your particular Linux OS.
Configuring APC For Magento
In your php.ini file add the following lines and then fine tune as per your server’s capacity
extension = apc.so
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 128
apc.optimization = 0
apc.num_files_hint = 1024
apc.user_entries_hint = 1024
apc.ttl = 0
apc.user_ttl = 0
apc.gc_ttl = 600
apc.cache_by_default = 1
apc.filters = "apc\.php$"
apc.slam_defense = 0
apc.use_request_time = 1
apc.mmap_file_mask = /tmp/apc-yourusernamehere.XXXXXX OR /dev/zero
apc.file_update_protection = 2
apc.enable_cli = 0
apc.max_file_size = 5M
apc.stat = 1
apc.write_lock = 1
apc.report_autofilter = 0
apc.include_once_override = 0
apc.rfc1867 = 0
apc.rfc1867_prefix = "upload_"
apc.rfc1867_name = "APC_UPLOAD_PROGRESS"
apc.rfc1867_freq = 0
apc.localcache = 1
apc.localcache.size = 512
apc.coredump_unmap = 0
apc.stat_ctime = 0
The above is just a required options for APC, you need to configure it now.
apc.shm_size=128 This is the main setting of APC which tells APC to use 128M from the shared memory pool. You can increase it to 256 or something in case you are running out of memory or fragmentation is happening quickly. Thumb of rule, don’t let fragmentation go higher than 50% at any time.
Both apc.num_files_hint and apc.user_entries_hint are used by APC to help optimize its memory usage when creating the cached memory space. The apc.ini defaults are both set very low at 1000 and 4096 respectably. Based on the amount and size of files within Magento, it is recommended to set these both high at a value of 10000.
apc.max_file_size=5M This sets the maximum file size that can be cached by APC. The dafault size is 1MB, but for Magento it should be increased to 5MB.
apc.stat=0 Keep apc.stat=0 on production site, if you are on staging and changing code then keep it as 1.
There are other lots of useful settings which should be fine tweaked depending upon your server and site’s requirements. Finally, restart your Apache and you will see a slight performance gain already. But, lets tell Magento to use APC’s user cache functionality for greater speed and performance gain.
app/etc/local.xml.additional, you will see that that there is a
<cache> element that allows you to specify which caching method you are using in the
<backend> element. This can have one of the following values:
We have already installed and configured APC cache so lets tell Magento to use it properly. Let’s add the following block here:
<global> ... <cache> <backend>apc</backend> <prefix>MAGE_</prefix> </cache> ... </global>
You must have noticed element containing MAGE_. This tells APC what to use
when it creates the cache.If you have multiple Magento store running you should
do like this:
Store A<global> ... <cache> <backend>apc</backend> <prefix>STOREA_</prefix> </cache> ... </global>Store B<global> ... <cache> <backend>apc</backend> <prefix>STOREB_</prefix> </cache> ... </global>APC comes with a cool tool to see how APC cache is performing on your sever.Copy apc.phpfile from the APC distribution to your root directory so that you can see it like
http://www.yourstore.com/apc.php. Use this to determine if you need to change some APCconfiguration variables. Additional Advice for APC configuration:
I would love to hear your thoughts and experiences of using APC for Magento. Please leave
- turn off apc.optimization (experimental and unstable, may cache user session info,
- useless items which
- should not be cached, such as admin pages and logged-in site pages)
- turn off apc.include_once_override
- turn off apc.slam_defense (may cause objects to be replaced with PHP warnings
- which are displayed to the user)
- set apc.user_ttl = 0 (allows your php scripts to set appropriate timeouts for
- queries and objects)
- set apc.mmap_file_mask = /tmp/apc-yourusernamehere.XXXXXX for file-backed mmap;
- make file mask unique by
- adding your unique string; XXXXXX (exactly 6 X’s) must remain to allow APC
- to add random string OR set to /dev/zero for anonymous mmap if you can spare the memoryme a comment and let me know. Subscribe our RSS to receive latest Magento updates.