OSI Global › Guides › Moodle

How to Install Moodle 5.x LMS on Ubuntu (Apache, PHP, MariaDB)

Set up a self-hosted Moodle learning management system the way Moodle 5.1+ expects it, with the new public/ web root, a protected moodledata folder and a working cron.

Level: IntermediateTime: ~45 minutesMoodle 5.1+Updated Nov 2026

Requirements (Moodle 5.1 and later)

What changed in 5.1: all web-accessible files moved into a public/ sub-directory. Your web server's document root must point to /var/www/moodle/public, not to /var/www/moodle. config.php stays one level above, out of reach of the web.

Step 1 — Install the stack

sudo apt update
sudo apt -y install apache2 mariadb-server git unzip \
  php php-cli php-mysql php-curl php-mbstring php-xml php-intl php-zip php-gd php-soap php-opcache libapache2-mod-php
php -v      # must report 8.2 or newer

In /etc/php/8.x/apache2/php.ini and /etc/php/8.x/cli/php.ini set max_input_vars = 5000 and memory_limit = 256M, then restart Apache.

Step 2 — Create the database

sudo mysql
CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'moodleuser'@'localhost' IDENTIFIED BY 'choose-a-strong-password';
GRANT ALL PRIVILEGES ON moodle.* TO 'moodleuser'@'localhost';
FLUSH PRIVILEGES; EXIT;

Step 3 — Get the Moodle code with Git

Using Git makes later upgrades a one-line git pull. Pick the newest MOODLE_5xx_STABLE branch listed on download.moodle.org (5.1 shown here):

cd /var/www
sudo git clone -b MOODLE_501_STABLE git://git.moodle.org/moodle.git
sudo mkdir /var/moodledata
sudo chown -R www-data:www-data /var/moodledata
sudo chmod 0770 /var/moodledata

moodledata holds uploads and caches. Keep it outside any web-accessible folder.

Step 4 — Point Apache at public/

<VirtualHost *:80>
  ServerName lms.example.org
  DocumentRoot /var/www/moodle/public
  <Directory /var/www/moodle/public>
    AllowOverride None
    Require all granted
    FallbackResource /r.php
  </Directory>
</VirtualHost>
sudo a2ensite moodle.conf && sudo a2dissite 000-default.conf && sudo systemctl reload apache2

Add HTTPS straight away with sudo apt install certbot python3-certbot-apache && sudo certbot --apache.

Step 5 — Run the installer

Browse to your site and follow the web installer, or run the CLI installer (it writes config.php for you):

cd /var/www/moodle
sudo -u www-data php admin/cli/install.php \
  --wwwroot=https://lms.example.org --dataroot=/var/moodledata \
  --dbtype=mariadb --dbname=moodle --dbuser=moodleuser --dbpass='choose-a-strong-password' \
  --fullname="My Academy" --shortname="Academy" --adminuser=admin --adminpass='another-strong-password' \
  --agree-license --non-interactive

Step 6 — Set up cron (required)

sudo crontab -u www-data -e
* * * * * /usr/bin/php /var/www/moodle/admin/cli/cron.php >/dev/null

Without cron, emails, forum digests, backups and completion tracking silently stop.

After install: quick hardening

Upgrading later

cd /var/www/moodle && sudo -u www-data git pull
sudo -u www-data php admin/cli/upgrade.php --non-interactive
Written by Dr. Vivek Khare — Author, Speaker, IT Leader with 25+ years of experience, ISO 27001 Lead Auditor and founder of OSI Global. Spotted something out of date? Tell us via the community form.