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.

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.

FAQ
Q
How do I obtain NGINX Plus from the repository?
A
To obtain NGINX Plus, please follow the instructions at https://cs.nginx.com/repo_setup
Q
When should I use NGINX open source?
A
You can use Nginx As Opensource,
you want to compile in 3rd party modules not included in NGINX Plus Extras, or you want to create your own custom modules;
you want to maintain your own packages and security fixes;
you are working in an environment where you don’t require support.
Q
How can I customize/add a configuration for a VHOST in nginx?
A
You can modify the default template by
# vim /usr/local/psa/admin/conf/templates/default/domain/domainVirtualHost.php
and then,
# /usr/local/psa/admin/sbin/httpdmng --reconfigure-all
Q
Does NGINX Plus support session persistence?
A
NGINX Plus support for cookie-insert, variable-based routing and learn session persistence. Please see: Application Load Balancing with NGINX Plus
Q
What is the proper location for the virtual hosts config file that won't get overwritten?
A
Simply stop using WebStation Virtual hosts (remove them to avoid duplicates) and set up your own in:
CODE: SELECT ALL

/etc/nginx/sites-enabled