Applications Azure

Moodle 4.2 on Rocky Linux 8 on Azure User Guide

| Product: Moodle 4.2 on Rocky Linux 8 on Azure

Overview

This guide covers the deployment and configuration of Moodle 4.2 on Rocky Linux 8 on Microsoft Azure using cloudimg's virtual machine image (offer prod-gc7depkulb7xe).

Moodle is the open source learning management system used by schools, universities and training teams to run courses, quizzes, assignments and grade books. This image runs Moodle 4.2.11 on Rocky Linux 8.10 with Apache, PHP 8.1 and MariaDB 10.11, all on one virtual machine. The first boot of every virtual machine generates its own administrator, database and MariaDB root passwords and installs a fresh Moodle site, so no two machines share a credential.

⚠️ Moodle 4.2 is past its upstream support — read before deploying

Moodle 4.2 no longer receives security fixes. Moodle's release table (https://moodledev.io/general/releases) lists the end of security support for 4.2 as 7 October 2024, and 4.2.11 (Build 20241007) is the release on this image. It suits sites that must stay on Moodle 4.2 for a while, for example while their plugins or themes are certified for a newer release. Plan the upgrade to Moodle 4.5, the long term support release, which runs on this image's PHP 8.1 and MariaDB 10.11: Step 11 gives the commands, tested on this image.

Rocky Linux 8 itself keeps receiving security updates until 31 May 2029. The image does not install them automatically: Step 9 shows how.

What's included:

  • Moodle 4.2.11 (Build 20241007) in /var/www/moodle, its data directory in /var/moodledata
  • Apache HTTP Server 2.4 with PHP FPM 8.1 (Remi's remi-8.1 module stream) and OPcache
  • MariaDB 10.11, listening on 127.0.0.1 only
  • Moodle's scheduled tasks run every minute from cron
  • A first boot service that makes the per VM passwords and the site, and writes them to /root/moodle-credentials.txt (readable by root only)
  • Two 10 GB Azure data disks: one holds the MariaDB data (/var/lib/mysql)
  • SELinux enforcing, SSH by key only
  • Azure Linux Agent, cloud-init and chronyd
  • Generation 2 virtual machine

Platform: Microsoft Azure (Generation 2) Default user: azureuser

Prerequisites

Before deploying this image, ensure you have:

  1. An active Microsoft Azure subscription
  2. The Azure CLI, installed locally or in Azure Cloud Shell
  3. An SSH key pair for Linux VM access
  4. The address range you will manage the machine from, to limit SSH to it

Step 1: Deploy the Virtual Machine

Deploy the image by its Marketplace URN. The image carries a purchase plan, so accept its terms once per subscription first. Replace <your-mgmt-cidr> with the network you connect from, for example 203.0.113.0/24.

az vm image terms accept --urn cloudimg1647283583153:prod-gc7depkulb7xe:prod-gc7depkulb7xe-gen2:latest

az group create --name myResourceGroup --location eastus
az vm create \
  --resource-group myResourceGroup \
  --name my-moodle-vm \
  --image cloudimg1647283583153:prod-gc7depkulb7xe:prod-gc7depkulb7xe-gen2:latest \
  --admin-username azureuser \
  --ssh-key-values ~/.ssh/id_ed25519.pub \
  --public-ip-sku Standard \
  --nsg-rule NONE
az network nsg rule create --resource-group myResourceGroup --nsg-name my-moodle-vmNSG \
  --name AllowSSHFromMgmt --priority 1000 --access Allow --protocol Tcp \
  --direction Inbound --source-address-prefixes <your-mgmt-cidr> --destination-port-ranges 22
az vm open-port --resource-group myResourceGroup --name my-moodle-vm --port 80 --priority 1010

Add --size with the size your site needs: two vCPUs and 4 GiB of memory (for example Standard_B2s) suit a small site. The image's two data disks are created with the machine.

Read the machine's public IP address once it is running:

az vm show --resource-group myResourceGroup --name my-moodle-vm \
  --show-details --query "[powerState, publicIps]" -o tsv
VM running
203.0.113.25

Step 2: Connect via SSH

ssh -i ~/.ssh/id_ed25519 azureuser@<public-ip>

Step 3: Check First Boot and Read the Login

First boot installs the Moodle site, which takes about two minutes after the machine shows Running; Apache starts once it has finished. Check that the first boot service and the stack are up:

systemctl is-active cloudimg-firstboot-moodle mariadb php-fpm httpd
active
active
active
active

The administrator user name and the login address are in the credentials file:

sudo grep -E '^(Moodle admin user|Login):' /root/moodle-credentials.txt
Moodle admin user: admin
Login: http://<private-ip>/login/index.php

sudo cat /root/moodle-credentials.txt shows the whole file: the Moodle administrator password, the MariaDB root password and the Moodle database user's password, all made on this machine at first boot. Keep them in a password manager and never commit them to source control.

Step 4: Set the Site Address

Moodle answers only on its configured site address ($CFG->wwwroot) and redirects every other address to it. First boot sets it to the address the machine had at first boot. On Azure that is the machine's private address (the <private-ip> above, in your virtual network's range), because the instance metadata service does not report a Standard public IP address, so a browser on the internet is sent to an address it cannot reach.

