Using AWS, Bitnami and Moodle to host a video platform

Paul Hands
10 min readJul 8, 2021

--

Overview

At our company we have created a lot of training videos for customers with regards to the products we sell. After a lot of discussion amongst the team it was decided that the best way to host these videos was to bootstrap our own version of Moodle, an online training platform that allows video hosting and a lot of customisation. This was to be hosted inside Amazon Web Service (AWS) architecture. Having never dealt with Moodle before, some steps were quite complicated in the initial setup, so we chose to use an Amazon Machine Image, hosted by Bitnami, to do some of the initial setup and configuration of the Moodle instance. The exact machine image that we used can be found here.

The process was interesting and complex, which always makes for quite a good tech piece! So the following is a step by step how to guide in setting up the website. In this piece a bit of prior knowledge of AWS (at least the broad strokes of “what each section does”) would be helpful (and is assumed!)

Once finished, the uploaded videos to the website will be hosted securely in an S3 bucket and the information regarding login details etc. are hosted in an RDS database instance.

CloudFormation script — and what it does

https://jsfiddle.net/paul_hands/yadug9vL/

Above is a JSFiddle containing the CloudFormation script I used to set Moodle up on an AWS instance. This would need saving locally as a JSON file. The script does a bunch of things:

  • Creates S3 buckets for storing videos, images and documents that the Moodle website will utilise.
  • Creates a mariaDB database instance in Amazon RDS (current engine is 10.2.32 as per the minimum requirements of Moodle v3.11) with a database called awsmoodle that we use to store relevant information in the DB.
  • Creates the relevant VPC, Security group, subnets, route tables, internet gateways etc. to control access and security to the website (and importantly the S3 buckets and RDS instance)
  • Creates the relevant IAM users to do everything that users will need to do automatically in AWS.
  • Goes to Bitnami and accesses the AMI ID, to create the externally pointing EC2 instance (as per Bitnami’s configuration).

It should be noted this is all using the availability zone of Eu-west-2 (London) and if it needs to be hosted elsewhere, then the AMI will change and the cloud formation script should be updated. The different AMI’s are all located here.

This CloudFormation script requires some basic inputs from the runner, as to set up what is needed for further configuration and changes.

Before we implement the script

Before implementing the script navigate in AWS to EC2 → Key Pairs. Generate a new key pair and save it securely, as you will need it to access the EC2 instance we generate to host Moodle in. DO NOT LOSE THE DOWNLOADED keypair.pem file, or you won’t be able to get access, and you might need to start again (you definitely will need to start again for the EC2 instance steps).

How to implement the script

Navigate in AWS to CloudFormation → Stacks → Create Stack with new resources (standard)

Select Template is ready and upload a template file and attach the template file you saved from the JSFiddle.

In the next section, add details as required. It is recommended to leave the default values. Be sure to set the DBUsername as something like ‘root’ or ‘admin’, and make a note of the DBPassword as we will be using it when we get into the EC2 instance. Use the same KeypairName that you used in the pre setup steps.

If you wish to add a tag please do so. Otherwise leave the defaults as they are and move to the bottom of the section. Ensure you check the acknowledgement box to allow the script to generate the IAM roles necessary, then click “Create Stack”. Once you’ve done that the CloudFormation script should start, and will generate all the necessary resources for our system.

Navigate into EC2 instance and update relevant pieces of information

Before starting

Because our RDS instance and EC2 instance live in the same security group, we can make changes to the database via the EC2 command line but not from any instance (we need to SSH into the EC2 instance to make these command line changes). The first thing we should do is note down the EC2 instance and the RDS instance to make sure we know where to access.

  • Navigate to EC2 → Instances and find the instance that is (in our example) MyMoodleStack-moodle-webserver. Replace “MyMoodleStack” with the precursor you provided in the CloudFormation script.
  • make a note of the “Public IPv4 DNS” that is given in the summary. It should look something like “ec2–12–34–56–78.eu-west-2.compute.amazonaws.com”.
  • Navigate to RDS → Databases and locate the instance that is identified by “MoodleDbInstance”. Make a note of it’s Endpoint, it should look something like “moodledbinstance.abcd1234.eu-west-2.rds.amazonaws.com”.

Access EC2 instance for user credentials and update databases to be moodle “friendly”

Inside a command line terminal navigate to the directory that you have the moodle-keypair.pem file stored locally. In EC2 there should be an option to connect to the instance we created for Moodle.

Once you click that select the SSH command and copy it into the terminal. It will look something like:

ssh -i "moodle-keypair.pem" bitnami@ec2-18-133-214-138.eu-west-2.compute.amazonaws.com

Notice the bitnami before the @, this is the access we need. And it uses the moodle-keypair.pem to ssh in.

Once in you should see something like

Run the following command

sudo cat /home/bitnami/bitnami_credentials

which will give you the default username and password to get access to the new website (note these down, as you will need them later)

Migrate Databases from local to our RDS instance

Go back to the terminal that is SSH’ed into the EC2 instance. Because this is in the same VPC as the database it allows us to use the command line to access the DB, which we need to do. Enter the following command to start using mysql:

mysql -u root -p -h <RDS_HOSTNAME>

Here I’ve used ‘root’ but we should be using the same dbusername we specified in the CloudFormation template, and replace <RDS_HOSTNAME> with the endpoint from the RDS instance. When prompted enter your password.

One we are here we need to create a new user in the database and give them administrative privileges. This is 3 lines that should be done inside the logged in mysql terminal. (For ease I used the same password for the user as I did for the RDS database because once set up we don’t use the moodleuser again)

create user moodleuser@'%' identified by '<PASSWORD>';

grant all privileges on awsmoodle.* TO moodleuser@'%';

flush privileges;

Since Moodle has already configured the required tables on the local database installed on the application server, we need to migrate them into RDS. Go to your web browser and navigate to http://0.0.0.0/admin/tool/dbtransfer/index.php where 0.0.0.0 is the noted down IPv4 address of our web server from the EC2 instance. You will need to log in as the user with the credentials you gathered earlier.

Fill the form in with the required values. The two that will be different to here are the database host (which will be your own RDS_HOSTNAME from earlier), and your database password, which you entered in the three lines of code above. Once this is done we should be able to navigate into the sql terminal window and run the lines:

use awsmoodle;

show tables;

You should then see a list of tables with a prefix of “mdl_”. You can exit the mysql terminal using exit .

Update the configuration of moodle to point at our RDS instance

There are a few files inside the EC2 instance we have SSH’ed into that we need to tweak. The first is the config.php file, which tells the application essentially which database to look at and how it’s configured. We could quite easily just work at the top level but it is worth understanding the file structure, as we will be tweaking a few. First, navigate to

cd /opt/bitnami/moodle

This is the actual Moodle application that lives inside the EC2 instance, set up by Bitnami (hence why it is the parent. Bitnami also hosts the mariaDB database and the php programme to run it). The first file we are going to tweak is the config.php file.

sudo vim config.php

we use sudo here to allow us to tweak the files, otherwise we get administrative issues. Update your config.php file to match the below

replacing DB_HOST and PASSWORD with your PASSWORD and DB_HOST respectively, here I have used a dbuser of ‘admin’ specified in the CloudFormation script.

Update php.ini to remove potential warning errors and issues with file size

(Note this is an optional step — we needed to do this as our video file sizes were larger than the default would allow us to upload)

Navigate into and open the php.ini file

cd /opt/bitnami/php/etc

sudo vim php.ini

This opens up the initialisation file for the php runner, and has a bunch of settings inside it, that we can and should tweak. The file is big, so rather than copy all of the file in here I’m just going to list the lines that need to be changed:

post_max_size — this is the maximum size in mb of the files you will accept being sent to the site

upload_max_filesize — this is the maximum file size that can be uploaded onto the website and into S3

max_execution_time — this is the maximum time an upload is allowed to attempt to execute. It is recommended to increase this if you want to upload large files

One of the lines that might be commented out (you can tell because it starts with a ; ) is

max_input_vars — the newer versions of Moodle complain if this is not set to be high, and is usually commented out in the php.ini file by default. Set this to be 5000 and uncomment it.

(Note that if you are using a DNS provider to navigate into the EC2 instance rather than the IPv4 address then you should check the upload limits that they also have in place!)

Update database collations to all match

In our config.php we set the db collation to be utf8mb4_general_ci which is a unicode collation allowing for a lot of different characters, including symbols, so email addresses can be added as users. We need to check that all the database tables are matching to do this.

Navigate to the command line directory of the Moodle administration and run the following lines

cd /opt/bitnami/moodle/admin/cli

sudo php mysql_collation.php -l

This will list all of the collations for the tables in the database. It should look something like

If the summary at the bottom has anything other than utf8mb4_general_ci then you should run

sudo php mysql_collation.php --collation='utf8mb4_general_ci'

This will set all the collations to be the desired collation and ignore any that are already set that way.

The final important step to take is to restart all the services to consider the new config and php runners. To do this run

sudo /opt/bitnami/ctlscript.sh restart

Update storage on application to point at S3

Now our application is running and working, the final step to take is to allow for the user to upload the videos to be stored inside the S3 buckets we created in the CloudFormation script. Fortunately Moodle comes with a pre-installed plugin Amazon S3 Moodle which we need to enable.

Getting the IAM access key and secret

In the AWS console navigate to IAM

Once in there we should see an option to view the users. Select this option

Find the user that has -moodle-repouser appended to the end of the User name and select it. Select the Security credentials tab.

Click the button for Create access key and note down the Access key ID and the secret, as we will need this to get into the S3 bucket via the website.

Updating Moodle

In the logged in Moodle site, navigate to:

Site administration > Plugins > Repositories > Manage repositories

Identify the repository Amazon S3 and select the option Enabled and visible. You are redirected to the configuration page.

Copy, and paste the Access key and Secret key generated previously and select Save.

You should now have a working, basic Moodle site, which can now be configured and customised with the correct databases and S3 buckets connected!

--

--

Paul Hands
Paul Hands

Written by Paul Hands

Software developer. Agile enthusiast. Toddler parent. Dad joke lover

Responses (1)