Pages

Sunday, July 3, 2011

How I speeded up my Moodle site (two essential tips)

Preface
If you are a Moodle 2.x user (Moodle 2.4.1 is the latest as of now - January 2013) looking to speed up your Moodle production site, you might want to check out Cloudways.com's article entitled "Boosting Moodle Performance - Tips To Speed Up Your Moodle Install", Jonathan Moore's excellent "Why Your Moodle Site is Slow: Five Simple Settings",  Frederic Never's equally magnificent "Make your Moodle courses load faster without fiddling with the server" and Jonathan Moore's "Why your Moodle site is slow: five simple settings".

My article below is still useful, but dated since I was using Moodle 1.9.15 during the time of writing. Anyway, I suppose you reading this because you're desperate for ideas to speed up your slow  Moodle site, right? If that is the case, Random Net Surfer, then you've come to the right place!

Credit: Warner Bros

This article was first posted on Moodle.org here on 15th January 2011. At the end of this post, you will know how to speed up your site with mod_deflate. You will also know how to enable PHP caching for your website by installing eaccelerator. Lastly you will know how to monitor your website's resources with munin graphs.

45 days ago, I started by journey of hosting my own Moodle site on a Web Host company. I started off with shared hosting, got turned off by the slow speeds, then upgraded to Virtual Private Server. The site just chugged along but my students complained that the site would load ever so slowly. Now, 1.5 months later, it's a different story.


I have spent countless hours and days, while others are asleep, surfing the Net feverishly for ways to optimize and to speed up my Moodle site. I've surfed to moodle.org, googled my way through, gingerly typed detailed Linux commands along the way. BTW, I love Linux now.

