Some easy and highly effective server optimizations for Magento
Recently I started digging into Magento because a few clients of mine switched to Magento as their e-commerce solution. The first thing I noticed about Magento, is that it doesn’t play nice on shared hosting platforms as it is really resource heavy. If you want to run a Magento shop with a fair amount of products in the catalog and a reasonable amount of visitors, you should look at a reasonable VPS (Virtual Private Server) as a starting point. You most probably can’t make the needed changes on a shared platform.
As a starting-point I use a 1GB virtual machine with 2 cores running at 2.20GHz. On this system I did a base install of CentOS 6.2. The only things I installed afterwards were PureFTPD, MySQL 5.1.61, Apache 2.2.22 and PHP 5.3.12. For Apache I’m using the worker MPM and PHP-FPM using default settings.
With a standard Magento 1.7.0.0 installation + sample data I get a transaction rate of 4.99 trans/sec which is a bit on the slow side. Potential customers don’t like slow shops, a load time of 2 seconds for example can make them leave quickly without placing an order.
The easiest win: APC caching
Reading through all the information and testing with different caching mechanisms I found APC to give the best performance. Also, it’s easy to install and configure. Installation is as easy as typing ‘pecl install apc‘. After you have installed APC you will have to activate the extension in your php.ini. The easiest way is to add the following lines:
[apc] extension=apc.so apc.enabled=1 apc.shm_size=128M apc.max_file_size = 10M
For a pretty much standard Magento installation the above settings will be good enough, for bigger shops you probably need to tweak these settings a bit more. The directive apc.shm_size sets the cache size per PHP process and the apc.max_file_size prevents files larger than the given value to be cached.
Make sure that Magento also uses APC for caching. You need to edit app/etc/local.xml and add the following lines somewhere between the <global> and </global> tags:
<cache>
<backend>apc</backend>
<slow_backend>database</slow_backend>
<prefix>shopname_</prefix>
</cache>
Note the slow_backend tag. If you don’t specify this, Magento will still use some kind of file caching which can get painfully slow when there’s alot of data cached. The prefix tag is used to prefix the cache for the Magento shop, useful if you have more applications using the APC cache.
With the installation of APC, a file called apc.php get installed into /usr/local/lib/php/. If you put this file in your document root, you can check how the cache is performing. Two important values to keep an eye on are the memory usage and the fragmentation. If there is too much fragmentation, the cache will perform badly. In this case it’s recommended to assign more memory to APC.
By just enabling APC with these settings made the performance jump to 7.54 trans/sec which is an increase of 51%!
Next step: Optimizing MySQL
If you have a small VPS you need to play with your memory allocation to get the most out of it. It’s easy to set all configuration variables to a high value, but this can affect the performance in a bad way. If you just start raising the values you will soon be over-committing and MySQL performance can drop. It doesn’t have to be a problem to over-commit, but if something goes wrong with a process that uses a database connection, MySQL might start eating up all memory, eventually starting to swap. Setting values too high can also make MySQL slower. So more doesn’t have to be better in this case. Be careful adjusting these settings.
If you don’t have lots of visitors you can lower the max_connections in the MySQL configuration in /etc/my.cnf. By default this value is set to 151 which is a bit high if you want to optimize with limited resources. Sort and join buffers for example, are per connection. If you set both to 4M and you keep the max connections at 151, your already committing 1.2GB of your memory.
Of course you can check all the status variables in MySQL manually, but if you don’t know exactly what all the variables mean you can get lost. Here’s a tool that will help you tune MySQL a bit: MySQLTuner
With this Perl script you easily check what settings need to be tweaked as the tool gives recommendations. As the optimization of MySQL is different for each configuration it’s best use this tuning script together with a benchmarking tool like Siege. With this tool you can check the transaction rate of the HTTP requests and see how your server performs under load.
In another post I will dive deeper into MySQL optimizations. For a small Magento shop you don’t win much (from my experience) with only the optimizations the MySQLTuner script recommends.
Having tweaked MySQL a bit, the performance went to 8.21 trans/sec. An increase of another 9%.
Upping the specs
Of course you can run a Magento shop on a low-end server. But since it likes to munch resources it’s better to spend a bit more money on better specs. As said before, potential customers don’t like slow shops. People tend to abandon your site if the load times are too long. Running Magento with only 1GB of memory might be a bit on the low side.
Below I’ve made a list of tests I did with different configurations.
| Specifications | Without APC | With APC | With APC + MySQL optimizations |
|---|---|---|---|
| 2 cores, 1GB RAM | 4.99 trans/sec | 7.54 trans/sec | 8.21 trans/sec |
| 2 cores, 2GB RAM | 5.07 trans/sec | 7.36 trans/sec | 8.00 trans/sec |
| 2 cores, 4GB RAM | 5.76 trans/sec | 9.53 trans/sec | 10.18 trans/sec |
| 4 cores, 2GB RAM | 7.50 trans/sec | 10.75 trans/sec | 14.32 trans/sec |
| 4 cores, 4GB RAM | 9.59 trans/sec | 14.46 trans/sec | 15.16 trans/sec |
| 8 cores, 4GB RAM | 13.12 trans/sec | 17.43 trans/sec | 18.76 trans/sec |
Share your thoughts with me! I myself am still pretty new at this and curious about how others are handling Magento.
Hello world! (again)
It’s been a few years since I’ve been blogging but a few days ago I decided to give it another try. I’m not exactly going to blog about my personal life as that’s not interesting for anyone, except the people around me. So no worries about that
Sometimes I get questions about things that I need to do some research for to find a solution. Now I thought it’d be an idea to share that information with you, the reader. Now I know you might think that information is already published on the net. But it can take a while to find the right information, spread over various websites, which can be annoying.
And sometimes it’s just fun to post something I’ve found on the net
Need to fill this space somehow as it’s there.
