Nginx is a high-performance web server with a wide range of capabilities, designed according to the latest technology standards. It is utilized by major corporations such as VKontakte, Google, Facebook, and others.


Before proceeding with the installation, ensure that Apache2 is not already installed on your system.


Web Server Installation


The installation of Nginx is straightforward. Utilize the following command:

apt-get install nginx -y


Configuring Nginx


You must navigate to the directory '/etc/nginx/sites-available' and create a file of the type "site.conf." Insert the following configuration and make the necessary adjustments:

server {
    listen       *:80;
    server_name  spacecore.pro; # Your website's domain
    client_max_body_size 1000M; # Maximum file size allowed through the website
    error_page 404 = @notfound;
    location / {
        root   /home/site/spacecore; # Path to your website
        try_files $uri $uri.html $uri/ @extensionless-php;
        index  index.html index.php;
    }
    # PHP integration, delete lines 13-21 if not needed
    location ~ \.(php|html|htm)$ {
        try_files $uri =404;
        root   /home/site/spacecore; # Path to your website
        fastcgi_pass unix:/run/php/php7.0-fpm.sock; # Path to PHP
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include /etc/nginx/fastcgi_params;
    }
}


Reload Nginx:

service nginx restart


PHP Notes


PHP is not mandatory for Nginx operation. This note is relevant only for those whose website uses PHP.


Connecting an SSL to Nginx


SSL integration is optional and should be performed if desired and if SSL certificates are available. Modify or create the site's configuration as follows:

server {
    listen 80;
    server_name spacecore.pro; # Your website's domain
    return 301 https://$server_name$request_uri; # Redirect from HTTP to HTTPS
}

server {
    listen 443 ssl http2;
    server_name spacecore.pro; # Your website's domain

    root /var/www/spacecore; # Path to your website
    index index.html index.htm index.php; # Index pages

    access_log /var/log/nginx/spacecore.app-access.log; # Successful connection logs
    error_log  /var/log/nginx/spacecore.app-error.log error; # Error connection logs

    # If something needs to be disabled, replace the file path with "off"

    client_max_body_size 1000m; # Maximum file size allowed through the website
    client_body_timeout 120s; # Timeout value

    sendfile off; # When enabled, Nginx sends HTTP response headers in one packet, reducing page load time

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/spacecore.pro/fullchain.pem; # Public certificate key
    ssl_certificate_key /etc/letsencrypt/live/spacecore.pro/privkey.pem; # Private certificate key
    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 page load time

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


Reload Nginx:

service nginx restart


Checking for Apache2
If Nginx is installed alongside Apache2, they may conflict due to port 80. Therefore, it is necessary to remove one of the web servers. Check if Apache2 is installed:

service apache2 status


If no extensive information message is displayed, Apache2 is not installed. If Apache2 is installed, it should be removed:

apt-get remove --purge apache2* -y


Uninstalling Nginx


To completely remove Nginx from your server, first stop it:

service nginx stop


Then execute the command to permanently remove it:

apt-get remove --purge nginx*