On this page
- How big should the server be?
- How do you install PHP, Nginx and PostgreSQL?
- Which PHP settings does Moodle require?
- How do you create the database?
- How do you download Moodle and create moodledata?
- What does the Nginx configuration look like?
- How do you finish the installation?
- Why does Moodle need cron every minute?
- How do you upgrade and back up Moodle?
- Questions
How big should the server be?
Size it by how many people are active at the same time, not by the total number of accounts. A pilot or a small school with a few dozen concurrent users runs on 2 vCPU and 4 GB of RAM. Quizzes with many simultaneous attempts, video-heavy courses and large backups need more CPU, memory and disk. The moodledata folder grows with every upload and course backup, so leave plenty of free disk space and check it with df -h regularly.
You need a domain such as learn.example.net pointing at the server, a sudo user, and a firewall allowing SSH, HTTP and HTTPS (see firewall rules).
How do you install PHP, Nginx and PostgreSQL?
Use distribution packages. Ubuntu 24.04 brings PHP 8.3, Debian 12 brings 8.2 and Debian 13 brings 8.4, all of which current Moodle releases accept.
Ubuntu and Debian
sudo apt update
sudo apt install -y nginx postgresql php-fpm php-pgsql php-curl php-gd php-intl \
php-mbstring php-xml php-zip php-soap php-bcmath php-opcache unzip git
php -v
psql --versionRocky Linux 9 and AlmaLinux 9 (9.4 or newer)
sudo dnf module reset -y php
sudo dnf module enable -y php:8.2
sudo dnf module enable -y postgresql:16
sudo dnf install -y nginx php-fpm php-pgsql php-gd php-intl php-mbstring php-xml \
php-pecl-zip php-soap php-bcmath php-opcache php-sodium postgresql-server unzip git
sudo postgresql-setup --initdb
sudo systemctl enable --now postgresql nginx php-fpm
sudo firewall-cmd --permanent --add-service={http,https} && sudo firewall-cmd --reloadOn Rocky and Alma set user = nginx and group = nginx in /etc/php-fpm.d/www.conf, and use nginx wherever this guide says www-data.
Which PHP settings does Moodle require?
Moodle refuses to install until max_input_vars is at least 5000, and it works better with higher upload and memory limits. Create a small override file:
# Ubuntu 24.04 path; use your PHP version on Debian, /etc/php.d/ on Rocky and Alma
sudo tee /etc/php/8.3/fpm/conf.d/90-moodle.ini > /dev/null <<'EOF'
max_input_vars = 5000
memory_limit = 512M
upload_max_filesize = 200M
post_max_size = 200M
max_execution_time = 300
opcache.memory_consumption = 256
opcache.max_accelerated_files = 20000
EOF
sudo cp /etc/php/8.3/fpm/conf.d/90-moodle.ini /etc/php/8.3/cli/conf.d/
sudo systemctl restart php8.3-fpm
php -i | grep max_input_varsThe copy into the CLI folder matters because the installer, cron and upgrades run through the command line PHP, which reads its own settings on Debian and Ubuntu.
How do you create the database?
Create a PostgreSQL role with a password and a UTF-8 database owned by it.
sudo -u postgres psql -c "CREATE USER moodleuser WITH PASSWORD 'strong-db-password';"
sudo -u postgres psql -c "CREATE DATABASE moodle WITH OWNER moodleuser ENCODING 'UTF8' TEMPLATE template0;"
sudo -u postgres psql -c "\l moodle"On Rocky and Alma, the default pg_hba.conf uses ident authentication for local TCP connections. Change the host lines for 127.0.0.1/32 and ::1/128 to scram-sha-256 in /var/lib/pgsql/data/pg_hba.conf and restart PostgreSQL, otherwise Moodle cannot log in with a password. The PostgreSQL guide explains roles and authentication in detail.
How do you download Moodle and create moodledata?
Fetch the current stable package from the official download site, unpack it under /var/www, and create the data folder outside the web root.
cd /tmp
wget https://download.moodle.org/download.php/direct/stable501/moodle-latest-501.tgz
sudo tar -xzf moodle-latest-501.tgz -C /var/www/
sudo mkdir -p /var/moodledata
sudo chown -R www-data:www-data /var/moodledata
sudo chmod 0770 /var/moodledata
sudo chown -R root:www-data /var/www/moodle
sudo chmod -R 0750 /var/www/moodleReplace 501 with the newest stable branch listed on the Moodle download page. From Moodle 5.1 the files served to browsers sit in a public subfolder; check with ls /var/www/moodle/public. If that folder exists, it is your web root; on older branches the web root is /var/www/moodle itself. Never place moodledata inside the web root, as it holds uploaded files that must only be served through Moodle.
What does the Nginx configuration look like?
Moodle uses URLs like /pluginfile.php/12/course/..., so PHP must receive the path info part. A working server block:
server {
listen 80;
listen [::]:80;
server_name learn.example.net;
root /var/www/moodle/public;
index index.php;
client_max_body_size 200m;
location / {
try_files $uri $uri/ /r.php;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 300;
}
location ~ (/vendor/|/node_modules/|composer\.json|/readme|readme\.txt|upgrade\.txt|db/install\.xml|/fixtures/|/behat/|phpunit\.xml|\.lock|environment\.xml) {
deny all;
return 404;
}
location /dataroot/ {
internal;
alias /var/moodledata/;
}
}On branches without a public folder, drop it from root and change the try_files fallback to =404. Use /run/php-fpm/www.sock on Rocky and Alma. Test with sudo nginx -t, reload, and add a certificate with sudo certbot --nginx -d learn.example.net as shown in the Certbot guide.
How do you finish the installation?
Open https://learn.example.net to run the web installer, or install from the shell, which is quicker to repeat. Moodle writes config.php during install, so allow that once:
sudo chown www-data /var/www/moodle
MCLI=$(dirname "$(find /var/www/moodle -path '*/admin/cli/install.php' | head -1)")
sudo -u www-data php "$MCLI/install.php" --wwwroot=https://learn.example.net \
--dataroot=/var/moodledata --dbtype=pgsql --dbhost=localhost --dbname=moodle \
--dbuser=moodleuser --dbpass='strong-db-password' --fullname="Example Learning" \
--shortname="EL" --adminuser=lmsadmin --adminemail=admin@example.net \
--agree-license --non-interactive --adminpass='change-me-after-login'
sudo chown root /var/www/moodle
sudo chmod 0640 "$(find /var/www/moodle -maxdepth 2 -name config.php | head -1)"
sudo chgrp www-data "$(find /var/www/moodle -maxdepth 2 -name config.php | head -1)"Then add $CFG->xsendfile = 'X-Accel-Redirect'; and $CFG->xsendfilealiases = ['/dataroot/' => $CFG->dataroot]; to config.php so Nginx serves files from moodledata directly. Sign in, change the admin password, and open Site administration, Server, Environment to confirm every check is green.
Why does Moodle need cron every minute?
Forum mail, enrolment syncs, grade calculations, cache cleanup and scheduled tasks all run from cron. Without it, emails and completion updates quietly stop.
MCLI=$(dirname "$(find /var/www/moodle -path '*/admin/cli/cron.php' | head -1)")
echo "* * * * * www-data /usr/bin/php $MCLI/cron.php > /dev/null" | sudo tee /etc/cron.d/moodle
sudo -u www-data php "$MCLI/cron.php" | tail -n 5Check Site administration, Server, Tasks, Scheduled tasks to see last and next run times. For outgoing email, set an authenticated SMTP relay on port 587 under Site administration, Server, Email, Outgoing mail configuration. Port 25 should not be relied on, and a support ticket is the place to ask before planning direct mail. The SMTP relay guide covers the relay side.
How do you upgrade and back up Moodle?
Back up first, enable maintenance mode, replace the code, and run the CLI upgrade.
sudo -u www-data php "$MCLI/maintenance.php" --enable
sudo -u postgres pg_dump -Fc moodle > ~/moodle-$(date +%F).dump
sudo tar -czf ~/moodledata-$(date +%F).tar.gz -C /var moodledata
# unpack the new release next to the old one, copy config.php and any added plugins, swap folders
sudo -u www-data php "$MCLI/upgrade.php" --non-interactive
sudo -u www-data php "$MCLI/maintenance.php" --disableUpgrade one major branch at a time when the release notes require it, and keep a copy of the old code folder until the site has run cleanly for a day. Store the database dump and moodledata archive off the server too; the optional daily backups add-on is a useful extra layer, restored through support.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
| Plan | Fits because | Size | Monthly | Order from |
|---|---|---|---|---|
| A2-4 | a pilot or a small class | 2 vCPU, 4 GB RAM, 100 GB NVMe | $14.40/mo | $43.20 for 3 months |
| A4-8 | a school with steady daily use | 4 vCPU, 8 GB RAM, 180 GB NVMe | $28.80/mo | $86.40 for 3 months |
| F4-16 | busy quizzes on fast NVMe | 4 vCPU, 16 GB RAM, 384 GB NVMe | $57.60/mo | $57.60 for 1 month |
Questions about Moodle learning platform
Can I use MariaDB or MySQL instead of PostgreSQL?
Yes, Moodle supports both, but each Moodle release sets a minimum database version. PostgreSQL is used here because current versions are easy to get on all four distributions.
Why does the environment page complain about max_input_vars?
The CLI and FPM PHP read different ini files on Debian and Ubuntu. Set max_input_vars = 5000 in both and restart PHP-FPM.
Where are uploaded course files stored?
In /var/moodledata, not in the web root. Back it up together with the database; one without the other cannot be restored cleanly.
Do I need Redis for Moodle?
Not to start. On busier sites, Redis for sessions and the application cache reduces database load; add it once you see sustained use.
Can students upload large videos?
Only up to the smallest of the Nginx, PHP and Moodle limits. Raise client_max_body_size, upload_max_filesize and post_max_size together, and keep an eye on disk space.
More in Websites, CMS and shops
- WordPress on Nginx, PHP-FPM, MariaDB
- Fix common WordPress errors
- Back up WordPress (3-2-1 rule)
- WordPress caching with Redis
- WooCommerce shop setup
- Ghost publishing platform
- phpBB forum
- Flarum forum
- Discourse community forum
- MediaWiki on Nginx and MariaDB
- Magento Open Source with OpenSearch
- PrestaShop store on Nginx
- OpenCart shop on Nginx
- Matomo web analytics
- Static site with Nginx and rsync
- Joomla on Nginx and PHP-FPM
- Drupal with Composer and Drush
- Vanilla Forums open source