Set the site address to the public IP address, or to a DNS name that points at it, then clear Moodle's caches:

sudo sed -i "s|^\$CFG->wwwroot = .*|\$CFG->wwwroot = 'http://<public-ip>';|" /var/www/moodle/config.php
sudo -u apache php /var/www/moodle/admin/cli/purge_caches.php
sudo grep wwwroot /var/www/moodle/config.php
$CFG->wwwroot = 'http://203.0.113.25';

Do the same whenever the address changes, for example when you move the site to your own domain or to HTTPS (Step 12). A Standard public IP address is static, so it does not change when the machine restarts.

Step 5: Sign In to Moodle

Open http://<public-ip>/login/index.php in your browser.

The Moodle 4.2 sign in page

Sign in as admin with the Moodle administrator password from /root/moodle-credentials.txt. To change it, open the user menu in the top right, choose Preferences, then Change password. The credentials file is not updated when you do, so keep the new password in your password manager.

Step 6: The Dashboard

The dashboard is where each user starts: the Timeline of upcoming activities and the Calendar. The top bar links to My courses and, for the administrator, Site administration.

The Moodle dashboard

Step 7: Create Your First Course

Open Site administration > Courses > Manage courses and categories. A new site has one category, Category 1. Select Create new course, give the course a full name and a short name, and save. Then add teachers and students under the course's Participants.

Manage course categories and courses

Step 8: Site Administration

Site administration holds every site wide setting: users and authentication, courses, grades, plugins, appearance, the server and reports. The banner Your site is not yet registered asks you to register the site with moodle.org; that is optional.

Site administration

To send email (password resets, notifications and forum posts), open Site administration > Server > Email > Outgoing mail configuration and enter an SMTP relay. Azure blocks outbound port 25, so use a relay on port 587, such as Azure Communication Services, SendGrid or Microsoft 365.

Step 9: Install Updates

The image does not install updates automatically. Rocky Linux, PHP and MariaDB updates come through dnf:

sudo dnf -y upgrade
sudo needs-restarting -r

needs-restarting -r says whether a restart is needed for the new kernel or core libraries; if it is, run sudo systemctl reboot. The site comes back by itself after a restart, and first boot does not run again.

PHP comes from Remi's remi-8.1 module stream and MariaDB from the Rocky Linux mariadb:10.11 module stream, so dnf upgrade keeps each on its series.

Step 10: Back Up

Back up the database and the data directory together:

sudo mkdir -p /root/backup
sudo mysqldump --single-transaction --databases moodle | sudo tee /root/backup/moodle-db.sql > /dev/null
sudo tar -czf /root/backup/moodledata.tgz -C /var moodledata

Copy both files off the machine, for example to Azure Blob Storage. Azure Backup can also take scheduled snapshots of the whole virtual machine, its data disks included.

Step 11: Upgrade to Moodle 4.5

Moodle 4.5 is the long term support release, and the newest Moodle that runs on PHP 8.1 (Moodle 5.0 and later need PHP 8.2). Take a backup first (Step 10). These commands put the site into maintenance mode, keep the 4.2 code as /var/www/moodle-4.2, unpack the latest 4.5 release in its place with your config.php, run the upgrade and leave maintenance mode:

cd /tmp
curl -fsSLO https://download.moodle.org/download.php/direct/stable405/moodle-latest-405.tgz
sudo -u apache php /var/www/moodle/admin/cli/maintenance.php --enable
sudo mv /var/www/moodle /var/www/moodle-4.2
sudo tar -xzf moodle-latest-405.tgz -C /var/www
sudo cp -p /var/www/moodle-4.2/config.php /var/www/moodle/
sudo restorecon -R /var/www/moodle
sudo -u apache php /var/www/moodle/admin/cli/upgrade.php --non-interactive
sudo -u apache php /var/www/moodle/admin/cli/maintenance.php --disable
sudo -u apache php /var/www/moodle/admin/cli/cfg.php --name=release

On this image the upgrade ends with:

Command line upgrade from 4.2.11 (Build: 20241007) (2023042411) to 4.5.16+ (Build: 20261124) (2024100716.02) completed successfully.
...
Maintenance mode has been disabled and the site is running normally again
4.5.16+ (Build: 20261124)

