Introduction

Nginx is one of the most widely used web servers in the world and can be used to host simple websites or also as a reverse proxy, load balancer, mail proxy, and/or HTTP cache system. The software behind Nginx is open-sourced and free.

In this article, we'll walk you through how to install an Nginx web server on your Debian 10 machine and configure it to display a custom HTML page.

Let's get started!

Table of Contents

Install Nginx

The first thing we'll go over is how to install Nginx on your Debian 10 machine.

Nginx is available in Debian's default repositories, so installation is a pretty straightforward process.

Run the following commands to update your local apt package index so we have access to the most recent package lists:

    
      sudo apt-get update
    
  

And then you can use install Nginx along with any other required dependencies using this command:

    
      sudo apt-get install nginx
    
  

When that command is finished, Nginx will be successfully installed on your machine.

In the next steps, we'll go over some additional configuration you'll need to complete to get Nginx up and running.

Adjust the Firewall

Before we can test Nginx, we need to reconfigure our firewall software to allow access to the service. Nginx registers itself as a service with ufw, our firewall, upon installation. This makes it rather easy to allow Nginx access.

We can list the applications configurations that ufw knows how to work with by typing:

    
      sudo ufw app list
    
  

You should get a listing of the application profiles:

    
      Available applications:
         Nginx Full
         Nginx HTTP
         Nginx HTTPS
         OpenSSH
    
  

There are three profiles available for Nginx:

  • Nginx Full: Opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Nginx Http: Opens only port 80 (normal, unencrypted web traffic)
  • Nginx Https: Opens only port 443 (TLS/SSL encrypted traffic)

It's recommended that you enable the most restrictive profile that will still allow the traffic you've configured. Since we haven't configured SSL for our server yet, in this guide, we will only need to allow traffic on port 80. When we setup SSL Encryption later on, we'll change these settings.

You can enable this by typing:

    
      sudo ufw allow 'Nginx HTTP'
    
  

You can verify the change with this command:

    
      sudo ufw status
    
  

Check your Web Server

The Nginx web server should already be up and running.

You can check with the systemd init system to make sure the service is running by typing:

    
      systemctl status nginx
    
  

You should see an output similar to this in your terminal:

    
      ● 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 Mon 2016-04-18 16:14:00 EDT; 4min 2s ago
        Main PID: 12857 (nginx)
          CGroup: /system.slice/nginx.service
            ├─12857 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
            └─12858 nginx: worker process
    
  

You can access the default Nginx landing page to confirm that the software is running properly. You can access this through your machine's IP address.

When you have your IP address, enter it into your browser's address bar:

    
      http://YOUR_IP_ADDRESS
    
  

You should see the default Nginx landing page, which should look something like this:

Default NGINX HTML Page

Congratulations! You now have a web server running using Nginx!

Configure Nginx to Display Custom HTML

After following the previous steps, you now have an Nginx web server running. Now we can add some custom HTML to display whatever we want instead of the default HTML Nginx displays.

Update Nginx Configuration

By default, Nginx is configured to serve documents out of a directory at /var/www/html. To have Nginx serve a different site instead, you need to create your own directory within /var/www for your site. The actual web content will be put in an HTML directory within this directory.

To create this directory (replace the highlighted text with the name of your site or directory):

    
      sudo mkdir -p /var/www/example.com/html
    
  

The -p flag tells mkdir to create any necessary parent directories along the way.

Now that we have the directory, you need to reassign ownership of the web directories to the normal user account. This will let you write to them without sudo.

You can use the $USER environmental variable to assign ownership to the account that you're currently signed in on (make sure you're not logged in as root). This will allow you to easily create or edit the content in this directory:

    
      sudo chown -R $USER:$USER /var/www/example.com/html
    
  

The permissions of your web roots should be correct already if you have not modified the umask value, but you can make sure by typing:

    
      sudo chmod -R 755 /var/www
    
  

The directory structure is now configured and you can move on.

Create a Sample HTML Page To Display

Now that the directory structure is set up, let's create a default HTML page for your Nginx webserver to display.

Create an index.html file in the directory you just created:

    
      nano /var/www/example.com/html/index.html
    
  

Inside the file, create a really basic HTML file. It will look like this:

    
      <html>
        <head>
        	<title>Welcome to Example.com!</title>
        </head>
        <body>
        	<h1>Success! The example.com server block is working!</h1>
        </body>
      </html>
    
  

Save and close the file when you are finished.

Configure Nginx to Serve the New HTML File

Now that you have the content created in the new /var/www/example.com/html directory, you need to tell Nginx to serve that directory instead of the default /var/www/html it's currently using.

To do this, open the default Nginx configuration file with nano:

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

You need to change the document root, specified by the root directive in the file. Change that line so it points to the directory you created for your site:

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

Everything else can be kept the same. Save and close the file when you are finished.

Next, test to make sure that there are no syntax errors in any of your Nginx files:

    
      sudo nginx -t
    
  

If no problems were found, restart Nginx to enable your changes:

    
      sudo systemctl restart nginx
    
  

Nginx should now be serving the new HTML we added.

Test the Results

Now that you are all set up, you should test that your server is functioning correctly. You can do that by visiting your ip address in your web browser:

    
      http://YOUR_IP_ADDRESS
    
  

You should see a page with these words:

Example NGINX HTML Webpage

If the site works, you have successfully configured Nginx.

Conclusion

After working through this article, you now have an Nginx web server installed, running, and configured to serve some custom HTML using your Debian 10 machine.

Thanks for reading and happy coding!