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

apc.shm_size

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 }'

apc.num_files_hint

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

apc.stat

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.

apc.max_file_size

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

Example

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.
extension=apc.so
apc.enabled=1
apc.shm_segments=1
apc.shm_size=100
apc.num_files_hint=5000
apc.ttl=7200
apc.stat=0
apc.include_once_override=0
apc.enable_cli=0
apc.max_file_size=2M
apc.rfc1867=0
apc.rfc1867_prefix =upload_
apc.rfc1867_name=APC_UPLOAD_PROGRESS
apc.rfc1867_freq=0
apc.rfc1867_ttl=7200
apc.write_lock=1
apc.optimization=1
apc.user_entries_hint=0
apc.user_ttl=7200

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
Source : http://www.iteachwithmoodle.com/2014/01/23/how-to-optimize-a-moodle-server-part-4-apc/

0 Response to "How to optimize a Moodle server? Part 4 – APC"