How to speed up Magento - A Magento Optimization guide
Magento is an open-source eCommerce platform with over 150,000 active online merchants using it. Used predominately by larger customers/merchants, Magento's great flexibility and customisation options comes at the cost of a steeper learning curve that other CMS solutions. Magento is built on the PHP Zend framework (http://framework.zend.com).
If you suffer from slow Magento performance, we can help you with Magento optimization to speed up Magento in no time! In this article, we share some tips and tricks on how you can dramatically increase the performance of your Magento installation with just a few configuration changes. MetaCDN offers a Magento CDN solution that can speed up your slow Magento website - however it is important to get the foundation right.
General
- Use SSD hard drives
- Don't run your MySQL server and webserver (ngnix, Apache) on the same machine
PHP configuration
Seeing that Magento is built on PHP, tweaking the PHP configuration will help make it perform better. One way of doing so is to increase the memory limit to at least 64MB.
To increase, follow these steps:
- locate your php.ini used by your webserver, e.g. /etc/php5/apache2/php.ini on Ubuntu
- open php.ini and change the value for memory_limit to at least 64MB (set it higher if your system configuration allows for it):
memory_limit=64M
Also have a look at php.ini.sample in your Magento document root folder as it provides some more basic parameters to play around with.
Apache KeepAlive's
If you're running the Apache webserver (http://httpd.apache.org/), the keep-alive directive is used to to keep an already established TCP session open between the server and a user's browser for it to be reused. This uses more memory on the webserver but increases the performance because the overhead of terminating and creating a new TCP connection for every single asset to be downloaded from the server is eliminated.
To enable it, add the following to your http.conf or .htaccess file:
<IfModule mod_headers.c>
Header set Connection keep-alive
</IfModule>
For a more in-depth discussion, see here: http://blog.ausweb.com.au/apache-optimization-keepalive/
Move Magento's var/cache to tmpfs
The default installation of Magento uses var/cache in your Magento document root as the slow cache location, i.e. reads and writes from the hard-drive. These reads/writes are very slow and will quickly become a performance killer for all but the smallest online stores.
Therefore, we can speed things up by having tmpfs take care of var/cache
- stop your webserver, e.g.:
sudo service apache2 stop
- Clear the cache:
rm -rf /var/www/magento/var/cache/*
- Mount var/cache using tmpfs:
mount tmpfs /var/www/html/magento/var/cache -t tmpfs -o size=64m
- restart your webserver, e.g.:
sudo server apache2 restart
Play around with the allocated memory size (here: 64MB). Try 128MB or even 256MB but this depends on your system configuration and available RAM.
Page caching
Using Magento's page caching, load is taken off from the webserver by serving pages already visited before from a cache which makes it a lot faster. There are a few page caching extensions available for Magento, we recommend one of the following:
- Zoom Full-Page Cache (free): http://www.magentocommerce.com/magento-connect/zoom-full-page-cache-1742.html
- Full Page Cache Pro (commercial): http://www.magentocommerce.com/magento-connect/full-page-cache-multi-level-5450.html
- Brim's Full Page Cache: http://www.magentocommerce.com/magento-connect/full-page-cache-7153.html
Also have a look at http://www.pixafy.com/blog/2013/03/overcoming-magentos-full-page-cache-through-hole-punching/ if you want to know in more detail how page caching and hole punching works.
Use a PHP cache: APC
Magento uses the 2-level caching system provided by the Zend framework, a fast front-end cache (in-memory, specialised solutions) and a slower backend cache (database, file-system). Here, we are looking at APC which caches the compiled PHP opcode so that subsequent requests to the same PHP resource can be served from the cache, and don't need to be re-interpreted and re-compiled for every request.
To enable APC on a Linux system, do the following:
- install the PHP extension for APC:
sudo apt-get install php-apc
OR
sudo apt-get install php5-apc -
find the apc.ini configuration file and edit it accordingly. In Ubuntu 12.04, it is located here: /etc/php5/conf.d/apc.ini
A good default Magento-tuned apc.ini has the following values:extension = apc.so
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 256M
apc.num_files_hint = 10000
apc.user_entries_hint = 10000
apc.max_file_size = 5Mapc.optimization = 0
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-dummy.XXXXXX
apc.file_update_protection = 2
apc.enable_cli = 0
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 - Enable APC in app/etc/local.xml.additional under config > global > cache > backend:
<config>
<global>
...
<cache>
...
<backend>apc</backend>
<prefix>APC_</prefix>
...
</cache>
...
</global>
</config>
- Once done, restart your web server, for example:
sudo service apache2 restart
- By now, APC should be working and you should see a noticeable performance enhancement.
Our own tests with APC enabled shows a performance boost of factor 3!
Redis cache
Redis (http://redis.io/) is another top-shelf cache solution, which unlike APC, supports tagging, i.e. logical groupings of cache entries (which really are key-value pairs). Since Magento uses tagging quite a bit, a cache that supports tagging is performing better and faster than one without.
Until we've setup and tested Redis oursevles, we refer you to the excellent article at http://www.neevtech.com/blog/2013/05/02/making-magento-run-faster-configuring-redis-as-session-back-end-2/ and the forum post here http://www.magentocommerce.com/boards/viewthread/278526/P0/
Use a CDN
Using a content delivery network (CDN) such as MetaCDN enables serving your assets from a location geographically closest to your customers and hence reduces latency and increases download speed for those assets. Fast and responsive website make customers happy.
Magento lets you define where certain assets (CSS, javascript, images, etc.) are loaded from such as a URL pointing to a CDN. See our Magento CDN integration guide for details how to integrate the MetaCDN Site Accelerator into your Magento installation.