Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Install Moodle On Ubuntu with Nginx/PHP-fpm: Difference between revisions

From MoodleDocs
No edit summary
No edit summary
Line 14: Line 14:


This is a starting guide to install Moodle with Nginx/PHP-fpm/Postgres on the  
This is a starting guide to install Moodle with Nginx/PHP-fpm/Postgres on the  
[http://releases.ubuntu.com/ latest Ubuntu LTS]. 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.).
[http://releases.ubuntu.com/ 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 ==
== Nginx 1.4.6 ==
<code bash>
<code bash>
sudo apt-get update
sudo apt-get update


sudo apt-get upgrade
sudo apt-get upgrade
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:nginx/stable
sudo apt-get update


sudo apt-get install nginx
sudo apt-get install nginx


sudo mkdir -p /home/nginx/localhost/{public,private,log,backup}
mkdir -p /home/nginx/localhost/public


sudo mkdir /etc/nginx/server.conf.d
sudo chgrp www-data /home/nginx/localhost/public
</code>
</code>


sudo vim /etc/nginx/server.conf.d/php.conf
<code bash>
# This code was found here : http://kbeezie.com/view/php-self-path-nginx/
# Thanks to KBeezie !!!
# It makes this configuration to be Moodle 2.x compliant ;)
fastcgi_intercept_errors on;
# this will allow Nginx to intercept 4xx/5xx error codes
# Nginx will only intercept if there are error page rules defined
# -- This is better placed in the http {} block as a default
# -- so that in the case of wordpress, you can turn it off specifically
# -- in that virtual host's server block
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# A handy function that became available in 0.7.31 that breaks down
# The path information based on the provided regex expression
# This is handy for requests such as file.php/some/paths/here/
fastcgi_param  PATH_INFO          $fastcgi_path_info;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
        fastcgi_param  QUERY_STRING      $query_string;
        fastcgi_param  REQUEST_METHOD    $request_method;
        fastcgi_param  CONTENT_TYPE      $content_type;
        fastcgi_param  CONTENT_LENGTH    $content_length;
        fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param  REQUEST_URI        $request_uri;
        fastcgi_param  DOCUMENT_URI      $document_uri;
        fastcgi_param  DOCUMENT_ROOT      $document_root;
        fastcgi_param  SERVER_PROTOCOL    $server_protocol;
        fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
        fastcgi_param  SERVER_SOFTWARE    nginx;
        fastcgi_param  REMOTE_ADDR        $remote_addr;
        fastcgi_param  REMOTE_PORT        $remote_port;
        fastcgi_param  SERVER_ADDR        $server_addr;
        fastcgi_param  SERVER_PORT        $server_port;
        fastcgi_param  SERVER_NAME        $server_name;
fastcgi_pass  127.0.0.1:9000; # this value depends on your configuration, can be unix:/var/run/php5-fpm.sock; for example
fastcgi_index  index.php;
}
</code>


sudo vim /etc/nginx/sites-available/default
sudo vim /etc/nginx/sites-available/default
<code bash>
<code bash>
server {
root /home/nginx/localhost/public;
        root /home/nginx/localhost/public;
index index.php index.html index.htm;
        index index.html index.htm index.php;
 
        server_name yourdomainname.com;
 
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
        }


        include /etc/nginx/server.conf.d/php.conf;
location / {
}
# First attempt to serve request as file
</code>
try_files $uri $uri/index.php;
}


== Postgres 9.0.3 ==
# 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


<code bash>
# With php5-fpm:
sudo apt-get install autoconf2.13 libssl-dev libcurl4-gnutls-dev libjpeg62-dev libpng12-dev  libmysql++-dev libfreetype6-dev libt1-dev libc-client-dev libevent-dev libxml2-dev libtool libmcrypt-dev
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
</code>
</code>


Note: also required to compiling PHP.
== Postgres 9.3.5 ==
<code bash>
cd /usr/local/src
 
sudo apt-get install build-essential libreadline6-dev zlib1g-dev
 
