Install PHP 7.1 with Nginx on an Ubuntu 16.04 VPS

 

PHP 7.1 comes with many new features and improvements and as a result of this many developers are using it for their projects. In this tutorial we are going to show you how to install PHP 7.1 with Nginx on an Ubuntu 16.04 VPS.

Step 1: Enable PPA

First of all, connect to your Linux VPS via SSH and enable the Ondrej’s PPA:

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

Step 2: Install PHP 7.1

Once you enable the PPA you can proceed and install PHP 7.1 using the following command:

sudo apt-get install php7.1

Step 3: Search and install specific PHP 7.1 modules

This will also install the required dependencies too. However, if you want to install specific PHP7.1 module, you can search if it is available using the following command:

sudo apt-cache search php7.1

Step 4: Install most commonly used modules

To install PHP7.1 including some of the most commonly used modules you can use the following command:

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

Step 5: Configure php.ini file

Once the installation is completed you need to edit the php.ini file. Find the configuration file:

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

Edit the file using your favorite text editor:

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

Make the following changes:

cgi.fix_pathinfo=0

Then, restart the PHP-FPM service:

sudo systemctl restart php7.1-fpm.service

Step 6: Install Nginx on Ubuntu 16.04

Installing Nginx on Ubuntu VPS is very easy. Run the following command to install it:

sudo apt-get install nginx

Create Nginx virtual server block for your domain name:

sudo nano /etc/nginx/sites-available/example.com

Paste the following content:

server {
        listen 80;
        server_name example.com www.example.com;
        root /var/www/example.com;
        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;
        }
}

Of course, you should replace example.com with your actual domain name. Save and close the file. To enable the server block in Nginx you need to create a symbolic link to site-enabled. Use the following command to create a symbolic link:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com

Check if there are errors with the configuration:

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 the syntax is OK and there are no errors you can restart Nginx.

sudo systemctl restart nginx.service

Enable Nginx and PHP-FPM on system boot:

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

Further steps

After you’ve installed PHp 7.1 and Nginx on your Linux VPS, you can follow our guide on how to secure your LEMP stack.

You can also get optimized LEMP hosting from us and we’ll install, configure and optimize PHP 7.1 and Nginx on your VPS, free of charge.

 

Source