Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hosamn/cc6563c0da558cd6e79403b879e27ff8 to your computer and use it in GitHub Desktop.
Save hosamn/cc6563c0da558cd6e79403b879e27ff8 to your computer and use it in GitHub Desktop.
Installing Moodle on Ubuntu Using AWS EC2 Instance with SSL on example.com

Installing Moodle on Ubuntu Using AWS EC2 Instance with SSL on example.com

Prerequisites

  • An AWS account with EC2 Free Tier
  • Basic knowledge of Linux command line
  • A registered domain name (example.com)
  • A Cloudflare account for SSL

Instructions

  1. 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.
  2. Note the public IP address of your EC2 instance.

  3. 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 and www.example.com pointing to your EC2 instance's public IP address.
  4. Configuring SSH Access

    • Windows:
      • Set file permissions for <your_keypair_file.pem> using icacls in PowerShell:
        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>
        eg.
        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>
    • 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>
  5. Updating the System

    sudo apt update
    sudo apt upgrade -y
  6. 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
  7. 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
  8. Setting Up Moodle Data Directory

    mkdir /var/www/moodledata
    sudo chown www-data /var/www/moodledata
  9. 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
  10. 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.
  11. 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
  12. Setting Up SSL with Certbot

    sudo certbot --apache -d example.com -d www.example.com
  13. Configuring Moodle for HTTPS

    sudo nano /var/www/html/config.php
    • Update the wwwroot variable:
      $CFG->wwwroot = 'https://example.com';
  14. 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
  15. Clear Moodle Caches

    sudo -u www-data php /var/www/html/admin/cli/purge_caches.php
  16. Set Up Auto-Renewal for SSL

    sudo crontab -e
    • Add this line:
      0 12 * * * /usr/bin/certbot renew --quiet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment