Many Moodle Sites Part 3

Posted on 2:44 PM, under

So, lets dive into some of the deep details of having your own multisite moodle.   First, lets layout some ground work for how I have my site setup so that you will understand why things are done the way they are.  
  • My Moodle sites are laid out as subdirectories inside of my normal web site in the form of http://mysite.org/elearning/moodle_1 and http://mysite.org/elearning/moodle_2.  
  • All of this is running on a TurnKey Linux virtual machine.
  • Moodle data directories are in /var/lib/moodle_data/site_name
  • Moodle actually runs in /usr/share/moodle
  • All the Moodle databases are named moodle-sitename
Now, to be able to access the Moodle site at http://mysite.org/elearning/moodle_1 I created a folder in the web root of http://mysite.org called elearning.  Inside of there I created a symlink called moodle_1 that points to /usr/share/moodle.  Utilizing symlinks allows me to accomplish two things:
  1. Have Moodle installed outside of my web root.
  2. Reference one Moodle install that is not directly web-accessible by many "sites" such as moodle_1 and moodle_2
Once I got the moodle_1 symlink in place, the moodle_1 data directory in place, and the database moodle-moodle_1 setup I navigated to http://mysite.org/elearning/moodle_1 and the install script started automatically.  I proceeded through it as normal.  Once it finished I ensured the site was functioning as it should.

The next part is where it got tricky.  Since there is only one config.php in the Moodle root I had to modify it to use variables instead of static paths.  The result is a config.php that uses the database and data directory that correspond to the web address being accessed.  Here is the file that I created:


unset($orig_page);
unset($pieces);
unset($CFG);

$orig_page  = $_SERVER["REQUEST_URI"];
$pieces = explode('/', $orig_page, 4);

$CFG->dbtype    = 'mysql';
$CFG->dbhost    = 'localhost';
$CFG->dbname    = 'moodle-'.$pieces[2];
$CFG->dbuser    = 'mUser';
$CFG->dbpass    = 'password';
$CFG->dbpersist =  false;
$CFG->prefix    = 'mdl_';

$CFG->wwwroot   = 'http://'.$_SERVER["HTTP_HOST"].$pieces[0].'/'.$pieces[1].'/'.$pieces[2];
$CFG->dirroot   = '/usr/share/moodle';
$CFG->dataroot  = '/var/lib/moodle_data/'.$pieces[2];
$CFG->admin     = 'admin';

$CFG->directorypermissions = 00777;  // try 02777 on a server in Safe Mode

require_once("$CFG->dirroot/lib/setup.php");
// MAKE SURE WHEN YOU EDIT THIS FILE THAT THERE ARE NO SPACES, BLANK LINES,
// RETURNS, OR ANYTHING ELSE AFTER THE TWO CHARACTERS ON THE NEXT LINE.
?>
As you can see, there are some odd looking lines in here now so lets break it down.  We start out by clearing the variables:
unset($orig_page);
unset($pieces);
unset($CFG);
We then make two new variables:
$orig_page  = $_SERVER["REQUEST_URI"];
$pieces = explode('/', $orig_page, 4);
The first one stores what ever is in your address bar after the mysite.org.  The second takes and uses the explode command to break the address at the /'s and stores the results in an array.  The last part of the second line says that everything after moodle_1 is stored as a single string since we do not care about that part.  Putting this all together, the web address www.mysite.org/elearning/moodle_1/admin/replace.php is stored in an array as
 0:  
1: elearning
2: moodle_1
3: admin/replace.php

As you can see, there is nothing stored in the first part of the array... this is because we did not pass in the www.mysite.org part.

Next comes
$CFG->dbname    = 'moodle-'.$pieces[2];
$CFG->dataroot  = '/var/lib/moodle_data/'.$pieces[2];
which looks at what is in position 2 of the array, in this case moodle_1, and inserts it in the database name and the dataroot path.

Lastly, we have the most complicated looking line:
$CFG->wwwroot = 'http://'.$_SERVER["HTTP_HOST"].$pieces[0].'/'.$pieces[1].'/'.$pieces[2];
This strange combination of things pieces together the base web address of the Moodle site by doing the following:
  • http:// --enough said.
  • . --concatenate symbol in PHP.
  • $_SERVER["HTTP_HOST"]   --this pulls the www.mysite.org part of your web address
  • $pieces[0] --gets what is in position 0 of the array (not really needed in this setup, but easier to under stand that way)
  • '/' --adds a / to the address
  • $pieces[1] --gets the elearning part
  • $pieces[2] --gets the moodle_1 part
To save space, I am guessing you can figure out the parts that are repetitive.  

There is one more thing you need to do.  Once the site is up, go to Administration --> Server --> Session Handling and check the top box to use the database for session information plus enter the site name (moodle_1 in this case) in the box for cookie prefix.  These ensure your browser does not get confused about what site you are visiting.

That is it.  All set.  I am running two sites this way now and about to roll out the template site so that I can start creating the other needed sites.  Once I get the script written that is referred to in my last post I will post it also.  Hope this helps!





| edit post

0 Reply to "Many Moodle Sites Part 3"

  • Post a Comment