Setup Nginx with PHP 7.1 on Ubuntu 16.04 / 17.04

PHP 7.1 has some new and interesting features that you might want to use with Nginx. Currently Ubuntu 17.04 and previous version can only install up to PHP 7.0 from the default repository. Ubuntu 17.10 will be released with PHP 7.1.

This brief tutorial shows students and new users how to install PHP 7.1 and setup Nginx webserver to use it and take advantage of some of its newest features. If you’re running Ubuntu 17.04 and prior version, you can’t install PHP 7.1 by default.

PHP 7.1 packages are not in Ubuntu default repository for those versions.. so if you’ll need PHP 7.1, you must install and enable third-party repositories.

To install PHP 7.1 on Ubuntu, follow the steps below:

Step 1: Add a Third Party to Ubuntu

Again, PHP 7.1 isn’t available to Ubuntu 17.04 and prior versions. It will be released with Ubuntu 17.10.. so if you’re running Ubuntu 17.04 and prior, chances are you can only install PHP 7.0 from the default repository.

To install PHP 7.1, add the repository below.

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

Step 2: Install PHP 7.1

Now that the third-party repository with PHP 7.1 has been installed, run the commands below to install PHP 7.1

sudo apt-get update
sudo apt-get install php7.1

Step 3: Install other PHP Modules

To properly support most PHP-based websites and other applications, you may have to install additional PHP modules. The commands below is appropriate for most environments.

sudo apt-get install php7.1 php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-mysql php7.1-mbstring php7.1-mcrypt php7.1-zip php7.1-fpm php7.1-ldap php7.1-tidy php7.1-recode php7.1-curl

Step 4: Setup Nginx

Now that PHP 7.1 is installed , run the commands below to find its default configuration file.

php --ini |grep Loaded
Loaded Configuration File: /etc/php/7.1/cli/php.ini

Then open the file and change the line to:


Open Nginx site configuration file and configure to use PHP7.1-FPM

sudo nano /etc/nginx/sites-available/

Then configure the it to look similar to this:

server {
        listen 80;
        root /var/www/;
        index index.php;

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

        location ~ \.php$ {
            fastcgi_pass unix:/run/php/php7.1-fpm.sock;
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        location ~ /\.ht {
                deny all;

Save your changes.

Step 5: Enable the new site

Run the commands below to enable the site you’re configuring.

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Finally, restart Nginx and PHP7.1-FPM

Finally, restart Nginx and PHP7.1-FPM services by running the commands below

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

That’s it!

You may also like the post below:

Install Drupal on Ubuntu 17.04 / 17.10 with Apache2, MariaDB and PHP