Kory Prince
AddThis Social Bookmark Button


How to Speed Up your Moodle (Or Any PHP-enabled Server) PDF Print E-mail
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
 
Using SSH Tunnels for Better Security PDF Print E-mail
Written by Kory Prince   
Sunday, 10 April 2011 14:38
AddThis Social Bookmark Button

Usage Case: Arnold is a responsible network administrator who does not wish to open up the often exploited phpmyadmin to the outside world or even to the curious sort on his internal network.

Susan needs to use Webmin to manage one of her servers remotely. Of course, since Susan is responsible, the Webmin is not accessible to the outside world.

There is a simple way to make this all happen, and that is what this tutorial is all about. We only need to do two things:

  • Block access to the service from the outside the server (be it phpmyadmin, webmin, or any other service...)
  • Make some way to access the service remotely and securely.

Note about this tutorial: We assume you are using linux as your server and apache as your webserver. This should cover most of the users reading this. If not, shoot me an email and I can help you out.

Blocking Access

This part is fairly simple, depending on the service. You basically need to block access from everywhere but localhost. For Webmin simply go to Webmin->Webmin Configuration-> IP Access Control. Select the "Only allow from listed addresses" and type:

127.0.0.1
127.0.1.1

Into the box below and click save.

For phpmyadmin you need to edit the apache configuration directly. All you need to do is edit the phpmyadmin configuration file and add the this to the bottom

Make sure that /usr/share/phpmyadmin is really where your phpmyadmin files are. This is the default for Ubuntu. The configuration file is at /etc/apache2/conf.d/phpmyadmin.conf on Ubuntu. Make sure to restart apache to take effect.

WARNING: I think it goes without saying, but I will say it anyways: Don't try and do this tutorial if you don't have SSH installed on the machine. Especially for the Webmin side, if you make the changes from a remote machine, you will effectively lock yourself out of your server if you don't have physical access to it.

Once you do the previous steps, try getting to the service from a remote computer. If remote access is denied Success! You get to move on. If not then make sure your followed all the steps and restarted the service. If you still need help let me know.

Accessing

Now we get to the fun easy part. All you need to do is open up a terminal and type:

ssh -L localport:example.com:remoteport user@example.com

That's a bit of a keyboardfull, so let me give you an example and then break it down for you:

ssh -L 8080:example.com:80 admin@example.com

This means I'm logging into the server at example.com with the user admin. With the "-L" switch I'm forwarding port 8080 on my local machine to example.com:80. This means that when I access 8080 on my local machine, I'm actually accessing port 80 on the remote machine, all the while being moved securely through ssh. It's crazy bananas.

Say you had webmin running on a server at webmin.myisd.edu (on port 10000 by default.) You would run:

ssh -L 8080:webmin.myisd.edu:10000 user@webmin.myisd.edu

Now just direct your web browser to https://localhost:8080/ and your lovely webmin which was quite unreachable just seconds ago will now pop up. (For phpmyadmin you would have to use port 80 instead of port 10000 and go to http://localhost:8080/phpmyadmin/ instead.)

As a quick aside, you do not have to pick port 8080. It just looks like a nice number. You can choose any port you like, however if you are not root you have to choose one above 1024. Just make sure the port you pick does not already run as a service on your machine and that the port matches in the ssh command and the url.

And that is it for this fairly short tutorial. As one last comment, if you are on a remote windows machine, we all know that you will usually use PuTTY for ssh access. It too can forward ports and this tutorial will show you how to do that. That's it for now! As always, if you need any help don't hesitate to contact me.

Last Updated on Sunday, 10 April 2011 14:57
 
Using Multiple Moodles with One Codebase PDF Print E-mail
User Rating: / 1
PoorBest 
Written by Kory Prince   
Sunday, 03 April 2011 21:30
AddThis Social Bookmark Button

Usage Case: Sam is an IT administrator with not enough hours in the week. He is given the task to create a separate moodle for each of the 10 campuses in his district. Knowing that he will have to keep each moodle updated, he has nightmares about not having enough time to manage all of them.

Bob is a lazy IT person who is given the task of creating two moodles for his company. Being lazy, he wants the install and updates as easy as possible.

Both cases can be solved easily with a simple method: using one moodle codebase for multiple moodles. What this means is that each moodle will have a separate data directory and database, but will all share the same web files. So let's begin!

Note about this tutorial: I don't add sudo by default to commands that you will probably need to add it to. If something doesn't work you might need to add "sudo" in front of the command. If you don't like having to type in sudo a lot, you can always su to the root user. You do so at your own caution.

Server Install

First you need to install your server, whether it be Ubuntu, Suse or any other linux distro (This IS for Linux in general.) I will be using Ubuntu in this tutorial, but most commands should work fine on a standard linux box. The specifics of setting up your server aren't going to be enumerated, but you will need the following:

  • Apache (or equivalent Webserver)
  • PHP 5
  • MySQL
  • git

I also recommend installing phpmyadmin. It will help make database things much easier.

Apache SetUp

Now you get to set up your site the way you want. I will use the following assumptions:

  • Your Moodle Web Directory is /var/moodlewww/
  • Your Moodle Data Directory is /var/moodledata/
  • Your Domain is example.com
  • Your Database naming scheme is moodle_sitename
  • Your Web Scheme is https://example.com/sitename

