Multiple Instances of Moodle on a Sysyem

Multiple Instances of Moodle on a Sysyem

by Davood Rahimi Kinchaa -
Number of replies: 23
Hi every body
I want to install multiple instances of moodle on my linux system.
How can I do that?
cheers
Average of ratings: -
In reply to Davood Rahimi Kinchaa

Re: Multiple Instances of Moodle on a Sysyem

by Mariana Curado Malta -
1. Unpack the moodle zip (or whatever) file in different directories (as many as the instances of moodle you want to install):

/var/www/moodle1/
/var/www/moodle2/
/var/www/moodle3/
.
.
.

2. Define different names when you create on MYSQL the correspondent databases

something like:
create database moodle1;

create database moodle2;

and so on.

3. Follow the installation documentation and do it as many times as the moodle instances you have!

4. Define Virtual hosts on Apache

Is that clear?

Good Luck, Mariana
In reply to Mariana Curado Malta

Re: Multiple Instances of Moodle on a Sysyem

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
You don't absolutly have to define virtual hosts on apache, thus my site has multiple moodles defined as

www.examulator.com/moodle
and
www.examulator.com/tiger

both on same hosts but entirely different installactions (and versions) of moodle. The magic is that the tables associated with each instance of moodle have different prefixes, so you can run multiple moodles on the same database. Thus the tiger moodle has the prefix of tgr_tablename and the moodle one has the default mdl_tablename prefix.

If you have the control, its probably better to have each moodle on its own database, but as is typical of shared hosting, I only have one database.
In reply to Davood Rahimi Kinchaa

Re: Multiple Instances of Moodle on a Sysyem

by Ugarit Ebla -
The recommendation given to you indicates that people are not aware of symlinking.

If moodle were written without the use of realpath() (which breaks the power of symlinking) there should be no need to install multiple copies of moodle. Sadly moodle has made it virtually impossible to do this. Please refer to http://moodle.org/mod/forum/discuss.php?d=85688

I've opted to not using moodle precisely for this reason since I need to deploy an LMS for hundreds of schools. Imagine if one had to install and maintain hundreds of moodle installations. Had symlinking been working, then all the installations would point to one installation directory and the config.php and data directory per school would be symlinked to that school's configuration area.

Perhaps the problem is that MSWindows or programmers who don't know the power of Unix/Linux are involved and since MSW does not have symlinking then those developers would not even think of that capability.

In reply to Ugarit Ebla

Re: Multiple Instances of Moodle on a Sysyem

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
I know that the developers are familiar with symlinking; however, keep in mind that Moodle tries to remain functional on multiple platforms. Please search the forums as I do recall this issue being raised previously - although it was a good while ago. As I recall, someone had been able to setup multiple installations of Moodle on one server using the same code base. As I recall it required a little customization but it was feasible. I'm not sure how it might work now. Which version of Moodle would you like to use in this fashion? Peace - Anthony
In reply to Anthony Borrow

Re: Multiple Instances of Moodle on a Sysyem

by Ugarit Ebla -
I'm very fond of moodle but can't recommend it for large deployments because of this issue.

It's interesting that you say that moodle tries to remain functional on multiple platforms yet in essence it disables the strongest feature of Un*x/Linux. This issue has nothing to do with cross platform capability. It's the use of php's realpath() function which is breaking symlinking. This would not be an issue in windows because windows doesn't know how to do symlinking.

I would love to use the latest version.

I would recommend that moodle adopt something similar to drupal's sites capability.
In reply to Ugarit Ebla

Re: Multiple Instances of Moodle on a Sysyem

by Gordon Bateson -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
When I want several Moodle sites on the same server, I use Apache's rewrite module to create the "symlink" effect.

In the .htaccess file in the Moodle directory I add the following (the RewriteRule should be one line):

# ==============================================
# set Moodle environment variables
# (used in Moodle's config.php)
# ==============================================
#
# MOODLE_DIR : the script sub-directory
# MOODLE_WWW : the www sub-url
# MOODLE_DATA : the data sub-directory (under "moodledata")
# MOODLE_DBNAME : the database name (will be prefixed by "mdl_")
#
RewriteEngine ON
RewriteRule ^(moodle1|moodle2|moodle3|moodle4)(()|(/.*))$ moodle$2 [E=MOODLE_DIR:moodle,E=MOODLE_WWW:$1,E=MOODLE_DATA:$1,E=MOODLE_DBNAME:$1,L]

In Moodle's config.php I use the following:

// database access information
$CFG->dbtype = 'mysql';
$CFG->dbhost = 'localhost';
$CFG->dbuser = 'moodleuser';
$
CFG->dbpass = 'secretpassword';
$
CFG->dbname = 'mdl_'.$_SERVER['REDIRECT_MOODLE_DBNAME'];
$CFG->prefix = 'mdl_';

$CFG->wwwroot = 'http://myserver.com/'.
$_SERVER['REDIRECT_MOODLE_WWW'];
$CFG->dirroot = '/path/to/public_html/'.
$_SERVER['REDIRECT_MOODLE_DIR'];
$CFG->dataroot = '/path/to/moodledata/'.
$_SERVER['REDIRECT_MOODLE_DATA'];

In this way, for each Moodle site I can specify the database, data directory and Moodle code base in the htaccess file. If I want all the sites to use the same code base, I use something like the above code. If I want certain sites to use one version of Moodle and certain sites to use another version of Moodle I can do that too simply being modifying or adding the RewriteRule-s in the htaccess file.

regards
Gordon

Please note that for readability I have simplified the PHP code above. On a live server you should be sure to check that the REDIRECT_MOODLE variables contain allowable values before assigning them to your $CFG.
Average of ratings: Useful (1)
In reply to Gordon Bateson

Re: Multiple Instances of Moodle on a Sysyem

by Richard Enison -
GB,
  1. I don't see the value of always assigning "moodle" to MOODLE_DIR regardless of which folder name is used in the URL. I assume you intended to assign $1 as with the other three variables. And if all four variables will have the same value in any case, why not just use one variable?
  2. What is the purpose of prepending the database names with mdl_?
  3. How do the environment variables like MOODLE_DIR become $_SERVER[REDIRECT_MOODLE_DIR]? I can't find anything in the PHP manual, or the Apache manual, on this. I would have just used $_ENV[MOODLE_DIR], etc.
  4. This method forces one to use Apache instead of, say, IIS.
RLE
In reply to Richard Enison

Re: Multiple Instances of Moodle on a Sysyem

by Gordon Bateson -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Hi Richard
  1. the MOODLE_DIR is always the same because in the example I gave the same Moodle script folder is used for all the sites
  2. if you prepend the databases with "mdl_", then you can share the database with other applications using different table suffixs and still keep it clear which application uses which table
  3. $_SERVER['REDIRECT_MOODLE_DIR'] works on my system (Windows 2003 + Apache). I cannot guarantee it will work on your system, but I am pretty sure something like it will work. If you print out $_SYSTEM you will soon see what name the values are passed as
  4. this method applies to Apache with the Redirect module enabled. I don't know if IIS has anything similar
cheers
Gordon

In reply to Gordon Bateson

Re: Multiple Instances of Moodle on a Sysyem

