How to Install Nginx Webserver on Debian 12

Updated on September 20, 2024
How to Install Nginx Webserver on Debian 12 header image

Introduction

Nginx is an open-source, high-performance webserver that also works as a reverse proxy and a load balancer for backend applications. Nginx integrates with modern server-side scripting languages like Python, Golang, and PHP to run dynamic data-driven web applications.

This article explains how to install Nginx on Debian 12, set up virtual hosts, and secure the webserver with Let's Encrypt.

Prerequisites

Before you begin:

Install Nginx

Nginx is available in the default package repositories on Debian 12. Follow the steps below to update the server package index and install Nginx using the APT package manager.

  1. Update the server's package information index.

    console
    $ sudo apt update
    
  2. Install Nginx.

    console
    $ sudo apt install nginx -y
    
  3. Allow HTTP port 80 through the firewall.

    console
    $ sudo ufw allow 80/tcp
    
  4. Reload UFW to apply the firewall changes.

    console
    $ sudo ufw reload
    
  5. Visit your server's public IP address using a web browser such as Chrome to test access to the default Nginx welcome page.

    http://SERVER-IP

    Nginx Welcome Page

Manage the Nginx System Service

Nginx uses the nginx systemd service profile to manage the webserver application processes. Follow the steps below to manage the service using systemctl.

  1. Enable the Nginx service to automatically start at boot.

    console
    $ sudo systemctl enable nginx
    

    Output:

    Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
    Executing: /lib/systemd/systemd-sysv-install enable nginx
    Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
  2. Start the Nginx service.

    console
    $ sudo systemctl start nginx
    
  3. View the Nginx service status and verify that it's running.

    console
    $ sudo systemctl status nginx
    

    Output:

    ● nginx.service - A high performance webserver and a reverse proxy server
         Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enabled)
         Active: active (running) since Sat 2024-08-24 07:00:58 UTC; 1min 35s ago
           Docs: man:nginx(8)
        Process: 2560 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
        Process: 2561 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
       Main PID: 2584 (nginx)
          Tasks: 2 (limit: 1091)
         Memory: 1.7M
            CPU: 12ms
         CGroup: /system.slice/nginx.service
                 ├─2584 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
                 └─2585 "nginx: worker process"
  4. Stop the Nginx service.

    console
    $ sudo systemctl stop nginx
    
  5. Restart the Nginx service.

    console
    $ sudo systemctl restart nginx
    

Create a New Nginx Virtual Host

Nginx virtual host configurations enable the webserver to host multiple websites on a single server. A virtual host configuration consists of multiple directives that define the root directory, server name, error files, and index pages for a specific domain name. Follow the steps below to create a new Nginx virtual host.

  1. Create a new Nginx virtual host configuration file. For example, app.example.com.conf.

    console
    $ sudo touch /etc/nginx/conf.d/app.example.com.conf
    
  2. Open the file using a text editor like nano.

    console
    $ sudo nano /etc/nginx/conf.d/app.example.com.conf
    
  3. Add the following contents to the file. Replace app.example.com with your actual domain.

    nginx
    server {
        listen 80;
        listen [::]:80;
        server_name app.example.com; 
    
        root /var/www/html/app.example.com;
        index index.html;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
    

    Save and close the file.

    The above Nginx configuration creates a new virtual host that listens for incoming connections for the app.example.com domain and delivers web application files from the /var/www/html/app.example.com directory.

  4. Create the virtual host's web root directory.

    console
    $ sudo mkdir -p /var/www/html/app.example.com
    
  5. Create a new index.html web application file in the directory.

    console
    $ sudo nano /var/www/html/app.example.com/index.html
    
  6. Add the following contents to the file.

    html
    <html>
        <head></head>
        <body>
            <h1>Greetings from Vultr</h1>
        </body>
    </html>
    

    Save and close the file.

    The above HTML web application displays a Greetings from Vultr message when you access it in a web browser.

  7. Grant the www-data webserver user and group ownership privileges to the /var/www/html/app.example.com directory.

    console
    $ sudo chown -R www-data:www-data /var/www/html/app.example.com
    
  8. Test the Nginx configuration for errors.

    console
    $ sudo nginx -t
    

    Output:

    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
  9. Restart the Nginx service to apply the changes.

    console
    $ sudo systemctl restart nginx
    
  10. Access your domain on a web browser and verify that your web application displays.

    http://app.example.com

    Greetings from Vultr

Secure the Nginx Webserver

Nginx listens for incoming HTTP connections on port 80. To secure the Nginx virtual host through the HTTP port 443, you must install a trusted SSL certificate. Follow the steps below to install a Let's Encrypt SSL certificate.

  1. Install the Certbot Let's Encrypt client tool and the Nginx plugin.

    console
    $ sudo apt install python3-certbot-nginx certbot -y
    
  2. Generate a new SSL certificate for the virtual host. Replace app.example.com with your actual virtual host domain and hello@example.com with your active email address.

    console
    $ sudo certbot --nginx -d app.example.com -m hello@example.com --agree-tos
    
  3. Ensure Certbot can renew the SSL certificate.

    console
    $ sudo certbot renew --dry-run
    
  4. Restart Nginx to apply the configuration changes.

    console
    $ sudo systemctl restart nginx
    

Set Up Firewall Rules

Uncomplicated Firewall (UFW) is available and active on Vultr Debian instances by default. Follow the steps below to allow HTTP and HTTPS ports through the firewall.

  1. View the UFW status and verify that it's active.

    console
    $ sudo ufw status
    
  2. Allow HTTP port 80 through the firewall.

    console
    $ sudo ufw allow 80/tcp
    
  3. Allow HTTPS port 443 through the firewall.

    console
    $ sudo ufw allow 443/tcp
    
  4. View the UFW status and verify the new connection rules are available.

    console
    $ sudo ufw status numbered
    

    Output:

    Status: active
    
     To                         Action      From
     --                         ------      ----
    [ 1] 22/tcp                     ALLOW IN    Anywhere
    [ 2] 80/tcp                     ALLOW IN    Anywhere
    [ 3] 443/tcp                    ALLOW IN    Anywhere
    [ 4] 22/tcp (v6)                ALLOW IN    Anywhere (v6)
    [ 5] 80/tcp (v6)                ALLOW IN    Anywhere (v6)
    [ 6] 443/tcp (v6)               ALLOW IN    Anywhere (v6)
  5. Visit your virtual host domain using HTTPS and verify that your web application displays.

    https://app.example.com

Conclusion

You have installed Nginx on Debian 12, set up a new virtual host, and installed a Let's Encrypt Certificate. Nginx works as a webserver, a load balancer, or a reverse proxy. For more information, visit the Nginx documentation.