Linux Apps
Now Reading
How to Install and Configure Nginx Virtual-Host in Debian
0

How to Install and Configure Nginx Virtual-Host in Debian

To Install Nginx and Configure Virtual-Host in Debian

Virtual hosting is a technique used to run more than one website on a single server. It allows the users to use multiple host names while sharing its resources. This article explains the installation and configuration process of Nginx Virtual-Host in Debian.

Installation of Nginx Virtual-Host

First install Nginx with the ‘apt-get install‘ command.

root@linuxhelp:~# apt-get install nginx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:	
  nginx-common nginx-full
Suggested packages:
  fcgiwrap nginx-doc
The following NEW packages will be installed:
  nginx nginx-common nginx-full
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 591 kB of archives.
After this operation, 1,413 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://security.debian.org/ jessie/updates/main nginx-common all 1.6.2-5+deb8u4 [88.1 kB]
Get:2 http://security.debian.org/ jessie/updates/main nginx-full amd64 1.6.2-5+deb8u4 [430 kB]
Get:3 http://security.debian.org/ jessie/updates/main nginx all 1.6.2-5+deb8u4 [72.6 kB]
Fetched 591 kB in 2s (209 kB/s) 
Preconfiguring packages ...
Selecting previously unselected package nginx-common.
(Reading database ... 136779 files and directories currently installed.)
Preparing to unpack .../nginx-common_1.6.2-5+deb8u4_all.deb ...
Unpacking nginx-common (1.6.2-5+deb8u4) ...
Selecting previously unselected package nginx-full.
Preparing to unpack .../nginx-full_1.6.2-5+deb8u4_amd64.deb ...
Unpacking nginx-full (1.6.2-5+deb8u4) ...
Selecting previously unselected package nginx.
Preparing to unpack .../nginx_1.6.2-5+deb8u4_all.deb ...
Unpacking nginx (1.6.2-5+deb8u4) ...
Processing triggers for systemd (215-17+deb8u5) ...
Processing triggers for man-db (2.7.0.2-5) ...
Setting up nginx-common (1.6.2-5+deb8u4) ...
Setting up nginx-full (1.6.2-5+deb8u4) ...
Setting up nginx (1.6.2-5+deb8u4) ...
Processing triggers for systemd (215-17+deb8u5) ...

Utilize the following command to start the Nginx service.

root@linuxhelp:~# service nginx start

Now its time to check the output. Open the web browser with the respective IP address.

1

 

To Configure Virtual-Host

Create the directory for virtual-host.

root@linuxhelp:~# mkdir -p /var/www/vhost1.com
root@linuxhelp:~# mkdir -p /var/www/vhost2.com

Then create the html file for the virtual host domains.

root@linuxhelp:~# nano /var/www/vhost1.com/ index.html


VHOST 1
 This is first virtualhost page...! 


root@linuxhelp:~# nano /var/www/vhost2.com/ index.html


VHOST 2
 This is second virtualhost page...! 

Open the nginx.conf file and check the ‘include /etc/nginx/conf.d/*.conf‘ line is enable or not. If its not enable, just enable it.

root@linuxhelp:~# nano /etc/nginx/nginx.conf

Then create the ‘vhost.conf‘ file into ‘/etc/nginx/conf.d/‘ directory for virtual host configuration.

root@linuxhelp:~# nano /etc/nginx/conf.d/vhost.conf
#first vhost configuration

server {
listen 80;
server_name     test.vhost1.com;
location / {
root    /var/www/vhost1.com;
index   index.html index.htm;
}
}

#second vhost configuration

server {
listen 80;
server_name     test.vhost2.com;
location / {
root    /var/www/vhost2.com;
index   index.html index.htm;
}
}

Next add the virtual host domain name with IP into the ‘hosts‘ file

root@linuxhelp:~# nano /etc/hosts
192.168.5.130   test.vhost1.com
192.168.5.130   test.vhost2.com

Restart the Nginx service to take effect.

root@linuxhelp:~# service nginx restart

Once the above process is completed successfully, open the browser with the created domain names to check the output.

2

3

Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.

For more help topics, browse our website www.linuxhelp.com

Be the first to comment, we value your suggestions. For further queries please comment below.

Leave a Response