How to use nginx with php

Prerequisites

  • nginx 1.10.3 and php 7 are already installed

Installation

1.  Install php7.0-fpm

apt-get install php7.0-fpm
2. Edit /etc/nginx/sites-available/default
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
}

3. Restart nginx

service nginx restart

4. Create testfile and open it in your browser

<?php
  phpinfo( );
?>