How to Create and Set Up Nginx Virtual Hosts on Ubuntu

Ever questioned 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 on the lookout for.

This information will have 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 broadly 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 sources or {hardware}.


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

Step 1: Installing the Nginx Server

In case you should not have Nginx put in, right here is how one can rapidly set up it on Ubuntu utilizing APT.

First, replace your package deal data in opposition to 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 affirm if Nginx has been put in efficiently. If it’s, your browser will show a web page related to the one beneath.

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 is suggested that you just place every separate web site in a distinct 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 do 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 inside the listing utilizing the next instructions:

cd /VirtualHost
contact index.html

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

<!doctype html>
<meta charset=”utf-8″>
<title>Nginx: Web & Reverse proxy server</title>
<h1>Welcome to Nginx</h1>
<p>I’ve simply configured a digital host utilizing Nginx net server on Linux</p>

Save and shut the file.

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

Step 3: Configuring the Virtual Host

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

cd /and so on/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’ve got simply created, i.e. virtual_host, utilizing your favourite textual content editor and paste the next strains 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 a few of the essential 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 placement 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’ve got made.

sudo systemctl restart nginx

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

sudo systemctl standing nginx

If every little thing seems fantastic, 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 among the most in-demand engineering expertise for the time being, so begin your net improvement journey with PHP at the moment.


How to Build Your First Simple PHP Website

Want to construct an internet site however do not know the place to begin? Creating a fundamental PHP web site will put you on the highway to net improvement.

Read Next

About The Author

Mwiza Kumwenda
(46 Articles Published)

Mwiza develops software program by career and writes extensively on Linux and front-end programming. Some of his pursuits embody historical past, economics, politics & enterprise-architecture.

From Mwiza Kumwenda

Subscribe to our publication

Join our publication for tech suggestions, opinions, free ebooks, and unique offers!

Click right here to subscribe

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button