Now we need to set up apache to handle everything correctly. You can see an apache config here that can help you get set up. For Ubuntu, the config will be at /etc/apache2/sites-available/default. For other distros inspect your user manual. This config WILL NOT work by default for you. First you need to change everywhere where it says example.com to your domain. Second, you need to change the site1, site2 lines to the sites you want. (Don't remove the default line yet!) You will have to change this in two places. Third it assumes you have ssl installed.

Brief Rant: A lot of people won't set up SSL because they don't understand it, or it takes longer. This is bad. You don't need it until it is too late. A teacher logging into your moodle at starbucks could easily get her account compromised. Suddenly bad things happen to your moodle. The situation gets even worse if your moodle is tied to your Open Directory or Active Directory structure. If you are still scared take a look here. Not only will it show you how to add your SSL certificate to apache, but it will also show you how to create your own if you are too cheap to buy an official one. In short, use SSL.

Now if you still wish not to use SSL, here is a config just for you. However I will assume you are being the responsible system administrator and you are using SSL.

This is perhaps the most difficult part to set up if you have never had to delve into apache's depths. Don't be hesitant to contact me for help getting set up.

MySQL Setup

Next we need to get our databases set up. Using our assumptions, you need to create a database named "moodle_default" It's easiest to do with phpmyadmin. Log in and you will see a box that says "Create new database." Below that you will see "MySQL connection collation:" Change the box beside it to "utf8_unicode_ci" (It should be at the very bottom.) THIS IS IMPORTANT! Now type in "moodle_default" into the box and click create. Yay we are done with this part!

Moodle Setup

Now we get to the fun part. We are going to install moodle with git. We do this for a few reasons:

  • Git is easy to use
  • Git makes updating super easy
  • Moodle is moving to git for 2.0
  • Git is cool.

For the sake of this tutorial, we'll assume you've never used git, and have no desire to learn anything more about it than you need. Now it's command-crunchin' time. So following the blinking cursor: (Note: some of the commands may fail because of permission errors. If so append a "sudo" to the beginning of them, or change the directory permissions.

cd /var/
git clone https://github.com/moodle/moodle.git moodlewww
cd moodlewww
git checkout .
git checkout origin/MOODLE_20_STABLE

Note: this tutorial assumes you'll be using 2.0. If this is not the case use MOODLE_19_STABLE. I can't assure that the tutorial will work completely however.

git branch mymoodle origin/MOODLE_20_STABLE

Now you need to create a data directory for the default moodle. Create the directory /var/moodledata/default.

Now we need to make sure that the permissions are correct for everything. Basically you need to make the owner of moodlewww and moodledata the webserver user. For Ubuntu its www-data. You will need to check other wise. Any easy way to do this is to use this script I made. Make sure to make the script executable!

Now it's time to install moodle through the web interface. Point your browser to https://example.com/default and go through the install. When you get to the MySQL page, make sure to make the database "moodle_default" and make the "Tables prefix" empty. You may get to the Server Check page and have some problems. On ubuntu I had to run the command:

sudo apt-get install php5-curl php5-xmlrpc php5-intl

The package names should be the same for most distros. If you can't figure it out, you can click on the link on the page for more info, or ask me. If you have to add anything restart apache and continue on.

Finish the installation. I have noticed that sometimes the installation will hang. (The page will finish loading, but won't refresh. Make sure it isn't still transferring data.) If this is the case just reload the page. Once done set up the moodle as a default moodle. This means setting up any settings your want to be on all your moodles. Examples include login policies and email.

Now it's time to edit the config file for moodle. Open up /var/moodlewww/config.php with your favorite text editor. Now compare your config with the one here. You need to add the three lines under "//Get site name" to your config as well as change any line that has "$site" in it to match. There should be 3 lines you have to edit.) DO NOT just copy and paste the whole config. That is bad. Reload the moodle to make sure it works right. If so then you are ready to start creating new sites. Note: you may have to fix permissions after editing the file. That's where the script comes in handy.

Adding New Sites

To add a new site you need to edit 3 places:

  • Apache Config
  • Database
  • Moodledata Directory

Apache config There are two places you need to edit in the apache config to add a site. (Only 1 if you aren't using ssl.) Just copy and paste a line and change it to your site name. Make sure to reload apache after you save!

DatabasePhpmyadmin makes this very simple. Login, and on the sidebar you should see your moodle_default database. Click on the "Operations" tab. You should see a part where it says "Copy database to." Type in moodle_newsitename and click go. Do this for all the new sites you are creating. Note: if you are doing quite a bit of sites it would be wise to find out how to do this with the command line. It should be simple and much faster.

Moodledata DirectorySimply run the command:

cp -R /var/moodledata/default/ /var/moodledata/newsitename

(You may have to use sudo.) Again, you will have to fix permissions. Just download the script and use it. You will have to fix the permissions every time you make a new site.

Now just navigate to https://example.com/newsitename and glory in your accomplishment

Cleaning Up

Since the default moodle is, after all, your default moodle, you don't want someone stumbling across it and messing anything up. So I suggest you go into your apache config and comment out the lines for your default site. (And reload apache!) Also to make sure upgrading moodle with git will be easy in the future we need to run a few commands:

git checkout .
git add .
git commit -m "Local Changes"

(May have to use sudo)

Upgrading, Adding Themes, etc

So now we got everything in shape, but this thing was all about easy upgrades right? So what do we do to upgrade moodle? This:

git checkout .
git fetch
git merge origin/MOODLE_20_STABLE

And that's it. You are now at the latest stable version of moodle. You will probably need to run the permissions script again to fix things up.

If you want to install new themes, plugins,etc into the moodlewww directory then follow these steps:

  • Do the same exact commands as above
  • Add the files you want to their correct locations
  • Run git add .
  • Run git commit -m "Local Changes"
  • Fix permissions

And that's it! If you successfully got through this tutorial you are well on your way to being a moodle master. As always, if you need any help with this tutorial or have any questions about moodle or linux in general don't hesitate to contact me with the contact form above or clicking here.

Last Updated on Sunday, 10 April 2011 14:41