Installing Moodle
This guide covers the complete installation process for Moodle 4.x/5.x on a web server. Moodle is a robust open-source learning management system that requires a properly configured server environment to run reliably.
System Requirements
Before beginning the installation, ensure your server meets the following minimum requirements:
- PHP: Version 8.1 or higher (PHP 8.2 recommended for Moodle 4.3+, PHP 8.3 supported in Moodle 4.5+). Required PHP extensions include:
iconv,mbstring,curl,openssl,tokenizer,xmlrpc,soap,ctype,zip,gd,simplexml,spl,pcre,dom,xml,intl,json,sodium. - Database: MySQL 8.0+ / MariaDB 10.6.7+ / PostgreSQL 14+ / Microsoft SQL Server 2017+ / Oracle 19c+
- Web Server: Apache 2.4+ with
mod_rewriteenabled, or Nginx 1.18+ - Memory: Minimum 256MB PHP memory limit (512MB recommended)
- Disk Space: At least 200MB for Moodle code, plus additional space for uploaded files and backups
Step-by-Step Installation
Step 1: Download Moodle
Download the latest stable version of Moodle from download.moodle.org or clone from the official Git repository:
cd /var/www
git clone -b MOODLE_405_STABLE git://git.moodle.org/moodle.git
Alternatively, download and extract the ZIP/TGZ archive into your web server document root.
Step 2: Create the Moodle Data Directory
Moodle requires a writable data directory outside the web root for security. This directory stores uploaded files, session data, cache files, and other runtime data:
sudo mkdir /var/moodledata
sudo chown www-data:www-data /var/moodledata
sudo chmod 750 /var/moodledata
Important: This directory must not be accessible via the web. Placing it inside the web root is a serious security risk.
Step 3: Create the Database
Create a dedicated database and user for Moodle. For MySQL/MariaDB:
CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'moodleuser'@'localhost' IDENTIFIED BY 'securepassword';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, CREATE TEMPORARY TABLES, DROP, INDEX, ALTER
ON moodle.* TO 'moodleuser'@'localhost';
FLUSH PRIVILEGES;
For PostgreSQL:
CREATE USER moodleuser WITH PASSWORD 'securepassword';
CREATE DATABASE moodle WITH OWNER moodleuser ENCODING 'UTF8';
Step 4: Directory Permissions
Set appropriate permissions for the Moodle source directory:
sudo chown -R root:root /var/www/moodle
sudo chmod -R 755 /var/www/moodle
The web server user needs read access to the Moodle code directory but should not have write access for security reasons. Only the moodledata directory should be writable by the web server process.
Step 5: Run the Installer
You can install Moodle via the web interface or the command line:
Web-based installation: Navigate to http://yourserver/moodle in a browser and follow the prompts.
Command-line installation:
sudo -u www-data php admin/cli/install.php
--wwwroot=https://yourmoodle.example.com
--dataroot=/var/moodledata
--dbtype=mysqli
--dbhost=localhost
--dbname=moodle
--dbuser=moodleuser
--dbpass=securepassword
--fullname="My Moodle Site"
--shortname="Moodle"
--adminuser=admin
--adminpass=Admin123!
--adminemail=admin@example.com
--agree-license
--non-interactive
Step 6: Configure config.php
After installation, verify the config.php file in the Moodle root directory contains the correct settings. Key parameters include:
$CFG->dbtype = 'mysqli';
$CFG->dbhost = 'localhost';
$CFG->dbname = 'moodle';
$CFG->dbuser = 'moodleuser';
$CFG->dbpass = 'securepassword';
$CFG->prefix = 'mdl_';
$CFG->wwwroot = 'https://yourmoodle.example.com';
$CFG->dataroot = '/var/moodledata';
Step 7: Set Up Cron
Moodle relies on a cron job that runs regularly to process scheduled tasks such as sending emails, cleaning up logs, and running backups:
* * * * * /usr/bin/php /var/www/moodle/admin/cli/cron.php > /dev/null 2>&1
Running cron every minute is the recommended frequency for Moodle 4.x and later.
Post-Installation Checks
- Visit Site administration > Notifications to check for any issues
- Verify the cron job is running by checking Site administration > Server > Scheduled tasks
- Configure HTTPS for production deployments
- Set up email delivery via Site administration > Server > Outgoing mail configuration
- Review the Site administration > Security settings