HowtoForge

How to Install Moodle LMS on Debian 12 Server

Moodle is an open solution for the Learning Management System (LMS). It is a platform for educational purposes, from creating online courses, managing online schools, managing content, and offering collaborative learning. Moodle is mainly written in PHP with MySQL/MariaDB as the database and can be run on any operating system. You choose between Apache or Nginx for your Moodle installation.

In this guide, you'll learn how to install the Moodle Learning Management System on a Debian 12 server. You'll run Moodle with the LAMP Stack (Linux, Apache, MariaDB, and PHP), secure your Debian server with UFW (Uncomplicated Firewall), and then secure Moodle with HTTP through Certbot and Letsencrypt.

Prerequisites

Make sure you have the following before you begin:

Installing Dependencies

The Moodle LMS is written in PHP and MySQL, so you must install those packages on your system. In this example, you'll be running Moodle with the LAMP Stack (Linux, Apache, MariaDB, and PHP). So for now, you'll be installing LAMP on your Debian server.

Before installing any package, run the command below to update your Debian package index.

sudo apt update

Now install the LAMP Stack (Linux, Apache, MariaDB, and PHP) dependencies with the following command. Enter 'Y' to proceed with the installation.

sudo apt install apache2 mariadb-server php-cli php-intl php-xmlrpc php-soap php-mysql php-zip php-gd php-tidy php-mbstring php-curl php-xml php-pear php-bcmath libapache2-mod-php

After the installation is complete, check the 'apache2' service to ensure that the service is running.

sudo systemctl is-enabled apache2
sudo systemctl status apache2

You can see below that the Apache web server is running and enabled.

Now check the 'mariadb' service by executing the following command.

sudo systemctl is-enabled mariadb
sudo systemctl status mariadb

In the output below, you can see that the MariaDB server is running.

Lastly, check the PHP version and list enabled modules using the command below.

php -v
php -m

You'll see that PHP 8.2 are installed with some default modules such as 'curl', 'exif', and 'date' enabled.

Installing UFW (Uncomplicated Firewall)

After you've installed LAMP Stack, let's install UFW (Uncomplicated Firewall) to secure the server. And then, open ports for SSH, HTTP, and HTTPS protocols. Lastly, let's start and enable UFW.

To install UFW, execute the 'apt' command below.

sudo apt install ufw

Once UFW is installed, run the 'ufw' command below to open ports for OpenSSH, HTTP (80), and HTTPS (443). You'll see an output such as 'Rules updated'.

sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Now run the command below to start and enable UFW on your Debian machine. When prompted, enter 'y' to confirm and proceed. You'll see an output 'Firewall is active and enabled on system startup'.

sudo ufw enable

Lastly, check the UFW status with the command below. You'll see that UFW is 'active' with OpenSSH, and ports '80' and '443' are 'ALLOWED'.

sudo ufw status

Configuring MariaDB Server

Before installing Moodle, you need to change the default storage engine to 'innodb' and set up 'Barracuda' as the default format. After that, you also need to secure the MariaDB server and set up the 'root' password using the 'mariadb-secure-installation' utility.

Open the default MariaDB server configuration '/etc/mysql/mariadb.conf.d/50-server.cnf' with the 'nano' editor.

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

Insert the configuration below to enable the 'Barracuda' format and set up the default storage engine to 'InnoDB'.

innodb_file_format = Barracuda 
default_storage_engine = innodb
innodb_large_prefix = 1
innodb_file_per_table = 1

Save the file and exit the editor.

Now run the 'systemctl' command below to restart the MariaDB server and apply your changes.

sudo systemctl restart mariadb

After that, execute the 'mariadb-secure-installation' command below to secure your MariaDB server installation.

sudo mariadb-secure-installation

With this, you'll be asked about MariaDB server configurations:

Once finished, go to the next step to create a new database and user.

Creating a Database and User for Moodle

With the MariaDB server configured, let's now create a new database and user that will be used by Moodle. You'll be using the 'mariadb' client to log in to MariaDB and create a new database and user.

Log in to the MariaDB server with the 'mariadb' command below. Enter your MariaDB root password when prompted.

sudo mariadb -u root -p

Run the following queries to create a new database and user 'moodle' with the password 'MoodlePassw0rd'. Make sure to change the following database credentials with your information.

CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL ON moodle.* TO 'moodle'@'localhost' IDENTIFIED BY "MoodlePassw0rd";
FLUSH PRIVILEGES;

Now run the query below to check user 'moodle'. Make sure that the user 'moodle' can access the database 'moodle'.

SHOW GRANTS FOR moodle@localhost;

Lastly, type 'QUIT' to exit from the MariaDB server.

Configuring PHP

Now that you've configured the MariaDB server and created a new database user, let's configure the 'php.ini' file and change some default configurations as needed by Moodle.

Open the 'php.ini' file with the 'nano' editor.

sudo nano /etc/php/8.2/apache2/php.ini

Change the default PHP configuration with the following. You can change the 'memory_limit' accordingly, depending on your server memory.

