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:

Then, install Nginx as follows:

Testing Nginx

Start the Nginx service using the systemctl command.

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.

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.

Next, create the website directory as follows:

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

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

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.

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

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:

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.

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

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

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.