The exact 4.5 release depends on the day you run it. Check your site and plugins, then remove the old code with sudo rm -rf /var/www/moodle-4.2. Moodle's release notes are at https://moodledev.io/general/releases.

Step 12: Serve the Site over HTTPS

The image serves plain HTTP on port 80. For HTTPS, point a DNS name at the public IP address, open port 443 and install a Let's Encrypt certificate with certbot from EPEL (enabled on this image):

sudo dnf -y install certbot python3-certbot-apache mod_ssl
sudo certbot --apache -d <your-domain>

Then set the site address to https://<your-domain> as in Step 4, and open port 443 with az vm open-port --resource-group myResourceGroup --name my-moodle-vm --port 443 --priority 1020.

Server Components

Component Version Notes
Rocky Linux 8.10 (Green Obsidian) Security updates until 31 May 2029
Moodle 4.2.11 (Build: 20241007) Official release, /var/www/moodle; past upstream security support
PHP 8.1 (PHP FPM) Remi remi-8.1 module stream; gd, intl, mysqli, soap, sodium, zip and OPcache
MariaDB 10.11 mariadb:10.11 module stream; bind-address=127.0.0.1
Apache HTTP Server 2.4.37 Document root /var/www/moodle; /install.php denied
Moodle cron every minute /etc/cron.d/moodle, as apache
Azure Linux Agent, cloud-init, chronyd Azure provisioning and time sync

Filesystem Layout

Path What it holds
/var/www/moodle Moodle code and config.php (OS disk)
/var/moodledata Moodle's data directory: uploaded files, caches, sessions (OS disk)
/var/lib/mysql MariaDB data, on a 10 GB Azure data disk
/var/www/html Apache's default document root, on the second 10 GB data disk; Moodle does not use it
/root/moodle-credentials.txt The per VM passwords and the login address (root only)
/etc/httpd/conf.d/moodle.conf Apache configuration for Moodle
/mnt Azure temporary disk: its contents are lost when the machine is deallocated

The data disks are mounted from /etc/fstab by UUID, with nofail, and checked at boot by the cloudimg-datadisk service.

Managing Moodle

sudo systemctl status httpd php-fpm mariadb
sudo systemctl restart httpd php-fpm
sudo -u apache php /var/www/moodle/admin/cli/purge_caches.php
sudo -u apache php /var/www/moodle/admin/cli/maintenance.php --enable
sudo -u apache php /var/www/moodle/admin/cli/maintenance.php --disable

Run Moodle's command line scripts as apache, the user Apache and PHP FPM run as.

Scripts and Log Files

Path Purpose
/usr/local/sbin/cloudimg-firstboot-moodle The first boot script (service cloudimg-firstboot-moodle); runs once, then leaves /var/lib/cloudimg/moodle-initialised
journalctl -u cloudimg-firstboot-moodle The first boot log, ending Installation completed successfully.
/var/log/httpd/access_log, /var/log/httpd/error_log Apache
/var/log/php-fpm/error.log PHP FPM
/var/log/mariadb/mariadb.log MariaDB

Troubleshooting

The browser is redirected to a 10.x.x.x address. The site address is still the private address from first boot. Set it as in Step 4.

The page does not load at all. Check that the network security group allows port 80 from your network, and that Apache is running: systemctl is-active httpd. In the first two minutes after the first boot Apache waits for the site to be installed.

The credentials file is missing. First boot has not finished. Follow it with sudo journalctl -u cloudimg-firstboot-moodle -f.

Database connection errors. Check MariaDB with systemctl status mariadb and that its data disk is mounted: findmnt /var/lib/mysql.

Moodle shows an error page. Turn on debugging under Site administration > Development > Debugging, or read /var/log/httpd/error_log and /var/log/php-fpm/error.log.

Security Recommendations

  • Upgrade Moodle. Moodle 4.2 gets no more security fixes; move to Moodle 4.5 (Step 11).
  • Serve the site over HTTPS (Step 12): the image serves plain HTTP, so passwords cross the network unencrypted until you do.
  • Limit SSH to your management network, as in Step 1. SSH accepts keys only, and the root password is locked.
  • Install updates regularly (Step 9).
  • Change the administrator password after the first sign in (Step 5).
  • SELinux stays in enforcing mode, and MariaDB listens on 127.0.0.1 only. The machine has no host firewall: the network security group is the firewall, so open only the ports you use.

Support

cloudimg's support team is on hand 24 hours a day by email (support@cloudimg.co.uk), with a guaranteed response within 24 hours.

For Moodle itself, see the documentation at https://docs.moodle.org/ and the community forums at https://moodle.org/.