This tutorial exists for these OS versions
- Ubuntu 24.04 (Noble Numbat)
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 16.04 (Xenial Xerus)
On this page
- Prerequisites
- Installing dependencies
- Configuring MariaDB server
- Creating database and user
- Configuring PHP
- Downloading Moodle source code
- Setting up Apache virtual host
- Securing Moodle with UFW
- Securing Moodle with HTTPS
- Installing Moodle
- Conclusion
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
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
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.
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
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.
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
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 theMoodle 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.
<VirtualHost *:80>
DocumentRoot /var/www/moodle/
ServerName moodle.howtoforge.local
ServerAdmin [emailprotected]<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 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 [emailprotected] -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.
Input your data directory for Moodle '/var/www/moodledata'.
Select the MariaDB as the database driver.
Enter details of your MariaDB database and user.
Click Continue to confirm the copyright notice.
On the server checks section, make sure your environment is ready.
Now the Moodle installation will be processed.
After the installation is complete, enter the new admin user, email, and password for Moodle.
Now you'll see the Moodle dashboard like the following:
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.