How to Setup NGINX Virtual Hosts on Ubuntu

Last updated on September 10th, 2024 at 06:01 am

Virtual hosts allow you to run multiple websites on a single server. Here’s how to setup NGINX virtual hosts on Ubuntu.

How to Setup NGINX Virtual Hosts on Ubuntu

Here’s how to setup NGINX virtual hosts on Ubuntu. Please ensure you have installed NGINX in Ubuntu before proceeding further. Here are the steps to install NGINX in Ubuntu.

In this article, we will create two virtual hosts for domains example1.com and example2.com

1. Create Directories

Open terminal and create 2 directories, one for each virtual host, at /var/www. We will place our website files in these 2 directories.

$ sudo mkdir -p /var/www/example1.com/html
$ sudo mkdir -p /var/www/example2.com/html

Bonus Read : How to Enable CORS in NGINX

2. Create Virtual Host Configuration files

Next, create 2 virtual host configuration files, one for each domain, at /etc/nginx/sites-available

First, create virtual host configuration file for example1.com domain

$ sudo vim /etc/nginx/sites-available/example1.com.conf

The above command will open a new file in a text editor. Add the following basic configuration in this file.

server {
   listen 80;
   listen [::]:80;
   server_name example1.com;
   root /var/www/example1.com/html;
   index index.html;
   location / {
    try_files $uri $uri/ =404;
   }
}

In the above commands we define a server block for example1.com that listens to port 80 and serves content located at /var/www/example1.com/html

Similarly, create virtual host configuration file for example2.com domain

$ sudo vim /etc/nginx/sites-available/example2.com.conf

The above command will open a new file in a text editor. Add the following basic configuration in this file.

server {
   listen 80;
   listen [::]:80;
   server_name example2.com;
   root /var/www/example2.com/html;
   index index.html;
   location / {
    try_files $uri $uri/ =404;
   }
}

In the above commands we define a server block for example2.com that listens to port 80 and serves content located at /var/www/example2.com/html

Bonus Read : How to Enable TLS1.3 in NGINX

3. Create HTML pages

Next, create sample index.html pages for both domain. Create index.html file for example1.com in a text editor

$ sudo vim /var/www/example1.com/html/index.html

Add the following HTML:

<html>
   <head>
    <title>Welcome to Example1.com!</title>
   </head>
   <body>
   <h1>Success! The Example1.com NGINX Virtual Host is working!</h1>
   </body>
</html>

Similarly, create index.html file for example2.com

$ sudo vim /var/www/example2.com/html/index.html

Add the following HTML:

<html>
   <head>
    <title>Welcome to Example2.com!</title>
   </head>
   <body>
    <h1>Success! The Example2.com NGINX Virtual Host is working!</h1>
   </body>
</html>

Bonus Read : How to Fix 503 Service Temporarily Unavailable in NGINX

4. Enable Virtual Hosts

Enable virtual hosts by adding symbolic links between the files present at /etc/nginx/sites-available and /etc/nginx/sites-enabled.

$ sudo ln -s /etc/nginx/sites-available/example1.com.conf /etc/nginx/sites-enabled/example1.com.conf
$ sudo ln -s /etc/nginx/sites-available/example2.com.conf /etc/nginx/sites-enabled/example2.com.conf

5. Restart/Reload NGINX Server

Restart/Reload NGINX server to apply changes.

$ sudo nginx -s reload

OR

$ sudo service nginx restart

6. Test Virtual Hosts

Open browser and enter http://example1.com and http://example2.com one by one. You will see the index pages of both domains respectively.

Hopefully, now you can easily setup NGINX virtual hosts on Ubuntu. Similarly, you can also setup virtual hosts for NGINX in CentOS.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it today!