Name Based Virtual Web Hosting – PART 5

Name Based Virtual Web Hosting on Apache

Virtual Web Hosting

Virtual Web hosting is a method for hosting multiple domain names on a server using a single IP Address. This allow one server to share its resources, such as memory and processor cycle, in order to use its resources more efficiently. Today we will seen how to configure the Named Based Virtual Web Hosting on RHEL/Centos7 Systems.

 

name_virtual_host_c_ittroubleshooter-in

Named-based virtual hosts use multiple host names for the same web server IP Address. In our demonstration (shown above screen shot), a server could be receiving requests for two domains (i.e blog1.ittroubleshooter.in and blog2.ittroubleshooter.in), both of which resolve to the same IP Address. For blog1.ittroubleshooter.in, the server would send the HTML file from the directory “/var/www/blog1/”, while requests for blog2.ittroubleshooter.in would make the server serve pages from “/var/www/blog2/” directory.

Previously we have seen various articles related to Apache Web server, refer below links;

Setting up Simple Web Server on RHEL7/Centos7

Create An Alias Web Site on Apache Web Server

Redirect the Website on Apache Web server

Port based hosting on Apache Web server

Steps to configure name based web hosting

1) Make a directories and create html files for Named based Web hosting.

In our demonstration we are creating two directories for Named based web hosting in document root (i.e /var/www/).

[root@server1 ~]# cd /var/www/
[root@server1 www]# mkdir blog1
[root@server1 www]# mkdir blog2
[root@server1 www]#

Navigate to “blog1″ directory and create an index.html file.

[root@server1 www]# cd blog1
[root@server1 blog1]# vim index.html

Edit index.html file #vim index.html

<html>
<head>
<title>blog1 Named based hosting</title>
</head>
<body>
<h1>Welcome to blog1.ittroubleshooter.in</h1>
</body>
</html>

~
~:wq

Similarly, we create the index.html file in “blog2” directory.

[root@server1 blog1]# cd ../blog2/
 [root@server1 blog2]# vim index.html

Edit index.html file # vim index.html

<html>
<head>
<title>blog2 Named based hosting</title>
</head>
<body>
<h1>Welcome to blog2.ittroubleshooter.in</h1>
</body>
</html>
~
~
:wq

Check the context using below command for both index.html files

[root@server1 ~]# ls -Z /var/www/blog1/index.html 
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/blog1/index.html
[root@server1 ~]# ls -Z /var/www/blog2/index.html 
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/blog2/index.html
[root@server1 ~]#

2) Edit the configuration file.

Navigate to ““/etc/httpd/conf.d”” directory, We already created (On previous articles) the  “ittroubleshooter.conf” file  and add a some lines on bottom, as shown below;

[root@server1 ~]# cd /etc/httpd/conf.d/
[root@server1 conf.d]# vim ittroubleshooter.conf

Edit ittroubleshooter.conf file # vim ittroubleshooter.conf

######## Name Based Hosting ##########
<VirtualHost 192.168.1.10:80>
ServerName blog1.ittroubleshooter.in
ServerAlias ittroubleshooter.in
DocumentRoot /var/www/blog1
ErrorLog logs/server1.ittroubleshooter.in-error_log
CustomLog logs/server1.ittroubleshooter.in-access_log common
</VirtualHost>

<VirtualHost 192.168.1.10:80>
ServerName blog2.ittroubleshooter.in
ServerAlias ittroubleshooter.in
DocumentRoot /var/www/blog2
ErrorLog logs/server1.ittroubleshooter.in-error_log
CustomLog logs/server1.ittroubleshooter.in-access_log common
</VirtualHost>

~
~
:wq

3)DNS configuration if you don’t want to use IP Address.

As we discussed on previous articles how to configure the DNS server, If you are not configured the DNS server, refer the link (Setting up DNS Master-Slave Server Step-by-Step config Guide).

Now, Open the DNS configuration file and add the canonical name as “blog1” and “blog2”, so that we can use our domain as full fledge website.

#vim /var/named/ittroubleshooter.fwd.zone

$TTL 1D
@  IN SOA  server1.ittroubleshooter.in.  root.ittroubleshooter.in. (
                                 01      ; serial
                                 1D      ; refresh
                                 1H      ; retry
                                 1W      ; expire
                                 3H )    ; minimum
 ; #### Name servers ###########
@       IN      NS      server1.ittroubleshooter.in.
@       IN      NS      server2.ittroubleshooter.in.
 ; #### Resolve Name server hostname to IP Adress #######
@       IN      A       192.168.1.10
@       IN      A       192.168.1.11
 ; #####  Hosts in this Domain #######
@       IN      A       192.168.1.12
@       IN      A       192.168.1.13

 ; #####  Hostname Name maps to IP Adress Using "A" record ######  
server1       IN      A       192.168.1.10
server2       IN      A       192.168.1.11
client1       IN      A       192.168.1.12
client2       IN      A       192.168.1.13

 ; #### DNS "CNAME" records ##########
www     CNAME   server1
blog1   CNAME   server1
blog2   CNAME   server1
~
~
:wq

 

Nate: If you are not configured DNS server you can also use easy way that is  add a “domain name to IP mapping” entry to the “/etc/hosts” file. To do this, on the Linux Machine that you want to access the blog1.ittroubleshooter.in and blog2.ittroubleshooter.in from, open the /etc/hosts file.

#vim  /etc/hosts

Add the following lines, using the static IP address of your server:

192.168.1.10    blog1           blog1.ittroubleshooter.in
192.168.1.10    blog2           blog2.ittroubleshooter.in

~
~
:wq

finally check the syntax, using below command.

[root@server1 ~]# httpd -t
Syntax OK
[root@server1 ~]#

4) Restart the httpd Service.

[root@server1 ~]# systemctl restart httpd
[root@server1 ~]#

5)Open the web browser and type the following url.

namedhttpd_blog1_ittroubleshooter-in

httpd_blog2_ittroubleshooter-in

Hope this post will help Linux/Unix beginners. Please share you feedback and Comments. Till then connected with us at ittroubleshooter.in .!!!

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz