Skip to content

Install and secure Nginx on Ubuntu 24.04

You have a brand-new Ubuntu server (VPS) and want to host a website on it over HTTPS, the right way.

CommandBeginnerServers & setupBash / terminalNginx
sudo apt updatesudo apt install -y nginxsudo systemctl enable --now nginxsudo ufw allow 'Nginx Full'systemctl status nginx --no-pager

Output

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running)

Explanation

  1. Install Nginx and open ports 80 and 443 in the firewall (1-installation tab).

  2. Create the site configuration in /etc/nginx/sites-available/: site folder, domain name and security headers (highlighted lines).

  3. Enable the site, test the configuration with nginx -t then reload Nginx. Never reload without testing: a syntax error would take all your sites down.

  4. Switch to HTTPS with Certbot: the Let's Encrypt certificate is free and renews automatically.

Before running Certbot, the domain name must already point to the server's IP address (DNS A and AAAA records).

Share