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.
Requirements (Moodle 5.1 and later)
- PHP 8.2 or higher with extensions:
curl, mbstring, xml, intl, zip, gd, soap, sodium, mysqli(orpgsql),opcache. - Database: MariaDB 10.11+, MySQL 8.4+ or PostgreSQL 13+.
- Web server: Apache or Nginx. 2 vCPU / 4 GB RAM is a sensible start for a small school or team; disk is 200 MB for code plus your content (5 GB minimum realistic).
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
- Site administration → Notifications: fix every warning in the environment check.
- Turn on forced HTTPS and set a strong password policy.
- Add Redis for caching and sessions once you pass a few hundred active users.
- Schedule database and
moodledatabackups. Course backups alone are not a server backup.
Upgrading later
cd /var/www/moodle && sudo -u www-data git pull
sudo -u www-data php admin/cli/upgrade.php --non-interactive