Installing PHP for Nginx on Ubuntu 18.04

Installing PHP for Nginx on Ubuntu 18.04

Last updated on | 12 replies

In this guide we will install and configure PHP to work with Nginx on Ubuntu 18.04 LTS (Bionic Beaver). Unlike Apache, Nginx does not contain native PHP processing. For that we have to install PHP-FPM (FastCGI Process Manager). FPM is an alternative PHP FastCGI implementation with some additional features useful for heavy-loaded sites.

1. Install PHP-FPM

Let’s begin by updating the package lists and installing PHP-FPM on Ubuntu 18.04. Below we have two commands separated by &&. The first command will update the package lists to ensure you get the latest version and dependencies for PHP-FPM. The second command will then download and install PHP-FPM. Press y and ENTER when asked to continue.

sudo apt update && sudo apt install php-fpm

Once installed, check the PHP version.

php --version

If PHP was installed correctly, you should see something similar to below.

PHP 7.2.3-1ubuntu1 (cli) (built: Mar 14 2018 22:03:58) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.3-1ubuntu1, Copyright (c) 1999-2018, by Zend Technologies

Above we are using PHP version 7.2, though this may be a later version for you.

Depending on what version of Nginx and PHP you install, you may need to manually configure the location of the PHP socket that Nginx will connect to.

List the contents for the directory /var/run/php/

ls /var/run/php/

You should see a few entries here.

php7.2-fpm.pid php7.2-fpm.sock

Above we can see the socket is called php7.2-fpm.sock. Remember this as you may need it for Step 3.

Note: If you want to use PHP in conjunction with MySQL, you should install and php-mysql

sudo apt update && sudo apt install php-mysql

2. Configure Nginx

We now need to make some changes to our Nginx server block.

The location of the server block may vary depending on your setup. By default, it is located in /etc/nginx/sites-available/default.

However, if you have previously set up custom server blocks for multiple domains in one of our previous guides, you will need to add the PHP directives to each server block separately. A typical custom server block file location would be /etc/nginx/sites-available/mytest1.com.

For the moment, we will assume you are using the default. Edit the file in nano.

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

Press CTRL + W and search for index.html.

Now add index.php before index.html

/etc/nginx/sites-available/default
        index index.php index.html index.htm index.nginx-debian.html;

Press CTRL + W and search for the line server_name.

Enter your server’s IP here or domain name if you have one.

/etc/nginx/sites-available/default
        server_name YOUR_DOMAIN_OR_IP_HERE;

Press CTRL + W and search for the line location ~ \.php.

You will need to uncomment some lines here by removing the # signs before the lines marked in red below.

Also ensure value for fastcgi_pass socket path is correct. For example, if you installed PHP version 7.2, the socket should be: /var/run/php/php7.2-fpm.sock

If you are unsure which socket to use here, exit out of nano and run ls /var/run/php/

/etc/nginx/sites-available/default
...
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }
...

Once you’ve made the necessary changes, save and close (Press CTRL + X, then press y and ENTER to confirm save)

Now check the config file to make sure there are no syntax errors. Any errors could crash the web server on restart.

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

If no errors, you can reload the Nginx config.

sudo service nginx reload

3. Test PHP

To see if PHP is working correctly on Ubuntu 18.04, let’s a create a new PHP file called info.php in the document root directory. By default, this is located in /var/www/html/, or if you set up multiple domains in a previous guide, it may be located in somewhere like /var/www/mytest1.com/public_html

Once you have the correct document root directory, use the nano text editor to create a new file info.php

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

Type or paste the following code into the new file. (if you’re using PuTTY for Windows, right-click to paste)

/var/www/html/info.php
<?php
phpinfo();

Save and close (Press CTRL + X, then press y and ENTER to confirm save)

You can now view this page in your web browser by visiting your server’s domain name or public IP address followed by /info.php: http://your_domain_or_IP/info.php

phpinfo() outputs a large amount of information about the current state of PHP. This includes information about PHP compilation options and extensions, the PHP version and server information.

PHP info test page on Ubuntu 18.04

You have now successfully installed PHP-FPM for Nginx on Ubuntu 18.04 LTS (Bionic Beaver).

Make sure to delete info.php as it contains information about the web server that could be useful to attackers.

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

What Next?

Now that you have PHP up and running for Nginx, you might want to configure and manage a database by installing MySQL and phpMyAdmin.

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

12 replies

Leave a reply

Your email address will not be published. Required fields are marked *

  1. If you encounter Not found 404 then you have to add the
    root /var/www/html;
    which shows the web server files path under the
    location ~ .php$ {
    line

  2. I’ve triple checked my steps and I’m pretty sure I have this right but fro some reason my ‘info.php’ downloads as a .php script instead of showing the info like in your example. I did install phpmyadmin and the login screen for that is working correctly. I am pretty new to this so thanks so much for any help.

    1. You say you’re using multiple domains in the other article, did you go through each configuration file and carry out the changes in Step 4 for each domain?

      eg:

      /etc/nginx/sites-available/test1.com
      
      /etc/nginx/sites-available/test2.com
      
      /etc/nginx/sites-available/test3.com
      

      You need to edit each one.

      1. If you visit these websites before doing these changes, your browser will cache the fact that its downloading it as a file. This confused me, and the solution (in chrome) was to enter inspector, click on network and tick the box to disable cache.

        I was bamboozled for a good half an hour figuring out how to deal with this problem, even though I followed your steps correctly and the nginx -t command says everything is in order.