This guide walks through a self-hosted installation of Moodle with Linux DevOps best practices.
Update packages and install prerequisites:
# On Debian/Ubuntu
sudo apt update && sudo apt upgrade -y
sudo apt install -y apache2 php8.3 php8.3-{cli,curl,mbstring,xml,zip,intl,gd,soap,xmlrpc,bcmath,ldap,sqlite3,mysql,pgsql} libapache2-mod-php8.3 mysql-server
# On RHEL/Rocky Linux
sudo dnf update -y
sudo dnf install -y httpd php83-php php83-php-{cli,curl,mbstring,xml,zip,intl,gd,soap,xmlrpc,bcmath,ldap,sqlite3,mysqlnd,pdo} mysql-server
Create a database and user for Moodle:
CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'moodleuser'@'localhost' IDENTIFIED BY 'your_secure_password';
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO 'moodleuser'@'localhost';
FLUSH PRIVILEGES;
# Create web directory
sudo mkdir -p /var/www/moodle
sudo chown www-data:www-data /var/www/moodle
# Download Moodle (adjust version as needed)
cd /tmp
wget https://download.moodle.org/download.php/direct/stable405/moodle-4.5.1.tgz
tar -xzf moodle-*.tgz -C /var/www/
sudo chown -R www-data:www-data /var/www/moodle
# Create data directory
sudo mkdir -p /var/moodledata
sudo chown -R www-data:www-data /var/moodledata
sudo chmod -R 755 /var/moodledata
Create Apache virtual host:
sudo nano /etc/apache2/sites-available/moodle.conf
Add the following configuration:
<VirtualHost *:80>
ServerName moodle.yourdomain.com
DocumentRoot /var/www/moodle
<Directory /var/www/moodle/>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Security headers
Header always set X-Content-Type-Options nosniff
Header always set X-Frame-Options DENY
Header always set X-XSS-Protection "1; mode=block"
# PHP settings for Moodle
php_value max_input_vars 5000
php_value memory_limit 512M
php_value post_max_size 64M
php_value upload_max_filesize 64M
php_value max_execution_time 180
php_value max_input_time 180
ErrorLog ${APACHE_LOG_DIR}/moodle_error.log
CustomLog ${APACHE_LOG_DIR}/moodle_access.log combined
</VirtualHost>
Enable the site and required modules:
sudo a2enmod rewrite headers ssl
sudo a2ensite moodle.conf
sudo systemctl restart apache2
http://moodle.yourdomain.com in your browser/var/moodledataFor containerized deployment, see Moodle Docker Setup.
For automated deployment, see Moodle Ansible Setup.
Set up scheduled tasks for Moodle:
sudo crontab -e
Add the following line (replace with your PHP path and Moodle directory):
*/5 * * * * /usr/bin/php /var/www/moodle/admin/cli/cron.php >/dev/null 2>&1
Review Moodle Security to apply hardening measures.
Review Moodle Configuration to set domains, authentication, and application defaults.
sudo tail -f /var/log/apache2/moodle_error.logphp -m | grep -E "(mysqli|pdo|gd|mbstring|xml|curl|soap|xmlrpc|bcmath|intl|ldap)"ls -la /var/moodledataAny questions?
Feel free to contact us. Find all contact information on our contact page.