How to Install Moodle with Nginx on Ubuntu Linux

|

,

|

The post provides a detailed step-by-step guide for installing the open-source course management system, Moodle, on Ubuntu using Nginx web server and Let’s Encrypt SSL certificate for secure communication. It covers multiple processes, including setting up a domain, installing necessary software such as Nginx, MariaDB, PHP, creating Moodle’s database, configuring Nginx for Moodle, and setting…

This post shows students and new users steps to install the Moodle course management platform on Ubuntu with an Nginx web server and a free Let’s Encrypt SSL certificate to ensure visitors communicate over HTTPS only.

Moodle is an open-source course management system allowing users to create and manage online courses securely. To run Moodle, you need a web server, a database server, and PHP.

Nginx is one of the most popular open-source web servers, and it is known for its high performance and stability. It is also a good choice for running Moodle. Ubuntu is a popular Linux distribution and is widely used for web servers.

It is easy to use and has a large community of users and developers. This makes it a good choice for running Moodle. Installing Moodle with Nginx on Ubuntu Linux will enable you to create and manage online courses securely with a fast and stable web server.

It will also allow you to run Moodle on a reliable and secure Linux server.

Follow the steps below to install Moodle on Ubuntu Linux with Nginx and Let’s Encrypt.

How to set your domain

If you’re using Let’s Encrypt, you should ensure your domain is configured correctly. This setup assumes that your domain name is called example.com and is pointing to your server with IP address 192.168.1.2

Don’t forget also to make sure www CNAME is pointing to the domain name. It should look like something below:

example.com        A       ==========>    192.168.1.2
www               CNAME    ==========>    example.com

How to install Nginx on Ubuntu Linux

As mentioned above, we will use the Nginx web server to run Moodle. This is because Moodle requires a web server to function, and Nginx is one of the most popular open-source web servers available today.

To install Nginx on Ubuntu, run the commands below:

sudo apt update
sudo apt install nginx

After installing Nginx, the commands below can stop, start, and enable Nginx services to start every time your server starts up.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

To test whether Nginx is installed and functioning, open your web browser and browse to the server’s IP address or hostname.

http://localhost

If you see the above page in your browser, Nginx works as expected.

How to install MariaDB on Ubuntu Linux

A database server is required for Moodle to function. This is because Moodle stores its content in a database, and MariaDB is probably the best database server to run Moodle.

MariaDB is fast and secure and is the default server for almost all Linux servers. To install MariaDB, run the commands below:

sudo apt install mariadb-server
sudo apt install mariadb-client

After installing MariaDB, the commands below can stop, start, and enable MariaDB services to start up when the server boots.

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

Next, run the commands below to secure the database server with a root password if you were not prompted to do so during the installation.

sudo mysql_secure_installation

When prompted, use the guide below to answer:

If you've just installed MariaDB, and haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): PRESS ENTER

Switch to unix_socket authentication [Y/n] n

Change the root password? [Y/n] n

Remove anonymous users? [Y/n] y

Disallow root login remotely? [Y/n] y

Remove test database and access to it? [Y/n] y

Reload privilege tables now? [Y/n] y

All done!

To verify and validate that MariaDB is installed and working, log in to the database console using the commands below:

sudo mysql -u root -p

You should automatically be logged in to the database server since we initiated the login request as root. Only the root can log in without a password and from the server console.

The server was successfully installed if you see a similar screen.

Next, run the commands below to open the MariaDB default config file…

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Then add the highlighted lines below and save.

[mysqld]
#
* Basic Settings
#
user = mysql
pid-file = /run/mysqld/mysqld.pid
socket = /run/mysqld/mysqld.sock
#port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
#skip-external-locking
innodb_file_format = Barracuda
innodb_file_per_table = 1
innodb_large_prefix = ON

Restart MariaDB after that.

sudo systemctl restart mariadb.service

How to install PHP on Ubuntu Linux

As mentioned above, we’re installing PHP on Ubuntu since Moodle requires it. PHP packages are added to Ubuntu repositories. The versions of the repositories might not be the latest. If you need to install the latest versions, you’ll need to add a third-party PPA repository.

Run the commands below to a third-party repository with the latest versions of PHP.

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

At the time of this writing, the latest PHP version is 7.4.

sudo apt update

Next, run the commands below to install PHP 7.4 and related modules.

sudo apt install php7.4-fpm php7.4-common php7.4-mysql php7.4-gmp php7.4-curl php7.4-intl php7.4-mbstring php7.4-xmlrpc php7.4-gd php7.4-xml php7.4-cli php7.4-zip

Next, you’ll want to change some great PHP configuration settings with Moodle. First, run the commands below to open the PHP default configuration file.

sudo nano /etc/php/7.4/fpm/php.ini

Then, change the line settings to something like the lines below. Finally, save your changes and exit.

file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
cgi.fix_pathinfo = 0
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

How to create Moodle database on Ubuntu

