Set up NGINX with PHP-FPM

Getting Started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) with a clean installation of Ubuntu 16.04.

Ubuntu 16.04 LTS is a long-term support release, meaning Canonical will provide updates for 5 years rather than for only 9 months.

Notice: Every command should be run as root user

Tutorial

Let’s get started. When finished, you’ll have a server platform capable of efficiently running any number of PHP and MySQL web apps.

Let’s begin by applying all the latest updates. Not only will this improve security, but it will also avoid potential package download failures in later steps.

sudo apt update && apt upgrade -y

reboot

Now we’ll install the MySQL database, where the web application will keep all of its data.

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

You’ll be asked to set a root password. Make this password a strong one and remember it for later. Anyone with this password can access and modify all of your database content.

A few additional steps are needed to further secure the database server. Let’s take care of those now.

sudo mysql_secure_installation

It is now time to install the Nginx web server. Let’s install the package.

sudo apt install nginx -y

The Nginx daemon must now be started so it will serve files.

systemctl start nginx.service
systemctl enable nginx.service

While Apache runs PHP scripts directly, Nginx must connect to an external FPM daemon to interpret scripts. That daemon must be installed before it can be used. Let’s install the necessary packages.

sudo apt -y install php7.0-fpm php7.0-mysql

Now that the daemon is installed and running, Nginx virtual hosts must be configured to know how to access it, and which paths and files should be run through the FPM interpreter.

sudo nano /etc/nginx/sites-available/default
[...]
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name_,
[...]
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# # With php7.0-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
[...]

Save the virtual host configuration, and restart Nginx to take advantage of the changes.

systemctl restart nginx.service
systemctl enable nginx.service

The php.ini file is where PHP and its modules are configured. It is necessary to change one line in order to successfully run scripts in the FPM daemon.

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

Find this block in the configuration file:

; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
; cgi.fix_pathinfo=1

Change the last line to this:

; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0

uncomment to following line:

extension=php_xmlrpc.dll

Save and exit. As with Nginx, it is now necessary to restart and enable the FPM daemon so the configuration changes take effect.

sudo apt install php7.0-xml
sudo apt install php7.0-sqlite3
systemctl restart php7.0-fpm.service
systemctl enable php7.0-fpm.service

Finally, we’ll create a simple phpinfo page that provides diagnostic information on the installation. This page will later be replaced with a real application.
Enter the document root of the Nginx virtual host.

cd /var/www/html/

Create the file index.php

sudo nano index.php

Now paste the following code:

<?php
phpinfo();
?>

Finally, visit the page you just created by visiting http://your_main_ip. You should see lots of diagnostic details about your PHP installation.