Home » Ubuntu » How to Install Moodle on Ubuntu 20.04

How to Install Moodle on Ubuntu 20.04

Moodle is a free and open-source learning management system (LMS). It is written in PHP programming language. Moodle is used by many schools, universities, and organizations for a better learning experience.

Moodle provides content management, user-friendly UI, reporting, quizzes, feedback, attendance lists, and more.

This guide explains how to set up your own Moodle server from scratch on Ubuntu 20.04 by using the LEMP stack.

Install LEMP Stack on Ubuntu

LEMP is a collection of open-source software comprising Linux, Nginx, MySQL/MariaDB, and PHP. LEMP is essentially an alternative to the LAMP stack with Nginx as the webserver instead of Apache.

For Linux, we are working with Ubuntu 20.04 in this guide. Ensure that you have yours ready to go with a user that has sudo capability.

Install Nginx

Nginx is required to serve the learning resources over the web. If you do not have an existing instance of NGINX web server, please follow the steps in this other guide to install NGINX on Ubuntu 20.04. Once you have successfully configured NGINX with your registered domain name, you may return to this guide and continue with the next steps below.

Install MySQL/MariaDB

A database is required to store moodle data. The most commonly used database for Moodle is typically MySQL/MariaDB or Postgres.

Moodle database table includes information about users, courses, site information and data for analytics.

We are going to install MariaDB which is an open-source database management system derived from MySQL.

Run the following command to install MariaDB on Ubuntu:

$ sudo apt-get install mariadb-server mariadb-client

The next command will execute a built-in script for configuring and securing your mariadb installation.

$ sudo mysql_secure_installation

It is important that you read the instructions carefully. Firstly, you would be prompted to enter the current password for the mysql root user. Since this is a new installation, the mysql root user has a blank password. So press the enter key to move on.

Enter current password for root (enter for none):
OK, successfully used password, moving on…

The second step is to set the root password for mariadb. Enter y and then type your desired password.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
… Success!

Next, remove anonymous users. Enter y to confirm.

Remove anonymous users? [Y/n] y
… Success!

The next step is regarding disabling root login remotely. Enter y to confirm.

Disallow root login remotely? [Y/n] y
… Success!

Enter y to confirm the removal of the test database.

Remove test database and access to it? [Y/n] y
Dropping test database…
… Success!
Removing privileges on test database…
… Success! 

The final step is to reload the privilege tables so as to make your changes take effect. Enter y to confirm.

Reload privilege tables now? [Y/n] y
… Success!
Cleaning up…
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!

You may now login to mariadb and create a database to store moodle data. Run the command below and enter the mariadb root password when prompted.

$ sudo mysql -u root -p

Once logged in, run the following command to create a database.

> CREATE DATABASE moodle;

Next, create a user account for Moodle in mariadb as follows. Remember to replace moodlesuper and password with your own values.

> CREATE USER 'moodlesuper'@'localhost' IDENTIFIED BY 'password';

The next command grants the necessary permissions to the newly created user account.

> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO 'moodlesuper'@'localhost';

Log out of mariadb as follows.

> quit

Install PHP

PHP is required to dynamically process user requests for Moodle contents. NGINX works with PHP by using PHP-FPM -- i.e. FastCGI Process Manager.

Install required PHP components by running the command below.

$ sudo apt-get install php-fpm php-mysql php-xml

Once installed, the next step is to configure NGINX to forward PHP requests to PHP-FPM. To do that, you would need to edit the NGINX server block file for your Moodle website. For example, the command below will open the server block file for my 'cloudindevs.com' Moodle website.

$ sudo nano /etc/nginx/sites-available/cloudindevs

In the server block file, add index.php to the line beginning with index. Also, copy the following configuration directive and paste it right below the existing location directive in your server block file.

# pass PHP scripts to FastCGI server
location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info  ^(.+\.php)(/.+)$;
    fastcgi_index            index.php;
    fastcgi_pass             unix:/var/run/php/php7.4-fpm.sock;
    include                  fastcgi_params;
    fastcgi_param   PATH_INFO       $fastcgi_path_info;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

After the changes, that portion of your server block file should look similar to what you see in the image below. The new additions are marked in red.

