How to Install Moodle on Ubuntu 20.04

Install Moodle on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure Moodle on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Moodle is a well-known open-source learning management system written in PHP. It offers a wide range of activities and educational tools that enable schools, universities, and related institutions to offer students a flexible learning platform any time, anywhere, without interruption to their course and curriculum.

Install Moodle on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install LEMP Stack.

WordPress is built with PHP. You can host it like you would any other web app is written in PHP. So, you’re going to need to set Ubuntu up as either a LAMP server. If you haven’t done so already, use our traditional LEMP guide to set up Ubuntu to serve PHP before you continue.

Step 3. Install Moodle on Ubuntu systems.

Download and install the latest version Moodle (version 3.9 at the time of writing). Run the following wget the command to grab the Moodle package:

wget -c
sudo tar -zvxf moodle-latest-39.tgz -C /var/www/html/
ls /var/www/html/

Finally, adjust file permissions as shown:

sudo chown www-data:www-data -R /var/www/html/moodle
sudo chmod 775 -R /var/www/html/moodle

Next, create the Moodle data directory:

sudo mkdir -p /var/moodledata
sudo chmod 775 -R /var/moodledata
sudo chown www-data:www-data -R /var/moodledata

Step 4. Create a Database for Moodle

Moodle uses MariaDB database to store all its data. Log in to your MariaDB server with the following command and enter your MariaDB root password:

mysql -u root -p

Once you’re in the MariaDB console, create a new database:

MariaDB [(none)]> CREATE DATABASE moodle;
MariaDB [(none)]> quit;

Then create the Moodle main configuration file from the sample configuration file provided with the package:

cd /var/www/html/moodle/
sudo cp config-dist.php config.php
sudo nano config.php

Next, configure the database where all Moodle data will be stored:

$CFG->dbtype    = 'mariadb';      // 'pgsql', 'mariadb', 'mysqli', 'sqlsrv' or 'oci'
$CFG->dblibrary = 'native';     // 'native' only at the moment
$CFG->dbhost    = 'localhost';  // eg 'localhost' or '' or IP
$CFG->dbname    = 'moodle';     // database name, eg moodle
$CFG->dbuser    = 'moodleadmin';   // your database username
$CFG->dbpass    = 'your-passwd';   // your database password
$CFG->prefix    = 'mdl_';       // prefix to use for all table names

Configure Moodle Database Settings

Also, configure the Moodle website location as well as the location of the Moodle data directory:

$CFG->wwwroot   = '';
$CFG->dataroot  = '/var/moodledata';

Step 5. Configuring Nginx.

Now we create a new server block configuration under the NGINX configuration, in the directory /etc/nginx/conf.d/:

nano /etc/nginx/conf.d/moodle.conf
   listen 80;
    root        /var/www/html/moodle;
    index       index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;

    location ~ ^(.+\.php)(.*)$ {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_index           index.php;
        fastcgi_pass           unix:/run/php/php7.4-fpm.sock;
        include                 /etc/nginx/mime.types;
        include                 fastcgi_params;
        fastcgi_param           PATH_INFO       $fastcgi_path_info;
        fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;

For the new configuration to take effect, restart the Nginx service by typing:

sudo nginx -t
sudo systemctl reload nginx

Step 6. Configure Firewall.

Open Apache on UFW to allow external access:

ufw allow 80/tcp

Step 7. Accessing Moodle.

In the last step of this guide, we need to access the Moodle Web Interface and finish the installation. To finish the installation, open your browser and navigate to:

That’s all you need to do to installing Moodle on Ubuntu 20.04 LTS Focal Fossa. I hope you find this quick tip helpful. For further reading on Moodle, please refer to their official knowledge base. If you have questions or suggestions, feel free to leave a comment below.