Install Moodle On Ubuntu with Nginx/PHP-fpm

Jump to: navigation, search

Migrating

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

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 127.0.0.1:9000
#
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

<?php
phpinfo();
?>

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

APC

With APC, Moodle will be a lot faster.

sudo /usr/local/php/bin/pecl config-set php_ini /usr/local/php/lib/php.ini
 
sudo /usr/local/php/bin/pecl install apc
 
sudo vim /usr/local/php/lib/php.ini

Add (if not already existing): extension=apc.so

Moodle

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

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