Oct 04, 2019 4:00 AM Feb 07, 2020 3:40 AM

Configuring Nginx to support PHP on Debian/Ubuntu (nginx + php-fpm)

In this article I will explain how to configure the Nginx server to support PHP on the Debian/Ubuntu operating system. We will understand the structure of the configuration file, find out how Nginx and its modules work. PHP-FPM is a FastCGI (FCGI) process manager in PHP, commonly used for heavily loaded sites. This PHP integration method uses Nginx. PHP-FPM 7.4 is the latest stable version available for installation. You can also install PHP-FPM 7.3, PHP-FPM 7.2, PHP-FPM 7.1, PHP-FPM 7.0 with this instruction.

This instruction was executed on operating systems: Debian 10, Debian 9, Ubuntu 20.10, Ubuntu 20.04, Ubuntu 19.10.

Configuration file structure

Usually, the main configuration file nginx.conf is located in the /etc/nginx directory. In it, one can distinguish block and simple directives that relate to a specific Nginx module. Block directives can also be called context if other directives are inside curly braces. Let's look at the default configuration file (I deleted the commented parts):

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
}

http {
	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	gzip on;

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}

This file contains global settings (basic, SSL, logging, Gzip) that can be redefined for a specific virtual host, which are loaded at the very end:

The main configuration file nginx.conf includes all the configuration files and combines them into one, which is requested when the server starts. This file needs to be edited only if you need other global settings. For starters, you can leave it unchanged.

Virtual hosts

The configuration files that describe your domains should be located in the /etc/nginx/sites-available directory, and the /etc/nginx/sites-enabled directory should contain symbolic links to the active virtual hosts. Each configuration file inherits directives from the main configuration file nginx.conf and, if necessary, they can be redefined for a specific virtual host. Let's look at the default server configuration /etc/nginx/sites-available/default (I deleted the commented parts):

server {
	listen 80 default_server;
	listen [::]:80 default_server;

	root /var/www/html;
	index index.html index.htm index.nginx-debian.html;

	server_name _;

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

In this file, the listen directive sets the listening port 80 (HTTP) for IPv4 and IPv6 connections, the default_server parameter means the default server. The root directive sets the root directory for requests, i.e. directory where all the files of your sites will be located. By default, index.html, index.htm, index.nginx-debian.html files are used as the index, and the server name is not specified.

Installing PHP-FPM

Before any software installation, it is recommended to update the list of repository packages by running the command in the terminal:

sudo apt-get update

Install PHP-FPM by running the command in the terminal:

sudo apt-get install php7.4-fpm

or

sudo apt-get install php-fpm

You can learn more about how to install PHP 7.x on Debian and how to install PHP 7.x on Ubuntu.

Configuring Nginx to support PHP-FPM

Before setting up Nginx to work with the PHP handler, we find out its version by running the command in the terminal:

php -v

If PHP is not already installed, an error will be displayed:

bash: php: command not found

Edit the server configuration file /etc/nginx/sites-available/default, adding PHP-FPM support:

sudo nano /etc/nginx/sites-available/default
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	root /var/www/html;
	index index.php index.html index.htm index.nginx-debian.html;

	server_name _;

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

	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
		fastcgi_pass unix:/run/php/php7.4-fpm.sock; # PHP version (php -v command)
	}
}

Save the changes and close the file. Check the configuration for validity by running the command in the terminal:

sudo nginx -t

Now apply the configuration changes by running the command in the terminal:

sudo service nginx reload

Testing Nginx with PHP-FPM support

Let's verify that Nginx correctly transfers files with the .php extension to our PHP handler. Create a test file that will return information about our server by running the command in the terminal:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Let's make sure that the Nginx server correctly displays the content generated by the PHP script by opening this page in the browser:

http://192.168.0.101/info.php

or for localhost

http://localhost/info.php

If you correctly configured Nginx for PHP-FPM support, a similar page will be displayed:

Testing Nginx with PHP-FPM support

After checking for security reasons, you need to delete this file by running the command in the terminal:

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

Other articles