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 jessie/updates/main nginx-common all 1.6.2-5+deb8u4 [88.1 kB]
Get:2 jessie/updates/main nginx-full amd64 1.6.2-5+deb8u4 [430 kB]
Get:3 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 ( ...
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.



To Configure Virtual-Host

Create the directory for virtual-host.

root@linuxhelp:~# mkdir -p /var/www/
root@linuxhelp:~# mkdir -p /var/www/

Then create the html file for the virtual host domains.

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

 This is first virtualhost page...! 

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

 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;
location / {
root    /var/www/;
index   index.html index.htm;

#second vhost configuration

server {
listen 80;
location / {
root    /var/www/;
index   index.html index.htm;

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

root@linuxhelp:~# nano /etc/hosts

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.



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
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: Nginx Virtualhost
user image Author :  Landon