Nginx is a web server with a lot of features, made according to the latest canons of technology. It is used by the largest companies such as VKontakte, Google, Facebook and others.

Before installing, make sure that you do not have Apache2 installed!

Installing a Web server

Its installation is extremely simple, use the following command:

apt-get install nginx -y

Customization Nginx

You need to go to the /etc/nginx/sites-available directory and create a file such as «site.conf».

Paste the following into the config and edit:

site.conf
server {
    listen       *:80;
    server_name  spacecore.pro; # site domain
    client_max_body_size 1000M; # the maximum file size transmitted through the site
    error_page 404 = @notfound;
    location / {
        root   /home/site/spacecore; # the path to the site
        try_files $uri $uri.html $uri/ @extensionless-php;
        index  index.html index.php;
    }
    # Подключения PHP, if you don't need it, then erase lines 13 to 21
    location ~ \.(php|html|htm)$ {
        try_files $uri =404;
        root   /home/site/spacecore; # the path to the site
        fastcgi_pass unix:/run/php/php7.0-fpm.sock; # the path to php
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include /etc/nginx/fastcgi_params;
    }
}

Restarting Nginx:

service nginx restart

Notes on PHP

PHP is not required to work with Nginx. This note is important only for those whose site uses PHP.

Additional information can be found here.

Connecting an SSL certificate to Nginx

SSL connection is not required, it is carried out if it is available and you wish.

We need to create or edit a site config to look like this:

server {
    listen 80;
    server_name spacecore.pro; # site domain
    return 301 https://$server_name$request_uri; # redirect from http to https
}

server {
    listen 443 ssl http2;
    server_name spacecore.pro; # site domain

    root /var/www/spacecore; # the path to the site
    index index.html index.htm index.php; # index pages

    access_log /var/log/nginx/spacecore.app-access.log; # logs of successful connections
    error_log  /var/log/nginx/spacecore.app-error.log error; # logs of erroneous connections

    # if you want to disable something, instead of the path to the file write «off»

    client_max_body_size 1000m; # the maximum file size transmitted through the site
    client_body_timeout 120s; # timeout value

    sendfile off; # after enabling, Nginx will send HTTP response headers in a single packet, rather than in separate parts.
    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/spacecore.pro/fullchain.pem; # the public key of the certificate
    ssl_certificate_key /etc/letsencrypt/live/spacecore.pro/privkey.pem; # the private key of the certificate
    ssl_session_cache shared:SSL:10m; # SSL session cache size
    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; # reduces the loading time of the site pages

    location ~ \.(php|html|htm)$ {
try_files $uri =404;
root /var/www/spacecore; # the path to the site
fastcgi_pass unix:/run/php/php7.2-fpm.sock; # the path to php
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include /etc/nginx/fastcgi_params;
    }
}

Rebooting Nginx:

service nginx restart

Checking for Apache2

If Nginx is installed with Apache2, they will conflict due to port 80. So you need to delete one of the Web servers. Check if Apache2 is installed:

service apache2 status

If there is not a huge message with information, then it is not installed.

If Apache2 is installed, you need to remove it:

apt-get remove --purge apache2* -y

Removal Nginx

In order to completely remove Nginx from the server, it must first be stopped:

service nginx stop

And then write a command that will permanently delete it:

apt-get remove --purge nginx*

Last updated