by Richard Enison -
GB,

  1. I see. So the reason you use four variables is that in different situations they might not all have the same value. OK. But there is a problem, I think, in having different Moodle sites share the same script folder: there are places in Moodle where it actually writes to a script folder. Like when it creates config.php, or an xml file. This problem would apply to IA's solution, and RB's
  2. You have explained why you prepend the database tables with mdl_. That has been a standard part of Moodle for as long as I have been aware of it (just over a year), but that's not what I asked about. If you are going to share the database with other applications, why prepend mdl_ to the database name? And btw, it has always been my understanding that the table prefix's purpose is to allow such sharing between instances of Moodle, and/or other applications. I have gotten into arguments over this with other Moodlers in this forum, who insisted that the database must not be shared, even with another Moodle site. See http://moodle.org/mod/forum/discuss.php?d=78218
  3. Your answer to #4 clarifies your answer to #3. Apparently, it is the Apache Redirect Module that sets the REDIRECT_ Server variables. The Apache manual I have (httpd-docs-2.0.54.en.pdf) doesn't mention a Redirect Module. Although it has an October 6, 2005 date on the cover, I assure you I downloaded it on April 23 of this year, and it was the latest version available at the time.
  4. Not to mention the Rewrite Module (which I recently learned about as a result of http://moodle.org/mod/forum/discuss.php?d=85602). Another reason this solution locks one into Apache is the use of .htaccess.
RLE
In reply to Richard Enison

Re: Multiple Instances of Moodle on a Sysyem

by Robert Brenstein -
Re 1: As far as I know, the only time Moodle writes to the config.php file is at the installation time and Moodle writes to no other files in the program directory.

Re 2: As I understood, he used mdl_ prefix for databases, so he can easily identify which databases are from Moodle in his shared environment. There is no significance to the name of the database as long as it matches the config.php file.
In reply to Robert Brenstein

Re: Multiple Instances of Moodle on a Sysyem

by Richard Enison -
RB,
  1. Yes, I was referring to the creation of config.php at installation. But in addition to that, I would direct your attention to admin/xmldb/actions/create_xml_file.class.php. The invoke method clearly writes to a file under $CFG->dirroot. And that's just one example. I'm sure I have seen others, but I won't take the additional time to find them. One example is enough to prove that Moodle does write to the script folder tree.
  2. That may or may not be what GB meant. I think he can speak for himself. But thank you for your opinion.
RLE
In reply to Richard Enison

Re: Multiple Instances of Moodle on a Sysyem

by Robert Brenstein -
Well, yes, xmldb-editor writes files within a tree but this does not occur during normal execution. This falls into category of installation, which is fair game IMHO.
In reply to Robert Brenstein

Re: Multiple Instances of Moodle on a Sysyem

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Re 1: As far as I know, the only time Moodle writes to the config.php file is at the installation time and Moodle writes to no other files in the program directory.

That is certainly my understanding, I often use an existing config.php file and thus my moodle never even writes to that file.



Re 2: As I understood, he used mdl_ prefix for databases, so he can easily identify which databases are from Moodle in his shared environment. There is no significance to the name of the database as long as it matches the config.php file.

It's more significant than allowing me to identify the tables, it means that you can have the same table name for more than one instance of moodle in the same database.  Thus say for example moodle has a table that is usually called mdl_questions, to allow another instance of moodle in the same database you could prefix with a different string so you could have mdl2_questions. This only has any interest if you are on the type of hosting that does not allow multiple databases (or charges you for multiple databases). If you have total control over your databases then you can put your moodle tables in separate databases (a more satisfactory solution).

In reply to Marcus Green

Re: Multiple Instances of Moodle on a Sysyem

by Richard Enison -
MG,

Re 2: You are making my point, because you are talking about table prefixes. GB's code also uses mdl_ as a prefix for the database name itself, which is what I was questioning.

RLE
In reply to Richard Enison

Re: Multiple Instances of Moodle on a Sysyem

by Gordon Bateson -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Hi Richard,
sorry I misunderstood your question.

> What is the purpose of prepending the database names with mdl_?

I do this so that all my "mdl" databases appear together when I list the names of databases. On some servers, I have databases owned by applications other than Moodle, so I like to be able to tell at a glance which application uses which database.

Also, I notice I typed "Redirect module" in my earlier post, which caused some confusion. As you guessed, I meant to write "Rewrite module" blush

regards
Gordon
In reply to Ugarit Ebla

Re: Multiple Instances of Moodle on a Sysyem

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

This would not be an issue in windows because windows doesn't know how to do symlinking.

In fact it does, if you are running your application under the POSIX execution environment (Win32 is just one of the several execution environments that Windows NT and later versions have).

Anyway, you can make it work without symlinking and with any web server you want, without relying on .htaccess, mod_rewrite or any other external dependecies, but pure PHP.

You just need to use a new file. I've called it 'moodle-sites.php' in the config.php I use below, but you can call it anything you want as long as you modifiy config.php to reflect the name change. In order to provision new sites, you just add a new server settings block to the file and you are done (from the Moodle point of view; of course you'll need to create the new database, optionally the dataroot directory, etc.)

