June 6th, 2011
Tuning eAccelerator for Magento on Linux - 7

Beauty farm by Vincent Luigi Molino
This article descripes how to modify a default eAcellerator configuration for use with Magento. This is an advanced level topic. You have installed Magento on your own dedicated server and want to squeeze some more juice out of it. Because installing eACellerator requires you to install software on the server this cannot be done on a shared hosting server. Here your hosting provider will already have installed a similar solution. But as you will read below — this is likely a subobtimal boost to overal performance.
With so many PHP files to parse for each page Magento will be slow unless you are using a PHP accelerator & optimizer. As each PHP script is compiled it is stored in memory and on disk by the accelerator. On a subsequent load PHP no longer needs to compile the script, it can just load the already compiled version and start executing immediately.
The two most commonly used PHP accelerators are APC Cache and eAccelerator. APC Cache is written and maintained by the PHP developers. eACellerator cannot claim such pedigree but it has been around for a very long time and is significantly faster than APC Cache.
The choice is then between which factor is more important: future compatibility with newer versions of PHP or raw speed now. Because of my experience with eACellerator I tend to pick it for new projects.
Installing eAcellerator is straightforward and the instructions can be found on the EACellerator installation page.
Once you have installed it and restarted your web server you are good to go. eAcellerator is running and caching PHP files. You should already notice a good boost in how quickly Magento responds immediately.
Tuning memory usage
The default installation of eACellerator is not optimal. The problem is with how it handles memory. Compiled PHP scripts are first stored in memory and then on the hard disk in the eAcellerator cache directory. Ideally you do not want anything to be read from disk for the best speed everything should come from memory.
The standard installation of eACellerator allocates 32Mb of memory for the cache in memory. This is shared memory so that each instance of PHP can access it. For most websites that is more than sufficient but for Magento it is too tight.
Create a little file called phpinfo.php in the root directory of your Magento webserver and call it.
<?php phpinfo(); ?>
If eAcellerator is properly installed it has its own separate section. If you just restarted your webserver the cache should be empty and memory allocated should be trivial. Now open a window with Magento and refresh the phpinfo page. You will quickly see the amount of memory allocated run towards the 32Mb limit. As soon as it hits this limit older files will be swapped to disk and on demand be read back in.

The above image comes from a small Magento website that has been running for a day and where we also used the Magento admin panel. It topped out at around 60Mb total for compiled scripts. About twice the default cache value. So if we had allocated just 32Mb, eAcellerator would be very busy hitting the disk to swap in and out compiled scripts.
The solution, increasing the amount of memory allocated to eACellerator is a little tricky. 32Mb is already the maximum amount of shared memory size allowed by a default Linux installation. If you increase the value in the eAcellerator configuration section your webserver will no longer start. So we first need to increase the system wide shared memory limit. For this you need root level privileges on the server.
To increased it to 100Mb, which is 100 * 1024 * 1024 = 104857600 bytes you need to do the following:
First determine the current maximum size of a shared memory segment, run:
# cat /proc/sys/kernel/shmmax 33554432
For most stock Linux installations this is likely 32Mb ( 33554432 / 1024 / 1024 = 32)
The default shared memory limit for SHMMAX can be changed in the proc file system without reboot:
# echo 104857600 > /proc/sys/kernel/shmmax
Alternatively, you can use sysctl(8) to change it:
# sysctl -w kernel.shmmax=104857600
To make a change permanent, add the following line to the file /etc/sysctl.conf (your setting may vary). This file is used during the boot process.
# echo "kernel.shmmax=104857600" >> /etc/sysctl.conf
Now modify the webservers php.ini file (/etc/php5/apache2/php.ini in my situation) to reflect the increase amount of shared memory.
eAccelerator.shm_size = "99"
Notice that I used 99 and not 100, for some reason the webserver crashes when I enter 100. Going down one Mb and it works for me.
Restart the webserver and Magento should now fit completely in shared memory. So all PHP scripts for your website should now come directly from memory and there is no swapping in and out anymore.
Tuning script file validation
But at this stage we haven’t completely broken our bond with the hard disk yet. eAcellerator is still hitting the file system on every page load. Why? Because for each compiled script it needs to check if it is still valid. If you make a change to one file it needs to know this and recompile it. It only checks the file modification date using an lstat() call, this is quick but with hundreds of scripts in its cache it still needs to do this a lot for every single page.
If your Magento installation is static, which it should be as you aren’t modifying a life environment these calls are completely unecessary. You can disable them by modifying the eacellerator configuration:
eAccelerator.check_mtime = "0"
Restart the webserver and once read and compiled, eAccelerator will assume that the PHP source code will not change again.
So what to do if you do make a change to your code, template etc ? You need to restart your webserver to flush the cache.
Did this work for you? Let me know !



Except where otherwise noted, content on this site is
June 7th, 2011 at 1:34 pm
Nice article, thank you for sharing your experience!
I’m thinking of doing this on a live VPS that is running Typo3 and Magento sites. If for some readon I’d need to disable eAccelerator temorarily, is this possible? Is it only a php.ini flag, or are there other things?
Thanks.
June 7th, 2011 at 4:50 pm
@Koopa
You can safely disable eaccelerator by just changing the settings in php.ini.
It is even possible to do this on a site by site basis. Just put php_flag eaccelerator.enable 0 and php_flag eaccelerator.optimizer 0 in a .htaccess file in the document root of the virtualhost or directly inside apache section.
June 7th, 2011 at 10:11 pm
@Martijn
Oh cool, that is very handy! I’ll try it out. Thanks for the htaccess trick as well.
October 24th, 2011 at 10:04 am
“Restart the webserver and Magento should now fit completely in shared memory. So all PHP scripts for your website should now come directly from memory and there is no swapping in and out anymore”
Have you really tested/verified this? I’ve had my settings exactly like yours:
eAccelerator.check_mtime = “0″
and also only use about 40% max of eaccelerator.shm_size = “64″
Still the /var/cache/eaccelerator fold aways repopulated with files. I thought maybe you found the trick to accomplish shm caching. What am I missing? It still reads from disk.
Thanks
October 24th, 2011 at 10:13 am
Having enough memory allocated is not enough. You also need to set the following:
eaccelerator.shm_only = “1″
By default eaccelerator will cache to BOTH disk & shm unless the above is set to “1″
and then set…
eaccelerator.keys = “shm_only”
eaccelerator.sessions = “shm_only”
eaccelerator.content = “shm_only”
again default for the above 3 is “shm_and_disk”
October 25th, 2011 at 8:44 am
@hayden thanks for the addition !
November 29th, 2011 at 11:17 am
Excelent article!!!!!! Thanks very much you help me to much and now i understand more the eaccelerator.
Thanks from Spain