Skip to content

Nginx Installation

Installing Nginx

Let's write the Nginx installation command:

shell
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:

nginx
server {
    listen *:80;
    server_name aeza.net; # site domain
    client_max_body_size 1000M; # maximum file size transferred through the site
    error_page 404 = @notfound;
    location / {
        root /home/site/aeza; # path to site
        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; # path to 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;
    }
}

Reboot Nginx:

shell
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:

shell
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:

shell
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

nginx

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

server {
    listen 443 ssl http2;
    server_name aeza.net; # site domain

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

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

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

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

    sendfile off; # once enabled, Nginx will send HTTP response headers in one packet rather than in separate pieces.

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/aeza.net/fullchain.pem; # SSL certificate public key
    ssl_certificate_key /etc/letsencrypt/live/aeza.net/privkey.pem; # SSL certificate private 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 the loading time of website pages

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

Reboot Nginx:

shell
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:

shell
service apache2 status

Information

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

To uninstall Apache2 write:

shell
apt-get remove --purge apache2* -y

Uninstall Nginx

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

shell
service nginx stop

And then the command to completely uninstall Nginx:

shell
apt-get remove --purge nginx.

AézaWiki © 2024