When you’re seeking to arrange a Moodle surroundings on a Ubuntu 24.04 VPS, this information will stroll you thru the method utilizing an automatic script. Moodle is an open-source studying administration system that’s strong, scalable, and appropriate for academic and coaching functions. Nevertheless, putting in Moodle manually will be time-consuming, particularly for these unfamiliar with Linux system administration.
To simplify the method, we’ve created a script that automates all the set up and configuration of Moodle. This script is designed to save lots of you time and be sure that all the required steps are accomplished accurately.
Why Use a Script?
- Effectivity: The script automates repetitive duties like putting in packages, setting permissions, and configuring the database.
- Accuracy: Reduces the chance of errors throughout guide setup.
- Scalability: Superb for deploying Moodle throughout a number of servers.
- Customization: The script permits for straightforward configuration of variables like domains and passwords.
Pre-Requisites
Earlier than working the script, make sure you meet the next necessities:
- A Ubuntu 24.04 VPS server with root or sudo entry.
- Primary understanding of Linux instructions.
- A website title or a static IP deal with (non-obligatory however advisable for HTTPS).
The Script
Beneath is the whole script for automating Moodle set up on Ubuntu. Put it aside as moodle_install.sh and run it as root or with sudo.
#!/bin/bash
set -e
# Variables
PROTOCOL="http://"
learn -p "Enter the net deal with (e.g., mymoodle123.com or 192.168.1.1): " WEBSITE_ADDRESS
# Step 1: Replace system and set up conditions
echo "Updating system and putting in conditions..."
sudo apt-get replace && sudo apt-get improve -y
sudo apt-get set up -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: Arrange the LAMP stack
echo "Organising the LAMP stack..."
sudo systemctl allow apache2
sudo systemctl allow mariadb
# Step 3: Obtain 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 solely
# Step 4: Moodle-specific necessities
echo "Configuring Moodle directories and permissions..."
sudo mkdir -p /var/www/moodledata
sudo chown -R www-data:www-data /var/www/moodledata
sudo discover /var/www/moodledata -type d -exec chmod 700 {} ;
sudo discover /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/' /and many others/php/8.3/apache2/php.ini
sudo sed -i 's/.*max_input_vars =.*/max_input_vars = 5000/' /and many others/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 /and many others/crontab
# Step 5: Configure HTTPS
echo "Configuring HTTPS..."
sudo sed -i "/ServerName/c ServerName $WEBSITE_ADDRESS" /and many others/apache2/sites-available/000-default.conf
sudo sed -i "/ServerAlias/c ServerAlias www.$WEBSITE_ADDRESS" /and many others/apache2/sites-available/000-default.conf
sudo certbot --apache
sudo systemctl reload apache2
PROTOCOL="https://"
# Step 6: Create Moodle database and consumer
echo "Creating Moodle database and consumer..."
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 set up
echo "Putting in Moodle through CLI..."
MOODLE_ADMIN_PASSWORD=$(openssl rand -base64 12)
sudo -u www-data /usr/bin/php /var/www/html/admin/cli/set up.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 Website" --shortname="Moodle"
--adminuser=admin --summary="" --adminpass="$MOODLE_ADMIN_PASSWORD"
--adminemail=admin@$WEBSITE_ADDRESS --agree-license
echo "Moodle set up accomplished."
echo "Admin credentials: Username: admin, Password: $MOODLE_ADMIN_PASSWORD"
# Step 8: Arrange MySQL backups
echo "Organising 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
consumer=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 * * * discover /var/backups/moodle -name "moodle_backup_*.sql" -type f -mtime +7 -delete") | crontab -
# Step 9: Safety configuration
echo "Making use of safety configurations..."
sudo discover /var/www/html -type d -exec chmod 755 {} ;
sudo discover /var/www/html -type f -exec chmod 644 {} ;
sudo mariadb-secure-installation
sudo ufw permit 22/tcp
sudo ufw permit 'Apache Full'
sudo ufw --force allow
sudo ufw default deny incoming
sudo ufw default permit outgoing
echo "Set up and safety configuration full."
echo "Go to your Moodle web site at $PROTOCOL$WEBSITE_ADDRESS."
How one can Use the Script
- Save the Script: Copy the script right into a file named
moodle_install.sh. - Make it Executable: Run the command
chmod +x moodle_install.sh. - Run the Script: Execute the script utilizing
sudo ./moodle_install.sh. - Comply with Prompts: Present the required inputs, resembling your net deal with.
What the Script Does
- Installs and configures the LAMP stack (Linux, Apache, MySQL, PHP).
- Downloads and units up Moodle utilizing Git.
- Creates needed directories with safe permissions.
- Configures HTTPS utilizing Certbot for SSL certificates.
- Automates Moodle’s CLI-based set up.
- Units up MySQL database backups and safety configurations.
Conclusion
This script automates the complicated course of of putting in Moodle on a Ubuntu VPS, saving time and lowering errors. After working the script, you’ll have a completely useful Moodle occasion prepared to be used. Customise the script additional when you’ve got particular wants, resembling extra plugins or modules. Completely satisfied educating!
Associated
Uncover extra from Patrick Domingues
Subscribe to get the most recent posts despatched to your electronic mail.


