Step 1: Install the Nginx Web Server

 

In order to display web pages to our site visitors, we are going to employ Nginx, a modern, efficient web server.

Since this is our first time using apt for this session, we should start off by updating our local package index. We can then install the server:

  • apt-get update
  • apt-get install nginx -y

If you see the below page after you executed those commands, you have successfully installed Nginx.

nginx

 

Step 2: Install MySQL to Manage Site Data

  • apt-get install mysql-server -y

You will be asked to supply a root (administrative) password for use within the MySQL system.

The MySQL database software is now installed.

 

Step 3: Install PHP7-FPM

  • apt-get install php7.1-fpm php7.1-mysql -y

We now have our PHP components installed, but we need to make a slight configuration change to make our nginx working with php.

 

Step 4: Configure Nginx to Use the PHP7-FPM

  • nano /etc/nginx/sites-available/default

Currently, with the comments removed, the Nginx default server block file looks like this:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name _;

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

We need to make some changes to this file for our site.

  • First, we need to add index.php as the first value of our index directive so that files named index.php are served, if available, when a directory is requested.
  • We can modify the server_name directive to point to our server’s domain name or public IP address.
  • For the actual PHP processing, we just need to uncomment a segment of the file that handles PHP requests by removing the pound symbols (#) from in front of each line. This will be the location ~\.php$ location block, the included fastcgi-php.conf snippet, and the socket associated with php-fpm.
  • We will also uncomment the location block dealing with .htaccess files using the same method. Nginx doesn’t process these files. If any of these files happen to find their way into the document root, they should not be served to visitors.

The changes that you need to make are in red in the text below:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

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

    server_name server_domain_or_IP;

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

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_read_timeout 300; }

location ~ /\.ht {

deny all; }
}

When you’ve made the above changes, you can save and close the file.
When you are ready, reload Nginx to make the necessary changes:

  • systemctl reload nginx

Done 🙂

%d bloggers like this: