Set up Nginx with PHP-FPM on Ubuntu 16.04 and 18.04

Posted on July 30th, 2019

In this informative piece of content, I am going to show you how to set up Nginx with PHP FPM on your Ubuntu 16.04 or Ubuntu 18.04 server. We will cover both the operating systems in a single guide. It is because the method to set up this stack is the same on both operating systems.

Nginx with PHP-FPM on Ubuntu server is such a powerful stack. If you are going to host your WordPress website on the server we are just going to configure, you will love it. The stack we are going to configure is a LEMP stack on steroids. With the proper caching system in place, this set up will handle a lot more traffic than you can expect, especially in the case of blogs.

We will discuss a bit about Virtual hosts in this guide. If you do not know anything about the virtual hosts, I recommend you to take a look at our guide on Nginx Virtual hosts. So, How our stack will work? It’s simple. The Nginx server will deal with the requests, and it will pass all the .php files required to serve the request to PHP processor.

The PHP processor we are going to use here is FPM. FPM stands for fast process manager. It will process PHP files that our web server requires to handle requests, and will return the rendered output. Our web server, Nginx, will then return the fully rendered page to the user’s browser.


There are some prerequisites that you have to fulfill to follow this guide. The list is not so big, you just need a clean Ubuntu server. And you also need root access to the server. It’s okay if you don’t have access to the server as an actual root user, but you must have root privileges on the server.

We need root or sudo privileges because we have to install new packages/software on the server. And we also have to edit some configuration files that only a user with sudo privileges can edit. So, If you are clear on prerequisites, continue following the guide.

The first task we have to complete is to install Nginx on our server. So, We will get started by installing Nginx on Ubuntu server. Let’s start setting up our stack.

Install Nginx on Ubuntu

It’s the simplest task we are going to perform in this guide. It is because Nginx is very easy to install on Ubuntu. We just have to run a few commands and that’s all. We don’t even have to edit any configuration file at this point.

So, execute the following set of commands to install Nginx on your server.

$ sudo apt-get update
$ sudo apt-get install nginx -y

It might take about 30-40 seconds depending on the CPU cores available on your server. Once done, verify the installation by visiting the server’s public IP address in the browser. You should see a page just like the following image.

Nginx default page

If you can see a similar page, you have completed the first task. Now, It’s time to install PHP on our server. Installing PHP is easy too. It might require you to run multiple commands and add repositories, but no manual configuration is needed.

Install PHP on Ubuntu

As I said, Installing PHP is easy on Ubuntu. However, there are many PHP version available to use. If your application requires a specific PHP version to work smoothly, you will have to install that specific PHP version on your server. It is easy too, you just have to replace the PHP version number in the following commands.

In this demonstration, we will install PHP 7.2 on our server. If you are using PHP version 5.6 or lower on your application, I recommend you to upgrade your code to support PHP 7.0 or higher. Once you are ready, execute the following commands to install PHP 7.2 on your Ubuntu server.

$ sudo apt-get install software-properties-common -y
$ sudo add-apt-repository ppa:ondrej/php -y
$ sudo apt-get update
$ sudo apt-get install php7.2 php7.2-fpm php7.2-mbstring php7.2-mysql php7.2-curl -y
$ sudo service php7.2-fpm restart

If you want to install any other PHP version than 7.2, replace 7.2 with the version you want to install on your server. Installing PHP will take some time. Once it is installed, verify the installation by executing the following command on your server.

$ php -v

The command will return the PHP version installed on your server. It will also show some extra information about the PHP installation. The output should look like the following.

Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache, Copyright (c) 1999-2018, by Zend Technologies

If you can see the output just like the above-given example, PHP is successfully installed on the server.

Configure Nginx with PHP-FPM

Right now, Nginx server and PHP can operate individually on the server. But our goal is to set up Nginx to use PHP for processing PHP files. It’s easy too! It does not require us to install anything extra on our server. But we have to edit our virtual host files to tell Nginx to pass .php files to FPM for processing.

In this demonstration, we will edit our default Nginx file. However, you can edit the virtual host files on your server as per your requirements. To open the default Nginx virtual host file in edit mode, execute the following command.

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

In the configuration file, find a line containing index directive. By default, it should look like this.

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

Add index.php before index.html and after the index directive. The updated line should look like this.

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

Now, scroll down a bit and you will find a section dedicated to PHP. By default, that section looks like this.

# pass the PHP scripts to FastCGI server listening on
#location ~ \.php$ {
#	include snippets/fastcgi-php.conf;
#	# With php7.0-cgi alone:
#	# fastcgi_pass;
#	# With php7.0-fpm:
#	#fastcgi_pass unix:/run/php/php7.2-fpm.sock;

As you can see, right now, the whole section is commented out. We have to uncomment some of the lines in this section. To give you an idea on which lines to uncomment, see how it should look when it is updated to pass PHP files to FPM.

# pass the PHP scripts to FastCGI server listening on
location ~ \.php$ {
	include snippets/fastcgi-php.conf;

	# With php7.0-cgi alone:
	# fastcgi_pass;
	# With php7.0-fpm:
	fastcgi_pass unix:/run/php/php7.2-fpm.sock;

Carefully uncomment the lines to make sure that the location block is complete and you are sending requests to PHP socket. Unix sockets are fast. That’s why we are using PHP-FPM socket to pass requests to FPM. Once you have updated the configuration file, press CTRL+X followed by Y followed by the Enter key to save the configuration.

The last step to perform in this task is to restart the Nginx server. We have to restart the Nginx server to apply the changes. Execute the following command to restart the Nginx server.

$ sudo service nginx restart

Once done, Our Nginx server and PHP can work together. If you faced any issue restarting Nginx server, It’s probably because of some error in the configuration file that you have edited. Make sure that everything is correctly edited and try restarting the server again.

Our main task, to configure Nginx with PHP-FPM is complete. Now, it’s time to test our configuration.

Test the configuration

If everything we did is correct, our PHP files should be interpreted by the server before returning the output to the browser. And that’s how we are going to test the configuration.

Execute the following command to create a test PHP file inside the document root of our default Nginx site.

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

Now, in your browser, try to access this test file. You can access it using the following URL template.


If you can see a page with all the information about your PHP installation, Congratulations! You have successfully configured Nginx with PHP-FPM on your Ubuntu server. The page should look like the following image.

PHP installation verification

If you can see a similar page, your task is complete! You have successfully configured Nginx with PHP FPM on your server.

Now, you can install MySQL or other Databases on your Ubuntu server as per your requirements.


Conclusion: So, this is how you can set up Nginx with PHP-FPM on your Ubuntu 16.04 or Ubuntu 18.04 server. This is one of the best stacks you can set up to host your PHP applications. It is especially easy on Ubuntu as it comes with almost every software we need to install on our server. If you want to set up Apache with PHP-FPM, you can follow our guide.

Let us know if you need help setting up this stack on your server. You can either use the comment section given below or contact our support staff, we are happy to help!

Leave a Reply