We have LEMP Stack installed in our server, but in order to make our server really work we still need to do some tweaks. The first thing that we need to do is to configure Nginx because currently it serves only static content. If we enter our Server's Ip Address in our browser we'll get this page.

So here we need to tell Nginx to use PHP processor  so that it can handle dynamic content. In order to do that we need to define new Nginx Server Block configuration file inside /etc/nginx/sites-available directory.

In my case I'll create new server block configuration called laraveldemo.site which is domain name that I have. But fill free to choose any name you like.

sudo nano /etc/nginx/sites-available/laraveldemo.site

Inside new server block configuration you can add following content.

# /etc/nginx/sites-available/laraveldemo.site
server {
    listen 80;
    listen [::]:80;

    root /var/www/html/laraveldemo.site/public;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name 128.199.161.82;
}

Here's what each of these directives do:

Still in the same server block configuration we can add some location blocks like this:

server {
    ...
    server_name 128.199.161.82;

    location / {
            try_files $uri $uri/ /index.php?$query_string;
    }


    location ~ \.php$ {
            include snippets/fastcgi-php.conf;

            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            fastcgi_param DOCUMENT_ROOT $realpath_root;

            fastcgi_pass unix:/run/php/php7.2-fpm.sock;

    }
  
  	location ~ /\.ht {
            deny all;
    }
}

Location blocks are sets configuration depending on a request URI. Here is what each location blocks do:

After adding this content we can hit Ctrl+x. This will show a message confirming the change. Hit Y and press Enter to continue.

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?
Y Yes
N No           ^C Cancel

Now if look inside the /etc/nginx/sites-available directory, we'll find two server block configuration files listed, the default configuration and our newly created one.

To enable our new server block we need to create a symbolic link  inside the /etc/nginx/sites-enabled/ directory. We can do that by execute this command:

sudo ln -s /etc/nginx/sites-available/laraveldemo.site /etc/nginx/sites-enabled/

Now we have our own server block enabled. To ensure that Nginx using our new server block, we can unlink the default configuration file by executing this command:

sudo unlink /etc/nginx/sites-enabled/default

We can also test our new configuration file for syntax errors by typing:

sudo nginx -t

If you found any errors, go ahead and recheck your configuration file. But if everything is good you would have these messages show up:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Last, we need to restart Nginx service in order to make the necessary changes:

sudo systemctl restart nginx

Now if we head over to our browser then reload the page, we'll find 404 Not Found error displayed. Don't worry we'll fix this shortly.

Creating a PHP file to test Nginx configuration

The 404 Error happened because in our server block configuration we pointed the Document root of our web to /var/www/html/laraveldemo.site/public. But this directory is currently does not exist.

So now let's head over to our Terminal. And create that directory by typing this command.

sudo mkdir -p /var/www/html/laraveldemo.site/public

Then, inside this directory let's create a PHP file to test our new server block configuration.

sudo nano laraveldemo.site/public/index.php

Inside this file we can add this command.

<?php phpinfo() ?>

Basically those syntax will display all about PHP information on your server. To save the change in nano editor you can hit Ctrl+x. Hit Y to confirm and hit Enter.

Next, because by default the /var/www/html directory owned by user/group www-data which associated with Web server such as Apache or Nginx, we need to change the ownership of this directory to become laravel:www-data. We can do that by executing this command:

sudo chown -R laravel:www-data /var/www/html

We also need to modify the file mode by running this command:

sudo chmod g+s /var/www/html

Basically this command will makes subsequent file or directory creation inherit group id from the parent directory. Now if we switch back over to our browser and reload the page. We'll see this information in our screen.

This means our configuration is working good. Now we can remove our test file by executing this command:

sudo rm -rf /var/www/html/laraveldemo.site/public

This command will force removing the public directory as well as index.php file inside it. And now there's only laraveldemo.site directory left inside /var/www/html. Letter we'll pull our Laravel app from Github and place it inside this directory.