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
 
 
Total Views: 357