At this point, we’re ready to create a Moodle database. As mentioned above, Moodle uses databases to store its content.

To create a database for Moodle, run the commands below:

sudo mysql -u root -p

Then, create a database called moodle

CREATE DATABASE moodle;

Next, create a database user called moodleuser and set a password

CREATE USER 'moodleuser'@'localhost' IDENTIFIED BY 'new_password_here';

Then, grant the user full access to the database.

GRANT ALL ON moodle.* TO 'moodleuser'@'localhost' WITH GRANT OPTION;

Finally, save your changes and exit.

FLUSH PRIVILEGES;
EXIT;

How to download and install Moodle on Ubuntu Linux

You may want to use the GitHub repository to get Moodle’s latest release. Install Curl and other dependencies to get started.

sudo apt install git curl

After installing git and Curl above, change into the Nginx root directory and download Moodle packages from GitHub. Always replace the branch number with the latest branch. The current major version is 36.

cd /var/www/html
sudo git clone -b MOODLE_36_STABLE git://git.moodle.org/moodle.git example.com
sudo mv moodle /var/www/

Then, run the commands below to set the correct permissions for Moodle to function.

sudo mkdir -p /var/www/moodledata
sudo chown -R www-data:www-data /var/www/
sudo chmod -R 755 /var/www/

How to configure Nginx for Moodle

Next, configure the Nginx site configuration file for Moodle. This file will control how users access Moodle content. Run the commands below to create a new configuration file called example.com.conf.

sudo nano /etc/nginx/sites-available/example.com.conf

Then copy and paste the content below into the file and save it. Replace the highlighted line with your domain name and directory root location.

