Tips / Nginx

Nginx Moodle Configuration

Moodle is a free software built to help educators to create online courses and help teachers to interact with students. It’s one of the most popular e-learning platforms around the world. And of course, it runs pretty well on Nginx webserver. So, today I’m going to show you how to get the latest version of Moodle working on Nginx.

Install Nginx

Install the php modules

yum install php php-fpm php-gd php-mbstring php-mcrypt php-mysql

Configure PHP-FPM to use sockets

nano -w /etc/php-fpm.d/www.conf

find the varible “listen” and set it to:

listen = /tmp/php5-fpm.sock

Configure cgi.fix_pathinfo to pass the right values as fastcgi parameters

nano -w /etc/php.ini

Make sure this variable looks like you see blow:


Save the file. Restart php-fpm to apply changes:

service php-fpm restart

Get latest Moodle version

mkdir /root/temp
cd /root/temp
mkdir /var/www/
mv moodle/* /var/www/

Nginx Moodle configuration just requires a little hack into the rewrite rules, which is pretty simple in fact:

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

So, now that you have nginx configured properly, let’s create the virtual host for

nano -w /etc/nginx/conf.d/

Then paste this inside:


server {
access_log logs/;
error_log logs/ crit;

listen 80;

location / {
root /var/www/;
index index.php index.html index.htm;

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

# php parsing
location ~ .php$ {
root /var/www/;
try_files $uri =404;
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;



As you see there you have the full working nginx configuration for moodle, it already contains the required rewrite rule and the php-fpm configuration for php interpretation. Of course, replace and the real document root you are using on all the configuration files. Once done, make sure to restart Nginx to apply changes:

service nginx restart

Now your Moodle website should be available from or your local IP address.

Popular search terms:

  • moodle nginx
  • nginx moodle
  • moodle nginx conf
  • moodle nginx user

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Abdul Kadir

    I have installed nginx on Ubuntu, want to standard configuration in nginx to serve all subfolder from root directory. having index.php to switch url.