Installing NGINX

Installing NGINX is a super simple and fast process. Please make sure to read the documents fully and not to skip through copy and pasting or you may run into errors! There is a bit of configuration required in this part!

Let's install NGINX onto our machine:

sudo apt update

sudo apt install -y nginx

sudo nginx -t
# should return OK and test successful

# If for whatever reason NGINX didn't auto start, you can manually start it here
sudo systemctl start nginx

That's pretty much it, NGINX is now installed and running on your system. Now all we have to do is configure the server blocks.

Uninstall Apache

From what I can tell on the Ubuntu package if you install php 7.4 it will install apache with it. So if you're going to use NGINX we're going to want to remove Apache as it will take the 80 port. The FPM package is already installed so there is nothing else you need to do for NGINX. We can easily remove it by using the command below:

sudo apt remove apache2

Server Blocks

Server blocks are used to basically determine what websites you want to show.

Now we'll go over creating a server block. Please note if you would like SSL you can either use CloudFlare to have their own certificate used or you can manually sign one yourself. At the moment SSL is not documented but it's basically the same as you would any other NGINX block, if you're doing self signed you probably know what you're doing.

We'll start off by creating the file.

sudo nano /etc/nginx/sites-available/freya.conf
server {
    listen 80;
    server_name YOUR_DOMAIN_HERE.com;
    root /var/www/freya/public;
    index index.php

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

    # Only keeping it on for API tracking purposes mostly
    access_log /var/log/nginx/freya.access.log;
    error_log /var/log/nginx/freya.error.log error;

    sendfile off;

    client_max_body_size 64m;
    client_body_timeout 120s;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param PHP_VALUE "upload_max_filesize = 64M \n post_max_size=64M";
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Once you're finished press CTRL + X then Y to save your changes.

Now we're going to want to link the file we've just created to the sites-enabled folder, thankfully linux has a super easy way to do this.

sudo ln -s /etc/nginx/sites-available/freya.conf /etc/nginx/sites-enabled/freya.conf

# Now lets test the config
nginx -t

# If all is good we restart nginx for our changes to take effect
sudo systemctl restart nginx

Congratz, you've just setup your NGINX web server. Assuming you've setup your DNS correctly your site should now be visible. If you came from the installation guide you can click here to continue where you left off.