ScaleScaleScaleScale

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:

cgi.fix_pathinfo=1

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

service php-fpm restart

Get latest Moodle version

mkdir /root/temp
cd /root/temp
wget http://download.moodle.org/download.php/direct/moodle/moodle-latest.zip
unzip moodle-latest.zip
mkdir /var/www/yoursite.com/
mv moodle/* /var/www/yoursite.com/

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 yoursite.com

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

Then paste this inside:

### yourwebsite.com

server {
access_log logs/yourwebsite.com-access_log;
error_log logs/yourwebsite.com-error_log crit;

listen 80;
server_name yourwebsite.com www.yourwebsite.com;

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

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

# php parsing
location ~ .php$ {
root /var/www/yourwebsite.com;
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 yoursite.com 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 www.yoursite.com or your local IP address.

Popular search terms:

  • moodle nginx
  • moodle nginx conf
  • moodle nginx user
  • install moodle nginx
profile

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.

    Regards
    Abdul