I've experienced hundreds of emails to and from my Web Host company, had a major server outage, times when my domain didn't load, recovered from a full harddisk due to automated & unmonitored backup files (I can't forget that one!), blank Moodle pages, loss of data, recovery and backups along the way. I survived. But TODAY, I think I've finally got it. Henry Higgens of "My Fair Lady" would say, "By George! I think (s)he's got it!!!". Anyway....here are two reasons why I am SO HAPPY TODAY.

Reason1: My Moodle site FINALLY runs with mod_deflate page compression.
So my moodle http requested pages are compressed before they are sent to the users' web browsers. Benefit: faster loading of pages, less waiting by users for their Moodle pages to load. http://moodle.org/mod/forum/discuss.php?d=165904








Enabling mod_gzip worked fine EXCEPT that on my Linux Moodle site (http://scm.moodleace.com), my images got gzipped. This resulted in the images on my Moodle site not being displayed in their image-holders! Urgh!!
So I disabled the compression of mod_gzip by removing this line from my php.ini: output_handler = ob_gzhandler
The GOOD NEWS is that I discovered the wonder of mod_deflate instead. My Centos VPS comes installed with mod_deflate. Refer to this website to see how it is implemented:
http://articles.sitepoint.com/article/mod_deflate-apache-2-0-x
-and-
http://www.electrictoolbox.com/compress-files-apache-mod-deflate
-and-
http://moodle.org/mod/forum/discuss.php?d=87024
Basically, you want to enable on-the-fly compression for these file types: php, html, text files, css, javascript. You also want to disable on-the-fly compression for these file types: pdf, images, zip, tar, tgz, any compression file, mp3, etc.
To test your Linux/Centos server to see whether it comes installed with mod_deflate, type:
# httpd -M

After modifying your /etc/httpd/conf/httpd.conf file, you need to:
1. Reload your Apache by typing in Linux: /etc/init.d/httpd reload
2. Check out your website to see if the mod_deflate is working by surfing to: http://www.whatsmyip.org/http_compression (don't you just love this site?).
It should be WORKING!
Hope this helps! wink

Here's the bottom part of my httpd.conf file:




Reason2: My Moodle site FINALLY runs with eAccelerator (PHP caching) just fine and dandy!
Benefit: faster loading of pages, less load on the VPS processor since first-time compiled PHP code is cached to shared RAM. Subseqent web browser http requests of cached PHP code is sent direct to the user with no need to re-compile php code.  See http://moodle.org/mod/forum/discuss.php?d=166284

Now, iniatially the caching did NOT work. It refused to work and I had no idea why!

I setup eaccelerator with these definitive guides. These links have helped me to set up eAccelerator on my 512Mb RAM Linux (Centos 5) Virtual Private Server !!!!!
http://www.theadminzone.com/forums/showthread.php?t=8152
-and-
http://eaccelerator.net/wiki/InstallFromSource
-and-
http://www.php.ph/2007/12/21/centos-5-eaccelerator-installation/
-and-
http://moodle.org/mod/forum/discuss.php?d=164199

But following the instructions, I discovered that that my pages still wouldn't cache. See my plight in http://moodle.org/mod/forum/discuss.php?d=166284
So in desperation, I contacted my Web Host and they gave me some clues. I then surfed somewhere where the webpage mentioned that "eAccelerator only works with mod_php or php in fastcgi mode. It can't be used in cgi or cli because eAccelerator needs to set up shared memory, and this can only be done when all php instances that need to access it are forks of the first process."

Aha! The game's afoot! So with guidance from my web host, I setup my Plesk Panel so that php would run not as cgi, but rather as fast-cgi!!!



THEN to my amazement and wonder, my pages actually started to cache. Not just a few cached pages, but hundreds of cached pages just by clicking around a few times around my Moodle site. Hallelujah!!!
See the attached images to see the caching of my php code working as it should.


Another screen capture, this time accessible from within my Moodle main page (Admin
settings). I.e., "Site Administrations | Server | PHP info". Scroll down through all the Php
info until you come to the eAccelerator section. Notice it says 621 pages are cached?
Well PHP code is being compiled and store in shared memory. Each time the same page
is requested by a web browser, my Virtual Private Server (VPS) will sent the already compiled
code from shared memory, thus saving time. So a person surfing my Moodle site will
experience faster page loads!!!

I have allocated 64Megs of RAM for the Php caching. As you might know, this amount
of RAM will quickly be filled up with cached PHP compiled code. So the more RAM my VPS has,
the better the surfing experience will be.

Why am I over the moon with Webpage compression with mod_deflate and Php Acceleration of my Moodle site? Simple! My VPS is running on a measly 512Mb RAM!!! Some of you  guys have a Moodle server running on 4 Gigs of RAM!!! And my server's (VPS's) download pipe is trickling in at 512Kbs best effort. Some of you are enjoying 10Mbs or 100Mbs speeds for your servers, you 'lucky' ones you. So now you know why I am so happy to have speeded up my Moodle site the way I have shared with you. wink



[Update: That was then. Here are my php caching statsas of 3rd July 2011]




My Moodle site is running FASTER now. I am safe for now, just as long as not all 20 students of mine log in at the same time inside the computer lab to my Moodle site...[2nd July 2011 update: I have experienced smooth access of my site even with 25 concurrent students, each taking the same quiz!]

here are the specifications of http://scm.moodleace.com: 512Mb RAM, 512Kbps pipe, (Centos 5, Php 5.2.17) Virtual Private Server (VPS). Now all I need is an additional 3.5Gig RAM and a 100Mbps line for my VPS. Ahh..that would be perfect.

My limited funds to upgrade my site have run dry. So if anyone else can share with me MORE concrete ways to speed up my Moodle site, I am all ears, and I will be HAPPIER STILL.  How can I optimize my MySQL? Anything else I can optimize my Linux Moodle site?

To monitor the site, I also got the Munin monitoring graphs working. Thanks to this webpage entitled "Install Munin On Plesk Dedicated Server".  What do the graphs mean? I have absolutely no idea at this time. But they were look pretty and colourful to me. big grin  Source: http://www.supportfacility.com/blog/install/install-munin-on-plesk-dedicated-server


Frankie Kam
http://scm.moodleace.com
Melaka, in sunny and rainy Malaysia

Update on June 9th 2013

Thanks to Sarah Jane for highlighting this very informative and easy-to-understand Youtube video by Chris Coyier on 4 steps to speed up your website. What I love about the video is Chris' style of of narrative, explanation and presentation visuals of making the concepts simple and easy to grasp. If you have another 28 minutes to invest in speeding up your website, please watch this video and learn from the expert.



Another good link by Kory Prince entitled "How to speed up your Moodle or any PHP-enabled server"
More tips can be found on this very nice post on Stackoverflow.

Added on 21st July 2013
Greg Padberg shares how he increased his Moodle server's speed in this forum. Very instructive.
Yulstar shares here how he speeded up the processing on his Moodle development server three times.
Tim Hunt has this tip to increase the speed of large Moodle sites. Verified by CP Lau.
It is interesting to note here that eaccelerator may not be the best PHP caching software to use. There are alternatives.
Please also read Tim Hunt's excellent blog post on performance-testing Moodle.
Lastly, here's a wealth of information on increasing your Moodle site's speed.

Added on 30th September 2013
More material on how to enhance the performance of Moodle 2.5 here.

End

If you like this post or site
a small donation would be nice but would last only a day,
otherwise leaving a comment (or a compliment) below will last me a month!

Ratings and Recommendations by outbrain