Virtual Hosting is a method of hosting multiple domain names on a server using a single IP address. This allows one server to share its resources, such as memory and process cycles, in order to use its resources more efficiently.
There are 3 types of Virtual Web Hosting possible in Apache
- Port based
- Name based
- IP based
Port Based Virtual Web Hosting
The default port for number for HTTP is 80. However most web servers can be configured to operate on almost any port number, provided the port number is not in use by any other program on the server.
For example, a server may host the website www.example.com. However, if the owner wishes to operate a second site, and doesnot have access to the domain name configuration for their domain name, and/or owns no other IP addresses which could be used to server the site from they could instead use another port number, for example www.example.com:81 for port 81, www.example.com:8080 for port 8080, www.example.com:8000 for port 8000
Steps to configure a port based wen hosting
# mkdir /var/www/port
# cd /var/www/port/
Create a sample index file for testing purpose
# vi index.html
<h1> PORT BASED WEB HOSTING <h1>
#### Welcome to Golinuxhub ####
Edit the apache configuration file and make the below changes
# vi /etc/httpd/conf/httpd.conf
(Search for the "Listen 80" by using "/" and paste your port under it)
Listen 80
Listen 8080
Add the below lines at the bottom of the page( make necessary changes as per your environment)
<VirtualHost 192.168.1.6:8080>
ServerAdmin root@server1.example.com
DocumentRoot /var/www/port
ServerName www.example.com
ErrorLog logs/server1.example.com-error_log
CustomLog logs/server1.example.com-access_log common
</VirtualHost>
Here "*" signifies your apache server will listen to any IP you have used for web server configuration on your machine. You should make an habbit of using proper IP or name instead of "*".
ServerAdmin ==> sets the contact address that the server includes in any error messages it returns to the client.If the httpd doesn't recognize the supplied argument as an URL, it assumes, that it's an email-address and prepends it with mailto: in hyperlink targets. However, it's recommended to actually use an email address.
ServerName ==> This directive sets the request scheme, hostname and port that the server uses to identify itself. This is used when creating redirection URLs.
DocumentRoot ==> This directive specifies the root directory of the files you want to be visible on your web server
Verify the configuration on your browserServerAdmin ==> sets the contact address that the server includes in any error messages it returns to the client.If the httpd doesn't recognize the supplied argument as an URL, it assumes, that it's an email-address and prepends it with mailto: in hyperlink targets. However, it's recommended to actually use an email address.
ServerName ==> This directive sets the request scheme, hostname and port that the server uses to identify itself. This is used when creating redirection URLs.
DocumentRoot ==> This directive specifies the root directory of the files you want to be visible on your web server
Name based virtual Web hosting
Name based virtual hosts are multiple host names for the same web server IP address
For example a server could be receiving request for two domains, www.example.com and www.example.net both of which resolve to the name IP address. The only thing is that for www.example.com the server would send the HTML file from the directory/var/www/user/deepak/site/ while request for www.example.net would make the server page from/var/www/user/amit/site/
A Name based server can also be hosted using blog1.example.com and blog2.example.com and so on.
Steps to configure name based virtual web hosting
Create two different directories for different user like below
# mkdir -p /var/www/user/deepak/site
# mkdir -p /var/www/user/amit/site
Next create two sample index.html inside both the directories
# cd /var/www/user/amit/site/
# vi index.html
<h1> NAME BASED WEB HOSTING <h1>
#### Welcome Amit ####
# cd /var/www/user/deepak/site/
# vi index.html
<h1> NAME BASED WEB HOSTING <h1>
#### Welcome Deepak ####
Next edit your httpd.conf file
# vi /etc/httpd/conf/httpd.conf
(Add the below lines at the bottom of the page)
<VirtualHost 192.168.1.6:80>
ServerAdmin root@server1.example.com
DocumentRoot /var/www/user/deepak/site
ServerName deepak.example.com
ErrorLog logs/server1.example.com-error_log
CustomLog logs/server1.example.com-access_log common
</VirtualHost>
<VirtualHost 192.168.1.6:80>
ServerAdmin root@server1.example.com
DocumentRoot /var/www/user/amit/site
ServerName amit.example.com
ErrorLog logs/server1.example.com-error_log
CustomLog logs/server1.example.com-access_log common
</VirtualHost>
Search for NameVirtualHost in the httpd.conf file and add the below line
NameVirtualHost 192.168.1.6:80
NOTE: Provide the IP address of your server instead of 192.168.1.6
Save and exit the file
IMPORTANT NOTE: Since we are configuring name based virtual hosting in a private network you will have to configure your own DNS server with proper records.
You can follow the below link on
How to configure BIND-9.8 DNS server in Red Hat Linux 6
Here I will show you briefly the changes I have done
I have created two CNAME records for my domain example.com
# vi /var/named/example.com.zone
IN NS example.com.
IN A 192.168.1.6
server1 IN CNAME example.com.
www IN CNAME example.com.
deepak IN CNAME example.com.
amit IN CNAME example.com.
As you see both amit and deepak are alias of my nameserver pointing to the same IP.
Reload you dns services
# service named reload
Reloading named: [ OK ]
Verify your CNAME records
# nslookup deepak.example.com
Server: 192.168.1.6
Address: 192.168.1.6#53
deepak.example.com canonical name = example.com.
Name: example.com
Address: 192.168.1.6
# nslookup amit.example.com
Server: 192.168.1.6
Address: 192.168.1.6#53
amit.example.com canonical name = example.com.
Name: example.com
Address: 192.168.1.6
Restart apache services
# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
Make sure your resolv.conf is reflecting your DNS
# cat /etc/resolv.conf
search example.com
nameserver 192.168.1.6
You can also verify your apache configuration using CLI
# curl amit.example.com
<h1> NAME BASED WEB HOSTING <h1>
#### Welcome Amit ####
# curl deepak.example.com
<h1> NAME BASED WEB HOSTING <h1>
#### Welcome Deepak ####
Verify the configuration on the browser
IP based Virtual Web Hosting
IP-based virtual hosting is a method to apply different directives based on the IP address and port a request is received on. Most commonly, this is used to serve different websites on different ports or interfaces.
System requirements
As the term IP-based indicates, the server must have a different IP address/port combination for each IP-based virtual host. This can be achieved by the machine having several physical network connections, or by use of virtual interfaces
In my case I have added an extra NIC in my virtual machine. if you don't have extra INC you can always create a virtual ethernet card for this purpose
# ifconfig
eth1 Link encap:Ethernet HWaddr 00:0C:29:51:AA:CD
inet addr:192.168.1.7 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe51:aacd/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:41253 errors:0 dropped:0 overruns:0 frame:0
TX packets:23317 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:60492500 (57.6 MiB) TX bytes:1641927 (1.5 MiB)
Interrupt:19 Base address:0x2424
eth3 Link encap:Ethernet HWaddr 00:0C:29:51:AA:E1
inet addr:192.168.1.6 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe51:aae1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7614 errors:0 dropped:0 overruns:0 frame:0
TX packets:5483 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2438197 (2.3 MiB) TX bytes:731907 (714.7 KiB)
Interrupt:17 Base address:0x2024
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:572 errors:0 dropped:0 overruns:0 frame:0
TX packets:572 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:60978 (59.5 KiB) TX bytes:60978 (59.5 KiB)
As you see I have eth1 configured with 192.168.1.7 and eth3 with 192.168.1.6
Steps to configure IP based virtual web hosting
Let us configure out httpd.conf
Copy the same Virtual hosting lines from name based virtual web hosting and make the below changes
<VirtualHost 192.168.1.6:80>
ServerAdmin root@server1.example.com
DocumentRoot /var/www/user/deepak/site
ServerName deepak.example.com
ErrorLog logs/server1.example.com-error_log
CustomLog logs/server1.example.com-access_log common
</VirtualHost>
<VirtualHost 192.168.1.7:80>
ServerAdmin root@server1.example.com
DocumentRoot /var/www/user/amit/site
ServerName amit.example.com
ErrorLog logs/server1.example.com-error_log
CustomLog logs/server1.example.com-access_log common
</VirtualHost>
Search for "Listen" by using "/" and make sure the below line exists
Listen 80
Next save and exit the file
Edit your respective index.html file to reflect IP based web hosting on the sample page
Lastly restart apache services
# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
No comments:
Post a Comment