How To Install Moodle with LEMP Stack on Ubuntu 22.04 LTS

Install Moodle with LEMP Stack on Ubuntu 22.04

In this tutorial, we will show you how to install Moodle with LEMP Stack on Ubuntu 22.04 LTS. For those of you who didn’t know, Moodle is a free and open-source Learning Management System and CMS written in PHP. With Moodle, you can create a fully-featured website for education and training courses, suitable for fully online, hybrid, and in-person classroom experiences. The Moodle platform is highly customizable and takes a modular approach to features, so it is extensible and adaptable to your needs. Moodle comes in handy, particularly for Long-distance institutions across the globe in providing training material for their learners.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of Moodle with LEMP Stack on Ubuntu 22.04 (Jammy Jellyfish). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo useror access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Moodle with LEMP Stack on Ubuntu 22.04 LTS Jammy Jellyfish

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade
sudo apt install software-properties-common dirmngr

Step 2. Installing the LEMP Stack.

Before starting this tutorial, the LEMP server must be installed on your server. If you do not have LEMP Stack installed, you can follow our guide here.

Step 3. Installing Moodle on Ubuntu 22.04.

By default, Moodle is not available on Ubuntu 22.04 base repository. Now run the following command below to download the latest stable version of Moodle to your Ubuntu system:

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

We will need to change some folders permissions:

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

Step 4. Configuring MariaDB for Moodle.

By default, MariaDB is not hardened. You can secure MariaDB using the mysql_secure_installation script. you should read and below each step carefully which will set a root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MariaDB:

mysql_secure_installation

Configure it like this:

- Set root password? [Y/n] y
- Remove anonymous users? [Y/n] y
- Disallow root login remotely? [Y/n] y
- Remove test database and access to it? [Y/n] y
- Reload privilege tables now? [Y/n] y

Next, we will need to log in to the MariaDB console and create a database for Moodle. Run the following command:

mysql -u root -p

This will prompt you for a password, so enter your MariaDB root password and hit Enter. Once you are logged in to your database server you need to create a database for Moodle installation:

MariaDB [(none)]> CREATE DATABASE moodle_db;
MariaDB [(none)]> CREATE USER 'moodle_user'@'localhost' IDENTIFIED BY 'your-strong-password';
MariaDB [(none)]> GRANT ALL ON moodle_db.* TO 'moodle_user'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

Step 6. Configuring Nginx Virtual Host for Moodle.

Now we create an Nginx virtual host configuration file:

nano /etc/nginx/conf.d/wordpress.conf

Add the following file:

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

    client_max_body_size 500M;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
	
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }	

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }	

    location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
}

Save and close the file then, restart the Nginx web server so that the changes take place:

nginx -t
sudo systemctl restart nginx
sudo systemctl restart php8.1-fpm

Step 6. Enable Secure HTTPS on Moodle.

First, we install Certbot on Ubuntu 22.04 using the following command:

sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

After complete install Certbot, now we set up Certbot for Nginx using the following command:

sudo apt install python3-certbot-nginx

Next, run the following command to start the creation of your certificate:

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email admin@your-domain.com -d www.your-domain.com

Output:

-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://domain.com and https://www.your-domain.com

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

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/your-domain.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/your-domain.comm/privkey.pem
Your cert will expire on 2022-07-20. 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"
- 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

Let’s Encrypt certificates have 90 days of validity, and it is highly advisable to renew the certificates before they expire. To test that this renewal process is working correctly, you can run:

sudo certbot renew --dry-run

Output:

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/your-domain.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Account registered.
Simulating renewal of an existing certificate for your-domain.com and www.your-domain.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded: 
  /etc/letsencrypt/live/domain.com/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Step 7. Configure Firewall.

Ubuntu 22.04 has ufw a firewall running by default. Enable connection through ports 80 HTTP and 443 HTTPS:

sudo ufw allow 'Nginx FULL'
sudo ufw enable
sudo ufw status

Step 8. Accessing Moodle Web Interface.

Once successfully installed, open your web browser and access the Moodle installation wizard using the URL https://your-domain.com. You will be redirected to the following page:

Install Moodle with LEMP Stack on Ubuntu 22.04 LTS

Install Moodle with LEMP Stack on Ubuntu 22.04

Congratulations! You have successfully installed Moodle. Thanks for using this tutorial for installing Moodle with LEMP on Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the official Moodle website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!