How to optimize a Moodle server? Part 1 – Apache

Non optimized Moodle servers can crash under light load

This ‘Speed up Moodle’ series of 4 posts will teach you step-by-step how to optimise your Linux server for Moodle. It is aimed at beginner server administrators. If you find any mistakes or inconsistencies, please comment and I’ll rectify ASAP. This first post is about optimising Apache for Moodle. Check out the other posts on optimising MySQL, installation of an opcode cache such as APC and other ways to optimise your Moodle server. Please see ‘Assumptions’ and ‘Technical notes’ at the bottom of this post.

Disclaimer: I am not a professional server administrator but I have looked after Moodle servers for several years now, and never experienced major issues. What I am sharing has worked for me, but might not work for you. You’ve been warned! 

Why it matters

If there is ever one configuration file you should tinker with, it is the Apache configuration file. More often than not the default configuration of Apache will crash a Moodle server when under a bit of load, or at least make it very slow. Before I explain how to ‘fix’ it, let’s explore the problem in plain English (what I understand of it, anyway).

  • When a user (proper word: connection) visits a Moodle site, the server allocates a ‘slot’ (proper word: process) for that user.
  • Each ‘slot’ uses a slice of the available RAM and remains reserved for that user for a pre-determined length of time (proper word: KeepAlive).
  • ‘Slots’ cannot be shared between users, even if they visit the same page, which means each user needs their own ‘slot’.
  • With Moodle, each ‘slot’ uses quite a bit of RAM (in my tests, on average 50MB but can reach up to 100MB) and this could crash your server very quickly if it is low on RAM. For example, it would take approximately only 20 users simultaneously performing some sort of action (i.e. click on something) in Moodle within 5 seconds of each other for a Ubuntu server with default Apache configuration and 2GB of RAM to crash, which is far from being inconceivable in a classroom environment.
  • The following is a graphical representation of a theoretical Moodle server crashing due to over-allocation of RAM (emphasis on *theoretical*).

Non optimized Moodle servers can crash under light load

Click to zoom in

What settings need to be changed

On Ubuntu, your Apache configuration file will most likely be found at

/etc/apache2/apache2.conf

In order to modify the contents of this file, you can either use your favourite FTP client, or use the Command Line Interface

sudo nano /etc/apache2/apache2.conf

MaxClients

Default: MaxClients 150     Recommended value: Depends

MaxClients is probably the most important value to change in the Apache configuration file. The default of 150 is too large a number in a lot of cases, as it equates to saying: “My server can cope with 150 users simultaneously using Moodle at the same time (i.e. clicking on something within 5 seconds of each other) “, which we found out is not possible unless you have a server with a lot of RAM. In order to find out what values you should be using for your server, you’ll need to get the calculators out.

1. First, you need to find out how many Apache processes are currently running, and the average size of a process. This is best done whilst using some sort of load testing tool to generate traffic, or on a production server. You can use the following code snippet to calculate the values.

ps aux | grep apache | grep -v "\(root\|grep\)" | awk '{sum+=$6; ++n} END {print "Total RAM="sum"\tNumber of processes="n"" "\tAverage="sum/n/1024"MB"}'

Calculate Apache total average and process size Moodle

In this example, there are 6 Apache processes, each consuming an average of 54.6MB of RAM

2. The second thing to work out is how much memory you can spare for Apache to use. This is done by finding our how much RAM your operating system needs to run, and thus the ‘free’ RAM on your server. You never want to run out of RAM, as it can either get your server to swap (upset users as site is super slow) or crash (irate users as site simply doesn’t work). I like to restart Apache and MySQL before performing this task, to get readings as ‘accurate’ as possible by flushing any buffers and stopping any processes (this will make your site unusable for a very short period of time).

sudo service apache2 restart
sudo service mysql restart
free && sync && echo 3 > /proc/sys/vm/drop_caches && free
free -m

free -m command for Moodle

In this example, the server has 1590MB available RAM

3. Time to calculate the value for MaxClients and modify. The calculation goes something like

