Installing and configuring Nginx + PHP-fpm in Debian

Installing and configuring Nginx + PHP-fpm in Debian

Read 9 minutes

Nginx is a web server that was released in 2004. This server is easy to use, but despite this, it handles a lot of connections perfectly. This feature is due to its internal architecture. Initially, it was created as a server running static content (so-called caching server). Unlike Apache, Nginx does not have a built-in interpreter to handle dynamic content requests.

This requires further interaction between the web server and the application. The PHP-FPM process manager (FastCGI) successfully solves this problem. The important point is that in working with PHP, the Nginx + PHP-FPM binder shows itself to be less productive than Apache + php_mod.

So if your server has high-performance requirements for PHP, it is better to look at the bundle apache + php_mod. Another important point is that Nginx requires more effort to configure than Apache.

This article deals with the installation and simple configuration of the Nginx server in conjunction with the PHP-FPM process manager in Debian 8. All operations are performed under the root user privileges.

First, you need to update:

apt-get update

Next, we install Nginx:

apt-get -y install nginx

After installation, we will test the server. To do this, just open the web browser address of our server. The following contents shall be open:

Installing and configuring Nginx + PHP-fpm in Debian

This is the standard plug-in page for Nginx. The next step is to install php5-fpm. It’s very simple:

apt-get -y install php5-fpm

Next, you need to do the setup. First, configure Nginx server. The configuration file is in the /etc/nginx/nginx.conf.

Open this file. We use the vi text editor, but you can use any other one that suits you.

vi  /etc/nginx/nginx.conf

Let’s understand a little bit about the structure of the file itself. The whole file is somewhat similar to the program code. It has directives. They are divided into two types:

  • Simple - a string with operators ending with the symbol «;». For example: pid /run/nginx.pid;
  • Block - a directive containing additional parameters placed in curly brackets {}.

Other directives can be set inside braces. In this case, it will be called context, and all directives that are in the configuration file but are not in any curly brackets are in the global context or the so-called main context.

Strings marked with the # symbol are comments and are not considered in the configuration.

As you can see in the file there is a directive:

include /etc/nginx/sites-enabled/*;

This directive says that this configuration file includes all configuration files from the /etc/nginx/sites-enabled/directory. This directory essentially contains symbolic links to the configuration of virtual hosts (essentially the sites served on the server).

Now we need to configure a virtual host that will be responsible for our simplest resource (let it be test.net). First, we’ll create a directory for him. Let’s make it a command:

mkdir -p /var/www/test.net/html

Now let’s create an index.html file in this directory with the following content:

<html>
   <head>
      <title>Hello World</title>
   </head>
   <body>
      <h>This is a test page.</h>
   </body>
</html>

Now we have the content of the site, let and one page of everything. For this site, you need to create a virtual host. Copy the default file for the virtual host:

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/test.net

Next, open it for editing. The listen directive has options for default_server. Only one block can be set with this value, which means that this block will serve queries for which nothing more suitable is found. Therefore the lines

listen 80 default_server;
listen [::]:80 default_server;

you must remove it from this file. Next, you must configure the root directory that contains the contents of our site. To do this, we write the root directive as follows:

root /var/www/test.net/html;

The following index directive defines the files to be used as index files. The order in which files are checked corresponds to the order in the configuration file. This directive is written as follows:

index index.html index.htm index.nginx-debian.html;

The following server_name directive specifies the server name. As values, we specify the domain name and alias when the domain name is specified with the www prefix.

server_name test.net www.test.net;

Next, you need only specify a location directive that sets the site configuration based on the URI name specified in the request. In our case, it will look simple:

location / {
   try_files $uri $uri/ =404;
}

This means that first files will be checked, then directories will be checked, and if nothing suitable will be found, Not Found (404) will be returned.

This is all with the configuration file. Its final content is as follows:

#cat /etc/nginx/sites-available/test.net
server {
     root /var/www/test.net/html;
     index index.html index.htm index.nginx-debian.html;
     server_name test.net www.test.net;
     location / {
           try_files $uri $uri/ =404;
     }
}

All other directives and their values can be read in the official Nginx documentation, which is here http://nginx.org/en/docs/.

Now we have the content of the site and its configuration file. It remains to include it. To do this, create a symbolic link:

ln -s /etc/nginx/sites-available/test.net /etc/nginx/sites-enabled/

Restart the server to make the changes effective:

/etc/init.d/nginx restart

Let’s check the work of the created resource.

Installing and configuring Nginx + PHP-fpm in Debian

To open a page, you may need to add a line to the hosts file on the computer you are checking from:

xxx.xxx.xxx.xxx test.net

Где xxx.xxx.xxx.xxx – это IP-адрес сервера с установленным Nginx. Файл hosts, находится:

  • C:\Windows\System32\drivers\etc (в Win7);
  • /etc/hosts (в Debian).

We have installed Nginx and php5-fpm as well as made the simplest site. The next step is to configure PHP and apply it with Nginx.

First, we will create a page with PHP code on our virtual host. Let’s make the /var/www/test.net/html/info.php file as follows:

<?php
phpinfo();
?>

Now open this page in the browser http://test.net/info.php

But what about the execution of PHP code? A PHP code we did not execute because the interpreter PHP is not configured in Nginx. Let’s do this.

Open our configuration file /etc/nginx/sites-available/test.net. Add the following block directive:

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

This directive describes the parameters for processing queries going to PHP files. I will not describe each parameter and its value. This information can be found in the official documentation here http://nginx.org/en/docs/dirindex.html

Now our configuration file is as follows:

server {
root /var/www/test.net/html;
index index.html index.htm index.nginx-debian.html;
server_name test.net www.test.net;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Save the changes to the file and restart our Nginx server.

/etc/init.d/nginx restart

Now let’s try again to open http://test.net/info.php. The result should be as follows:

Installing and configuring Nginx + PHP-fpm in Debian

I marked the information in the screenshot that indicates the use of the FPM manager.

In fact, the simplest configuration of Nginx + PHP-FPM server is complete. The simplest site is created and configured to work with PHP.

See the following article about configuring PHP-FPM.

GPUDedicUA1