Making a new moodle instance
Once you have installed Moodle and have it working, you might need to make new instances for new organisations. That is, you might not want to share your first instance (copy) with other organisations, but you might get asked to nonetheless do so. The easiest way around this is to make a copy of a working copy of Moodle.
I find Moodle is a bit easy to destabilise/break with plugins or changes, hence, it is also best just to take a backup copy anyway.
By default, Moodle consists of three main parts. The app, the database, and the varying/variable data or caches.
Caches and variable data are usually in /var/www/moodledata
The app itself is usually in /var/www/html/moodle
The database is usually in mysql or similar.
Once you have it installed and working, I'd suggest immediately making a backup copy to use for creating instances later on. I'd suggest doing it straight away because it is quite quick to climb up to 1 GB or larger which takes ages to process.
To make the backup, once it is working and you know the admin password, etc., do this:
mkdir /var/www/moodle_template
cp -dpRufv /var/www/moodledata /var/www/template/moodledata_template
cp -dpRufv /var/www/html/moodle /var/www/template/moodle_app_template
mysqldump -u root -p"mysqlpassword" moodle > /var/www/html/moodle /var/www/template/template.sql
Thereafter, every time you want to make a new instance, just run a script. I suggest the following script. You probably will want to edit it.
First, edit the moodle config file in /var/www/moodle_template/moodleapp/ ... replace the domain name (yours) with a bunch of Xes like shown in the script below.
After that, this script should just work.
#!/bin/sh
if [ -z "$1" ] ; then
echo "Please give a name for the new instance of moodle"
echo "please limit it to about 10-12 letters max"
echo "please do not use spaces or other non-letter characters in it"
exit;
fi
newinstance=$1
echo "Creating instance of moodle called: "$newinstance
cd /var/www/
echo "Copying template app to moodle app folder: "$newinstance
time cp -R template/moodle_app_template/ html/$newinstance
chown -R www-data:www-data html/$newinstance
chmod -R 777 html/$newinstance
echo "Copy completed"
echo
echo "Copying template data to moodle data folder: "$newinstance
time cp -R template/moodledata_template/ ./moodledata_$newinstance
chown -R www-data:www-data ./moodledata_$newinstance
chmod -R 777 ./moodledata_$newinstance
echo "Copy completed"
echo
echo "Creating moodle database for: "$newinstance
echo "CREATE DATABASE IF NOT EXISTS $newinstance CHARACTER SET utf8 COLLATE utf8_unicode_ci;" > $newinstance.sql
mysql < $newinstance.sql
echo "Step completed"
echo
echo "now granting privileges"
echo "grant all on $newinstance.* to 'moodle'@'localhost';" > $newinstance.sql
echo "flush privileges;" >> $newinstance.sql
mysql < $newinstance.sql
echo "Step completed"
echo
echo "Now loading the template database for moodle app; This step might take 7 minutes"
time mysql $newinstance < template/template.sql
echo "Now erasing unneeded users"
echo "use $newinstance" > $newinstance.sql
echo "delete from $newinstance.mdl_user " >> $newinstance.sql
echo 'where (username not like "%pule%" and username not like "%admin%" and username not like "%sechaba%" and username n
ot like "%aadminn2%");' >> $newinstance.sql
echo 'exit;' >> $newinstance.sql
mysql < $newinstance.sql
echo "Step completed"
echo
rm $newinstance.sql # delete the temp SQL file
echo "Configuring the moodle to use the database called:"$newinstance
cp /var/www/template/moodle_app_template/config.php /var/www/html/$newinstance/config.php
cat /var/www/html/$newinstance/config.php | sed -e s/xxxxxx/$newinstance/g > /var/www/html/$newinstance/config2.php
mv /var/www/html/$newinstance/config.php /var/www/html/$newinstance/config_orig.php
mv /var/www/html/$newinstance/config2.php /var/www/html/$newinstance/config.php
echo "All tasks completed, please go to http://"`hostname`"/"$newinstance/" to test"