memory_limit = 256M
upload_max_filesize = 60M
max_execution_time = 300
date.timezone = Europe/Amsterdam
max_input_vars = 5000

When finished, save the file and exit the editor.

Lastly, run the 'systemctl' command below to restart the 'apache2' web server and apply your new PHP configuration.

sudo systemctl restart apache2

Downloading Moodle Source Code

After configuring PHP, you're ready to download Moodle source code, then set up the installation directory and 'data' directory for Moodle. In this example, you'll set up Moodle installation directory to '/var/www/moodle' and the 'data' directory to the '/var/www/moodledata'.

Go to the '/var/www' directory and download the Moodle source code with the 'wget' command below.

cd /var/www
sudo wget https://download.moodle.org/download.php/direct/stable405/moodle-latest-405.tgz

Once downloaded, run the 'tar' command below to extract the Moodle source code and rename the extracted directory to 'moodle'. With this, your Moodle installation will be located in the '/var/www/moodle' directory.

tar -xf moodle-latest-405.tgz

Next, create a new directory '/var/www/moodledata' that will be used as the data directory for your Moodle installation.

mkdir -p /var/www/moodledata

Lastly, run the command below to change the ownership of the '/var/www/moodle' and '/var/www/moodledata' directories to the user 'www-data'. And then, make sure that the 'www-data' user can read, write, and execute files within those directories.

sudo chown -R www-data:www-data /var/www/moodle /var/www/moodledata
sudo chmod u+rwx /var/www/moodle /var/www/moodledata

Setting up Apache Virtual Host

Now that you've downloaded Moodle, the next step is to create a new Apache virtual host file for Moodle. So make sure that you have a domain name resolved/pointed to the server IP address.

Create a new virtual host configuration '/etc/apache2/sites-available/moodle.conf' with the 'nano' editor.

sudo nano /etc/apache2/sites-available/moodle.conf

Insert the configuration below to set up a virtual host file for Moodle. Make sure to change the 'ServerName' parameter with your domain name.

<VirtualHost *:80>
DocumentRoot /var/www/moodle/
ServerName moodle.howtoforge.local
ServerAdmin admin@example.com

<Directory /var/www/moodle/>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>

ErrorLog /var/log/apache2/moodle_error.log
CustomLog /var/log/apache2/moodle_access.log combined
</VirtualHost>

Save the file and exit the editor when done.

Now run the 'a2enmod' command below to activate the 'rewrite' module, and then run the 'a2ensite' command below to activate the 'moodle.conf' virtual host.

sudo a2enmod rewrite
sudo a2ensite moodle.conf

Next, run the 'apachectl' command below to verify your Apache configuration. If you have proper Apache configuration, you'll see an output such as 'Syntax is OK'.

sudo apachectl configtest

Lastly, run the 'systemctl' command below to restart the Apache web server and apply your changes.

sudo systemctl restart apache2

Securing Moodle with HTTPS

At this point, your Moodle installation is ready. Before you continue, you'll secure Moodle with HTTPS through Certbot and Letsencrypt. Do this when you're installing Moodle on a public server. If you're on the local development, you can skip or implement HTTPS through Self-Signed certificates.

Install the 'certbot' and 'python3-certbot-apache' plugin with the following 'apt' command. Input 'Y' to confirm the installation.

sudo apt install certbot python3-certbot-apache

After the installation is complete, execute the following 'certbot' command to generate SSL certificates for Moodle. Make sure to change the following domain name and email address with your domain.

sudo certbot --apache --agree-tos --no-eff-email -m admin@howtoforge.local -d moodle.howtoforge.local

When finished, your SSL certificates will be available at the '/etc/letsencrypt/live/domain.com' directory and your Moodle installation will be running under HTTPS.

Moodle Installation

Open your web browser and visit the domain name of your Moodle installation such as https://moodle.howtoforge.local/, and you'll see the Moodle installation wizard.

Select your default language for Moodle and click 'Next'.

Enter your domain name and Moodle data directory to '/var/www/moodledata'.

Select 'MariaDB' as the default database for Moodle.

Input your MariaDB database details such as dbname, dbuser, and password.

Click 'Continue' on the copyright notice.

Within the 'Server checks' section, make sure that your Debian server is met with the Moodle requirements. The status of every item should be 'OK'.

Now you'll see the Moodle installation like the following:

Once the installation is finished, enter your admin details (username, email, and password).

Now enter your Moodle site information.

Lastly, you'll see the following Moodle admin dashboard.

Conclusion

Congratulations! You've completed the installation of Moodle Learning Management System (LMS) on the Debian 12 server. You've Moodle up and running with the LAMP Stack (Linux, Apache, MariaDB, and PHP) and secured with UFW (Uncomplicated Firewall) and HTTPS through Certbot and Letsencrypt. From here, you can apply a new theme for Moodle, install new addons to extend the functionality, or you can learn the Moodle User Guide for managing your Moodle installation.

How to Install Moodle LMS on Debian 12 Server