1 Preliminary Note

In this tutorial, I will use the hostname server1.example.com with the IP address 192.168.1.100. These settings might differ for you, so you have to replace them where appropriate.

2 Installing MariaDB

I will install MariaDB instead od MySQL, MariaDB is a MySQL fork maintained by the original MySQL founder Monty Widenius which has some speed and feature improvements in comparison to MySQL. In order to install MariaDB, run:

sudo apt-get -y install mariadb-server mariadb-client

MariaDB has currently no password set for the root user. Run the mysql_secure_installation command to configure a password and remove the test database.

Type in your web server's IP address or hostname into a browser (e.g. http://192.168.1.100), and you should see the following page:

Why does it show "Apache2" instead of Nginx in that page? The text of the page does not reflect the actual running web server, the above page is just the default page on Ubuntu in the document root /var/www/html/ that is used by Apache and Nginx. To verify that Nginx is actually serving this page, you can either take look at the HTTP headers in the Firefox Network analysis console:

Or you check on the shell that nginx is running with:

ps aux | grep nginx

The result should show the nginx processes like this:

4 Installing PHP5

We can make PHP5 work in Nginx through PHP-FPM. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites) which we install as follows:

sudo apt-get install php5-fpm

PHP-FPM is a daemon process (with the init script php5-fpm) that runs a FastCGI server on the socket /var/run/php5-fpm.sock.

5 Configuring Nginx

The Nginx configuration is in the file /etc/nginx/nginx.conf which we open now in the nano editor:

First (this is optional) set the keepalive_timeout to a reasonable value:

[...]
keepalive_timeout 2;
[...]

The virtual hosts are defined in server {} containers. The default vhost is defined in the file /etc/nginx/sites-available/default - let's modify it as follows:

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

### You should look at the following URL's in order to grasp a solid understanding# of Nginx configuration files in order to fully unleash the power of Nginx.# http://wiki.nginx.org/Pitfalls# http://wiki.nginx.org/QuickStart# http://wiki.nginx.org/Configuration## Generally, you will want to move this file somewhere, and start with a clean# file but keep this around for reference. Or just disable in sites-enabled.## Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.##

[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]

Reload PHP-FPM:

sudo service php5-fpm reload

Now create the following PHP file in the document root /var/www/html:

sudo nano /var/www/html/info.php

<?php
phpinfo();
?>

Now we call that file in a browser (e.g. http://192.168.1.100/info.php):

As you see, PHP5 is working, and it's working through FPM/FastCGI, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL is not listed there which means we don't have MySQL support in PHP5 yet.

6 Getting MariaDB / MySQL Support in PHP

To get MySQL support in PHP, we can install the php5-mysqlnd package. It's a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:

APCu is a free PHP opcode cacher for caching and optimizing PHP intermediate code. It's similar to other PHP opcode cachers, such as eAccelerator and Xcache. It is strongly recommended to have one of these installed to speed up your PHP page.

APC can be installed as follows:

sudo apt-get install php5-apcu

Now reload PHP-FPM:

sudo service php5-fpm reload

Now reload http://192.168.1.100/info.php in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:

7 Making PHP-FPM use a TCP Connection

By default PHP-FPM is listening on the socket /var/run/php5-fpm.sock. It is also possible to make PHP-FPM use a TCP connection. To do this, open /etc/php5/fpm/pool.d/www.conf...

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

... and make the listen line look as follows:

[...]
;listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000
[...]

This will make PHP-FPM listen on port 9000 on the IP 127.0.0.1 (localhost). Make sure you use a port that is not in use on your system.

Then reload PHP-FPM:

sudo php5-fpm reload

Next go through your nginx configuration and all your vhosts and change the line fastcgi_pass unix:/var/run/php5-fpm.sock; to fastcgi_pass 127.0.0.1:9000;, e.g. like this: