There is a new version of this tutorial available for Ubuntu 22.04 (Jammy Jellyfish).

How to Install Moodle with Nginx and Let's Encrypt SSL on Ubuntu 20.04

Moodle is a free and open-source Learning Management System written in PHP. It provides a way for tutors and instructors to create courses for their students or learners. Moodle provides a robust and secure integrated system and comes with a custom dashboard that helps users to access current, past or future courses, as well as review pending work. It is used by many schools, universities, and organizations across the globe and provides a better learning experience. It provides a rich set of features including, wiki, grading, assignment submission, online quizzes, discussion boards, and more.

In this tutorial, we will show you how to install Moodle with Nginx web server and Let's Encrypt SSL on Ubuntu 20.04.

Prerequisites

  • A server running Ubuntu 20.04.
  • A valid domain name pointed with your server IP.
  • A root password is configured the server.

Getting Started

Before starting, you will need to update your system packages to the latest version. You can update them using the following command:

apt-get update -y

Once your server is updated, you can proceed to the next step.

Install LEMP

First, you will need to install the Apache, MariaDB, PHP and other PHP libraries to your system. You can install all of them using the following command:

apt-get install nginx mariadb-server php-fpm php-common php-mysql php-gmp php-curl php-intl php-mbstring php-soap php-xmlrpc php-gd php-xml php-cli php-zip unzip git curl -y

Once all the packages are installed, edit the php.ini file and change some settings:

nano /etc/php/7.4/fpm/php.ini

Change the following lines:

memory_limit = 256M
cgi.fix_pathinfo = 0
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

Save and close the file then restart the PHP-FPM service to apply the changes:

systemctl restart php7.4-fpm

Once you are finished, you can proceed to the next step.

Create a Database

Moodle uses a MySQL or MariaDB as a database backend so you will need to create a database and user for Moodle.

First, connect to the MySQL shell with the following command:

mysql

Once login, create a database and user with the following command:

CREATE DATABASE moodledb;
CREATE USER 'moodle'@'localhost' IDENTIFIED BY 'password';

Next, grant all the privileges to the Moodle database with the following command:

GRANT ALL ON moodledb.* TO 'moodle'@'localhost' WITH GRANT OPTION;

Next, flush the privileges and exit from the MySQL with the following command:

FLUSH PRIVILEGES;
EXIT;

Next, edit the MariaDB default configuration file and define the innodb_file_format:

nano /etc/mysql/mariadb.conf.d/50-server.cnf

Add the following lines inside the [mysqld] section:

[mysqld]
innodb_file_format = Barracuda
innodb_file_per_table = 1
innodb_large_prefix = ON

Save the file then restart the MariaDB service to apply the changes:

systemctl restart mariadb

Install Moodle

First, change the directory to the Apache root directory and download the latest version of the Moodle with the following command:

cd /var/www/html
git clone -b MOODLE_38_STABLE git://git.moodle.org/moodle.git moodle

Once the download is completed, edit the Moodle config.php and define the database type:

nano /var/www/html/moodle/config.php

Find the following line:

$CFG->dbtype    = 'mysqli';

And, replaced it with the following line:

$CFG->dbtype    = 'mariadb';

Save and close the file then create a Moodle data directory set proper ownership and permission with the following command:

mkdir -p /var/www/html/moodledata
chown -R www-data:www-data /var/www/html/moodle
chmod -R 755 /var/www/html/*
chown www-data:www-data /var/www/html/moodledata

Once you are finished, you can proceed to the next step.

Configure Nginx for Moodle

Next, you will need to create an Nginx virtual host configuration file to host Moodle:

nano /etc/nginx/conf.d/moodle.conf

Add the following lines:

server {
    listen 80;
    root /var/www/html/moodle;
    index  index.php index.html index.htm;
    server_name  moodle.example.com;

    client_max_body_size 100M;
    autoindex off;
    location / {
        try_files $uri $uri/ =404;
    }

    location /dataroot/ {
      internal;
      alias /var/www/html/moodledata/;
    }

    location ~ [^/].php(/|$) {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Save and close the file then verify the Nginx for any syntax error with the following command:

nginx -t

You should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally, restart the Nginx service to apply the changes:

systemctl restart nginx

At this point, Nginx is configured to host Moodle. You can now proceed to the next step.

Access Moodle Web Interface

Now, open your web browser and access the Moodle web interface using the URL http://moodle.example.com. You should see the Moodle installation page:

Choose language

Select your language and click on the Next. You should see the following page:

Web address and path

Provide your Moodle web address, directory path, data directory path and click on the Next. You should see the following page:

Database driver

Select your database driver type and click on the Next. You should see the following page:

MySQL details

Provide your database host, database name, username, password and click on the Next. You should see the following page:

Confirm installation

Click on the Continue to confirm all conditions. You should see the following page:

System check

Makesure all required PHP extensions are installed then click on the Continue. You should see the following page:

PHP extension check

Click on the Continue. You should see the following page:

Set username and password

Choose country settings

Provide your admin username, password, email, country, timezone, and click on the Update profile. You should see the following page:

Front page settings

Provide your front page settings and click on the Save changes button to save the changes.

Secure Moodle with Let's Encrypt SSL

Next, you will need to install the Certbot tool to download Let's Encrypt SSL and configure Nginx to use this SSL.

First, install the Certbot with the following command:

apt-get install python3-certbot-nginx -y

Once installed, run the following command to download all SSL and configure Nginx to use it:

certbot --nginx -d moodle.example.com

You will be asked to provide a valid email address and accept the term of service as shown below:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for moodle.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/moodle.conf

Next, choose whether or not to redirect HTTP traffic to HTTPS as shown below:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

Type 2 and hit Enter to continue. You should see the following output:

Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/moodle.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://moodle.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=moodle.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/moodle.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/moodle.example.com/privkey.pem
   Your cert will expire on 2021-05-23. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

 - We were unable to subscribe you the EFF mailing list because your
   e-mail address appears to be invalid. You can try again later by
   visiting https://act.eff.org.

You can now access the Moodle website using the URL http://moodle.example.com

Conclusion

Congratulations! you have successfully installed Moodle with Nginx and Let's Encrypt SSL on Ubuntu 20.04. You can now create your own Learning Management System with Moodle easily.

Share this page:

1 Comment(s)