Moodle is a popular open-source platform used by educational institutions worldwide for creating online courses, managing content, and facilitating collaborative learning. Running Moodle on Ubuntu is a common choice due to Ubuntu’s stability, security, and support for a wide range of software packages. The process involves installing necessary components like Apache or Nginx as the web server, MySQL or PostgreSQL as the database, and PHP as the scripting language, all of which are well-supported on Ubuntu. This setup allows institutions to create a robust, scalable, and customizable online learning environment that can be tailored to specific educational needs.

In this tutorial, we’ll show you how to install Moodle on Ubuntu 24.04 server. You’ll be installing Moodle with the LAMP Stack, and then secure Moodle with UFW and HTTPS through Certbot and Letsencrypt.

Prerequisites

Before you start, make sure you have the following:

  • An Ubuntu 24.04 server.
  • A non-root user with administrator privileges.
  • A domain name pointed to a server IP address.
  • An UFW firewall is up and running.

Installing dependencies

Moodle is an open-source learning platform written in PHP. To install Moodle, you need Apache/Nginx, MySQL/MariaDB/PostgreSQL, and PHP. In this section, you’ll install the LAMP Stack (Linux, Apache, MariaDB, and PHP) on Ubuntu and set up Moodle on top of it.

To start, run the following command and update your Ubuntu package index.

sudo apt update

<img alt="update repo" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/1.png66d7065458595.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="200" loading="lazy" src="data:image/svg xml,” width=”750″>

Now execute the command below to install the LAMP Stack (Apache, MariaDB, and PHP) dependencies. Enter ‘Y’ to confirm 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

<img alt="install lamp stack" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/2.png66d7065498de1.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="196" loading="lazy" src="data:image/svg xml,” width=”750″>

After the installation is complete, check the Apache service status with the command below.

sudo systemctl is-enabled apache2

sudo systemctl status apache2

In the following, you can see the Apache web server is running.

<img alt="check apache2" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/3.png66d70654b08cb.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="210" loading="lazy" src="data:image/svg xml,” width=”750″>

Check the MariaDB database server with the following command. You’ll see the MariaDB server is running.

sudo systemctl is-enabled mariadb

sudo systemctl status mariadb

<img alt="check mariadb" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/4.png66d70654cf161.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="240" loading="lazy" src="data:image/svg xml,” width=”750″>

Lastly, check the PHP version and enabled extensions with the following:

php -v

php -m

You’ll see PHP 8.3 is installed on your Ubuntu server.

<img alt="check php" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/5.png66d70654ea623.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="447" loading="lazy" src="data:image/svg xml,” width=”750″>

Configuring MariaDB server

After dependencies are installed, you need to change the default MariaDB storage engine to ‘InnoDB‘, which is required by Moodle. This can be done by editing the MariaDB server configuration. And then, you’ll also secure MariaDB server deployment using the ‘mariadb_secure_installation’ utility.

Open the MariaDB server config file ‘/etc/mysql/mariadb.conf.d/50-server.cnf‘ with the ‘nano’ editor.

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

Add the following configuration under the ‘[mysqld]‘ section. This will change 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

Lastly, run the ‘mariadb_secure_installation‘ command below to set up the MariaDB root user and secure your deployment.

sudo mariadb_secure_installation

Within the process, you will be asked with the following:

  • For the default MariaDB server installation without a root password, press ENTER when asked about the password.
  • The local authentication for MariaDB root users is secured by default, input ‘n’ when asked to change the authentication method to ‘unix_socket’.
  • Input ‘Y’ to create a new MariaDB root password. Then, input the strong password for your MariaDB root user and repeat.
  • When asked to disable remote authentication for the MariaDB root user, input ‘Y’ to agree.
  • The default MariaDB server installation comes with the database ‘test’ and allows an anonymous user to access it. Input ‘Y’ for both settings to remove the default database ‘test’ and remove the anonymous privilege.
  • Lastly, input ‘Y’ to confirm reloading table privileges.

Creating database and user

Now that you’ve configured the MariaDB server, let’s create a new database and user via the ‘mariadb’ client.

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

sudo mariadb -u root -p

Now run the following queries to create a new database ‘moodle‘, a user ‘moodle‘, and make sure to change the password 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;

QUIT

<img alt="create database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/6.png66d70655462ee.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="358" loading="lazy" src="data:image/svg xml,” width=”750″>

Configuring PHP

In this section, you’ll edit the PHP configuration ‘php.ini’ and change some default values that are required by Moodle.

Open the PHP configuration ‘/etc/php/8.3/apache2/php.ini‘ with the ‘nano‘ editor.

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

Change the default configuration with the following. Make sure to adjust the ‘memory_limit‘ and ‘date_timezone‘ options with your information.

memory_limit = 256M

upload_max_filesize = 60M

max_execution_time = 300

date.timezone = Europe/Amsterdam

max_input_vars = 5000

When done, save the file and exit.

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

sudo systemctl restart apache2

Downloading Moodle source code

At this point, you’ve installed and configured the LAMP Stack. Let’s download the Moodle source code and configure the Moodle installation directory.

