How to install Nginx with PHP 7.1 on ubuntu 16.04

Written by Abdullah Diaa

First Let’s add PHP repo and update package list:

sudo add-apt-repository ppa:ondrej/php
sudo apt update

Install PHP and nginx:

sudo apt install -y nginx php7.1 php7.1-cli php7.1-common php7.1-fpm php7.1-mcrypt

You can leave cgi.fix_pathinfo as it’s however some guide insist to change it to cgi.fix_pathinfo = 0 but doing that make PHP_SELF variable broken (not equal to DOCUMENT_URI).

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

And set cgi.fix_pathinfo=0

Update config file with your folder:

sudo vim /etc/nginx/sites-enabled/default
  1. Update server_name with your domain.
  2. set index with index.php as first priority.
  3. Use php7.1-fpm sock
    server_name   abdullahdiaa.com;
    root /home/ubuntu/server;
    index index.php index.html index.htm;
    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

Finally validate your config and restart nginx and php-7.1 service:

sudo nginx -c /etc/nginx/nginx.conf -t
sudo systemctl restart php7.1-fpm.service
sudo systemctl restart nginx