How to Speed Up your Moodle (Or Any PHP-enabled Server) PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Kory Prince   
Thursday, 08 September 2011 17:27
AddThis Social Bookmark Button

How to Speed Up your Moodle (Or Any PHP-enabled Server)

It's been a while since I posted, but as you can see, I am not dead or in the witness protection program.

My topic today is fairly simple to implement, and can make things a lot better for you.

At work we have a single Moodle server running 6 separate moodles (plus 1 test moodle) using the single codebase method I have written about before. We are deploying a 1:1 initiative, and one of our main pushes is for teachers to move to using Moodle in the classroom.

As I lay in bed at night, I realized that our Moodle server, while not the wimpiest box, might could use some extra power when being assaulted by an extra 400 users. Thus, I set about researching ways to improve server performance.

One of the easiest and best methods I have found so far is to use eAccelerator. To understand how it works you need to understand a little about how PHP works:

Nerd Alert

In terms of programming languages, you have for the most part two kinds, those that are compiled, and those that are interpreted. Languages such as C, C++, Java, and VisualBasic are all compiled languages - meaning you write the code, then run a complier on the code which spits out a nice executable file.

Languages such as PHP, Perl, Python, and Javascript are interpreted languages, meaning there is no compiler. When you run the program an interpreter goes over each line of code and runs it. This makes it a whole lot easier to code and test, but there is also a performance hit because it has to interpret it each time.

Now PHP does some funky stuff when you run it: it sort of compiles it then runs it. The sort of compiled code is called opcode. So that's really cool, but the problem comes when it gets run multiple times. Say you get a hundred hits at https://mymoodle.com/index.php. Well the PHP interpreter has to convert the PHP to opcode a hundred times. "That seems a bit silly," you say. "Why not just compile it once then use it a hundred times?" Bingie. Meet eAccelerator.

Enough with the Nerd! Make my Moodle faster!!!

Eaccelerator does exactly that: it caches the PHP opcode so that it doesn't get converted every time. Depending on who you believe, eAccelerator can double, triple, or even increase your speed 10-fold. So let's get on with it shall we? Note: for this tutorial, I will be using Ubuntu 11.04. But the install should be almost identical on other Linux distros. Look for Windows stuff here.

To Compile or not to Compile, That is the Question

I decided to go ahead and compile eAccelerator myself using the instructions over at Ubuntu help.

You'll need to break out your terminal and just follow along. Don't worry it won't hurt you.

sudo apt-get install php-dev

(Or use whatever package manager you have)

cd /tmp
wget http://bart.eaccelerator.net/source/0.9.6.1/eaccelerator-0.9.6.1.tar.bz2
tar xfj eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1
phpize
./configure
make

At this point you have a couple options if you are on a Debianish distro. You can use the checkinstall utility to generate a .deb file that you can install on all your servers by running: sudo checkinstall
and going through the menus. Or you can simply run sudo make install
and be done with it. Now you will want to configure it. Taking the default config from the howto, open up /etc/php5/conf.d/eaccelerator.ini and paste in:

extension="eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/var/cache/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

If you want to tweak the settings, see here, but these settings worked for me.

Even a little bit better...

I wasn't just satisfied with that though. I wanted it a little bit better. What if those opcode files just stayed in ram all the time? It would help a lot if the disk is already being used a lot. This is really really easy to do with linux with the idea of a tmpfs. Basically its a part of ram mounted as part of the filesystem. So even though it may look like you're making a file on your hard disk, it's actually stored in ram (and thus reading and writing will be a lot faster.)

Edit your /etc/fstab and add the following line at the end:

tmpfs /var/cache/eaccelerator tmpfs defaults 0 0

Now run:

sudo mkdir /var/cache/eaccelerator
sudo chmod 777 /var/cache/eaccelerator

Now just restart PHP (or if you use apache, just restart apache.) If you run:

ls /var/cache/eaccelerator

You should get something like:

0 1 2 3 4 5 6 7 8 9 a b c d e f

That means it's working. If you don't see any folders in there go back over this tutorial to make sure you did everything right. If you do see those folders, go try out your Moodle or other PHP website and see if it doesn't run snappier.

I am still looking into other ways to get better performance. On some of our other servers I switched from apache Cherokee. Cherokee is an awesome up and coming web server which has high marks in performance. What's better is an awesome web interface for configuring it. I am using it in conjuction with php-fpm, a high-performance PHP implementation that works a bit better than the mod_php that comes standard with apache.

Our other servers, but it seems to hate our moodle, as several things just don't work when I switch over. There is a way to use php-fpm with apache, and I will look into that soon. For now, however, eAccelerator is a quick install that can make your server performance much better. As always don't hesitate to contact me.

Last Updated on Thursday, 08 September 2011 17:34
 
 
Total Views: 426