How to Configure Nginx VirtualHost in Ubuntu

To Configure Nginx VirtualHost in Ubuntu

Virtual Hosting is a method which facilitates hosting multiple domains on a web server. Virtual Hosting is of two types &ndash Name based and IP based. While multiple sites can be run on the same IP address with Name based Virtual Hosting, the IP based virtual hosting can have different IP addresses for each site. This article simplifies the configuration procedure of name based Nginx Virtual Host in Ubuntu.


Configuration of Nginx VirtualHost

The initial step to configure Nginx VirtualHost is to go to its web root directory and create two directories as follows.

root@linuxhelp1:/home/user1# cd /var/www/html/
root@linuxhelp1:/var/www/html# mkdir site1 site2

Once the directories are created, use the following command to create a file with text in the site1 directory. Repeat the same for site2 directory.

root@linuxhelp1:/var/www/html/site1# vim index.html

Now, use the following commands to create server block files.

root@linuxhelp1:~# cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site1.conf
root@linuxhelp1:~# cp /etc/nginx/sites-available/default /etc/nginx/sites-available/site2.conf

Configure the site1.conf file as follows.

root@linuxhelp1:~# vim /etc/nginx/sites-available/site1.conf
server {
        listen 80 default_server 
        listen [::]:80 default_server 
        # SSL configuration
        #
        # listen 443 ssl default_server 
.
.
.
root /var/www/html/site1 
   # Add index.php to the list if you are using PHP
        index index.html index.htm  
  server_name     linuxhelp1.com  www.linuxhelp1.com 
  location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404 
        }

Now configure the site2.conf as follows.
Make sure you remove default_server behind listen 80 and listen [::]80

root@linuxhelp1:~# vim /etc/nginx/sites-available/site2.conf

server {
listen 80  
listen [::]:80 
# SSL configuration
# listen 443 ssl default_server 
.
.
.
root /var/www/html/site2 
# Add index.php to the list if you are using PHP
index index.html index.htm  
server_name linuxhelp2.com www.linuxhelp2.com 
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404 
}

You can remove the default server block with the following command.

root@linuxhelp1:~# rm /etc/nginx/sites-enabled/default

Now enable the new server block by running the following command.

root@linuxhelp1:~# ln -s /etc/nginx/sites-available/site1.conf /etc/nginx/sites-enabled/
root@linuxhelp1:~# ln -s /etc/nginx/sites-available/site2.conf /etc/nginx/sites-enabled/

You can check the Nginx configuration with the help of below command.

root@linuxhelp1:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

After checking the Nginx configuration, restart the Nginx service by running following command.

root@linuxhelp1:~# systemctl restart nginx

Now you have to edit your hosts file by entering your IP address and domain name as below.

root@linuxhelp1:~# vim /etc/hosts
192.168.5.126   www.linuxhelp1.com
192.168.5.126   www.linuxhelp2.com

You can check the VirtualHost configuration by calling http://www.linuxhelp1.com on your browser' s address bar. The browser displays the content of that particular file as follows.

Now to check the second virtual domain, type http://www.linuxhelp2.com and see its content.

Comment
linuxhelp
Dec 27 2018
Internetwork 1. If you want to allow the site to other hosts within a network you need to Put an entry in client hosts as well as localhost 2. If you do so you also need to check your Firewall ( UFW ) whether the port is allowed If you want your site to available globally 1. Own server hosting - configure DNS
qyl
Dec 27 2018
Hi, i've followed exactly the tutorial, but now my sites only work in localhost... How can I deploy it to public to allow others hosts to access these sites?
Add a comment
FAQ
Q
How to enable the new server block during Nginx VirtualHost configuration in ubuntu ?
A
To enable the new server block during Nginx VirtualHost configuration in ubuntu use the following command

ln -s /etc/nginx/sites-available/site1.conf /etc/nginx/sites-enabled/
Q
How to remove the default server block while configuring Nginx VirtualHost in Ubuntu?
A
To remove the default server block while configuring Nginx VirtualHost in Ubuntu use the following command

rm /etc/nginx/sites-enabled/default
Q
How to install nginx virtualhost in freebsd, please guide?
A
To install nginx virtualhost in freebsd follow this link : "https://www.linuxhelp.com/how-to-configure-nginx-virtualhost-in-freebsd/"
Q
How To Configure Nginx VirtualHost On OpenBSD?
A
To Configure Nginx VirtualHost On OpenBSD follow this link : "https://www.linuxhelp.com/how-to-configure-nginx-virtualhost-on-openbsd/"
Q
How To Configure Nginx VirtualHost On SUSE linux?
A
To Configure Nginx VirtualHost On SUSE linux follow this link : " https://www.linuxhelp.com/how-to-configure-nginx-virtualhost-in-opensuse/ "