How to optimize a Moodle server? Part 4 – APC

Moodle APC

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 fourth post is about enabling APC, an opcode cache on your server, probably the easiest and most efficient step to increase server performance.

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

Why it matters

As explained in a previous post, Moodle is made up of files & folders saved on a hard drive (server). Whenever a user performs an action on Moodle (e.g. clicks on a link, posts a forum message, etc.), a bunch of files are retrieved from the hard drive. It takes a certain amount of time to retrieve those files, but the faster your hard drive, the faster the files will be retrieved. An opcode cache places those files in the RAM, for a length of time you decide on. Retrieving files from RAM is significantly faster than retrieving them from the hard drive (even if you have a fast hard drive).

Note: in my tests I have found that without APC, it takes on average 0.56 seconds to generate a Moodle page, with it that figure is only 0.22 seconds. That’s over twice as fast!

How to enable it on your server

SSH to your server and type the following command, which will install APC for PHP:

sudo apt-get install php-apc

Restart Apache for the changes take effect:

sudo service apache2 restart

That’s it. Load a Moodle page, then try to load it again. The second load time should be much faster.

Enable APC.php

Enable the APC administration console to have access to graphs & other useful statistics about your APC installation, as well as an ’empty cache’ button should problems arise.

sudo cp /usr/share/doc/php-apc/apc.php.gz /var/www/ 
cd /var/www 
sudo gunzip apc.php.gz 
sudo nano apc.php

Change the values for ‘ADMIN_USERNAME’ & ‘ADMIN_PASSWORD’ to something hard to guess. Then exit and save the document.

Visit http://yourserver/apc.php to check it works.

Fine tune APC

By default, APC isn’t usually setup to get the most out of it for your particular installation. Adjusting some of the values will ensure your APC cache never fills up and isn’t too fragmented.

From my tests, there are a few values that really should be adjusted and those are


This determines the amount of RAM APC can use, and therefore the amount that it can store in its cache. Ideally you never want your cache to fill up. To find out how much space your Moodle installation uses, you can use the following command (assuming your Moodle is in /var/www/moodle):

find /var/www/moodle -name '*.php' -exec ls -l {} \; | awk '{ SUM += $5} END { print SUM/1024/1024 }'


This tells APC how many files you will store in its cache. To find out how many files your Moodle installation contains, you can use the following command (same assumption as above):

find /var/www/moodle -type f -name "*.php" -print | wc -l


In the APC world, to ‘stat’ means to check. Basically, every time a script is run APC checks if the version it has in its cache is the freshest available (i.e. has it been changed on the server). If the file in cache no longer matches the one in the server (i.e. the file has been changed), APC will remove the file in its cache and replace it with the newer version. This process adds a slight amount of time to each request. For pure performance, turn it off.


This setting prevents any files bigger than the value set to be saved into APC cache. By default, it is 1MB and you may have larger files in your Moodle installation (some PDF lib files are large). In order to find out, you can use this command (same assumption as above):

find /var/www/moodle -type f -name "*.php" -size +500k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

In order to edit your own apc.ini file, you need to type:

sudo nano /etc/php5/conf.d/apc.ini


This is what my apc.ini file looks like. I don’t pretend this to be the best configuration possible, but it has certainly worked well for me so far.
apc.rfc1867_prefix =upload_

Further reading

You can find all of the information you need on the official APC documentation page.

Photo credit: F-18 Super Hornet by dcis_steve


  1. And there you have it. Good write up and my hearty thanks for the effort put in this 4 part series on how to optimize your ‘Moodle server, Am linking to your posts in my blog. Keep up the good work, Frederic.

    1. Hi Frankie,
      glad you found it of use. I have a few ideas for the blog, but little time to make it happen. Hopefully by August I’ll have a new series of posts available.

  2. Frederic,

    Great article, very helpful.

    Do you do performance consulting or do you know anyone that does it professionally for the Moodle platform that you could recommend?

    1. Hi David,
      sorry for the late reply – your comment got lost somewhere! Unfortunately my current contract does not allow me to ‘work on the side’. Maybe from August.

  3. Hello Frederic,

    My name is Ola, i have used moodle for a while as well (10yrs) however, recently on one of my installations, i keep getting crashes every 2-3mins. this makes the site unusable for anybody. i have done a clean install of 3.2.3 and it is still the same. i am at a loss to what or where to turn. site is

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.