How to create NGINX server blocks on Ubuntu 18.04

Jack Wallen shows you how to create name-based server blocks (aka virtual hosts) on NGINX running on Ubuntu Server 18.04.

nginxhero.jpg
Image: NGINX

NGINX is a powerful and flexible web server that any business can benefit from. Like Apache's virtual hosts, NGINX has its own means of serving up multiple websites on a single machine. That means is called server blocks.

Effectively, server blocks are the same as Apache virtual hosts. Although the name might be different, the functionality is the same. As you might expect, the configuration is slightly different as well. I want to show you how to configure these blocks, so you can start serving up more than one site with NGINX.

What you'll need

  • NGINX properly installed and running on your Ubuntu 18.04 server.
  • Account on your Ubuntu server with sudo privileges.
  • A domain name pointing to the IP address of your NGINX server.

SEE: Wireless networking policy (Tech Pro Research)

I am going to show you how to set up a single server block, but the process is the same for setting up multiple blocks. I'll use the myexample.com address as an example. For your setup, make sure to replace myexample.com with the domain you have pointing to the server.

Creating the necessary directories

The first thing to do is create the necessary directories. Our document root for the base NGINX server is /var/www, so we're going to create a new document root (for our server block), called myexample.com. Open a terminal window and issue the command:

sudo mkdir /var/www/myexample.com

With the document root created, create the subdirectory for public HTML with the command:

sudo mkdir /var/www/myexample.com/public_html

Let's create a file for testing our server block. Issue the command sudo nano /var/www/myexample.com/public_html/index.html. Inside that new file, add the following contents:

<!DOCTYPE html>
<html lang="en" dir="ltr">
   <head>
      <meta charset="utf-8">
      <title>myexample.com</title>
   </head>
   <body>
       <h1>Hello, TechRepublic! This is myexample.com.</h1>
   </body>
</html>

Save and close that file. Next, give the myexample.com directory the correct ownership with the command:

sudo chown -R www-data: /var/www/myexample.com 

Creating the server block

Now it's time to create the actual server block. For anyone that's worked with Apache on Ubuntu, this will be fairly familiar. Create a new file with the command:

sudo nano /etc/nginx/sites-available/myexample.com

In this new file, add the following content:

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

    root /var/www/myexample.com/public_html;

    index index.html;

    server_name myexample.com www.myexample.com;

     access_log /var/log/nginx/myexample.com.access.log;
     error_log /var/log/nginx/myexample.com.error.log;

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

Enable the new server block with the command:

sudo ln -s /etc/nginx/sites-available/myexample.com /etc/nginx/sites-enabled/

Test the configuration file with the command:

sudo nginx -t

You should see the configuration is ok (Figure A).

Figure A

Figure A

Our NGINX configuration checks out.


Testing the new block

Before we point our browser to the newly created server block, we must restart NGINX with the command:

sudo systemctl restart nginx

Once the server restarts, point your browser to http://myexample.com, and you should see the greeting from the index.html file (Figure B).

Figure B

Figure B

Our Welcome, TechRepublic page.


If your server block doesn't come up, you might need to remove the default configuration file from sites-enabled with the command:

sudo rm /etc/nginx/sites-enabled/default

Welcome to NGINX server blocks

And that, my friends, is the basics for configuring name-based server blocks in NGINX. It's not all that much different from creating virtual hosts in Apache. You might have noticed this only addresses name-based server blocks for Ubuntu 18.04. I've yet to find if it's possible to create IP-based server blocks with Ubuntu's new Netplan network configuration. Hopefully, for those that need to use IP-based server blocks, a solution will be coming soon.

Also See

By Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.