DEV Community

Cover image for How to manage and use Nginx Virtual host in Ubuntu
Suresh Ramani
Suresh Ramani

Posted on • Originally published at techvblogs.com

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.

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.
How to manage and use Nginx Virtual host in Ubuntu
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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Once done, test the configuration for any syntax errors with:

sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

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.

Discussion (1)

Collapse
peter279k profile image
peter279k • Edited on

By default, the Nginx version on Ubuntu 20.04 LTS is outdated.

We can run following command to prove that:

lee@lee-VirtualBox:~$ sudo apt-cache policy nginx
nginx:
  Installed: (none)
  Candidate: 1.18.0-0ubuntu1.2
  Version table:
     1.18.0-0ubuntu1.2 500
        500 http://tw.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
        500 http://tw.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages
        500 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu focal-security/main i386 Packages
     1.17.10-0ubuntu1 500
        500 http://tw.archive.ubuntu.com/ubuntu focal/main amd64 Packages
        500 http://tw.archive.ubuntu.com/ubuntu focal/main i386 Packages
Enter fullscreen mode Exit fullscreen mode

If we want to install the latest Nginx version, it can consider following steps:

  • Running the sudo add-apt-repository ppa:ondrej/nginx -y to import this PPA repository.
lee@lee-VirtualBox:~$ sudo add-apt-repository ppa:ondrej/nginx -y
Hit:1 http://tw.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/ InRelease
Hit:3 https://download.docker.com/linux/ubuntu focal InRelease
Hit:4 http://tw.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:5 http://tw.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease
Hit:7 http://ppa.launchpad.net/ansible/ansible/ubuntu focal InRelease
Hit:8 http://ppa.launchpad.net/ondrej/nginx/ubuntu focal InRelease
Hit:9 http://ppa.launchpad.net/ondrej/php/ubuntu focal InRelease
Reading package lists... Done
Enter fullscreen mode Exit fullscreen mode
  • Running the sudo apt-cache policy nginx command again and we can notice that we've the latest Nginx version can be installed.
lee@lee-VirtualBox:~$ sudo apt-cache policy nginx
nginx:
  Installed: (none)
  Candidate: 1.20.1-2+ubuntu20.04.1+deb.sury.org+1
  Version table:
     1.20.1-2+ubuntu20.04.1+deb.sury.org+1 500
        500 http://ppa.launchpad.net/ondrej/nginx/ubuntu focal/main amd64 Packages
        500 http://ppa.launchpad.net/ondrej/nginx/ubuntu focal/main i386 Packages
     1.18.0-0ubuntu1.2 500
        500 http://tw.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
        500 http://tw.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages
        500 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu focal-security/main i386 Packages
     1.17.10-0ubuntu1 500
        500 http://tw.archive.ubuntu.com/ubuntu focal/main amd64 Packages
        500 http://tw.archive.ubuntu.com/ubuntu focal/main i386 Packages
Enter fullscreen mode Exit fullscreen mode