This post shows students and new users steps to install Moodle course management platform on Ubuntu with Nginx web server and free Let’s Encrypt SSL certificate to make sure visitors are communicating over HTTPS only.
Moodle is an open source course manage system that works great for professionals, businesses and colleges who want to run and manage their courses or training materials online securely.
It is probably the best and most popular open source learning management platform available today. However, if you’re going to be setting up Moodle in your own environment, make sure to run it over HTTPS.
This setup might take a while to complete and the process below should work on other Linux platform as well.
To get started with installing Moodle on Ubuntu Linux with Nginx and Let’s Encrypt, follow the steps below.
How to setup your domain
If you’re going to be using Let’s Encrypt, you should then make sure 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 to also make sure www CNAME is pointing to the domain name. 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’re going to be using Nginx web server to run Moodle. 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 be used to stop, start and enable Nginx services to always start up everytime 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.
If you see the above page in your browser, then Nginx is working as expected.
How to install MariaDB on Ubuntu Linux
A database server is required for Moodle to function. Moodle stores its content in a database, and MariaDB is probably the best database server available to run Moodle.
MariaDB is fast, secure and 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 be used to stop, start and enable MariaDB services to always 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, login 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 login without password, and only from the server console.
If you see a similar screen as shown above, then the server was successfully installed.
Next, run the commands below to open 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 we also mentioned above, we’re installing PHP on Ubuntu since Moodle requires it. PHP packages are added to Ubuntu repositories. The versions 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.
To a third party repository with the latest versions of PHP, run the commands below.
sudo apt-get install software-properties-common sudo add-apt-repository ppa:ondrej/php
At the time of this writing, the latest PHP version 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 PHP configuration settings that work great with Moodle. Run the commands below to open PHP default configuration file.
sudo nano /etc/php/7.4/fpm/php.ini
Then change the line settings to be something line the lines below. 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 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 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
To get Moodle latest release you may want to use Github repository. 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 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 own 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.
Now the 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.
However, we want to make sure our server is protected with Let’s Encrypt free SSL certificates. So, continue below to learn how to generate Let’s Encrypt SSL certificate for websites.
How to setup Let’s Encrypt for Moodle
We have written a great post on how to generate and manage Let’s Encrypt SSL certificates for Nginx web server. 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 website, click on the link below:
How to install Let’s Encrypt on Ubuntu Linux with Nginx
If you were successful in generating a Let’s Encrypt SSL certificate, you should then 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 blocks 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 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 } 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 } 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 Moodle setup wizard by browsing to the server hostname or IP address over HTTPS.
Then follow the on-screen instructions and select the installation language here.
Next, select MariaDB connection driver and continue.
On the next screen, enter the database connection info you created above 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 have complete control over the site. Make sure you give it a secure username and password as well as a valid email address. You can create more admin accounts later on.
Login and begin configuring your Moodle website.
In the future when you want to upgrade to a new released version, simply run the commands below to upgrade.
How to upgrade Moodle on Ubuntu Linux
First stop the webserver.
sudo systemctl stop nginx
For students and new users who already have Moodle installed and wish to upgrade, asuming 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 webserver 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 Moodle config file, theme and data folder… If you updated your themes… a theme content should be there…. If you also installed aditional 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, logon to Moodle and go to:
Administration > Site administration > Notifications.
Moodle will automatically detect the new version and perform all the SQL database or file system upgrades that are necessary. 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) then 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. If you find any error above or have something to add, please use the comment form below.