server {
    listen 80;
    listen [::]:80;
    root /var/www/example.com;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

    client_max_body_size 100M;
    autoindex off;
    location / {
        try_files $uri $uri/ =404;
    }

    location /dataroot/ {
      internal;
      alias /var/www/moodledata/;
    }

    location ~ [^/].php(/|$) {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Save the file and exit.

Once the example.com configuration file is created, run the commands below to enable it.

sudo a2ensite example.com.conf

At this stage, Moodle is ready and can be launched by going to the server’s IP or hostname.

http://localhost

However, we want to protect our server with Let’s Encrypt free SSL certificates. So, continue below to learn how to generate a Let’s Encrypt SSL certificate for websites.

How to set up Let’s Encrypt for Moodle.

We have written a great post on generating and managing Let’s Encrypt SSL certificates for the Nginx web servers. You can use that post to apply it here for your Moodle website.

To read the post on how to generate Let’s Encrypt SSL certificates for a website, click on the link below:

How to install Let’s Encrypt on Ubuntu Linux with Nginx

If you successfully generate a Let’s Encrypt SSL certificate, you should reopen the server block for our Moodle website by running the commands below.

sudo nano /etc/nginx/sites-available/example.com.conf

The new Moodle server block configurations should look similar to the line below. Take notes of the highlighted lines.

  • The first server block listens on port 80. It contains a 301 redirect to redirect HTTP to HTTPS.
  • The second server block listens on port 443. It contains a 301 redirect to redirect www to the non-www domain.
server {
    listen 80;
    listen [::]:80;
    root /var/www/example.com;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

    include snippets/well-known.conf;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    root /var/www/example.com;
    index  index.php index.html index.htm;
    server_name www.example.com;
   
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;

    add_header Strict-Transport-Security "max-age=31536000;  includeSubDomains";
    
    include snippets/well-known.conf;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    root /var/www/example.com;
    index  index.php index.html index.htm;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;

    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:50m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 30s;

    ssl_dhparam /etc/ssl/certs/dhparam.pem;

    add_header Strict-Transport-Security "max-age=31536000;  includeSubDomains";
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    
    include snippets/well-known.conf;

    client_max_body_size 100M;

    autoindex off;


    location / {
        try_files $uri $uri/ =404;
    }

    location /dataroot/ {
       internal;
       alias /var/www/moodledata/;
    }

    location ~ [^/].php(/|$) {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Save the file above, then restart Nginx and PHP using the commands below.

sudo systemctl reload nginx

Finally, if everything went as planned, you should be able to start the Moodle setup wizard by browsing to the server hostname or IP address over HTTPS.

https://example.com/

Then, follow the on-screen instructions and select the installation language here.

Next, select the MariaDB connection driver and continue.

Enter the database connection info you created above on the next screen and continue.

Then, create an admin account and the Moodle site info and finish the installation.

On this page, you should configure your main administrator account, which will be controlled entirely. Ensure you give it a secure username, password, and valid email address. You can create more admin accounts later on.

Log in and begin configuring your Moodle website.

In the future, when you want to upgrade to a newly released version, simply run the commands below to upgrade.

How to upgrade Moodle on Ubuntu Linux

First, stop the web server.

sudo systemctl stop nginx

For students and new users who already have Moodle installed and wish to upgrade, assuming that you followed the steps above to install, run the commands below to backup your old Moodle folder…

sudo mv /var/www/example.com /var/www/example.com_bak

Then change into the web server root directory and download the latest version of Moodle from GitHub…. always change the version number to the current (latest)

cd /var/www/html
sudo git clone -b MOODLE_37_STABLE git://git.moodle.org/moodle.git example.com

Next, copy the Moodle config file, theme, and data folder… If you updated your themes… theme content should be there…. If you also installed additional modules… you should find them in the /mod directory… copy them to the new Moodle folder….

sudo cp /var/www/example.com_bak/config.php /var/www/example.com
sudo cp -pr /var/www/example.com_bak/theme/mytheme /var/www/example.com/theme/mytheme
sudo cp -pr /var/www/example.com_bak/mod/mymod /var/www/example.com/mod/mymod

After that, update the web server permissions.

sudo chown -R www-data:www-data /var/www/example.com/
sudo chmod -R 755 /var/www/example.com/

Restart your web server.

sudo systemctl start nginx

The last step is to trigger the upgrade processes within Moodle…. If you put your site into Maintenance mode earlier, take it out now!

Once you browse to the server IP or hostname, Moodle should prompt you to begin upgrading your database… After upgrading the database, login to Moodle and go to:

Administration > Site administration > Notifications.

Moodle will automatically detect the new version and perform all the necessary SQL database or file system upgrades. If there is anything it can’t do itself (very rare), then you will see messages telling you what you need to do.

Assuming all goes well (no error messages), you can start using your new version of Moodle and enjoy the new features!

That should do it!

Conclusion:

This post showed you how to install Moodle on Ubuntu Linux with Nginx and Let’s Encrypt. Please use the comment form below if you find any errors above or have something to add.

Like this:



13 responses to “How to Install Moodle with Nginx on Ubuntu Linux”

  1. ken collinson Avatar
    ken collinson

    Hi,
    A great tutorial, only you left the moodle folder inside the ‘web’ folder and it doesn’t work when you go to the domain via the browser.

    or it doesn’t work for me.

    Kind Regards
    ken

  2. ken Avatar
    ken

    My bad.. 😉

  3. Emmanuel Canadas Avatar
    Emmanuel Canadas

    Hello,

    good job, it saved me because i’m just start on ubuntu and dependancies.

    so, just a lottle error for SSL :
    server {
    listen 443 ssl http2;
    listen [::] 443 ssl http2;

    just correcte by :
    listen [::] :443 ssl http2;

    My Nginxwas able to start now

  4. Andreoni Vieira Avatar
    Andreoni Vieira

    This is amazing, thanks for this explanation, could you help me with one more thing?

    I’m trying to publish my website for external access, my address is ead.flyeagle.com.br, but I only see the NGINX pages and I can’t access ead.flyeagle.com.br/moodle

    could you help me with these settings?

    Thank you

  5. Mark Avatar
    Mark

    Sorry, I’m new to nginx. I went through steps 1-6 and am still getting the Welcome to nginx screen. I’m sure I can just delete “/etc/nginx/sites-available/default” file but is there a more elegant way?

  6. Erwin Avatar
    Erwin

    Thank you for the step-by-step tutorial
    I followed till the end of step 6 without any issues on a fresh installation of Ubuntu. Unfortunately, when opening localhost it still shows “welcome to nginx” etc. but no Moodle.
    The upper post from ken collinson already mentions an issue but I couldn’t figure out how to solve it on my own. Can someone help out

    1. Mark Avatar
      Mark

      Figured it out.
      Go to /etc/nginx/nginx.conf and change the line where it says include /etc/nginx/sites-enabled*; . Add your site to the end of that line. So for me it was /etc/nginx/sites-enabled/moodle*;

  7. Thembi Moyo Avatar
    Thembi Moyo

    This worked for me

    listen 443 ssl http2;
    listen [::]:443 ssl http2;

  8. Christian Muland Avatar
    Christian Muland

    Hello since tutorial and thanks, but what if I want example.com/moodle? Please.

  9. Liko Avatar
    Liko

    Can’t renew the SSL certificate

    Could not choose appropriate plugin: The manual plugin is not working; there may be problems with your existing configuration.
    The error was: PluginError(‘An authentication script must be provided with –manual-auth-hook when using the manual plugin non-interactively.’)

  10. Corleroux Avatar
    Corleroux

    Well done on a thorough tutorial. Over the last couple of months I’ve read through quite a few of these tutorials (LEMP specifically) . This tutorial is by far one of the most complete pieces I’ve come across. Everything from start to finish covered beautifully and succinctly. This applies to every application you require on the LEMP stack (wordpress, moodle, etc…) Thanks again for the details

  11. Paul Avatar
    Paul

    A lot of dodgy Moodle-nginx tutorials about, but this one absolutely nailed it. Can’t thank you enough. 👏

  12. ---- Avatar
    —-

    didn’t work for me.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.