How to Install Moodle on a Ubuntu 24.04 VPS with a Scripted Guide
If you’re looking to set up a Moodle environment on a Ubuntu 24.04 VPS, this guide will walk you through the process using an automated script. Moodle is an open-source learning management system that is robust, scalable, and suitable for educational and training purposes. However, installing Moodle manually can be time-consuming, especially for those unfamiliar with Linux system administration.
To simplify the process, we’ve created a script that automates the entire installation and configuration of Moodle. This script is designed to save you time and ensure that all the necessary steps are completed correctly.
Why Use a Script?
- Efficiency: The script automates repetitive tasks like installing packages, setting permissions, and configuring the database.
- Accuracy: Reduces the risk of errors during manual setup.
- Scalability: Ideal for deploying Moodle across multiple servers.
- Customization: The script allows for easy configuration of variables like domain names and passwords.
Pre-Requisites
Before running the script, ensure you meet the following requirements:
- A Ubuntu 24.04 VPS server with root or sudo access.
- Basic understanding of Linux commands.
- A domain name or a static IP address (optional but recommended for HTTPS).
Moodle Automated Installation Script
Below is a complete script I created for automating Moodle installation on Ubuntu. Save it as moodle_install.sh and run it as root or with sudo.
#!/bin/bash
set -e
# Variables
PROTOCOL="http://"
read -p "Enter the web address (e.g., mymoodle123.com or 192.168.1.1): " WEBSITE_ADDRESS
# Step 1: Update system and install prerequisites
echo "Updating system and installing prerequisites..."
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install -y apache2 php libapache2-mod-php php-mysql graphviz aspell git clamav php-pspell php-curl php-gd php-intl ghostscript php-xml php-xmlrpc php-ldap php-zip php-soap php-mbstring unzip mariadb-server mariadb-client certbot python3-certbot-apache ufw nano clamav clamav-daemon
# Step 2: Set up the LAMP stack
echo "Setting up the LAMP stack..."
sudo systemctl enable apache2
sudo systemctl enable mariadb
# Step 3: Download Moodle code
echo "Downloading Moodle code..."
cd /var/www/html
sudo git clone https://github.com/moodle/moodle.git .
sudo git checkout origin/MOODLE_405_STABLE
sudo git config pull.ff only
# Step 4: Moodle-specific requirements
echo "Configuring Moodle directories and permissions..."
sudo mkdir -p /var/www/moodledata
sudo chown -R www-data:www-data /var/www/moodledata
sudo find /var/www/moodledata -type d -exec chmod 700 {} \;
sudo find /var/www/moodledata -type f -exec chmod 600 {} \;
sudo chmod -R 777 /var/www/html
sudo sed -i 's/.*max_input_vars =.*/max_input_vars = 5000/' /etc/php/8.3/apache2/php.ini
sudo sed -i 's/.*max_input_vars =.*/max_input_vars = 5000/' /etc/php/8.3/cli/php.ini
echo "* * * * * www-data /usr/bin/php /var/www/html/admin/cli/cron.php >/dev/null 2>&1" | sudo tee -a /etc/crontab
# Step 5: Configure HTTPS
echo "Configuring HTTPS..."
sudo sed -i "/ServerName/c\ ServerName $WEBSITE_ADDRESS" /etc/apache2/sites-available/000-default.conf
sudo sed -i "/ServerAlias/c\ ServerAlias www.$WEBSITE_ADDRESS" /etc/apache2/sites-available/000-default.conf
sudo certbot --apache
sudo systemctl reload apache2
PROTOCOL="https://"
# Step 6: Create Moodle database and user
echo "Creating Moodle database and user..."
MYSQL_MOODLEUSER_PASSWORD=$(openssl rand -base64 12)
sudo mysql -e "CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
sudo mysql -e "CREATE USER 'moodleuser'@'localhost' IDENTIFIED BY '$MYSQL_MOODLEUSER_PASSWORD';"
sudo mysql -e "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, CREATE TEMPORARY TABLES, DROP, INDEX, ALTER ON moodle.* TO 'moodleuser'@'localhost';"
echo "Moodle database created. Password: $MYSQL_MOODLEUSER_PASSWORD"
# Step 7: Command-line Moodle installation
echo "Installing Moodle via CLI..."
MOODLE_ADMIN_PASSWORD=$(openssl rand -base64 12)
sudo -u www-data /usr/bin/php /var/www/html/admin/cli/install.php \
--non-interactive --lang=en --wwwroot="$PROTOCOL$WEBSITE_ADDRESS" \
--dataroot=/var/www/moodledata --dbtype=mariadb --dbhost=localhost \
--dbname=moodle --dbuser=moodleuser --dbpass="$MYSQL_MOODLEUSER_PASSWORD" \
--fullname="Moodle Site" --shortname="Moodle" \
--adminuser=admin --summary="" --adminpass="$MOODLE_ADMIN_PASSWORD" \
--adminemail=admin@$WEBSITE_ADDRESS --agree-license
echo "Moodle installation completed."
echo "Admin credentials: Username: admin, Password: $MOODLE_ADMIN_PASSWORD"
# Step 8: Set up MySQL backups
echo "Setting up MySQL backups..."
BACKUP_USER_PASSWORD=$(openssl rand -base64 12)
sudo mysql -e "CREATE USER 'backupuser'@'localhost' IDENTIFIED BY '${BACKUP_USER_PASSWORD}';"
sudo mysql -e "GRANT LOCK TABLES, SELECT ON moodle.* TO 'backupuser'@'localhost'; FLUSH PRIVILEGES;"
cat <<EOF | sudo tee /root/.my.cnf
user=backupuser
password=${BACKUP_USER_PASSWORD}
EOF
sudo chmod 600 /root/.my.cnf
sudo mkdir -p /var/backups/moodle && sudo chmod 700 /var/backups/moodle && sudo chown root:root /var/backups/moodle
(crontab -l 2>/dev/null; echo "0 2 * * * mysqldump --defaults-file=/root/.my.cnf moodle > /var/backups/moodle/moodle_backup_\$(date +\%F).sql") | crontab -
(crontab -l 2>/dev/null; echo "0 3 * * * find /var/backups/moodle -name \"moodle_backup_*.sql\" -type f -mtime +7 -delete") | crontab -
# Step 9: Security configuration
echo "Applying security configurations..."
sudo find /var/www/html -type d -exec chmod 755 {} \;
sudo find /var/www/html -type f -exec chmod 644 {} \;
sudo mariadb-secure-installation
sudo ufw allow 22/tcp
sudo ufw allow 'Apache Full'
sudo ufw --force enable
sudo ufw default deny incoming
sudo ufw default allow outgoing
echo "Installation and security configuration complete."
echo "Visit your Moodle site at $PROTOCOL$WEBSITE_ADDRESS."
How to Use the Script
- Save the Script: Copy the script into a file named
moodle_install.sh. - Make it Executable: Run the command
chmod +x moodle_install.sh. - Run the Script: Execute the script using
sudo ./moodle_install.sh. - Follow Prompts: Provide the required inputs, such as your web address.
What the Script Does
- Installs and configures the LAMP stack (Linux, Apache, MySQL, PHP).
- Downloads and sets up Moodle using Git.
- Creates necessary directories with secure permissions.
- Configures HTTPS using Certbot for SSL certificates.
- Automates Moodle’s CLI-based installation.
- Sets up MySQL database backups and security configurations.
Conclusion
This script automates the complex process of installing Moodle on a Ubuntu VPS, saving time and reducing errors. After running the script, you’ll have a fully functional Moodle instance ready for use. Customize the script further if you have specific needs, such as additional plugins or modules. Happy teaching!