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 does not 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 web hosting
I will create a path to demonstrate this scenario
# mkdir /var/www/port # cd /var/www/port/
Create a sample index file for testing purpose
# cat index.html <h1> PORT BASED WEB HOSTING <h1> #### Welcome to Golinuxhub ####
Edit the apache configuration file /etc/httpd/conf/httpd.conf
and make the below changes. 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 habit 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 your configuration
You can access your page on the browser to verify the configuration
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/
Configure Apache server
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 index.html
for user AMIT
# cd /var/www/user/amit/site/ # cat index.html <h1> NAME BASED WEB HOSTING <h1> #### Welcome Amit ####
Similarly create index.html
for user DEEPAK
# cd /var/www/user/deepak/site/
Sample index.html
content
# cat index.html <h1> NAME BASED WEB HOSTING <h1> #### Welcome Deepak ####
Next edit your /etc/httpd/conf/httpd.conf
file and 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
Configure DNS Server
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
Step-by-Step Tutorial: Configure DNS Server using bind chroot (CentOS/RHEL 7/8)
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
Verify your configuration
You can also verify your apache configuration using curl
tool
# curl amit.example.com
<h1> NAME BASED WEB HOSTING <h1>
#### Welcome Amit ####
Verify the same on the browser
Similarly verify the name based web hosting via CLI
# 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
Configure Apache Server
Let us configure our 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
" and we will use Port 80
for our Apache server
Listen 80
Next save and exit the file
I will edit my existing index.html
file to reflect IP based web hosting on the sample page
# cat index.html <h1> IP BASED WEB HOSTING <h1> #### Welcome Amit ####
Similarly update index.html for user DEEPAK
# cd /var/www/user/deepak/site/
Sample index.html
content
# cat index.html <h1> IP BASED WEB HOSTING <h1> #### Welcome Deepak ####
Lastly restart apache services
# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
Verify your configuration
Verify on the browser for user DEEPAK
Similarly verify for user AMIT's
index.html
I hope the article was useful. let me know your feedback using the comment section.