Skip to content

Moodle Setup with MariaDB 10.2, PHP 7.1.x, Nginx

Step -1: Setting the Locale

$ locale -a
$ /etc/default/locale
$ sudo apt-get install language-pack-en
$ sudo locale-gen en_US.UTF-8

sudo vi /etc/environment
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8

Step -2: Update packages

$ sudo apt-get update
$ sudo apt-get upgrade

Step -3: Install MariaDB

$ sudo apt-get install software-properties-common
$ sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
$ sudo add-apt-repository 'deb [arch=amd64,i386,ppc64el] http://mirror.fibergrid.in/mariadb/repo/10.2/ubuntu xenial main'
$ sudo apt update
$ sudo apt install mariadb-server
$ sudo service mariadb start
$ mysql_secure_installation

Step -4: (Optional) Install PhpMyAdmin

$ sudo apt-get install phpmyadmin

Step -5: Install PHP 7.1.x from custom repository

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update

$ sudo apt-get install -y graphviz aspell php7.1-fpm php7.1-cli php7.1-pspell php7.1-curl php7.1-gd php7.1-intl php7.1-mysql php7.1-xml php7.1-xmlrpc php7.1-ldap php7.1-zip php7.1-json php7.1-opcache php7.1-readline php7.1-mbstring php7.1-soap

Step -6: PHP Custom configuration

Note: It is a good practise to run the web-server, php-fpm and set the ownership of document_root to the same user.

$ cd /etc/php/7.1/fpm
$ cp php.ini php.ini.default
$ cd /etc/php/7.1/fpm/pool.d
$ cp www.conf www.conf.default

Step -7: Install Nginx

$ sudo apt-get install nginx

Step -8: Configure Nginx

# Author : Shashi Yebbare
# nginx.conf file [/etc/nginx]

user shashi;
worker_processes auto;
pid /run/nginx.pid;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;
	gzip_disable "msie6";

	gzip_vary on;
	gzip_proxied any;
	gzip_comp_level 6;
	gzip_buffers 16 8k;
	gzip_http_version 1.1;
	gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}

Step -9: Nginx Server block to process the application

# HTTP Configuration
server {
	listen 80;
	server_name moodle-32.skydevops.co.in;
	root /var/www/html/moodle/;
	try_files $uri $uri/ /index.php?$args;
	index index.php index.html;
	rewrite ^/(.*.php)(/)(.*)$ /$1?file=/$3 last;

	location ~ ..*/.*.php$ {
		return 403;
	}

	# Block access to "hidden" files and directories
	location ~ (^|/). {
		return 403;
	}

	location ^~ / {
		try_files $uri $uri/ /index.php?q=$request_uri;
		index index.php index.html index.htm;
		location ~ .php$ {
			try_files $uri =404;
			fastcgi_split_path_info ^(.+.php)(/.+)$;
			fastcgi_pass 127.0.0.1:9000;
			fastcgi_index index.php;
			fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
			include fastcgi_params;
		}
	}

	location /phpgui {
		alias /var/www/html/moodle/phpgui;
		# Optionally set separate access and error logs for phpgui
		access_log /var/log/nginx/phpgui_access.log;
		error_log /var/log/nginx/phpgui_error.log;
		index index.php index.html;
		try_files $uri $uri/ /index.php?$args;
		# Deny some static files
		location ~ ^/phpgui/(README|LICENSE|ChangeLog|DCO)$ {
			deny all;
		}
		# Deny .md files
		location ~ ^/phpgui/(.+.md)$ {
			deny all;
		}
		# Deny some directories
		location ~ ^/phpgui/(doc|sql|setup)/ {
			deny all;
		}
	}
}

Step -10: Application Specific Settings – Moodle – Opcache Settings

# PHP INI configuration
[opcache]
opcache.enable = 1
opcache.memory_consumption = 64
opcache.max_accelerated_files = 8000
opcache.revalidate_freq = 60

; Required for Moodle
opcache.use_cwd = 1
opcache.validate_timestamps = 1
opcache.save_comments = 1
opcache.enable_file_override = 0

Shashi View All

A passionate devops and automation engineer

Leave a comment