MaxClients = [Free memory*80%]/Apache process size

Example:  [1590*80%]/54.6      –>   MaxClients = 23.3

So in our example of a server with 2GB of RAM, we end up with 23 maximum concurrent connections, which is considerably less than the 150 by default. In fact, in our example one would need up to over 10GB of RAM to be able to serve 150 concurrent connections on a default configuration serving Moodle!

4. Restart Apache for the changes to take effect

sudo nano /etc/apache2/apache2.conf

Apache configuration file MaxClients

5. Restart Apache for the changes to take effect

sudo service apache2 restart

6. Test & monitor. All of your efforts would be worthless if you didn’t test your new configuration. I would recommend you try and simulate load at least equivalent to your MaxClients. Here is a post outlining 5 free tools you to monitor your Moodle server performance. You can find out how to simulate load here.

MaxRequestsPerChild

Default value: 0     Recommended value: 30

The recommendation in the Moodle performance page is to set this to 20 or 30, but no lower. I must say that in my tests, changing this value has never made a huge difference, so I do as I’m told and set it to 30.

KeepAlive Timeout

Default value: 5     Recommended value: 2

As we have seen when a user performs an action,  an Apache process is started. This process stays ‘alive’ on the server for a pre-determined amount of time, using up RAM. After that period of time is over, the process ‘dies’ and the RAM is freed up. The lower the value for this setting, the shorter the amount of time RAM is used up by an Apache process, allowing for other Apache processes to use the available RAM. On my systems, I find that a value of 2 seconds yields the best results but your mileage may vary. There’s no simple answer to this one, apart that if you’re really short of RAM, then you’re probably better off keeping this value as low as possible and I would definitely not recommend setting it to higher than the default value of 5.

sudo nano /etc/apache2/apache2.conf

Apache configuration file KeepAliveTimeOut

sudo service apache2 restart

KeepAlive

Default value: On     Recommended value: On

This settings set to ‘Off’ equates to KeepAliveTimeOut 0. Basically the Apache process ‘dies’ as soon as it has served the request. Because of the way Moodle works, on a ‘out-of-the-box’ server I would recommend you against changing this setting to ‘Off’. If you use a reverse-proxy then you could try and see if it makes a difference on your setup.

Timeout

Default value: 300     Recommended value: 30

Sometimes it might take a while for your server to serve a particular request and Apache will wait a pre-defined amount of time before ‘giving up’, which is what the ‘Timeout’ setting is for. The Moodle performance page recommends 30 to 60 seconds for this value. I personally use 30 seconds.

sudo nano /etc/apache2/apache2.conf

Apache configuration file Timeout

sudo service apache2 restart

Example for my Moodle site

Here is what my Apache configuration file looks like for a production server with 2GB of RAM. I am not saying that it is perfect or you should use the same values, but it gives you an idea of what it looks like when the changes mentioned above have been applied.


Assumptions

  • You have already visited the Moodle performance page
  • You are running Ubuntu Server 12.04 LTS  (although most of the tips can be adapted to other Linux flavours very easily)
  • You are using Apache/MySQL
  • You know how to SSH into your server, and you have ‘root’ privileges (actually logged in as ‘root’). If not, please visit this page for a quick tutorial to get you started on Windows, and this page for Mac users
  • You have installed Moodle and it is running well. If not, please visit this page

Technical notes

All of my tests were run on old hardware (released in 2005), typically what one would have lying around the school, unused.

  • IBM ThinkCentre A52 (8298)
  • Dual-core 3GHz Pentium D (no other service running on host)
  • No virtualisation
  • 2GB DDR RAM 400MHz
  • 7,200 RPM HDD 80GB with 8MB cache
  • Moodle 2.4.3 (build 20130318)
  • Ubuntu 12.04 LTS with no modifications whatsoever
  • UnixBench results
  • Sysbench results: CPU, Disk I/OMySQL

9 comments

  1. Pingback:  | Moodle News

Leave a Reply

Your email address will not be published.