Go to the ‘/var/www‘ directory and download Moodle source code using the ‘wget‘ command. Make sure to visit the Moodle download page to get the link for the latest version. In this case, you’ll download the latest stable Moodle 40.4.

cd /var/www

wget https://download.moodle.org/download.php/direct/stable404/moodle-latest-404.tgz

After Moodle is downloaded, extract it with the ‘tar‘ command below. The Moodle source code will be available in the ‘/var/www/moodle’ directory.

tar xvf moodle-latest-404.tgz

Lastly, execute the command below to create a new data directory ‘/var/www/moodledata‘, change the ownership of Moodle directory to ‘www-data‘ user, and make sure both Moodle and data directory are writable by user ‘www-data‘.

sudo mkdir -p /var/www/moodledata

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

With the Moodle downloaded, you’ll be creating a new Apache virtual host file to run Moodle. So make sure that you’ve your domain ready and resolved to your Ubuntu server IP address.

First, run the ‘a2enmod‘ command below to activate the ‘rewrite‘ module.

sudo a2enmod rewrite

Create a new Apache virtual host file ‘/etc/apache2/sites-available/moodle.conf‘ with the following ‘nano‘ editor.

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

Insert the following configuration and make sure to change the domain name with your information.



DocumentRoot /var/www/moodle/

ServerName moodle.howtoforge.local

ServerAdmin [email protected]



Options FollowSymlinks

AllowOverride All

Require all granted

ErrorLog /var/log/apache2/moodle_error.log

CustomLog /var/log/apache2/moodle_access.log combined

Save the file and exit the editor when done.

Now execute the following command to enable the ‘moodle.conf‘ file and verify your Apache syntax. If you have the correct Apache syntax, you’ll see an output ‘Syntax is OK‘.

sudo a2ensite moodle.conf

sudo apachectl configtest

Lastly, run the ‘systemctl‘ command below to restart Apache and apply your changes.

sudo systemctl restart apache2

Securing Moodle with UFW

In this step, you’ll open HTTP and HTTPS protocols through UFW (Uncomplicated Firewall). Make sure that you’ve UFW running before you start.

Run the command below to enable the ‘Apache Full‘ profile on UFW. With this, the HTTP and HTTPS traffic will be allowed.

sudo ufw allow 'Apache Full'

Now check the list of enabled rules on UFW with the following. You’ll see the ‘Apache Full‘ profile is enabled.

sudo ufw status

Securing Moodle with HTTPS

In addition to the firewall, you’ll also generate SSL/TLS certificates and secure Moodle with HTTPS. In this section, you’ll implement HTTPS for Moodle through Certbot and Letsencrypt. If you’re installing Moodle locally, skip this.

Install ‘certbot‘ and ‘python3-certbot-apache‘ packages with the following command.

sudo apt install certbot python3-certbot-apache -y

After the installation is complete, execute the ‘certbot‘ command below to generate SSL/TLS certificates for Moodle. Make sure to change the domain name and email address with your information.

sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d moodle.howtoforge.local

When the process is finished, your SSL certificates will be available at the ‘/etc/letsencrypt/live/domain.com‘ directory, and your Moodle installation should be secured automatically with HTTPS.

Installing Moodle

Visit your Moodle domain name such as https://moodle.howtoforge.local/ and you’ll see the installation wizard.

Select your default language and click Next.

<img alt="language" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/7.png66d706556ea35.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="301" loading="lazy" src="data:image/svg xml,” width=”750″>

Input your data directory for Moodle ‘/var/www/moodledata‘.

<img alt="moodle data" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/8.png66d70655bcf16.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="529" loading="lazy" src="data:image/svg xml,” width=”750″>

Select the MariaDB as the database driver.

<img alt="db driver" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/9.png66d70655d0cdd.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="292" loading="lazy" src="data:image/svg xml,” width=”750″>

Enter details of your MariaDB database and user.

<img alt="database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/10.png66d70655e718f.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="536" loading="lazy" src="data:image/svg xml,” width=”750″>

Click Continue to confirm the copyright notice.

<img alt="copyright notices" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/11.png66d7065611f22.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="369" loading="lazy" src="data:image/svg xml,” width=”750″>

On the server checks section, make sure your environment is ready.

<img alt="system checks" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/12.png66d7065633bd9.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="495" loading="lazy" src="data:image/svg xml,” width=”750″>

Now the Moodle installation will be processed.

<img alt="installation" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/14.png66d7065652418.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="510" loading="lazy" src="data:image/svg xml,” width=”750″>

After the installation is complete, enter the new admin user, email, and password for Moodle.

<img alt="setup admin" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/15.png66d706566fb74.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="506" loading="lazy" src="data:image/svg xml,” width=”750″>

Now you’ll see the Moodle dashboard like the following:

<img alt="moodle dashboard" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/09/echo/17.png66d70656856e7.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="356" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

Congratulations! You’ve completed the installation of Moodle on the Ubuntu 24.04 server. You’ve installed Moodle 40.4 on Ubuntu with the LAMP Stack (Linux, Apache, MariaDB, and PHP), and secured Moodle with UFW (Uncomplicated Firewall) and HTTPS via Certbot and Letsencrypt.