How to manage and use Nginx Virtual host in Ubuntu
Tags: nginx, webdev, beginners, tutorial
Published: 2022-01-17 04:39:56

Nginx is a web server that is gaining too much popularity recently. It is because Nginx is very lightweight and we can use it for multiple purposes. For example, We can use Nginx as a web server, We can use it as a reverse proxy for Apache and We can also use it as a load balancer. In this guide, we will use Nginx as a web server and we will show you How to use Nginx Virtual host to host multiple websites on your server.

## What is Nginx Virtual Host?
A virtual host is an Apache term. However, it is commonly used by Nginx users. The proper term for Nginx is server block. Both of these words have the same meaning, which is the feature of hosting multiple websites on a single server. This is extremely useful given that you own multiple sites and don't want to go through the lengthy (and expensive) process of setting up a new web server for each site.

This blog will cover everything to know about virtual hosts in Nginx. By the end of this guide, you will know How to set up multiple websites on a single Ubuntu VPS with an Nginx web server using virtual hosts.

## 1. Prerequisites

- The operating system running Ubuntu Linux
- A root or non-root user with Sudo privileges
- Has stable internet connection
- Terminal window / Command line

## 2. Update Local Repositories
Update all system packages to the latest. Run the following command:
```
#! /bin/bash
sudo apt-get update
sudo apt-get upgrade -y
```
Depending on how many packages your server has to update, it might take a couple of minutes.

## 3. Installing Nginx On Ubuntu
We assume that you already have Nginx installed on your system, but if you don’t have installed it already, use the following command to install it.
```
sudo apt-get install nginx -y
```
The -y flag in the command will automatically allow the server to install Nginx. If you don’t add the -y flag, it will simply ask you for confirmation. It might take a minute or two to install Nginx on your server. Once the process is complete, verify the installation by visiting the public IP address of your server in your browser. It should show something like this. Also, you can read this blog about [How to Install Nginx on Ubuntu 20.04 LTS](https://techvblogs.com/blog/install-nginx-on-ubuntu-20-04).
![How to manage and use Nginx Virtual host in Ubuntu](https://res.cloudinary.com/techvblogs/image/upload/v1642058263/blog/how-to-manage-and-use-nginx-virtual-host-in-ubuntu/Screenshot_2022-01-13_at_12-46-43_How_to_manage_and_use_Nginx_Virtual_host_in_Ubuntu_-_Interserver_Tips_xj3rur.png)
Now, it’s time to understand what is the Nginx virtual host. In the next part, we will appreciate the Nginx virtual host directories and the Nginx virtual host.

## 4. Understanding Nginx Virtual Host Directories
Now, let us first understand the meaning of “Nginx Virtual Host directories”. Like Apache, Nginx has many configuration files and directories inside the /etc/nginx directory in Ubuntu. Execute the following command to navigate the directory in which all the Nginx configuration files are stored.
```
cd /etc/nginx
```
Once you are in, execute the ls command to list all the directories and files inside the main directory. You will see plenty of configuration files and directories that contain some more configuration files. However, we are interested in two directories as we are just learning Nginx virtual hosts in this guide. The first one is sites-available, and the second one is sites-enabled.

The main Nginx configuration file is nginx.conf, and it includes all the configuration files or symlinks available inside the sites-enabled directory. Now, both directories contain the Nginx virtual hosts. So, why two directories to keep virtual hosts?

The sites-available will contain all the virtual hosts you will ever need on your server. For example, one for maintenance of your site, one for the site you no longer manage but might manage again in the future, and all the other wanted and unwanted virtual hosts.

However, a sites-enabled the directory will mainly contain the symlinks of the virtual host files from the sites-available directory. As I mentioned a few paragraphs prior, the main Nginx configuration file will include all the virtual host files and symlinks available inside the sites-enabled directory.

The benefit of this directory structure is that we can keep all the essential virtual host files ready inside the sites-available directory that we might need in the future. We can enable the virtual hosts by creating a symlink of the main virtual host file inside the sites-enabled directory.

## 5. Creating Nginx Virtual Host
A virtual host is simply a configuration file that tells the web server where to redirect a specific request. For example, you can redirect a request for yourdomain.com to /var/www/yourdomain.com and a request for yourdomain2.com to /var/www/yourdomain2.com.

Similarly, you can create different log files for other virtual hosts at different locations. You can also keep different rules for every virtual host or site hosted on your server.

Finally, run the following command to create a virtual host in Nginx.
```
sudo nano /etc/nginx/sites-available/yourdomain.conf
```
Here we are going to create a virtual host for an imaginary domain name called yourdomain.com. Populate our new virtual host with the following content.
```
server {
	listen 80;
	listen [::]:80;

	server_name yourdomain.com;

	root /var/www/yourdomain.com;
	index index.html;

	location / {
		try_files $uri $uri/ =404;
	}
}
```
You can replace the domain name and the file's name based on your requirements. After populating the configuration file, press CTRL+X, Y, and Enter to save the configuration file.

We also have to create a directory and the index file we mentioned in the virtual host file. Execute the following commands to create a directory and a sample index file.
```
sudo mkdir /var/www/yourdomain.com
sudo nano /var/www/yourdomain.com/index.html
```
Add the following HTML:
```
<html>
   <head>
    <title>Welcome to TechvBlogs!</title>
   </head>
   <body>
   <h1>Welcome To TechvBlogs</h1>
   <h3>Hooray! The NGINX Virtual Host is working!</h3>
   </body>
</html>
```
So, our virtual host and all the directories and files that support the virtual host are ready. We have to enable the virtual host file to make our site live on the internet.

## 6. Enable Nginx Virtual Host
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/yourdomain.conf /etc/nginx/sites-enabled/yourdomain.conf
```
Once done, test the configuration for any syntax errors with:
```
sudo nginx -t
```
The answer Syntax OK should be returned. Then, restart Nginx to apply the changes and have the web server use your configuration file.
```
sudo systemctl restart nginx
```

## 7. Test Nginx Virtual Host
Now that you have your virtual hosts configured, you can test your setup by going to the domains that you configured in your web browser:

http://yourdomain.com

![How to manage and use Nginx Virtual host in Ubuntu](https://res.cloudinary.com/techvblogs/image/upload/v1642332093/blog/how-to-manage-and-use-nginx-virtual-host-in-ubuntu/Selection_017_zsizve.png)

## 8. Disable Nginx Virtual Host
It will throw you an error if something is wrong with the code in a virtual host or if some file or directory is missing that is required for any virtual host to run. To disable a virtual host in Nginx, run the following commands.
```
sudo rm /etc/nginx/sites-enabled/yourdomain.conf
sudo systemctl restart nginx
```
In disabling the virtual host, we have to remove the symlink we created inside the sites-enabled directory. So, this is how you can enable and disable virtual hosts in Nginx.

Conclusion: Nginx is a very powerful web server if configured properly. We can also configure Nginx with php-fpm which is a great combination that can give you the best experience with PHP-based websites. You can also add additional directives inside virtual host configuration files for further customization. You can add some essential directives like the location of the error log and access log.

Thank you for reading this blog.