Skip to content

Instantly share code, notes, and snippets.

@rcotrina94
Last active May 6, 2021 09:48
Show Gist options
  • Save rcotrina94/d7a9c97a643e096794f1 to your computer and use it in GitHub Desktop.
Save rcotrina94/d7a9c97a643e096794f1 to your computer and use it in GitHub Desktop.
Install Moodle on NGINX

Installing Moodle on NGINX (Moodle - LEMP Stack)

Based on this links:

Downloading Moodle v.2.8.10

$ cd /usr/share/nginx/html/
# git clone --depth=1 -b MOODLE_28_STABLE git://git.moodle.org/moodle.git 

Installing NGINX

Installing MySQL

Installing PHP5 and Moodle extensions dependences

(MySQL Driver and php5-gd)

sudo apt-get install php5-fpm php5-mysql php5-dg php5-curl

Configuring PHP

Edit:

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

Replace ; cgi.fix_pathinfo=1 for cgi.fix_pathinfo=0

sudo nano /etc/php5/fpm/pool.d/www.conf

Replace listen = 127.0.0.1:9000 for listen = /var/run/php5-fpm.sock

Restart PHP-fpm

sudo service php5-fpm restart

Configuring NGINX

sudo nano /etc/nginx/sites-available/default
  • Add index.php at index line in server {}.
  • Pass the PHP scripts to FastCGI server listening on the php-fpm socket
  • Add rewrite ^/moodle/(.*\.php)(/)(.*)$ /moodle/$1?file=/$3 last; inside server.

Example:

server {
  listen   80;
  root /usr/share/nginx/www;
  rewrite ^/moodle/(.*\.php)(/)(.*)$ /moodle/$1?file=/$3 last;
  index index.php index.html index.htm;
  
  # pass the PHP scripts to FastCGI server listening on the php-fpm socket
  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;    
  }
...
}

Test configuration

Create file in public folder.

sudo nano /usr/share/nginx/www/info.php

Add in the following lines:

<?php
  phpinfo();
?>

Configuring MySQL

Run these commands with your name and key.

mysql> CREATE DATABASE moodle DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,CREATE TEMPORARY TABLES,DROP,INDEX,ALTER ON moodle.* TO <USER>@localhost IDENTIFIED BY '<PASSWORD>';

Install Moodle

Go to http://localhost/moodle and setup Moodle as usual.

@vladyslav2
Copy link

Hey i just changed
rewrite ^/moodle/(..php)(/)(.)$ /moodle/$1?file=/$3 last; to
rewrite ^(..php)(/)(.)$ $1?file=/$3 last;

and it finally works!
Thank you

PS I have no idea why they cannot add this config to the official docs

@scara
Copy link

scara commented Feb 11, 2018

Hi @vladyslav2,
the official Moodle docs tells about using PATH_INFO directly and not in the old way via the rewrite rule to present the 'slasharguments' using the HTTP GET file param: https://docs.moodle.org/34/en/Nginx#Nginx .

HTH,
Matteo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment