Apache : What is Virtualhost ? and how to configure Virtualhost with apache?
Virtual Host as explained by name itself, is something with which you can run multiple websites on same server. for example I have two website myblog.com and myweblogicblog.com running on same physical server where i have an apache server is running. So, if i will access myblog.co.com.com or myweblogicblog.com, both will server by same physical server and by same apache server. ( multiple websites and one apache server )
There are two type of virtual hosting -
1. I.P based virtual hosting ( old one and rarely in use as of now )
2. Name based virtual hosting ( widely used option as of today )
In I.P based virtual hosting, you have to use separate i.p for each website, means you need to have separate ethernet card for each website. So, if you have myblog.c
om and myweblogicblog.com on same physical machine then you have to use separate NIC card for both to have a separate i.p for websites, this directly related with high cost.
In Name base Virtual hosting, you need only one i.p address for your server, your each website will point to same server, You have register all your websites on DNS to point to same i.p address. When apache webserver receives requests, based oh the hostname on header, it serves different websites.
Configuration in Apache
1. First you have to uncomment below lines in apache httpd.cong
Include conf/extra/httpd-vhosts.conf
2. You have to add entries in httpd-vhosts.conf under conf/extra folder.
NameVirtualHost *:80
This shows that all of the name-based virtual hosts will be listen on the default port 80
<VirtualHost *:80> </VirtualHost>
You have to configure for each virtual host
I will define two virtual hosts, one for myblog.com and other for myweblogicblog.com, both running on same default port 80
So, there will be two <VirtualHost *:80> </VirtualHost>, one for each website.
When you will access myblog.com, the files under C:\Apache\myblog will be served by Apache and the access_log and error_log for this site will go under C:\Apache\logs\myblog\
When you will access myweblogicblog.com, the files under C:\Apache\myweblogicblog will be served by Apache and the access_log and error_log for this site will go under C:\Apache\logs\myweblogicblog\
# C:\Apache\conf\extra\httpd-vhosts.conf
NameVirtualHost *:80
# myblog virtual host
<VirtualHost *:80>
<Directory />
Allow from all
</Directory>
ServerAdmin mukesh.negi0910@outlook.com
DocumentRoot "C:\Apache\myblog"
ServerName myblog.com
ServerAlias www.myblog.com
ErrorLog "logs\myblog\error_log"
CustomLog "logs\myblog\access_log" common
</VirtualHost>
#myweblogicblog virtual host
<VirtualHost *:80>
<Directory />
Allow from all
</Directory>
ServerAdmin mukesh.negi0910@outlook.com
DocumentRoot "C:\Apache\myweblogicblog"
ServerName myweblogicblog.com
ServerAlias www.myweblogicblog.com
ErrorLog "logs\myweblogicblog\error_log"
CustomLog "logs\myweblogicblog\access_log" common
</VirtualHost>
3. Restart your apache server
So, when you hit myblog.com then it will server from C:\Apache\myblog, and when you hit myweblogicblog.com then it will server from C:\Apache\myweblogicblog
For testing you can create a simple index.html files under C:\Apache\myblog and C:\Apache\myweblogicblog with a simple text like "from myblog.com" and "from myweblogicblog.com"