We often hear complaints about how Magento is slow and performs poorly. Developers know, however, that performance is relative and that we can do a number of things to speed up a Magento site. This article will focus on configuring and using APC and/or Memcached. We have a resources section at the end of this article with links to more performance optimization techniques.
The options obviously also depend on the specific hosting arrangement. I will assume for the purposes of this article, that you have control over your hosting server and are able to install the necessary add-ons and make configuration changes to Apache and PHP. If you are on a shared environment, you may have to check with your hosting support if you can apply the tips from this article. But, you know that you should at least be on a VPS if you are running Magento.
APC – Alternative PHP Cache
Using APC is really easy. You can just install the PHP module and restart your Apache server and your sites should immediately benefit from this.
To install APC on a Debian based Linux distro, run:
sudo apt-get install php5-apc
Note that you will need either root or sudo privileges to perform the install
Then just restart your Apache server. You should start seeing improvements after browsing to a few pages on your site.
This is great already, however, there is another step you can do to integrate APC with your Magento site.
If you look at:
app/etc/local.xml.additional, you will notice 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 are dealing with APC here so we’ll just add:
<global> ... <cache> <backend>apc</backend> <prefix>MAGE_</prefix> </cache> ... </global>
This is the recommended entry from the Understanding Magento Scalability and Performance Magento blog post.
You will notice the
<prefix> element containing
MAGE_. This is simply a prefix to tell APC what to use when it creates the cache. This is particularly important if you are running several Magento sites on the same web server. In this case, you will need to enter a unique prefix for each site. For example:
<global> ... <cache> <backend>apc</backend> <prefix>SITEA_</prefix> </cache> ... </global>
<global> ... <cache> <backend>apc</backend> <prefix>SITEB_</prefix> </cache> ... </global>
Finally, to put the polish on your speed improvements, you can also tweak the APC configuration to further optimize your cache. This is done by editing the
apc.ini file usually located (on Debian based distros) in:
We will recommend this APC configuration that was posted in the official Magento forums.
extension = apc.so #name dependent on your APC cache install [APC] apc.enabled = 1 # Turn APC cache on apc.optimization = 0 # Experimental keep off apc.shm_segments = 1 # Shared memory segments apc.shm_size = 128 # Max shared memory dependent on OS apc.ttl = 7200 apc.user_ttl = 7200 apc.num_files_hint = 1024 apc.mmap_file_mask = /tmp/apc.XXXXXX apc.enable_cli = 1 # Allow command line php to function apc.cache_by_default = 1 # Enabled, 0 for filters apc.max_file_size = 10M # Maximum cached file size apc.stat = 1 # 1 for dev, 0 for production, whether the source file is checked for mod date #apc.include_once_override = 1 # Use PHP5.3+ for include_once optimization
Make sure you remove all the # comments from your real file as stated in the corresponding forum post.
Of note is the
apc.shm_size parameter. By default it is only 32M so the recommendation is to increase it to 128M. However, be careful if you are on a VPS with limited RAM, often you only have 384M or 512M so you may need to reduce your value to 64M. Especially if you want to combine this optimization technique with optimizing the MySQL parameters which also usually require increasing values for memory usage.
Memcached is a free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.
Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Magento supports Memcached out of the box but by default, it is not enabled since we can’t assume that your server will have all the prerequisites installed.
To enable Memcached you will need to check if the daemon is running. By default the daemon is configured to listen to port 11211 so you can perform a
netstat command and see if there is activity on 11211. If it’s running, you need to check if Memcached PHP support is enabled by looking at the output of
Once again, if you need to install the necessary components, on a Debian type distro the commands are something like:
sudo apt-get install memcached php5-memcache
You’ll need to enable Memcached via its configuration file (there will be a notice about this when you run the above command). Then start the service and restart Apache.
After this, you can add the following
<cache> section to your
<global> ... <cache> <backend>memcached</backend> <memcached> <servers> <server> <host><![CDATA[127.0.0.1]]></host> <port><![CDATA]></port> <persistent><![CDATA]></persistent> </server> </servers> <compression><![CDATA]></compression> <cache_dir><![CDATA]></cache_dir> <hashed_directory_level><![CDATA]></hashed_directory_level> <hashed_directory_umask><![CDATA]></hashed_directory_umask> <file_name_prefix><![CDATA]></file_name_prefix> </memcached> </cache> ... </global>
These settings are for a single local server running Memcached on the default 11211 port.
To see your Memcached in action you can issue a
telnet localhost 11211 command and type: stats
This is by far not all you can do in terms of optimizing your Magento site. We have only outlined a couple of popular caching mechanisms that are natively supported by Magento. Some people report better performance when using eAccelerator or XCache in place of APC and you can also try combining the various cache engines. However, be careful if you are also using Zend Server or Zend Optimizer as some of the caching engines may clash with those.
Here are some resources we’ve come across on the net that talk about Magento performance:
- Magento Blog: Several Articles on Magento Performance
- Magento Forum: A Guide to Making Magento Faster
- Magento Forum: APC Setup
- Magento Groups: Magento Performance and Optimization
- Magento Group Wiki: Optimising Your Web Stack Performance for Magento
- Slides (from Magento Developers Paradise): Methods and Best Practices for High Performance eCommerce
- Guido Jansens Blog:
101 ways to speed up your Magento e-commerce website
- Yoast Blog: Magento performance hosting
- Nexcess Blog: Enable Memcached in Magento
- Inchoo Blog: Boost the speed of your Magento
- Inchoo Blog: Magento performance, research and improvement
- Oggetto Web Blog: Magento performance: Enterprise vs Community
- Tinybrick: (Magento) Speed Testing
Do you have links or comments about Magento performance tuning and optimization that you think are useful. Please share them in the comments.
Originally published on magebase.com. Copyright © 2010 Magebase - All Rights Reserved.