How to Setup NGINX Virtual Hosts in Ubuntu

setup NGINX virtual hosts

Virtual Hosts (known as server blocks) allow you to host multiple website on a single server. Let us take a look at how to setup NGINX virtual hosts in Ubuntu.


How to Setup NGINX Virtual Hosts in Ubuntu

Here are the steps to setup NGINX virtual hosts in Ubuntu.


1. Install NGINX web server

If you haven’t installed NGINX on your Linux system, open a terminal session or SSH into your Linux system and run the following command

$ sudo apt-get install nginx


2. Create a sample website

We will create a sample project folder that will host our website files. It will act as the document root for our website. We will add an index.html file with some content in it.

$ sudo mkdir -p /var/www/
$ sudo echo "Hello World...Testing" > /var/www/


Next, we will setup appropriate permissions so that NGINX can access them.

$ sudo chown -R www-data:www-data /var/www/
$ sudo chmod -R 755 /var/www/


3. Create Virtual Hosts Configuration File

Create a copy of the default virtual hosts file provided by NGINX so we can customize it for our website (

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/


Open the config file in a text editor

$ sudo vim /etc/nginx/sites-available/


Add the following lines to it. If you don’t use HTTPS, just comment out its lines by adding ‘#’ in front of each line.

# HTTP Server Block

server {
 listen 80;

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



# HTTPS Server Block

server {

 listen 443;

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

 ssl on;
 ssl_certificate /etc/nginx/ssl/;
 ssl_certificate_key /etc/nginx/ssl/;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
 ssl_prefer_server_ciphers on;



4. Enable Virtual Hosts

By default, NGINX only reads virtual host config files in /etc/nginx/sites-enabled directory. So we create a symbolic link to our config file to this directory.

$ sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/


5. Restart NGINX web server

Restart NGINX web server to apply changes

$ sudo service nginx restart




About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!