Make sure the moodle-sites.php file is inside $CFG->dirroot directory.

<?php $sites = array ( 'site1.moodle.org' => array ( 'dbtype' => 'dtype_for_site1', 'dbhost' => 'dbhost_for_site1', 'dbname' => 'dbname_for_site1', 'dbuser' => 'dbuser_for_site1', 'dbpass' => 'dbpass_for_site1', 'dbpersist' => false, 'prefix' => 'prefix_for_site1', 'wwwroot' => 'http://site1.moodle.org', 'dataroot' => '/path/to/dataroot/for/site1', ), 'site2.moodle.org:8080' => array ( 'dbtype' => 'dbtype_for_site2', 'dbhost' => 'dbhost_for_site2', 'dbname' => 'dbname_for_site2', 'dbuser' => 'dbuser_for_site2', 'dbpass' => 'dbpass_for_site2', 'dbpersist' => false, 'prefix' => 'prefix_for_site2', 'wwwroot' => 'http://site2.moodle.org:8080', 'dataroot' => '/path/to/dataroot/for/site2', ), // ............................... // Add new moodle sites here // ............................... // // // ............................... // This is the default site. // ............................... '*' => array ( 'dbtype' => 'dbtype_default_for_site', 'dbhost' => 'dbhost_default_for_site', 'dbname' => 'dbname_default_for_site', 'dbuser' => 'dbuser_default_for_site', 'dbpass' => 'dbpass_default_for_site', 'dbpersist' => false, 'prefix' => 'prefix_default_for_site', 'wwwroot' => 'http://defaultsite.moodle.org', 'dataroot' => '/path/to/dataroot/for/default_for_site', ) ); ?>

As you see, you can even use special port numbers for your moodle sites.

Then you need the following modified config.php file. Make sure you adjust the $CFG->dirroot setting to reflect the path of the only directory used for all the moodle sites (no need to copy, symlink, bind-mount or other tricks to use the same shared code).

<?php // Moodle Configuration File unset($CFG);

// This two settings are common for all the hosted moodle sites.
$CFG->dirroot   = '/path/to/your/shared/moodle/code';
$CFG->admin     = 'admin';

// Pull in site specific settings
require_once ($CFG->dirroot . '/moodle-sites.php');

// Get the server name the client is trying to connect to. The logic is
// copied from qualified_me() in lib/weblib.php

// The default server name.
$hostname = '*'; 

if (!empty($_SERVER['SERVER_NAME'])) {
    $hostname = $_SERVER['SERVER_NAME'];
} else if (!empty($_ENV['SERVER_NAME'])) {
    $hostname = $_ENV['SERVER_NAME'];
} else if (!empty($_SERVER['HTTP_HOST'])) {
    $hostname = $_SERVER['HTTP_HOST'];
} else if (!empty($_ENV['HTTP_HOST'])) {
    $hostname = $_ENV['HTTP_HOST'];
}

if (!empty($_SERVER['SERVER_PORT'])) {
    if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
        $hostname .= ':' . $_SERVER['SERVER_PORT'];
    }
}

if ($hostname != '*') {
    // If we have a non-default hostname, some of the web server variables
    // were set, so we are being called from the web. Set the server name 
    // to that value.
    $moodle_site = $hostname;
} else if (isset($_SERVER['argv'][1])){
    // No web server variables set, so check if we are begin called from the
    // command line. This must be a cron.php invocation (otherwise it doesn't
    // make sense at all). The server name should be specified as the
    // first (and only) parameter to the cron.php script.
    $moodle_site = $_SERVER['argv'][1];
} else {
    // Called from the command line, but we weren't passed the server name.
    // Use the default server settings.
    $moodle_site = '*';
}

// Now fetch its settings. If there are no server settings for that server
// name, fall back to the defaul server settings.
if (array_key_exists ($moodle_site, $sites)) {
    $site_params = $sites[$moodle_site];
} else {
    $site_params = $sites['*'];
}