Configure NGINX to work with PHP-FPM
Configure NGINX to work with PHP-FPM

To test your configuration, run the command below to create a sample index.php file in the root directory of your moodle website. Remember to replace 'cloudindevs.com' with your own website address.

$ sudo nano /var/www/cloudindevs.com/index.php

Copy and paste the sample PHP code below into the index.php file.

<?php echo 'NGINX is working with PHP-FPM!'; ?>

Save and close the index.php file. Visit yourwebsite.com/index.php in a web browser and you should see the sample page below.

Test PHP-FPM configuration
Test PHP-FPM configuration

To have SSL (https) follow Nginx with Let's Encrypt.

Install Moodle on Ubuntu

At the time of writing this guide, the latest Moodle version is 3.10.1 and can be downloaded as follows.

$ sudo wget https://download.moodle.org/download.php/direct/stable310/moodle-3.10.1.tgz

Next, extract the content of the downloaded package and place it in the root directory of your Moodle website. Here is an example.

$ sudo tar -zxvf moodle-3.10.1.tgz -C /var/www/cloudindevs.com

It is necessary to change the ownership and permissions on the extracted Moodle folder as follows. The default NGINX service account is www-data.

$ sudo chown -R www-data:www-data /var/www/cloudindevs.com/moodle
$ sudo chmod -R  755 /var/www/cloudindevs.com/moodle

Run the next three commands to create a directory with appropriate permissions for Moodle to store uploaded files and folders. The Moodle data directory should not be accessible over the web.

$ sudo mkdir /var/moodledata
$ sudo chown -R www-data:www-data /var/moodledata
$ sudo chmod -R  755 /var/moodledata

Moodle Configuration

Now, it is time to create the main Moodle configuration file. To do this, change directory and then copy the sample configuration file in the extracted Moodle folder by running the commands below.

$ cd /var/www/cloudindevs.com/moodle
$ sudo cp config-dist.php config.php

Open the config.php file for editing.

$ sudo nano config.php

In the DATABASE SETUP section of the config.php file, do the following.

  1. Replace pgsql with mariadb
  2. Replace moodle with the name of the database you created while setting up mariadb
  3. Change username to the actual database username which you created earlier
  4. Change password to your database password

Press the down arrow key until you get to the WEB SITE LOCATION section of the config.php file.

Replace http://example.com/moodle with http://yoursite.com/moodle

For instance, mine would be 'https://cloudindevs.com/moodle'

Scroll down further to the DATA FILES LOCATION section of the config.php file.

Change /home/example/moodledata to your moodle data directory which you created earlier. Mine would be /var/moodledata

Access moodle web interface

At this point, you may open a web browser and visit yoursite.com/moodle to complete the installation.

Here is what I get when I go to 'www.cloudindevs.com/moodle' in a web browser.

Moodle web installation page
Moodle web installation page

Click Continue to proceed. On the next page, you might encounter errors regarding missing plugins. See an example in the image below.

Moodle server checks
Moodle server checks

To resolve the issues, click must be installed and enabled and follow the instructions to install the required extensions. After installing the requisite extensions, restart NGINX with sudo systemctl restart nginx and then reload the Server Checks page to confirm if the errors are gone.

Moodle server checks passed
Moodle server checks passed

If the errors are gone, you may click Continue to proceed.

Configure Moodle Main Administrator Account

After successfully installing Moodle, you would be presented with a page to configure your main administrator account. You would need to specify a username, a password and other required information.

Also, you would be asked to configure your site name, front page summary, location settings, etc. When you are done, click Save changes at the bottom of the page.

Save Moodle settings
Save Moodle settings

And that concludes the installation of Moodle. Here's what my new Moodle website looks like.

Moodle successfully installed
Moodle successfully installed

Conclusion

Online learning has gained a lot of attention since the Covid-19 pandemic started -- and that's a fact. A lot of educational institutions are creating e-learning platforms through online learning management systems like Moodle.

In this guide, we have successfully installed and configured a Moodle server from scratch on Ubuntu 20.04. For additional information on how to use and manage your Moodle website, please consult the official Moodle Docs.

Leave a Comment