How to Create and Set Up Nginx Virtual Hosts on Ubuntu

Ever puzzled how one can host a number of web sites on the identical server with out utilizing digital machines or sophisticated setups? Nginx digital hosts is what you are in search of.

This information will take a look at how to configure a digital net host on Ubuntu utilizing the Nginx net server. Nginx is a extremely performant net and reverse proxy server. It is light-weight, cross-platform, and open-source.

What Is a Virtual Host?

A digital net host is a technique of operating or internet hosting a number of web sites with completely different domains on a single bodily server or digital machine.

Virtual internet hosting is extensively utilized by web site internet hosting firms so as to obtain economies of scale and to cater to a number of shoppers with out spending a lot on devoted server assets or {hardware}.

If you have ever used shared internet hosting, it’s most definitely a digital host that’s at play behind the scenes.

Step 1: Installing the Nginx Server

In case you do not need Nginx put in, right here is how one can shortly set up it on Ubuntu utilizing APT.

First, replace your bundle data towards the configured sources:

sudo apt replace

Then, set up Nginx as follows:

sudo apt set up nginx

Testing Nginx

Start the Nginx service utilizing the systemctl command.

sudo systemctl begin nginx

In your net browser, head over to http://localhost:80 to verify if Nginx has been put in efficiently. If it’s, your browser will show a web page comparable to the one under.

nginx default website

Step 2: Creating and Configuring the Website

By default, the web site served by Nginx runs on port 80 and is saved within the /var/www/html listing.


To configure a digital host, it’s endorsed that you simply place every separate web site in a unique listing, for higher safety and administration.

Create a listing underneath the /var/www/ listing. You can identify it VirtualHost however be happy to use any significant identify of your selection. To try this, navigate to the /var/www listing utilizing the cd command.

cd /var/www

Next, create the web site listing as follows:

mkdir -p VirtualHost

Create an index.html file throughout the listing utilizing the next instructions:

cd /VirtualHost 
contact index.html

Open the index.html file along with your favourite textual content editor and add the next traces of code to it:

Nginx: Web & Reverse proxy server

Welcome to Nginx

I've simply configured a digital host utilizing Nginx net server on Linux

Save and shut the file.

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

Step 3: Configuring the Virtual Host

You can discover Nginx configuration information within the /and many others/nginx listing. To configure the digital host, first, create a digital host configuration file for the location within the /and many others/nginx/sites-enabled listing.

cd /and many others/nginx/sites-enabled

We’ve named the file virtual_host however be happy to use any significant identify of your selection.

contact virtual_host

Open the file you have simply created, i.e. virtual_host, utilizing your favourite textual content editor and paste the next traces of code in it:

server {
hear 81;
hear [::]:81;
root /var/www/VirtualHost;
index index.html;
location / {
try_files $uri $uri/ =404;

Save and shut the file.

Here are among the necessary configurations within the file defined:

  • hear: Specifies that Nginx ought to serve the web site at port 81, i.e. https://localhost:81.
  • server_name: You may give this any identify since you aren’t utilizing any actual area at this level. I’ve named mine
  • root: It is the situation of the web site. In this case, the /var/www/VirtualHost listing.
  • index: Specifies the web site’s begin web page, which is index.html.

Step 4: Serving the Website

Restart the Nginx server to save the adjustments you have made.

sudo systemctl restart nginx

You can examine the standing of the Nginx server by operating:

sudo systemctl standing nginx

If all the things appears superb, navigate to the URL http://localhost:81, in your net browser.

nginx server serving a website

You now have two web sites on your server, one operating on port 81 and one other operating on port 80.

Build and Host Your First Website on Linux

This information has proven you how one can host a number of web sites on Ubuntu utilizing the Nginx net server. Website improvement is likely one of the most in-demand engineering abilities for the time being, so begin your net improvement journey with PHP in the present day.

How to Build Your First Simple PHP Website

Want to build a website but don’t know where to start? Creating a basic PHP website will put you on the road to web development.

Read Next

About The Author

Related Posts