// Now put the server settings in the place Moodle expects them :-)
$CFG->dbtype    = $site_params['dbtype'];
$CFG->dbhost    = $site_params['dbhost'];
$CFG->dbname    = $site_params['dbname'];
$CFG->dbuser    = $site_params['dbuser'];
$CFG->dbpass    = $site_params['dbpass'];
$CFG->dbpersist = $site_params['dbpersist'];
$CFG->prefix    = $site_params['prefix'];
$CFG->wwwroot   = $site_params['wwwroot'];
$CFG->dataroot  = $site_params['dataroot'];

$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.
?&gt;

NOTICE: If you are calling cron.php from the command line, you HAVE TO specify the server name of the moodle site you want to run cron.php for. Something like:

/usr/bin/php -f /path/to/your/shared/moodle/code/admin/cron.php site1.moodle.org /usr/bin/php -f /path/to/your/shared/moodle/code/admin/cron.php site2.moodle.org:8080 ...

would do it.

I've tested all of this with both Apache 2.2.4 and IIS 6.0 on Windows 2003, and Apache 1.3.4 on Debian Etch.

[Edit: I see that the markdown/code filters are munging the code a bit at the start of config.php, but I'm unable to correct it, so watch out! ]

Saludos. Iñaki.

Average of ratings: Useful (1)
In reply to Iñaki Arenaza

Re: Multiple Instances of Moodle on a Sysyem

by Ugarit Ebla -
Dear Iñaki Arenaza that was really helpful and I'm glad that I've learned that Windows has symlinks. Aren't symlinks in Windows only for directories? If that's the case then that won't do.

The issue I would have with your method is that now all sites are connected via that one file and they are less portable and presumably one php mistake in that file would disable all the sites.

In addition, if there is an upgrade one has to recall how this specific configuration is done, which is not the standard moodle one. These issues are not encouraging me to deploy moodle for anything more than a handful of schools. There should be no workarounds. This should really work right "out of the box" with standard symlinking and a more robust 'sites' capability (look below)

BTW, how does one designate different modules/themes for different sites?

Please look at drupal's sites capability which would be ideal for moodle.

http://drupal.org/node/53705



In reply to Ugarit Ebla

Re: Multiple Instances of Moodle on a Sysyem

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

BTW, how does one designate different modules/themes for different sites?

Modules and themes are handled at the database level. I mean, installed modules and themes would be available for all sites, but you can enable/disable modules per site (the list of enabled/disabled modules is kept in the database) and specify which theme you want to use per site (again, kept in the database).

Anyway, if you feel that Moodle doesn't fit your needs, and you don't want to do workarounds, feel free to choose the product that best fits your needs smile

On the other hand, if realpath in the only thing preventing the symlink setup, you can vote the bug you pointed to so developers can have a look at it and maybe 'fix' it. I don't really know what's the reasoning behind having that realpath() check in admin/index.php, but there could be a good reason for it. I just don't know.

Saludos. Iñaki.

In reply to Iñaki Arenaza

Re: Multiple Instances of Moodle on a Sysyem

by Ugarit Ebla -
I am not sure if realpath() is the only issue with symlinks but it's probably the major one. I believe that there is already a ticket open for realpath for index.php but it's in many other places in the code.
In reply to Iñaki Arenaza

Re: Multiple Instances of Moodle on a Sysyem

by Ugarit Ebla -
I am certainly free to choose another product smile I do love moodle, however and I want others to experience it. With the features I've mentioned it will even have a brighter future.
In reply to Iñaki Arenaza

Re: Multiple Instances of Moodle on a Sysyem

by Robert Brenstein -
Am I correct, Iñaki, that your setup assumes that each instance of Moodle has its own domain name? Have you tried to have common code in /moodle directory and two client config files in /moodle1 and /moodle2 doing a similar trick in using the common software directory?
In reply to Ugarit Ebla

Re: Multiple Instances of Moodle on a Sysyem

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

"I've opted to not using moodle precisely for this reason since I need to deploy an LMS for hundreds of schools"

What do you use instead of moodle?