Install Moodle CMS on Ubuntu 18.04 LTS with Nginx, MariaDB and PHP 7.1 Support

Ubuntu 18.04 LTS is released… and I have installed it in my lab environment to test out different applications settings… The steps below is how I got Moodle CMS installed on Ubuntu 18.04 LTS .
Moodle is an open source course manage system (CMS) written and based on PHP… it allows learning institutions and organizations create powerful courses for students and other users… Moodle is used by many distance learning institutions around the world to provide courses for their students… it’s free and released under the GNU general public license.
We’re going to show you how to install and start using Moodle CMS on your Ubuntu 18.04 LTS Server
Since you need a Linux machine, a webserver , database and PHP, we’re going to be using the LEMP stack with Ubuntu as our Linux system.. Continue with the steps below to get it working.
Step 1: Prepare Ubuntu 18.04 LTS
This post assumes you already have Ubuntu 18.04 LTS installed and that you have root access to the system. Before installing other software, run the commands below to update Ubuntu
sudo apt update && sudo apt dist-upgrade && sudo apt autoremove
Step 2: Install Nginx HTTP Server
Now that Ubuntu is updated, run the commands below to install Nginx web server…
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
Step 3: Install MariaDB Database Server
You’ll also need a database server to run Moodle… and MariaDB is a great place to start… to install it, run the commands below
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.
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
Then add the below lines just below [mysqld] section.
default_storage_engine = innodb innodb_file_per_table = 1 innodb_file_format = Barracuda innodb_large_prefix = 1
Restart MariaDB server
sudo systemctl restart mariadb.service
Now that MariaDB is installed, go and create a blank Moodle database.
Run the commands below to logon to the database server. When prompted for a password, type the root password you created above.
sudo mysql -u root -p
Then create a database called moodle
CREATE DATABASE moodle;
Create a database user called moodleuser with new 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' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;
Finally, save your changes and exit.
FLUSH PRIVILEGES; EXIT;
Step 4: Install PHP7.1-FPM and Related PHP Modules
PHP 7.1 may not be available in Ubuntu default repositories… in order to install it, you will have to get it from third-party repositories.
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
Then update and upgrade to PHP 7.1
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
After install PHP, run the commands below to open PHP-FPM default file.
sudo nano /etc/php/7.1/fpm/php.ini
Then change the following lines below in the file and save. You may increase the value to suite your environment.
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
Step 5: Downloading Moodle
Next, run the commands below to download Moodle latest release. The commands below to download Moodle archive package.
cd /tmp && wget https://download.moodle.org/download.php/direct/stable33/moodle-latest-33.tgz
Then run the commands below to extract the downloaded file to Nginx default root.
tar -zxvf moodle-latest-33.tgz sudo mv moodle /var/www/html/moodle sudo mkdir /var/www/html/moodledata
Change modify the directory permission.
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
Step 6: Configure Nginx Moodle Site
Finally, 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 moodle
sudo nano /etc/nginx/sites-available/moodle
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/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;
}
}
Save the file and exit.
Step 7: 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/
Step 8 : Restart Nginx
To load all the settings above, restart Nginx by running the commands below.
sudo systemctl restart nginx.service
After that, open your browser and browse to the server IP address or hostname and you should see Moodle default setup wizard page.

Verify that the path and info are correct.. then click Next.

Choose the database type and click Next.

Type the database name, username and the password for the user and continue.

After that Moodle should install and complete. If you run into trouble, go back and look at the tutorial to make sure you follow all the steps.

Complete all the info and save.. This is how to install Moodle on Ubuntu server.
Enjoy!
You may also like the post below:
Hi there,
In the configure of file i must repair 2 point lost ;
server {
listen 80;
#listen [::]:80;
root /var/www/html/moodle;
index index.php index.html index.htm;
server_name moodle.com http://www.moodle.com;
location / {
try_files $uri $uri/ =404;
}
location /dataroot/ {
internal;
alias /var/www/html/moodledata/; # <– expected ;
}
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;
}
}
Thanks so much !