Ever wondered how you can host several websites on the same server without using virtual machines or complicated setups? Nginx virtual hosts is what you're looking for.

This guide will look at how to configure a virtual web host on Ubuntu using the Nginx web server. Nginx is a highly performant web and reverse proxy server. It is lightweight, cross-platform, and open-source.

What Is a Virtual Host?

A virtual web host is a method of running or hosting several websites with different domain names on a single physical server or virtual machine.

Virtual hosting is widely used by website hosting companies in order to achieve economies of scale and to cater to multiple clients without spending much on dedicated server resources or hardware.

If you've ever used shared hosting, it is most likely a virtual host that is at play behind the scenes.

Step 1: Installing the Nginx Server

In case you do not have Nginx installed, here is how you can quickly install it on Ubuntu using APT.

First, update your package information against the configured sources:

        sudo apt update
    

Then, install Nginx as follows:

        sudo apt install nginx
    

Testing Nginx

Start the Nginx service using the systemctl command.

        sudo systemctl start nginx
    

In your web browser, head over to http://localhost:80 to confirm if Nginx has been installed successfully. If it is, your browser will display a page similar to the one below.

nginx default website

Step 2: Creating and Configuring the Website

By default, the website served by Nginx runs on port 80 and is stored in the /var/www/html directory.

To configure a virtual host, it is recommended that you place each separate website in a different directory, for better security and management.

Create a directory under the /var/www/ directory. You can name it VirtualHost but feel free to use any meaningful name of your choice. To do that, navigate to the /var/www directory using the cd command.

        cd /var/www
    

Next, create the website directory as follows:

        mkdir -p VirtualHost
    

Create an index.html file within the directory using the following commands:

        cd /VirtualHost 
touch index.html

Open the index.html file with your favorite text editor and add the following lines of code to it:

        <!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Nginx: Web & Reverse proxy server</title>
</head>
<body>
    <h1>Welcome to Nginx</h1>
    <p>I've just configured a virtual host using Nginx web server on Linux</p>
</body>
</html>

Save and close the file.

Learn More: How to Create New Files on Linux Using touch

Step 3: Configuring the Virtual Host

You can find Nginx configuration files in the /etc/nginx directory. To configure the virtual host, first, create a virtual host configuration file for the site in the /etc/nginx/sites-enabled directory.

        cd /etc/nginx/sites-enabled
    

We've named the file virtual_host but feel free to use any meaningful name of your choice.

        touch virtual_host
    

Open the file you've just created, i.e. virtual_host, using your favorite text editor and paste the following lines of code in it:

        server {
listen 81;
listen [::]:81;

server_name my.virtualhost.com;

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

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

Save and close the file.

Here are some of the important configurations in the file explained:

  • listen: Specifies that Nginx should serve the website at port 81, i.e. https://localhost:81.
  • server_name: You can give this any name since you are not using any real domain at this point. I've named mine my.virtualhost.com.
  • root: It is the location of the website. In this case, the /var/www/VirtualHost directory.
  • index: Specifies the website's start page, which is index.html.

Step 4: Serving the Website

Restart the Nginx server to save the changes you've made.

        sudo systemctl restart nginx
    

You can check the status of the Nginx server by running:

        sudo systemctl status nginx
    

If everything looks fine, navigate to the URL http://localhost:81, in your web browser.

nginx server serving a website

You now have two websites on your server, one running on port 81 and another running on port 80.

Build and Host Your First Website on Linux

This guide has shown you how you can host multiple websites on Ubuntu using the Nginx web server. Website development is one of the most in-demand engineering skills at the moment, so start your web development journey with PHP today.