- An AWS account with EC2 Free Tier
- Basic knowledge of Linux command line
- A registered domain name (example.com)
- A Cloudflare account for SSL
-
Log in to AWS Management Console
- Navigate to the EC2 Dashboard and click "Launch Instance."
- Select "Ubuntu Server 20.04 LTS" as the AMI.
- Choose an instance type (t2.micro for Free Tier).
- Configure instance details (1 instance, Auto-assign Public IP).
- Add storage (default 8 GB).
- Configure security groups (allow SSH, HTTP, and HTTPS traffic).
- Create a new key pair (name it moodleserver) and download the key pair file (
<your_keypair_file.pem>
). - Review and launch the instance.
-
Note the public IP address of your EC2 instance.
-
Point your domain to the EC2 instance
- Log in to your domain registrar's dashboard.
- Update the DNS settings to point to the public IP address of your EC2 instance.
- Ensure you have an A record for
example.com
andwww.example.com
pointing to your EC2 instance's public IP address.
-
Configuring SSH Access
- Windows:
- Set file permissions for
<your_keypair_file.pem>
using icacls in PowerShell:eg.icacls <your_keypair_file.pem> /inheritance:r icacls <your_keypair_file.pem> /grant:r <YourWindowsUsername>:F icacls <your_keypair_file.pem> /remove "BUILTIN\Users" icacls <your_keypair_file.pem>
icacls "Moodle-OpenSSH-key.pem" /inheritance:r && icacls "Moodle-OpenSSH-key.pem" /grant:r <YourWindowsUsername>:F && icacls "Moodle-OpenSSH-key.pem" /remove "BUILTIN\Users" && icacls "Moodle-OpenSSH-key.pem"
- Connect to the EC2 instance using PowerShell:
ssh -i "<your_keypair_file.pem>" ubuntu@<EC2_Instance_Public_IP>
- Set file permissions for
- Mac:
- Set file permissions for
<your_keypair_file.pem>
:chmod 400 <your_keypair_file.pem>
- Connect to the EC2 instance using Terminal:
ssh -i "<your_keypair_file.pem>" ubuntu@<EC2_Instance_Public_IP>
- Set file permissions for
- Windows:
-
Updating the System
sudo apt update sudo apt upgrade -y
-
Installing Required Packages
sudo apt install -y mysql-client mysql-server apache2 php php-mysql php-mbstring php-xml php-curl php-zip php-gd php-intl php-soap
-
Downloading and Setting Up Moodle
Search for the latest Moodle tgz file, get the direct link, then:
cd /var/www/html sudo wget https://download.moodle.org/download.php/direct/stable404/moodle-latest-404.tgz sudo tar -xf moodle-latest-404.tgz
-
Setting Up Moodle Data Directory
mkdir /var/www/moodledata sudo chown www-data /var/www/moodledata
-
Creating the Moodle Database
sudo mysql -u root -p
if you did not set a password, just press enter.
CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE USER 'moodleuser'@'localhost' IDENTIFIED BY 'yourpassword'; GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO 'moodleuser'@'localhost'; FLUSH PRIVILEGES; exit
-
Starting Moodle Installation
- Open a web browser and navigate to the EC2 instance's public IP address:
http://<EC2_Instance_Public_IP>
- Follow the on-screen instructions to complete the Moodle installation.
- Open a web browser and navigate to the EC2 instance's public IP address:
-
Configuring Apache for Moodle
sudo nano /etc/apache2/sites-available/example.com.conf
- Add the virtual host configuration:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/html <Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
- Enable the new site and restart Apache:
sudo a2ensite example.com.conf sudo a2dissite 000-default.conf sudo apache2ctl configtest sudo systemctl reload apache2
- Add the virtual host configuration:
-
Setting Up SSL with Certbot
sudo certbot --apache -d example.com -d www.example.com
-
Configuring Moodle for HTTPS
sudo nano /var/www/html/config.php
- Update the wwwroot variable:
$CFG->wwwroot = 'https://example.com';
- Update the wwwroot variable:
-
Setting Up Cron Job for Moodle
sudo crontab -e
- Add the following lines:
#* * * * * /usr/bin/php /var/www/html/moodle/admin/cli/cron.php >/dev/null #*/15 * * * * /usr/bin/php /var/www/html/moodle/admin/cli/cron.php >/dev/null 2>&1 */15 * * * * /usr/bin/php /var/www/html/moodle/admin/cli/cron.php >> /var/log/moodle_cron.log 2>&1
- Add the following lines:
-
Clear Moodle Caches
sudo -u www-data php /var/www/html/admin/cli/purge_caches.php
-
Set Up Auto-Renewal for SSL
sudo crontab -e
- Add this line:
0 12 * * * /usr/bin/certbot renew --quiet
- Add this line: