Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

How to Install Moodle with Nginx on Ubuntu 24.04

How to Install Moodle with Nginx on Ubuntu 24.04

Setting up Moodle on Ubuntu 24.04 with Nginx allows you to create a robust, scalable, and high-performance learning management system (LMS) server. This guide walks you through the entire process, from preparing your system to configuring Moodle to run smoothly behind an Nginx web server. Before starting, ensure your Ubuntu server is up-to-date, and you have root or sudo privileges to install packages and configure the system. You’ll also need a domain name pointed to your server’s IP address for an optimal setup.

What You’ll Need

  • Ubuntu 24.04 server with root or sudo access
  • A registered domain name pointing to your server’s IP
  • Basic knowledge of Linux command line
  • Minimum 4GB RAM recommended for Moodle performance
  • Stable internet connection to download packages and dependencies

Step-by-Step

  1. Update Your System: Start by updating your package list and upgrading existing packages to ensure system stability.
    sudo apt update && sudo apt upgrade -y
  2. Install Required Dependencies: Install PHP, MariaDB (or MySQL), Nginx, and other necessary tools.
    sudo apt install nginx mariadb-server php php-fpm php-mysql php-xml php-mbstring php-zip php-gd php-curl php-intl php-bcmath unzip git -y
  3. Secure the MariaDB Server: Run the security script to set root password and remove anonymous users.
    sudo mysql_secure_installation
  4. Create Moodle Database and User: Log into MariaDB and create a dedicated database and user for Moodle.
    sudo mariadb -u root -p
    CREATE DATABASE moodle DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    CREATE USER 'moodleuser'@'localhost' IDENTIFIED BY 'your_secure_password';
    GRANT ALL PRIVILEGES ON moodle.* TO 'moodleuser'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;
  5. Download Moodle: Fetch the latest Moodle package from the official repository.
    cd /var/www
    sudo git clone https://git.moodle.org/moodle.git --branch MOODLE_400_STABLE moodle
  6. Set Permissions: Adjust ownership and permissions for Moodle directory.
    sudo chown -R www-data:www-data /var/www/moodle
    sudo chmod -R 755 /var/www/moodle
  7. Configure PHP-FPM: Ensure PHP-FPM is configured to handle Moodle requests.
    sudo nano /etc/php/8.1/fpm/pool.d/www.conf

    * (Make sure the user is www-data and restart PHP-FPM after changes)

  8. Create Nginx Server Block: Set up a new server block for your Moodle site.
    sudo nano /etc/nginx/sites-available/moodle.conf

    And add the following configuration:

    server {
        listen 80;
        server_name yourdomain.com;
    
        root /var/www/moodle;
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ [-]\.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    
        location ~ /(
            \.ht|\.git|\.svn|\~|\.bak|\.config|\.ini|\.sh|\.zip|\.tar|\.gz|\.bz2|\.7z|\.sql|\.bak)$ {
            deny all;
        }
    }
    
  9. Enable the Site and Restart Nginx: Link the configuration and reload Nginx.
    sudo ln -s /etc/nginx/sites-available/moodle.conf /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl restart nginx
    
  10. Complete Moodle Installation via Web Browser: Navigate to http://yourdomain.com and follow the on-screen instructions to finish setup, including database credentials and admin account creation.

Common Mistakes

  • Not updating the server before starting, leading to package conflicts.
  • Forgetting to set correct permissions on the Moodle directory, causing access issues.
  • Misconfiguring PHP-FPM or Nginx, resulting in 502 Bad Gateway errors.
  • Using an incorrect database password or not creating the database beforehand.
  • Failing to open necessary ports in the firewall, blocking web traffic.

Tips

  • Always keep your system and Moodle updated to the latest versions for security and features.
  • Use strong, unique passwords for database and admin accounts.
  • Configure SSL certificates (via Let’s Encrypt) for HTTPS security.
  • Regularly back up your Moodle data and database.
  • Monitor server performance and logs for issues.

Troubleshooting

If Moodle does not load properly or you encounter errors:

  • Check Nginx error logs: sudo tail -f /var/log/nginx/error.log
  • Verify PHP-FPM is running: sudo systemctl status php8.1-fpm
  • Ensure database connection details are correct in config.php.
  • Test DNS resolution: ping yourdomain.com to confirm it points correctly.
  • Clear browser cache or try accessing via incognito mode.
  • Check permissions on Moodle directory and files.

If problems persist, consult Moodle community forums or server logs for more detailed diagnostics.

Frequently Asked Questions

1. Can I install Moodle with Nginx on Ubuntu 24.04 without a domain name?

Yes, you can access Moodle via your server’s IP address, but using a domain name is recommended for SSL and easier access.

2. Is SSL necessary for Moodle?

While not mandatory, SSL encrypts data transmitted between users and your server, which is crucial for security, especially in e-learning environments.

3. How do I upgrade Moodle later?

To upgrade, back up your database and Moodle files, replace the Moodle code with the new version, then run the upgrade script via the web interface.

4. How can I improve Moodle performance on Ubuntu?

Use caching plugins, enable PHP opcode cache (like OPcache), optimize database settings, and ensure your server hardware meets Moodle’s requirements.

5. What should I do if I get a 502 Bad Gateway error?

This typically indicates PHP-FPM is not responding. Restart PHP-FPM with sudo systemctl restart php8.1-fpm and check logs for errors.

}

Related

How to Install Moodle with Nginx on Ubuntu 24.04
Scroll to top