Installing Moodle on LEMP / Ubuntu 18.04 / Nginx?

Posted October 9, 2019 12.4k views
NginxLEMPInitial Server Setup

Is there a more recent hand-holding tutorial somewhere to guide me through the installation on Ubuntu 18.04 running a LEMP stack? I’m admittedly a deployment newbie, so I need my handheld pretty well. I’m finding some older information for Ubuntu 16 and more for Apache, but I’d already set up and configured Nginx before realizing I needed to install a LMS.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers

Hi @ljwaloski,

Let’s say we start from the beginning so it’s more clear,

Prepare Ubuntu 18.04 LTS

First, you need to update your system

sudo apt update && sudo apt dist-upgrade && sudo apt autoremove

Install Nginx HTTP Server

Now that Ubuntu is updated, we need to install Nginx

sudo apt install nginx

After installing Nginx, the commands below can be used to stop, start and enable Nginx service to always startup when your computer boots.

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

Install MariaDB Database Server

You’ll also need a database server to run Moodle, my recommendation is MariaDB

sudo apt install mariadb-server mariadb-client

After installing, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots.

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

After that, run the commands below to secure MariaDB server.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

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

Next, run the commands below to open MySQL default configuration file.

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

Now to add some configuration to your instance

default_storage_engine = innodb
innodb_file_per_table = 1
innodb_file_format = Barracuda
innodb_large_prefix = 1

Once done, restart MariaDB

sudo systemctl restart mariadb.service

Now we need to create a Database for our Moodle installation. Enter your MariaDB instance like

sudo mysql -u root -p

Then create a database and configure it for usage

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

Then grant the user full access to the database.

GRANT ALL ON yourdbname.* TO 'youruser'@'localhost' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;

Finally, save your changes and exit.


Install PHP7.1-FPM and Related PHP Modules

Run the commands below to add the below third party repository to upgrade to PHP 7.1

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

Update Ubuntu again

sudo apt update

Finally, run the commands below to install PHP 7.1 and related modules..

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

Configure Nginx

You can type the following to create the file needed

sudo nano /etc/nginx/sites-available/moodle

Put the followng in it

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

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

    location /dataroot/ {
    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;


PS. Notice you need to update your server_name in the config.

Enable the Moodle site

After configuring the VirtualHost above, enable it by running the commands below

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

Restart Nginx
To load all the settings above, restart Nginx by running the commands below.

sudo systemctl restart nginx.service

Kind regards,

  • Kalin,

    Thank you so much for the detailed walk though! When I’m configuring Nginx, it looks like this adds an extra server block for the Moodle app itself, correct? I’ve got two server blocks already set up for multiple domains (and to be honest, that process was the one that hung me up the absolute worst for a bit during initial setup), so just checking I should do this again for the Moodle installation.

    Thank you,


So, everything goes fine until I go to secure the installation of MariaDB:

sudo mysql_secure_installation

and this is what I get back:


In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

I ran into something on StackOverflow that suggested there might be a conflict with apparmor since I had MySQL on this server before, but setting /usr/sbin/mysqld to complain mode didn’t seem to help at all. I’m still stuck at this point and MariaDB will not start. Trying to force the start gives me a timeout error.

  • Hi @ljwaloski,

    If you have both MySQL and MariaDB installed, one will work and the other won’t. If your MySQL is actually working properly, you can use that.

    As for your question Nginx configuration, yes you’ll need to create another block of configuration for the separate Moodle installation.