Optimise Magento for your VPS
Magento is one of the most popular ecommerce systems in use today offering both great flexibility and power through its modular architecture. This article will discuss various techniques you can implement to optimise Magento and fine tune the software stack to obtain the best performance possible.
There are settings within Magento that can be adjusted to provide a performance boost. As with any changes made to a live site we would recommend creating a full backup of the database and all the files first before implementing any of these changes.
Enable Magento Caching
Enabling all the available caches in the Admin Panel is an ideal first step in optimising your Magento store.
- Log into the Magento administrator interface.
- On the menu bar, click System > Cache Management. This will take you to the Cache Storage Management panel providing you with the current status of your cache within Magento.
- On the left side, click Select All, and from the drop down Actions menu select Enable > Submit.
Disabling Magento Modules
Disabling Magento modules that are not required results in less resources being used. Modules can be disabled through the Magento Admin Panel, or by editing XML-files in app/etc/modules. For instance disabling Mage_Log, which performs queries on every request, is not needed if you gather site statistics using external programs.
Use Magento Compiler module
The Magento compiler module limits the number of directories that PHP has to search through when looking for PHP-files. This decreases the PHP execution-time, which in turn speeds up the Magento application.
Be warned that you need to be careful when making changes to your Magento system, while the Magento compiler is enabled. Upgrades should only be undertaken when the compiler is (temporarily) disabled.
Enable Flat Catalogue
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 catalogue for categories and products merges product data into one table, thereby improving performance. Generally, all stores should enable flat catalogue for categories. Stores with over 1000 products should enable flat catalogue for products.
- From the Magento administrator interface, go to System > Configuration > Catalogue.
- Under Frontend, change Use Flat Catalogue Category to YES.
- If desired, under Frontend, change Use Flat Catalogue Product to YES.
- Clear the cache.
MySQL is a popular choice of database for use in web applications, and is a central component of the widely used LAMP open source web application software stack. LAMP is an acronym for “Linux, Apache, MySQL, PHP.” Free-software-open source projects such as Magento that require a full-featured database management system often use MySQL.
Enable the Query Cache
Each time a Magento page is loaded, a series of database queries are made and the database server must go to work.
Each query is first parsed and the execution of the query is initiated. Then the data must be retrieved from the disk or storage media, sorted and manipulated, and then ultimately returned to the client. This results in performance slow downs because of, among other things, slower disk access.
MySQL offers a built in configuration parameter known as query_cache_size. This configuration directive tells the MySQL server to store the result of the query in memory, which is faster than accessing from disk.
MySQLTuner is a script written in Perl that allows you to review a MySQL installation quickly and make adjustments to increase performance and stability. The current configuration variables and status data is retrieved and presented in a brief format along with some basic performance suggestions.
Depending on the size of your store maintaining the database through log cleaning can result in a dramatic improvement in performance and latency.
- From the Magento administrator interface, go to System > Configuration.
- In the left menu under Advanced, click System.
- Under Log Cleaning, change Enable Log Cleaning to Yes and configure the Save Log for 15 days.
- Click Save Config.
Caching is a collection of methods that speed up the loading of your site.
There are a variety of different caching systems that can improve Magento’s performance in different areas:
- Cache Backend
- Session Store
- Reverse Proxy
Some popular caching systems, such as Memcache and Alternative PHP Cache, can be used to implement a combination of these cache types, whilst other packages just focus on one particular area of optimisation only.
You should review all these forms of caching to see which combination yields the best performance results for your site.
By default Magento stores its cache on the local file system which is not really optimized for high performance, especially with slower SATA disks, leading to significant IO overhead.
A disk based cache backend is fine for small websites but if you get more requests through visitors, reading and writing to the file system gets slower and slower as the cache grows.
The aim is to minimize latency so pages can load as quickly as possible. This can be achieved by moving the cache to RAM, which provides virtually zero latency.
Magento uses a plugin architecture allowing you to easily replace the cache backend implementation. Premade implementations exist for the following cache backends:
Two-level backend Caching
While RAM is extremely fast, it provides much less storage capacity than durable storage technologies such as the file system or a database. Therefore, Magento supports a two-level backend, which makes it possible to combine two different cache backends -a fast but small one, and a slower but bigger one.
However some Magento developers recommend going a step further and implementing Redis which can provide a super fast backend cache with cache tag support thereby eliminating the need for a system cache.
Sessions are user-specific data that is stored on the server for each client. The server then maps clients to their sessions with a cookie. A typical use for sessions is to allow a visitor to store items in a shopping cart, and then return to the store at a later time to view them. Regardless of when the visitor returns the items will always appear in their cart ready for purchasing.
How quickly data can be put in and got out of the session storage, how volatile it is, and how it is distributed in a clustered environment are all important considerations when choosing how to store sessions.
Magento provides session store caching through the following options:
A PHP accelerator such as Alternative PHP Cache (APC) or ZendOpcache will help to reduce server overhead in processing PHP code by caching previous requests and thereby avoid the parsing and compiling of PHP source code each and every time a page is visited.
APC has the added benefit of offering an a database object cache, allowing objects you create to be stored in memory right next door to the scripts that need them. The only drawback to this level of caching is that it’s local to each respective application server you have.
Other popular PHP accelerators are:
Reverse Proxy Cache
A reverse proxy is a type of caching system that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client as though they originated from the proxy server itself. The goal of a reverse proxy is to lighten the load on the web servers behind them.
One of the most popular reverse proxy caching systems is Varnish. Some times referred to as a web accelerator or HTTP accelerator, Varnish is a reverse HTTP proxy that will significantly enhance the performance of your Magento store.
It does this by storing a copy of the page in RAM the first time a user visits that page. Subsequent visits to that page are then retrieved from the Varnish cache instead of your Magento store allowing you to handle more traffic.
Content Delivery Network
A content delivery network or content distribution network (CDN) is a large distributed system of servers deployed in multiple data centers across the Internet. The goal of a CDN is to serve content to end-users with high availability and high performance. Visitors retrieve scripts, pages, images and other important media files from the CDN point closest to them, instead of your local web server.
Image Compression and Script Minification
Select a suitable image file format and compress the images using image compressor to lessen the bandwidth between browser and web server. There are several image compression tools offered online such as; imageoptimizer.net, hjpeg-optimizer.com, JPEGMini.com etc.
Minification is also vital to improving end user experience. Reducing the file size and number of files, and there the number of HTTP requests has a dramatic impact on the speed of pages loading with your site.
One popular tool for Magento that includes minification along with image compression is GTspeed.
Enable GZIP compression
Compression of your HTML and CSS files with GZIP typically saves around fifty to seventy percent of the file size. This means that it takes less time to load your pages, and less bandwidth is used over all.
For most people reading this, compression is enabled by adding some code to a file called .htaccess on their web host/server. This means going to the file manager (or wherever you go to add or upload files) on your webhost.
The .htaccess file controls many important things for your site. If you are not familiar with the .htaccess file, please read my working with .htaccess article to get some know how before changing it.
The code below should be added to your .htaccess file…
Save the .htaccess file and then refresh your webpage.