Tutorials  /  Ubuntu

Install and Secure Nginx on Ubuntu 22.04 with SSL

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

Nginx is an open-source web server used to deliver both static and dynamic web content. It can act as a web server, reverse proxy, load balancer, or HTTP cache, integrating seamlessly with various applications to provide efficient web delivery via your server’s IP or domain.

This guide explains how to install Nginx on Ubuntu 22.04 and set up sample web applications for optimal performance.

Prerequisites

  • An Ubuntu 22.04 server.
  • A domain A record pointing to your server’s IP address.
  • SSH access as a non-root user with sudo privileges.
  • An updated server package index.
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 22.04

Nginx is included in Ubuntu 22.04’s default APT repositories. Follow the steps below to install and verify the web server.

Update the Package Index

Console
$ sudo apt update

Install Nginx

Console
$ sudo apt install nginx -y

Check the Installed Nginx Version

Console
$ nginx -v

Example output:

nginx version: nginx/1.18.0 (Ubuntu)

Manage the Nginx System Service

Nginx runs as a systemd service that manages the web server’s processes. Use the following commands to control its status.

Enable Nginx on 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 Service Status

Console
$ sudo systemctl status nginx

Example output:

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2025-04-06 07:47:16 UTC; 1min 49s ago
       Docs: man:nginx(8)
   Main PID: 3143 (nginx)
      Tasks: 5 (limit: 9385)
     Memory: 4.8M
     CGroup: /system.slice/nginx.service
             ├─3143 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─3144 "nginx: worker process"
             ├─3145 "nginx: worker process"
             ├─3146 "nginx: worker process"
             └─3147 "nginx: worker process"

Create an Nginx Virtual Host

Virtual hosts define how Nginx serves specific domains or web apps. The following steps show how to create a basic configuration to host a sample website.

Create the Virtual Host File

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

Add the Virtual Host 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 HTTP port 80 and serves files from the web root /var/www/app.example.com.

Test the Configuration

Console
$ sudo nginx -t

Enable the Virtual Host

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

Create the Web Root Directory

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

Create a Sample HTML File

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

Add HTML Content

Code
 <head></head>
<body>
<h1>Greetings from centron</h1>
</body>
</html> </pre>
 <head></head>
<body>
<h1>Greetings from centron</h1>
</body>
</html> </pre>

Restart Nginx

Console
$ sudo systemctl restart nginx

Test Access to Your Domain

Use curl to confirm your site is accessible:

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

Secure the Nginx Web Server

SSL certificates ensure encrypted communication between a user’s browser and the Nginx web server via HTTPS. By default, Nginx listens on the unencrypted HTTP port 80. Follow these steps to generate and apply trusted Let’s Encrypt SSL certificates, securing your web server.

Install Certbot

Install the Certbot Let's Encrypt client package using Snap:

Console
$ sudo snap install --classic certbot

Check Certbot Version

Verify that Certbot was installed successfully:

Console
$ certbot --version

Example output:

certbot 3.3.0

Allow HTTP Traffic for Certificate Verification

Certbot uses HTTP port 80 for ACME domain validation. Allow incoming connections temporarily:

Console
$ sudo ufw allow 80/tcp

Generate the SSL Certificate

Create a new SSL certificate for your domain. Replace app.example.com with your actual domain:

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

Set Up Firewall Rules

Ubuntu 22.04 includes the Uncomplicated Firewall (UFW) by default. Configure it to permit Nginx to handle incoming HTTP and HTTPS connections.

Allow HTTPS Traffic

Open the HTTPS port (443) to allow secure web access:

Console
$ sudo ufw allow 443/tcp

Check Firewall Status

Verify that UFW rules for Nginx are active:

Console
$ sudo ufw status

Example 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

In this tutorial, you installed and configured Nginx on Ubuntu 22.04 to host secure web applications. You also enabled HTTPS using Let’s Encrypt SSL certificates and configured UFW firewall rules. Nginx supports multiple virtual hosts for secure application deployment and integrates easily with services like MySQL and PHP to power 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?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

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