Home  »  ArticlesGuidesHow ToSoftwareTechnologyTools   »   How To Install Nginx Webserver on Debian 12 Linux Systems

How To Install Nginx Webserver on Debian 12 Linux Systems

Nginx is a powerful, high-performance web server that can also function as a reverse proxy, load balancer, and HTTP cache. This guide will walk you through the steps to install and configure Nginx on a Debian 12 system.

Prerequisites

Before you begin, ensure you have:

  • A running Debian 12 system
  • A user account with sudo privileges
  • Internet connectivity to download necessary packages

Step 1: Update Your System

First, update your system to ensure all existing packages are up to date.

$ sudo apt update
$ sudo apt upgrade -y

Step 2: Install Nginx

Nginx is available in the default Debian repositories. Install it using the apt package manager.

$ sudo apt install nginx -y

Step 3: Start and Enable Nginx

After the installation, start the Nginx service and enable it to start on boot.

$ sudo systemctl start nginx
$ sudo systemctl enable nginx

Step 4: Configure Firewall

If your system has a firewall enabled, allow HTTP and HTTPS traffic to access the web server.

$ sudo ufw allow 'Nginx Full'

This command allows both HTTP and HTTPS traffic through the firewall.

Step 5: Verify Nginx Installation

To verify that Nginx is installed and running correctly, open a web browser and navigate to your server’s IP address. You should see the default Nginx welcome page.

Alternatively, you can use the curl command to check the Nginx welcome page:

$ curl http://localhost

You should see the HTML content of the Nginx welcome page.

Step 6: Basic Nginx Configuration

Nginx configuration files are located in the /etc/nginx directory. The main configuration file is /etc/nginx/nginx.conf, and the default server block configuration file is /etc/nginx/sites-available/default.

Edit the Default Server Block

To customize your server, edit the default server block configuration.

$ sudo nano /etc/nginx/sites-available/default

You can modify the server block to match your requirements. Here is a basic example:

server {
    listen 80;
    server_name your_domain_or_IP;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

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

    error_page 404 /404.html;
        location = /404.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

Replace your_domain_or_IP with your domain name or IP address. Save the file and exit the text editor.

Test Nginx Configuration

Before restarting Nginx, it’s a good practice to test the configuration for any syntax errors.

$ sudo nginx -t

If the test is successful, restart Nginx to apply the changes.

$ sudo systemctl restart nginx

Step 7: Set Up a Basic Website

To ensure your Nginx server is working, you can set up a basic HTML page.

Create a Directory for Your Website

Create a new directory for your website and set the appropriate permissions.

$ sudo mkdir -p /var/www/your_domain
$ sudo chown -R $USER:$USER /var/www/your_domain
$ sudo chmod -R 755 /var/www/your_domain

Create an HTML File

Create an index.html file in your website directory.

$ nano /var/www/your_domain/index.html

Add the following content:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Your Domain!</title>
</head>
<body>
    <h1>Success! Your Nginx server is working!</h1>
</body>
</html>

Save the file and exit the text editor.

Configure Nginx to Serve Your Website

Create a new server block configuration file for your website.

$ sudo nano /etc/nginx/sites-available/your_domain

Add the following configuration:

server {
    listen 80;
    server_name your_domain www.your_domain;

    root /var/www/your_domain;
    index index.html;

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

Create a symbolic link to enable the server block.

$ sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

Test the Nginx configuration again and restart the service.

$ sudo nginx -t
$ sudo systemctl restart nginx

Conclusion

You have successfully installed and configured Nginx on your Debian 12 system. Your web server is now ready to serve your web content. For more advanced configurations and optimizations, refer to the official Nginx documentation.

Happy hosting!

References

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.