install moodle with nginx in ubuntu

How to Install Moodle with NGINX on Ubuntu

Moodle is an open-source, PHP-based course management system used by many universities and colleges to create and run their courses online. It is easy to use for both teachers & students and provides many collaborative features. In this article, we will look at how to install Moodle with NGINX on Ubuntu.


How to Install Moodle with NGINX on Ubuntu

Here are the steps to install Moodle with NGINX on Ubuntu.


1. Install NGINX

Open terminal and run the following commands to update system and install NGINX.

$ sudo apt-get update
$ sudo apt-get install nginx

Run the following commands to enable NGINX to autostart during reboot, and also start it now.

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


2. Install MariaDB/MySQL

Run the following commands to install MariaDB database for Moode. You may also use MySQL instead.

$ sudo apt-get install mariadb-server mariadb-client

Like NGINX, we will run the following commands to enable MariaDB to autostart during reboot, and also start now.

$ sudo systemctl stop mysql.service 
$ sudo systemctl start mysql.service 
$ sudo systemctl enable mysql.service

Run the following command to secure MariaDB installation.

$ sudo mysql_secure_installation

You will see the following prompts asking to allow/disallow different type of logins. Enter Y as shown.

 Enter current password for root (enter for none): Just press the Enter
 Set root password? [Y/n]: Y
 New password: Enter password
 Re-enter new password: Repeat password
 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

After you enter response for these questions, your MariaDB installation will be secured.



3. Install PHP-FPM & Related modules

Run the following command to add PHP repository, install it along with other related modules.

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt update
$ sudo apt install php7.1-fpm php7.1-common php7.1-mbstring php7.1-xmlrpc php7.1-soap php7.1-gd php7.1-xml php7.1-intl php7.1-mysql php7.1-cli php7.1-mcrypt php7.1-ldap php7.1-zip php7.1-curl

Open PHP-FPM config file.

$ sudo vi /etc/php/7.1/fpm/php.ini

Add/Update the values as shown. You may change it as per your requirement.

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


4. Create Moodle Database

Log into MySQL and create database for Moodle.

$ sudo mysql -u root -p

Create a database for moodle, a database user to access it, and also grant full access to this user. Replace moodle_user and moodle_password with your choice of username and password.

CREATE DATABASE moodle;
CREATE USER 'moodle_user'@'localhost' IDENTIFIED BY 'moodle_password';
GRANT ALL ON moodle.* TO 'moodle_user'@'localhost' IDENTIFIED BY 'moodle_password' WITH GRANT OPTION;

Flush privileges to apply changes.

FLUSH PRIVILEGES; 
EXIT;


5. Download & Install Moodle

Run the following command to download Moodle package.

$ cd /tmp && wget https://download.moodle.org/download.php/direct/stable33/moodle-latest-33.tgz

Run the following command to extract package to NGINX website root folder.

$ sudo tar -zxvf moodle-latest-33.tgz 
$ sudo mv moodle /var/www/html/moodle 
$ sudo mkdir /var/www/html/moodledata

Change the folder permissions.

$ sudo chown -R www-data:www-data /var/www/html/moodle/ 
$ sudo chmod -R 755 /var/www/html/moodle/ 
$ sudo chown www-data /var/www/html/moodledata


6. Configure NGINX

Now we will configure NGINX to serve files from moodle folder. For that, we will create a virtual host configuration file in NGINX.

$ sudo vi /etc/nginx/sites-available/moodle.conf

Copy paste the following configuration in the above file. Replace example.com below with your domain name.

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

    location / {
    try_files $uri $uri/ =404;        
    }
 
    location /dataroot/ {
    internal;
    alias /var/www/html/moodledata/
    }

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

}

Enable Moodle site in NGINX by creating a symlink as shown below.

$ sudo ln -s /etc/nginx/sites-available/moodle /etc/nginx/sites-enabled/

Restart NGINX Server to apply changes.

$ sudo service nginx restart


7. Configure Moodle

Open browser and go to your website (e.g. http://example.com) hosting your Moodle site. You will see the following installation screeen.

Select English from the dropdown and click Next.

On next screen, accept default values and click Next.

On next screen, select MariaDB database driver from dropdown and click next.

Enter database user details that you had entered in Step 4.

On next screen, enter username admin and enter password of your choice and click Next.

Continue the wizard until you have provided the required information and set up the site. When it is complete, Moodle will be installed & ready to use.

In this article, we have listed the various steps to install Moodle with NGINX in Ubuntu.

Also read:

How to Download File in Python
How to Run CGI Script in Apache
How to Create Permanent Alias in Linux
How to Give User Folder Access in Linux
How to Install Swift In Ubuntu

Leave a Reply

Your email address will not be published.