We have often heard people talking about Apache Virtual Host
Let’s take a ride to virtual host to know it better purpose:
/www
, so it has high chances of being hacked. Hence Virtual Host is used as it provides security.By using virtual host we can set the document root the way we want.
e.g.
In the above example, we can see multiple websites on a single server.
We can configure Apache Virtual Host in 2 ways:
IP-Based Virtual Host:
In this method, the two different web sites having different IP addresses point to the same server which is run by Apache.
e.g.
In the below example we can see there are 2 different sites, test1.com and test2.com having IP addresses 192.168.1.21 and 192.168.1.22 respectively.
Although they are 2 different IPs, their request is served by the same Apache running on that server using IP based virtual host.
Name-Based Virtual Host:
In this method, when we make a request Apache looks at the host name in the header and points the website as per the request.
Name based virtual host having same IP address but different host names.
It’s like shared hosting where each user has a different website but still they are pointing to the same IP address.
In the above example we can see the two different hosts, test1.com and test2.com having the same IP address but the request is served by one Apache running on that server through Name based virtual host configuration.
Steps to make Name-Based Virtual Host:
httpd.conf
by following the below command#vi /etc/httpd/conf/htttpd.conf
Include conf/extra/httpd-vhosts.conf
Modify the httpd-vhosts.conf
as shown below to setup name-based virtual host setting for two hosts.
Uncomment below line
NameVirtualHost *:80
– Indicates that all the name-based virtual hosts will be listening on the default port 80<VirtualHost *:80></VirtualHost>
– Enclose all the Apache configuration parameters for each and every virtual host between these VirtualHost tags. Any Apache directives can be used within the virtualhost container.<VirtualHost *:80></VirtualHost>
, one for each website./var/www/html/test1
will be served by Apache.#vi /etc/httpd/conf/extra/httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80> ServerAdmin kailash@test.com DocumentRoot /var/www/html/test1 ServerName test1.com ServerAlias www.test1.com ErrorLog /var/log/httpd/test1_a-error_log common CustomLog /var/log/httpd/test1_a-access_log common </VirtualHost> <VirtualHost *:80> ServerAdmin kailash@test2.com DocumentRoot /var/www/html/test2" ServerName test2.com ServerAlias www.test2.com ErrorLog /var/log/httpd/test2_a-error_log common CustomLog /var/log/httpd/test2_a-access_log common </VirtualHost>
Verify virtual configuration syntax using “httpd -S”
as shown below. When everything is setup properly, it just displays “Syntax OK”
.
# /usr/local/apache2/bin/httpd -S
VirtualHost configuration:
Syntax OK
# service httpd restart
Now, when you go to test1.com (or www.test1.com), the Apache will serve the files from /var/www/html/test1
directory.
When you go to test2.com (or www.test2.com), the same Apache running on the same server will serve the files from /var/www/html/test2
directory.
References: