Get a VPS server with a 15% discount
Sale ends in
00Days
:
00Hours
:
00Minutes
:
00Seconds

Installing Nginx

Nginx is a web server with a wide range of features, built using modern technologies. It is used by major companies such as Google, Facebook and others.

 
 

Web server installation

Installation is very simple; use the following command:

apt-get install nginx -y
 

Configuring Nginx

You need to go to the /etc/nginx/sites-available directory and create a file named "site.conf".

Insert and adjust the following configuration:

server {
    listen       *:80;
    server_name  spacecore.pro; # website domain
    client_max_body_size 1000M; # maximum file size uploaded through the site
    error_page 404 = @notfound;

    location / {
        root   /home/site/spacecore; # path to the site
        try_files $uri $uri.html $uri/ @extensionless-php;
        index  index.html index.php;
    }

    # PHP configuration; if not needed, delete lines 13 to 21
    location ~ \.(php|html|htm)$ {
        try_files $uri =404;
        root   /home/site/spacecore; # path to the site
        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;
    }
}

Restart Nginx:

service nginx restart
 

PHP notes

PHP is not required for Nginx to operate. This note is relevant only if your site uses PHP.

You can find more information here.

 

Connecting an SSL certificate to Nginx

Using SSL is optional and can be configured if you have a certificate and want to enable it.

You need to create or edit the site configuration so that it looks as follows:

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

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

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

    access_log /var/log/nginx/spacecore.app-access.log; # access log
    error_log  /var/log/nginx/spacecore.app-error.log error; # error log

    # if you need to disable something, use "off" instead of the file path

    client_max_body_size 1000m; # maximum file size uploaded through the site
    client_body_timeout 120s; # request body timeout

    sendfile off; # when enabled, Nginx sends HTTP headers in a single packet instead of multiple parts

    # 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 the site
        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;
    }
}

Restart Nginx:

service nginx restart
 

Checking for Apache2

If you install Nginx alongside Apache2, they will conflict over port 80, so one of the web servers must be removed. Check whether Apache2 is installed:

service apache2 status
 

If there is no large status message with detailed information, it means Apache2 is not installed.

If Apache2 is installed, remove it:

apt-get remove --purge apache2* -y
 

Removing Nginx

To completely remove Nginx from the server, first stop the service:

service nginx stop

Then run the following command to remove it permanently:

apt-get remove --purge nginx*