wget http://wwwmaster.postgresql.org/redir/333/h/source/v9.0.3/postgresql-9.0.3.tar.gz
tar zxvf postgresql-9.0.3.tar.gz 
 
cd postgresql-9.0.3
 
./configure
 
make
 
sudo make install


sudo vim /etc/profile
<code bash>
<code bash>
Add to the end of /etc/profile:
    sudo apt-get install postgresql
export PATH="/usr/local/pgsql/bin:${PATH}"
</code>
</code>


<code bash>
Create database user and database see [https://docs.moodle.org/en/PostgreSQL Moodle doc PostgreSQL]
source /etc/profile
 
sudo adduser postgres
 
sudo mkdir /usr/local/pgsql/data
 
sudo chown postgres /usr/local/pgsql/data
 
sudo su postgres
 
/usr/local/pgsql/bin/initdb-E UTF8 -D /usr/local/pgsql/data
 
/usr/local/pgsql/bin/pg_ctl start -D /usr/local/pgsql/data
 
exit
 
psql -U postgres
 
\password
 
createdb -E UTF8 -O postgres -T template0 -U postgres moodle
 
\q
</code>


== PHP 5.3.3 ==
== 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.
Note: if you use postgres do not take PHP 5.3.5, pg_set_client_encoding() will crash with a http 500 error.


<code bash>
<code bash>
sudo apt-get install libltdl-dev
    sudo apt-get install php5-fpm
 
    sudo apt-get install php5-pgsql
cd /usr/local/src
    sudo apt-get install php5-curl
 
    sudo apt-get install php5-gd
sudo wget -O php-5.3.3.tar.gz http://us.php.net/get/php-5.3.3.tar.gz/from/us.php.net/mirror/
    sudo apt-get install php5-xmlrpc
 
    sudo apt-get install php5-Intl
tar zxvf php-5.3.3.tar.gz
 
cd php-5.3.3
 
./configure --enable-fpm --with-gd --with-mcrypt --enable-mbstring --with-openssl --with-jpeg-dir=/usr/lib --enable-gd-native-ttf --with-libxml-dir=/usr/lib --with-curl --enable-zip  --enable-sockets --with-zlib --enable-exif --enable-ftp --with-iconv --with-gettext --enable-gd-native-ttf --with-t1lib=/usr --with-freetype-dir=/usr --prefix=/usr/local/php --with-fpm-user=www-data --with-fpm-group=www-data --with-pgsql=/usr/local/pgsql --enable-tokenizer --with-xmlrpc --enable-soap --enable-ctype
</code>
 
Note: this is to support postgres. TODO: intl extension is not enabled.
<code bash>
sudo make
 
sudo make install
 
sudo cp php.ini-production /usr/local/php/lib/php.ini
 
sudo chmod 644 /usr/local/php/lib/php.ini
 
sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
 
sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
 
sudo chmod 755 /etc/init.d/php-fpm
 
sudo update-rc.d -f php-fpm defaults
 
sudo touch /var/run/php-fpm.pid
 
sudo vim /etc/init.d/php-fpm
</code>
 
Change location of pid (remove ${prefix}) to:
<code bash>
php_fpm_PID=/var/run/php-fpm.pid
</code>
 
<code bash>
sudo vim /usr/local/php/etc/php-fpm.conf
</code>
 
Changes these values
<code bash>
--------------------------------
pid = /var/run/php-fpm.pid
error_log = /var/log/php-fpm.log
--------------------------------
</code>
 
Uncomment the lines for:
<code bash>
-------------------------
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
</code>
<code bash>
sudo service php-fpm start
 
sudo /etc/init.d/nginx restart


vim /home/nginx/localhost/public/index.php
    sudo service nginx restart
    sudo service php5-fpm restart
</code>
</code>


Enter in:
vim /home/nginx/localhost/public/info.php
 
<code bash>
<code bash>
<?php
<?php
Line 237: Line 83:
</code>
</code>


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


== APC ==
== APC ==

Revision as of 11:49, 2 October 2014

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