Nginx Setup with PHP 7.1 on Ubuntu 18.04 LTS Server

I recently installed Ubuntu 18.04 LTS on a guest machine of VMware Workstation 14 Pro… I am doing some testing and wanted to to make sure Nginx and PHP-FPM would work in my environment when 18.04 LTS is released.

Also, Ubuntu 18.04 LTS will be released with PHP 7.1… which is the latest version currently.

If you’re going to be developing any PHP based applications, you’re mostly going to need PHP server scripts installed. PHP is an open source server scripting language use for creating dynamic, powerful web applications and websites.

PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP.

For those wanting to test Nginx with PHP-FPM support on Ubuntu 18.04 LTS, the steps below show you how to do that. Majority of the popular content management systems like WordPress, Joomla, Drupal use it, so PHP is a big deal!

When you’re ready to set up Nginx environment with PHP-FPM support, follow the steps below:

Step 1: Install Nginx Web Server

sudo apt update
sudo apt install nginx

After installing Nginx, the commands below can be used to stop, start and enable Nginx service to always startup when the server boots up.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Step 2: Install PHP-FPM  and Related Modules

After installing Nginx above, run the commands below to install PHP-FPM and related PHP modules. PHP-FPM is a version for Nginx webserver while PHP is Apache2. There are many PHP modules that perform different functions.. however, there are some important ones that are almost always needed when developing PHP based applications and websites.

PHP 7.1 may not be available in Ubuntu default repositories… in order to install it, you will have to get it from third-party repositories.

Run the commands below to add the below third party repository to upgrade to PHP 7.1

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.1

sudo apt update

Finally, run the commands below to install PHP 7.1 and related modules..

sudo apt install php7.1-fpm php7.1-mcrypt php7.1-cli php7.1-xml php7.1-mysql php7.1-gd php7.1-imagick php7.1-recode php7.1-tidy php7.1-xmlrpc

The line above will allow PHP to function with many popular PHP based websites and applications.

Step 3: Configure Nginx PHP Settings

Now that Nginx and PHP-FPM are installed, you may want to configure Nginx to use PHP properly. The default Nginx PHP configuration file is located at /etc/php/7.1/fpm/php.ini

Open PHP-FPM Nginx configuration file by running the commands below

sudo nano /etc/php/7.1/fpm/php.ini

Then edit the file to suit your environments. Some important lines to consider:

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

Next, open the Nginx site configuration file… by default it’s stored at /etc/nginx/sites-available/default

If you have a custom file, then edit it to enable Nginx PHP support. Run the commands below to open Nginx default site configuration file

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

Then uncomment or remove the # symbol on the highlighted lines below to enable Nginx PHP support.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index  index.php index.html index.htm;

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

     # pass PHP scripts to FastCGI server
        location ~ \.php$ {
               include snippets/fastcgi-php.conf;
        #       # With php-fpm (or other unix sockets):
               fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass;

Restart Nginx and PHP-FPM services

sudo systemctl restart nginx.service
systemctl restart php7.1-fpm.service

Step 4: Test PHP-FPM Setup

At this point, Nginx and PHP-FPM should be installed and ready.. to test your Nginx PHP settings, create a blank file with the line below:

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

Then add the line in the file and save.

<?php phpinfo( ); ?>

Save the file and open your browser and browse to the server name or IP address followed by phpinfo.php


You should see something similar to the image below… if you do, then you’re all good!

lighttpd php ubuntu


Congratulations! You’ve successfully installed and configured Nginx and PHP on Ubuntu servers

You may also like the post below:

3 Replies to “Nginx Setup with PHP 7.1 on Ubuntu 18.04 LTS Server”

  1. got it to work! btw, small detail but
    systemctl restart php7.1-fpm.service
    also should be
    sudo systemctl restart php7.1-fpm.service

    big thanks!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.