Installing Moodle on Ubuntu 17.04 with Nginx Support

This post is about installing Moodle on Ubuntu 17.04. It will detail a step by step guide of Moodle installation with Nginx web server support on Ubuntu. For those who don’t know what Moodle is, it’s an open source learning platform written in PHP and based on the LAMP or LEMP stack.

It’s used by many distance learning institutions around the world. It’s free and to use and released under the GNU general public license.

We’re going to show you how to install and start using it on your own Ubuntu servers.

Since Moodle is based on the LAMP or LEMP stack, we’re going to be installing it on Ubuntu 17.04 system with Nginx, MySQL and PHP support. So without wasting your time, continue below to get Moodle working on Ubuntu.

Step 1: Ubuntu

This post assumes you already have Ubuntu installed and that you have root access to the system. Before you can install the servers and packages below, run this command to update Ubuntu first.

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

After running the above command, continue below with installing Nginx web server

Step 2: Install Nginx

Now that Ubuntu is updated, run the commands below to install Nginx web server

sudo apt-get install nginx

After installing Nginx, run the commands below to start and enable it.

sudo systemctl start nginx
sudo systemctl enable nginx

Next, go and install MySQL

Step 3: Install MySQL

After installing Nginx web server, go and install MySQL database server. To do that, run the commands below.

sudo apt-get install mysql-server mysql-client

During MySQL installation, you’ll be prompted to create and confirm MySQL root password. Please do.

Next, run the commands below to start and enable MySQL

sudo systemctl start mysql
sudo systemctl enable mysql

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

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Then add the below lines just below [mysqld] section.

default_storage_engine = innodb
innodb_file_per_table = 1
innodb_file_format = Barracuda

When you’re done, save the file.

Next, go and create a database and user for Moodle. To do that, run the commands below to logon to the database server

sudo mysql -u root -p

Type the root password you created during MySQL installation.

Next, run the commands below to create a database called moodledb

CREATE DATABASE moodledb;

Next, run the commands below to create a database user called moodleuser and set a new password

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

Next, grant the new user full access to the database

GRANT ALL ON moodledb.* TO 'moodleuser'@'localhost';

Finally, save your changes to disk.

FLUSH PRIVILEGES;
exit;

After creating the database and user, continue below to install PHP.

Step 4: Install PHP

Now that Nginx and MySQL are installed, run the commands below to install PHP and other PHP modules.

sudo apt-get install php7.0-fpm graphviz aspell php7.0-pspell php7.0-curl php7.0-gd php7.0-intl php7.0-mysql php7.0-xml php7.0-xmlrpc php7.0-ldap php7.0-zip php7.0-soap php7.0-mbstring

Next, open PHP configuration file below, umcomment the line below, just below:

;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;

Change the value to 0

sudo nano /etc/php/7.0/cli/php.ini

cgi.fix_pathinfo=0

You may have to do the same here and uncomment the line and change the value to 0

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

cgi.fix_pathinfo=0

Save both files and continue below.

Step 5: Downloading Moodle

Now that Nginx, MySQL and PHP are installed, continue below to download Moodle. You may need git package. Run the commands below to install it.

sudo apt-get install git

The change into the /html directory and download Moodle.

cd /var/www/html
sudo git clone git://git.moodle.org/moodle.git

Change into the moodle directory.. then run the commands below to see the latest moodle packages

cd moodle
sudo git branch -a
sudo git branch --track MOODLE_32_STABLE origin/MOODLE_32_STABLE

Now, after see the latest, run the commands below to check it out the current latest.

sudo git checkout MOODLE_32_STABLE

Moodle latest version should now be downloaded into the /var/www/html/moodle directory.

Next, run the commands below to create moodledata directory.

sudo mkdir -p /var/www/html/moodledata

Next, run the commands below to set good permissions on moodle folder

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

Continue below to configure Nginx site.

That last thing now is to create Moodle site configuration file and enable it. To do that, copy the default site config file to create a new moodle file. The commands below will do that.

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/moodle

Then run the commands below to open the newly created file.

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

Make the changes as shown in the code below.. then save the file.

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	root /var/www/html/moodle;

        rewrite ^/(.*\.php)(/)(.*)$ /$1?file=/$3 last;

	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ /index.php?q=$request_uri;
	}

	# pass PHP scripts to FastCGI server
	#
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
	
		# With php-fpm (or other unix sockets):
		fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
		# With php-cgi (or other tcp sockets):
                #fastcgi_pass 127.0.0.1:9000;
	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}

Next, run the commands below to enable the new site you just created.

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

Then disable the default site by running the commands below.

sudo rm /etc/nginx/sites-enabled/default

Also disable Apache2 server, since we’re using Nginx instead.

sudo systemctl disable apache2.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:

Apache2 301 Redirect Without htaccess File on Ubuntu 17.04