How to manage and use Nginx Virtual host in Ubuntu

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.

If you are going to follow this guide practically, We recommend you to deploy a new Ubuntu VPS with InterServer. In this guide, We will cover everything there is 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 Nginx web server using virtual hosts.

So, Let’s get straight into the guide. We will first learn how to install Nginx on our brand new Ubuntu VPS.

Install Nginx on Ubuntu

Nginx is very easy to install on Ubuntu. First, connect to your server via SSH as a root user or a user with sudo privilege. We need root access or sudo privilege because we want to install additional packages on our server. It is only possible if a user has sudo privilege or if you log in as a root.

Once logged in, you can execute the following commands in your console to refresh repositories and update other packages if updates are available.

$ sudo apt-get update
$ sudo apt-get upgrade -y

It might take a couple of minutes depending on how many packages your server has to update. Once done, execute the following command to finally install Nginx on your server.

$ sudo apt-get install nginx -y

The -y flag in the command will automatically allow 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.

Nginx default page - Manage Nginx virtual host in Ubuntu

Now, it’s time to understand what is the Nginx virtual host. In the next part, we will understand the Nginx virtual host directories and what exactly is the Nginx virtual host.

Understanding Nginx Virtual Host Directories

Now, Let us first understand the meaning of “Nginx Virtual Host directories”. Just like Apache, Nginx has many configuration files and directories inside the /etc/nginx directory in Ubuntu. Execute the following command to navigate to 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 just 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 of these directories contain the Nginx virtual hosts. So, why two directories to keep virtual hosts?

It is because, the sites-available will contain all the virtual host that 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, sites-enabled directory will mainly contain the symlinks of the virtual host files from the sites-available directory. As I mentioned 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 we get with this directory structure is, we can keep all the important virtual host files ready inside our sites-available directory that we might need in the future and we can enable the virtual hosts at our will by creating a symlink of the main virtual host file inside the sites-enabled directory.

Now, Let’s practically create a virtual host in Nginx.

Create 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 domain1.com to /var/www/domain1.com and a request for domain2.com to /var/www/domain2.com.

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

Finally, execute the following command to create a virtual host in Nginx.

$ sudo nano /etc/nginx/sites-available/example1.conf

Here we are going to create virtual host for an imaginary domain name called example1.com. Populate our new virtual host with the following content.

server {
	listen 80;
	listen [::]:80;

	server_name example1.com;

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

	location / {
		try_files $uri $uri/ =404;
	}
}

You can replace the domain name and the name of the file based on your requirements. After populating the configuration file, press CTRL+X followed by Y followed by Enter to save the configuration file.

Now, 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/example1.com
$ echo "Hello, world!" | sudo tee /var/www/example1.com/index.html

So, our virtual host and all the directories and files that support the virtual host are ready. Now, we just have to enable the virtual host file to take our site live on the internet.

Enabling and Disabling Nginx Virtual Host

Unlike Apache, Nginx does not provide a simple command to enable and disable virtual host. In case of Nginx, we have to manually create symlinks of our virtual host files stored inside sites-available directory to sites-enabled directory.

To enable a virtual host in Nginx, execute the following commands.

$ sudo ln -s /etc/nginx/sites-available/example1.conf /etc/nginx/sites-enabled/example1.conf
$ sudo service nginx restart

We have to restart or reload the Nginx service to reload the configuration in action. It will throw you an error if something is wrong with the code in 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, execute the following commands.

$ sudo rm /etc/nginx/sites-enabled/example1.conf
$ sudo service nginx restart

In case of disabling the virtual host, we just 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 customisation. You can add some essential directives like the location of error log and access log.

Also, let us know if you need help with Nginx virtual host in the comment section given below. If you are using InterServer VPS, you can also contact our 24*7 support for assistance!

Leave a Reply