NGINXNGINX

NGINX


Information

The instructions in this article apply only to the NGINX web server, not to the software that will be installed on it.

Requirements

  • A server running Debian Linux
  • A domain name
  • DNS configured to point to the server’s IP address
  • su or sudo privileges to execute commands as root

Preparation

Ensure the system is up to date before starting the installation.

ssh user@server
sudo apt update && sudo apt upgrade

NGINX

Installation

Install NGINX using the apt command.

sudo apt install nginx

Configuration

Information

In the code below, replace example with your domain name, without https:// or www unless explicitly required (e.g., example.com).

  1. Enable NGINX to start automatically with the server.
sudo systemctl enable --now nginx
  1. Copy the default site configuration.
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
  1. Edit the configuration with nano.
sudo nano /etc/nginx/sites-available/example.com
server {
    server_name example.com www.example.com;

    root /var/www/example.com/public/;
    index index.html;

    location / {
            try_files $uri $uri/ =404;
    }

    listen 80;
    listen [::]:80;
}

Tip

You can paste into the terminal using CTRL + SHIFT + V.

  1. Activate the configuration by creating a symbolic link.
sudo ln -s /etc/nginx/sites-available/example.com \
           /etc/nginx/sites-enabled/example.com
  1. Test and reload NGINX.
sudo nginx -t
sudo systemctl reload nginx

CertBot

Installation

Install CertBot using apt.

sudo apt install certbot python3-certbot-nginx

Configuration

Generate and apply a valid SSL certificate with the following command.

sudo certbot --nginx

Follow the prompts, providing your email address and selecting the sites to secure.

Tip

If you want to test the configuration before fetching a certificate, use the --dry-run option.

sudo certbot --nginx --dry-run

Activating the website

  1. Create an index.html file and add some content, for example:
sudo mkdir -p /var/www/example.com/public
sudo nano /var/www/example.com/public/index.html
<html>
  <body>
    <h1>Hello world</h1>
  </body>
</html>
  1. Assign ownership to the www-data user and group, and set proper permissions.
sudo chown www-data:www-data /var/www/example.com -R
sudo chmod -R g+rwX /var/www/example.com
  1. Open a browser and navigate to your website; you should now see the contents of the index.html file.