Install Moodle On Ubuntu with Nginx/PHP-fpm

Jump to: navigation, search


from lighttpd/php-fpm

Moving from a fully-functional lighttpd/php-fpm setup to nginx/php-fpm is fairly easy. The only trick is to pass the right values as fastcgi parameters.

If your current setup requires cgi.fix_pathinfo=1 you should set the SCRIPT_FILENAME parameter in nginx as follows:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name$fastcgi_path_info;

Without the above you'll probably have some issues with missing images and css styling.

From scratch

This is a starting guide to install Moodle with Nginx/PHP-fpm/Postgres on the latest Ubuntu LTS 14.04. It is important to take note that Moodle is heavily tested in an Apache2 environment, not in a nginx environment. This document is for people having previously installed a Moodle site and a Ubuntu distribution, and having some basic knowledge of Linux command lines (vim, linux permissions, compiling, etc.).

Nginx 1.4.6

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install nginx
mkdir -p /home/nginx/localhost/public
sudo chgrp www-data /home/nginx/localhost/public

sudo vim /etc/nginx/sites-available/default

server {
listen 80;
root /home/nginx/localhost/public;
index index.php index.html index.htm;
location / {
# First attempt to serve request as file
try_files $uri $uri/index.php;
# pass the PHP scripts to FastCGI server listening on
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;

Postgres 9.3.5

sudo apt-get install postgresql

Create database user and database see Moodle doc PostgreSQL

PHP 5.5.9

Note: if you use postgres do not take PHP 5.3.5, pg_set_client_encoding() will crash with a http 500 error.

sudo apt-get install php5-fpm
sudo apt-get install php5-pgsql
sudo apt-get install php5-curl
sudo apt-get install php5-gd
sudo apt-get install php5-xmlrpc
sudo apt-get install php5-Intl
sudo service nginx restart
sudo service php5-fpm restart

vim /home/nginx/localhost/public/info.php


Check if the site is available. localhost/info.php

If you get a blank page... check if the following line is in your /etc/nginx/fastcgi_params file:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

and if not then add it. (credit: serverfault)

PHP 5.5 comes with OPCache. on older versions you might want to use APC:

APC for PHP < 5.5

With APC, Moodle will be a lot faster.

sudo apt-get install php-apc
sudo vim /etc/php5/fpm/php.ini

Add (if not already existing):


Install Moodle into /home/nginx/localhost/public/. Note that it is recommended to install Git and to get the Moodle files with Git.

Enabling Slash Arguments on Nginx by adding the following line to the "server" section of the Nginx config file:

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

More info stackoverflow


In the Moodle administration, disable 'slash arguments' (http://YOURMOODLESITE/admin/search.php?query=slashargument). Without disabling the 'slash arguments', you may notice that the admin setup page is missing the images and css styling. However, if you turn off slash arguments, then other things won't work, so really someone ought to work out what is wrong here and fix it.

If you cannot access the Admin interface, you can edit the database using a tool like phpMyAdmin.

1. Go to the table labeled 'mdl_config'

2. Browse to line 281 (line 309 in 2.5), and change the value of 'slasharguments' from 1 to 0

3. Use Control-F5 to fully refresh the setup page.

Workaround if the above steps don't work:

1. Empty the database and try the command line installation.

cd /var/www/YOURMOODLESITE/public/moodle
php /admin/cli/install.php

Enabling PHP errors in php.ini will help you diagnose any errors that may arise.

Use if you ran the cli install.php as root: chown www-data:www-data config.php

2. Go to your Moodle site and log in. It will ask you to fill in your email address, City and Country. Save the changes.

3. Go to http://YOURMOODLESITE/admin/search.php?query=slashargument and uncheck "Use slash arguments"

Note: some version of Internet Explorer will display the page CSS with slash arguments on, while Firefox, Chrome and IE10 will not.

More to do

There are still a lot more to do (setting up your domain name, establishing a maintenance plan, performance tweaking, security testing...). Be patient and have fun.

Related document