Tutorials  /  Ubuntu

Install and Secure Nginx on Ubuntu 20.04 – Step-by-Step Guide

Ccentron Redaktion · October 2025 ·6 min read ·Ubuntu, Tutorial

Nginx is a free and open-source web server that can deliver both static and dynamic applications or services. It is commonly used as a web server, load balancer, reverse proxy, or HTTP cache, and integrates seamlessly with existing applications or serves content directly via a server's IP or domain.

This guide explains the steps required to set up Nginx on Ubuntu 20.04 and configure a sample web application for best performance.

Prerequisites

Before you begin, ensure that you have the following in place:

  • An Ubuntu 20.04 server.
  • A new A record for your domain pointing to the server’s IP address.
  • SSH access as a non-root user with sudo privileges.
  • The server updated with the latest packages.
VM

Matching infrastructure at centron

Ubuntu servers without your own hardware: ccloud³ VMs with full root access from €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

Install Nginx on Ubuntu 20.04

The current version of Nginx is available in the default APT repositories of Ubuntu 20.04. Follow these steps to update your server packages and install Nginx.

Update the package index

Console
$ sudo apt update

Install Nginx

Console
$ sudo apt install nginx -y

Verify the installed version

Console
$ nginx -v

Your output should look similar to:

nginx version: nginx/1.18.0 (Ubuntu)

Manage the Nginx Service

Nginx runs as a systemd service, allowing you to control its processes. Use the following commands to manage it.

Enable Nginx at boot

Console
$ sudo systemctl enable nginx

Start Nginx

Console
$ sudo systemctl start nginx

Stop Nginx

Console
$ sudo systemctl stop nginx

Restart Nginx

Console
$ sudo systemctl restart nginx

Check Nginx status

Console
$ sudo systemctl status nginx

If the output shows Active: active (running), then Nginx is running correctly. If it shows Active: active (failed), stop any processes using port 80 and restart Nginx.

Create a New Nginx Virtual Host

A virtual host configuration allows Nginx to serve files from a specific directory under a given domain. Follow the steps to set up a sample configuration.

Create a new configuration file

Console
$ sudo nano /etc/nginx/sites-available/app.example.com.conf

Add the configuration

Code
server {
        listen 80;
        listen [::]:80;

        server_name app.example.com;

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

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

This configuration listens on port 80 for app.example.com and serves files from /var/www/app.example.com.

Test configuration for errors

Console
$ sudo nginx -t

Activate the site

Console
$ sudo ln -s /etc/nginx/sites-available/app.example.com.conf /etc/nginx/sites-enabled/

Create the web root

Console
$ sudo mkdir -p /var/www/app.example.com

Create an HTML test page

Console
$ sudo nano /var/www/app.example.com/index.html

Add the following HTML to the file:

Greetings from centron

Restart Nginx

Console
$ sudo systemctl restart nginx

Verify access

Use curl or a browser to confirm your setup:

Console
$ curl http://app.example.com

Expected output:

Greetings from centron

Secure the Nginx Web Server

SSL certificates enable encrypted communication between a user’s browser and the Nginx server over HTTPS. By default, Nginx listens on the unsecured HTTP port 80. Follow these steps to obtain trusted Let's Encrypt SSL certificates and secure Nginx for encrypted HTTPS connections.

Install Certbot

Use Snap to install the Let's Encrypt Certbot client package.

Console
$ sudo snap install --classic certbot

Check Certbot version

Console
$ certbot --version

Example output:

certbot 3.3.0

Allow HTTP traffic for ACME verification

Certbot requires access to port 80 to verify domain ownership.

Console
$ sudo ufw allow 80/tcp

Generate an SSL certificate

Replace app.example.com with your actual domain configured in the Nginx virtual host.

Console
$ sudo certbot --nginx -d app.example.com --agree-tos

Set Up Firewall Rules

Ubuntu 20.04 comes with Uncomplicated Firewall (UFW) enabled by default. Use the following steps to configure firewall rules so that Nginx can accept both HTTP and HTTPS traffic.

Allow HTTPS traffic

Console
$ sudo ufw allow 443/tcp

Verify firewall rules

Console
$ sudo ufw status

Expected output:

Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                  
443/tcp                    ALLOW       Anywhere                  
22/tcp (v6)                ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)             
443/tcp (v6)               ALLOW       Anywhere (v6)             

Conclusion

You have now successfully installed Nginx on Ubuntu 20.04 and configured it to serve web applications. With virtual hosts, you can securely deliver applications, and by integrating Nginx with other technologies such as MySQL and PHP, you can also host dynamic web applications.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Ubuntu
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren