This guide lists a collection of performance optimisations for running magento, this guide is not guaranteed to produce the best results and should be used as a general guide on how to optimise magento performance, results may vary.
Please implement steps one by one and retest after each step is complete, a step could break the system or decrease performance in some cases so please use caution when implementing steps, the reader is assumed to be very comfortable with installing each of the components or making the listed modifications and be aware of their effect.
Initial Configuration
Setup the following
This guide will not detail how to install these individual components.
Nginx - http://wiki.nginx.org/Main
Apache - http://www.apache.org/
PHP - http://php.net/
PHP APC - http://php.net/manual/en/book.apc.php
Varnish - https://www.varnish-cache.org/
For ease of use, you can optionally install:
http://www.magentocommerce.com/magento-connect/Phoenix/extension/6322/varnish_cache
Filesystem Enhancements
Turn off atime on the default partition - noatime
Tools
Install the following performance monitoring tools:
- atop
- htop
- sysstat
- dstat
- innotop
Apache Configuration
Recommended Modules
- Asis
- AuthnDefault
- Deflate
- Env
- Expires
- ModFCGID (fastCGI support)
- Fileprotect
- Headers
- MPM Prefork
- Mod SuPHP
- Proxy
- Version
- Bcmath
- CGI
- Calendar
- CurlSSL
- FTP
- GD
- Gettext
- Iconv
- Imap
- Mbregex
- Mbstring
- Mcrypt
- Mysql
- Mysql of the system
- Openssl
- PDO
- PDO MySQL
- POSIX
- Path
- Info
- Check
- Pear
- Phar
- SOAP
- SQLite3
- Sockets
- TTF(FreeType)
- Zip
- Zlib
Recommended Configuration
KeepAlive On
KeepAliveTimeout 2
Database Optimisation
Recommended MySQL Configuration
innodb_file_per_table = 1
#innodb_buffer_pool_size = >50% of total RAM
innodb_buffer_pool_size = 1G
#innodb_thread_concurrency = 2 * [numberofCPUs] + 2
innodb_thread_concurrency = 6
innodb_flush_log_at_trx_commit = 2
innodb_autoextend_increment=512
#thread_concurrency = [number of CPUs] * 3
thread_concurrency = 6
thread_cache_size = 32
table_cache = 1024
query_cache_size = 64M
query_cache_limit = 2M
join_buffer_size = 8M
tmp_table_size = 256M
key_buffer = 32M
max_allowed_packet = 16M
max_heap_table_size = 256M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
NOTE: these settings will depend greatly on your available resources and your Magento installation requirements.
Please install this tool http://mysqltuner.com/ as this will help you to refine your MySQL settings appropriately for your system.
slow_query_log = 1
slow_query_log_file = /var/lib/mysql/slow-query.log
long_query_time=2
The purpose of the slow_query_log setting is to log any database queries that are over a certain, in this instance 2 seconds set by “long_query_time=2”
Note: This does not mean all queries that appear in the slow-query.log file will be problematic, it merely logs slow results as a source for tracking down queries that are a potential performance drain.
PHP
Magento Required PHP extensions
- PDO_MySQL
- simplexml
- mcrypt
- hash
- GD
- DOM
- iconv
- cur
- l SOAP (required for Webservices API
PHP.ini Recommended Settings
memory_limit = 256M //256M as minimum 512M or higher preferred!
safe_mode = Off
log_errors = On
output_buffering = On
realpath_cache_size = 32k
realpath_cache_ttl = 7200
max_execution_time = 30 //As low as possible – requires testing under load
max_input_time = 30
default_socket_timeout = 90
pdo_mysql.cache_size = 2000
output_buffering = On
PHP Error Reporting
In development/testing – set “display_errors = On”. This provides you with visual troubleshooting cues that you have PHP errors. In production – set “display_errors = Off”
APC
In order to use APC as the opcode cache storage for your Magento install you must modify your ‘app/etc/local.xml
Edit the file making sure the lines in bold are added.
<global>
<cache>
<backend>apc</backend>
<prefix>mgt_</prefix>
</cache>
</global>
Recommended Settings
apc.enabled = 1 //enables apc
apc.shm_size = 256M //this is the shared memory size for APC ie. The amount of memory dedicated to cache.
Set as high as possible depending on your catalog – single installation 128-256Mb.
Consider allocating >1-2GB if you are using full page caching
apc.enable_cli = 1 // allows cron processes to use the magento cache
apc.ttl = 0 //used to auto clean the data cache.
Fragmentation will grow as items expire – setting to zero will prevent expiry.
You will have to manually clear the cache on occasion should it become full – or set higher value for ‘apc.shm_size’
Magento
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.
Enable Flat Catalog
Magento uses the EAV model to store customer and product data. This enables these objects to be incredibly extensible, but results in longer SQL queries and more reads. Enabling the Flat Catalog for Categories and Products merges product data into one table, thereby improving performance. Generally, all stores should enable Flat Catalog for Categories. Stores with over 1000 products should enable Flat Catalog for Products.
-
In the Magento Admin, go to System > Configuration > Catalog.
-
Under "Frontend", change "Use Flat Catalog Category" to YES.
-
Under "Frontend", change "Use Flat Catalog Product" to YES. (optional)
-
Clear the cache.
- See more at: http://docs.nexcess.net/article/optimizing-magento-performance.html#sthash.o7UJHwul.dpuf
Combine CSS and JS Files
This feature reduces the number of HTTP requests. For versions earlier than 1.4.x, the Fooman_Speedster extension can be used instead.
WARNING: Combining CSS/JS when using CDN will cause CSS/JS to "break" until the CDN updates completely. It is recommended to perform this process at the beginning off-peak hours, to allow enough time for the "new" CSS/JS to reach the CDN.
-
In the Magento Admin, go to System > Configuration > Developer.
-
Under "Javascript Settings", change "Merge Javascript Files" to YES.
-
Under "CSS Settings", change "Merge CSS Files" to YES.
-
Clear the cache.
Tweak .htaccess
Enable Output Compression
<IfModule mod_deflate.c>
############################################
## enable apache served files compression
## http://developer.yahoo.com/performance/rules.html#gzip
# Insert filter on all content SetOutputFilter DEFLATE
# Insert filter on selected content types only
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Don't compress images
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary
</IfModule>