Skip to content

Installing NGINX

Installing Nginx

Let's write the Nginx installation command:

apt-get install nginx -y

Configuring Nginx

Go to sFTP at /etc/nginx/sites-available and create a file server_name.conf (the name can be anything), containing the following text with your data:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
server {
    listen *:80;
    server_name aeza.net; # (1)!
    client_max_body_size 1000M; # (2)!
    error_page 404 = @notfound;
    location / {
        root /home/site/aeza; # (3)!
        try_files $uri $uri.html $uri/ @extensionless-php;
        index index.html index.php;
    }
    # PHP connections, if not needed, delete lines 12 through 19
    location ~\.(php|html|htm)$ {
        try_files $uri =404;
        root /home/site/aeza; # (4)!
        fastcgi_pass unix:/run/php/php7.0-fpm.sock; # (5)!
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include /etc/nginx/fastcgi_params;
    }
}
  1. site domain
  2. maximum file size transferred through the site
  3. path to site
  4. path to site
  5. path to php

Reboot Nginx:

service nginx restart

Connecting PHP to Nginx

Information

PHP is not required to work with Nginx. Use this part only for sites that require PHP scripts to be executed.

Let's run the following commands one by one:

wget -q https://packages.sury.org/php/apt.gpg -O- | apt-key add -
echo "deb https://packages.sury.org/php/ stretch main" | tee /etc/apt/sources.list.d/php.list
sudo apt-get -y install php7.4 php7.4-{mcrypt,mysql,fpm}

Reboot Nginx:

service nginx restart

Enable SSL (encryption protocol)

Info

This is an optional item to increase the credibility of your site

Modify the previously created config to look like this

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
server {
    listen 80;
    server_name aeza.net; # (1)!
    return 301 https://$server_name$request_uri; # (2)!
}

server {
    listen 443 ssl http2;
    server_name aeza.net; # (3)!

    root /var/www/aeza; # (4)!
    index index.html index.htm index.php; # (5)!

    access_log /var/log/nginx/aeza.app-access.log; # (6)!
    error_log /var/log/nginx/aeza.app-error.log error; # (7)!

    # if something needs to be disabled, write "off" instead of the file path

    client_max_body_size 1000m; # (8)!
    client_body_timeout 120s; # (9)!

    sendfile off; # (10)!

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/aeza.net/fullchain.pem; # (11)!
    ssl_certificate_key /etc/letsencrypt/live/aeza.net/privkey.pem; # (12)!
    ssl_session_cache shared:SSL:10m; # (13)!
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
    ssl_prefer_server_ciphers on; # (14)!

    location ~\.(php|html|htm)$ {
        try_files $uri =404;
        root /var/www/aeza; # (15)!
        fastcgi_pass unix:/run/php/php7.2-fpm.sock; # (16)!
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include /etc/nginx/fastcgi_params;
    }
}
  1. site domain
  2. redirect from http to https
  3. site domain
  4. path to site
  5. index pages
  6. logs of successful connections
  7. logs of failed connections
  8. maximum file size transferred through the site
  9. timeout value
  10. once enabled, Nginx will send HTTP response headers in one packet rather than in separate pieces.
  11. SSL certificate public key
  12. SSL certificate private key
  13. SSL session cache size
  14. reduces the loading time of website pages
  15. path to the site
  16. path to php file

Reboot Nginx:

service nginx restart

Checking for Apache2

Information

When using Nginx with Apache2, **** they cannot work correctly, conflicting over port - 80. Therefore it is important to remove one of the web server's **POs.

Let's check for the presence of Apache2:

service apache2 status
Information

If you don't see a big info message, then Apache2 is not installed.

To uninstall Apache2 write:

apt-get remove --purge apache2* -y

Uninstall Nginx

To remove Nginx, we will run the command to stop it:

service nginx stop

And then the command to completely uninstall Nginx:

apt-get remove --purge nginx.