Real Clear Computing

How to Install Nginx PHP-FPM on Ubuntu Server

In this tutorial, we will explain how to configure a Ubuntu Server using Nginx, PHP, and MySQL to act as a web server.

Ubuntu Software repository supports the Nginx web server, which can easily be installed using the apt package manager.

In order to reduce errors and server administrative tasks, it's recommended you use the Nginx package, which comes with the Ubuntu packaging system (APT).

To check whether the Nginx is already installed, run the following command:

apt list --installed | grep nginx

If you already have an older version of Nginx installed, read the following guide: How to Uninstall Nginx on Ubuntu Server.

Steps to Install Nginx on Ubuntu Server 20.04 LTS

First, we will need to update the package index by running the following command:

apt update

After Updating the package index, install Nginx:

apt install nginx

To check the status of the Nginx service, enter the following command at the command prompt:

systemctl status nginx.service

By default, Ubuntu will immediately start and enable the Nginx service as soon as its package is installed. You can manually start and enable the service by running the following two commands:

systemctl start nginx.service
systemctl enable nginx.service

To confirm the web server is up and running, open up a web browser and enter the IP Address of your Ubuntu Server in the address bar.

install nginx ubuntu
Default Nginx page

Understanding NGINX's Configuration File

Everything related to the configuration of your Nginx server is in the /etc/nginx directory. The main configuration file is nginx.conf, which looks like this (once comments are removed):

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 TLSv1.3; # 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/*;
}

The modules-enabled, conf.d, and sites-enabled directories contain files that are included in the main configuration.

The default document root directory is located at /var/www/html/. The virtual host configuration file for the default site exists in /etc/nginx/sites-available directory and a symbolic link to it is already existing in /etc/nginx/sites-enabled directory.

Understanding NGINX's Configuration File

At this point, you can deploy your website in the /var/www/html/ directory and Nginx will serve the content.

Installing PHP-FPM

Hosting a dynamic Website with Nginx is not possible without a server-side scripting language. We are going to use PHP because it is the most popular web-based programming language.

In Ubuntu, install php-fpm as follows:

apt install php-fpm

After installing the php-fpm package, We will configure the default site to work with PHP.

Open the /etc/nginx/sites-available/default and edit the file so that it looks like the following:

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

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}

	location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php-fpm.sock;
        }

}

After changing the configuration, restart Nginx with the following command:

systemctl restart nginx

Next, create an index.php file at the DocumentRoot and insert the phpinfo() function:

echo "<?php phpinfo(); ?>" > /var/www/html/index.php

Access the index.php via your web browser, and it will display information about your PHP installation.

Install PHP-FPM on Ubuntu Server

LEMP Stack (Linux, eNginx, MySQL, and PHP)

Now that we have installed Nginx and PHP programming language, you can install MySQL Server to complete the LEMP (Linux, eNginx, MySQL, and PHP) stack on Ubuntu.

apt install mysql-server

In the next tutorial, we will learn how to create an Nginx VirtualHost in Ubuntu Linux. After that, you can also read our WordPress installation guide if you are ready to host your WordPress blog on Nginx Web Server.