Moodle

Internal & External Moodle Access

Recently i made a post on the creation of a new in-house moodle site. Today we moved over ISP’s and now been given access to be able to put my moodle site live on the web.

I set up external DNS to point to new ip but all i got was the moodle trying to force an internal address then failing.

From past experience i knew the fix would be to do with the config file, so started their.

 

Below was the original config file (minor edits to protect password, internal ip’s/host names…)

 

Old Config.txt

php // Moodle configuration file

unset($CFG);
global $CFG;
$CFG = new stdClass();

$CFG->dbtype = ‘sqlsrv’;
$CFG->dblibrary = ‘native’;
$CFG->dbhost = ‘localhost’;
$CFG->dbname = ‘moodle_DB_name’;
$CFG->dbuser = ‘A_Moodle_user’;
$CFG->dbpass = ‘moodleDBpassword’;
$CFG->prefix = ‘mdl_’;
$CFG->dboptions = array (
‘dbpersist’ => 0,
‘dbport’ => ”,
‘dbsocket’ => ”,
);

$CFG->wwwroot = ‘http://moodle’;
$CFG->dataroot = ‘D:\\inetpub\\moodledata’;
$CFG->admin = ‘admin’;

$CFG->directorypermissions = 0777;

require_once(dirname(__FILE__) . ‘/lib/setup.php’);

 

 

As you can see the users only had to open IE… and navigate to http://moodle. then single sign on did its thing then opened the front page.

 

First thing i did was to open my hosting cPanel and edit the DNS so that http://moodle.school.com redirected to the external IP of the server.

This did work but in the config file $CFG->wwwroot = ‘http://moodle’; the site then tried to redirect my browser to http://moodle. Internal users would be ok but external users would then bomb out at this point.

 

What i needed was to have an if statment so that if internal user accessed http://moodle it would open, but also if an external user opened http://moodle.school.com it would still load.

below is the code i used to achieve needed results.

 

Open the config from the wwwroot folder and paste the code below:

New Config.txt

<?php // Moodle configuration file

unset($CFG);
global $CFG;
$CFG = new stdClass();

$CFG->dbtype = ‘sqlsrv’;
$CFG->dblibrary = ‘native’;
$CFG->dbhost = ‘localhost’;
$CFG->dbname = ‘moodle_DB_name’;
$CFG->dbuser = ‘A_Moodle_user’;
$CFG->dbpass = ‘moodleDBpassword’;
$CFG->prefix = ‘mdl_’;
$CFG->dboptions = array (
‘dbpersist’ => 0,
‘dbport’ => ”,

‘dbsocket’ => ”,
);

$moodle = ‘10.66’;
$client_ip = $_SERVER[‘REMOTE_ADDR’];
if (strpos($client_ip,$moodle)===0) {
$CFG->wwwroot = ‘http://moodle’;
} else {
$CFG->wwwroot = ‘http://moodle.school.com’;
}

$CFG->dataroot = ‘D:\\inetpub\\moodledata’;
$CFG->admin = ‘admin’;

$CFG->directorypermissions = 0777;

require_once(dirname(__FILE__) . ‘/lib/setup.php’);

 

The code added:

$moodle = ‘10.66’;
$client_ip = $_SERVER[‘REMOTE_ADDR’];
if (strpos($client_ip,$moodle)===0) {
$CFG->wwwroot = ‘http://moodle’;
} else {
$CFG->wwwroot = ‘http://moodle.school.com’;
}

As you can see, if user opened http://moodle and has an ip address starting with 10.66.*.* it will load as normal and perform a SSO. But now if an external users accessed http://moodle.school.com it wont try and redirect them to http://moodle as they arn’t an internal user. It will then also prompt for a username/password.

 

To edit this for your setup, simply change the ip address to match yours. Nothing stopping you from being more more specific by using more a range ‘10.66.22’; or even just ’10’;

Then simply change the first $CFG->wwwroot =  to match your internal DNS setup and the 2nd for your external cPanel… DNS configuration.

 

 

Internal DNS

If you don’t know how to make a DNS record for this:

Open DNS and navigate down to the “Forward Lookup Zones”

Right click and click “New Alias (CNAME)

and for Alias name put in what you want you internal users to use. For me i put Moodle

Then, for the “Fully qualified domain name (FQDN) for target host” put in the full path to the moodle server.  (eg, shinto.localdomain.sch.uk)

internalDNS
Internal DNS

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *