Types of Virtual Web Hosting in Apache

Virtual Hosting is a method of hosting numerous domain names on a server using a single IP address. This is particularly helpful to individuals who need to run a several sites on one virtual private server. There is no restriction to the number of virtual hosts that can be added to a VPS.

apache-2.2

See Also:

  • How to create Multiple Virtual Host in CentOS/RHEL 5/6/7
  • How To Set Up Apache Virtual Hosts on CentOS 5/6/7
  • How to Password Protect your Website using .htaccess
  • How to Install LAMP on CentOS/RHEL 7
  • How to Install LAMP on CentOS/RHEL 5/6
  • There are 3 types of Virtual Web Hosting possible in Apache:

  • Port based Virtual Web Hosting
  • Name based Virtual Web Hosting
  • IP based Virtual Web Hosting
  • Port Based Virtual Web Hosting:

    The default port number for HTTP is 80. We can configure web servers on almost any port number, provided the port number is not in use by any other program on the server.

    Steps to configure a port based web hosting

    Before creating an virtual host, you need to create document root where you will keep the new website’s information.

    # mkdir /home/multiple-one 
    # mkdir /home/multiple-one
    

    Create a sample index file for testing purpose in both document root.

    # cd /home/multiple-one
    # vi index.html
    
    <h1> PORT BASED WEB HOSTING <h1>
    #### Welcome to Multiple-One ####
    
    # cd /home/multiple-two
    # vim index.html
    
    <h1> PORT BASED WEB HOSTING <h1>
    #### Welcome to Multiple-Two ####
    

    Now Edit the apache configuration file and make the below changes.

    # vim /etc/httpd/conf/httpd.conf
    

    (Search for the “Listen 80” by using “/” and paste your port under it)

    Listen 80
    Listen 81
    Listen 8080
    

    Add the below lines at the end of the page:

    <VirtualHost *:81>
        ServerAdmin dennis.r@techoism.com
        DocumentRoot /home/multiple-one
        ServerName www.multiple-one.com
        ErrorLog logs/www.multiple-one.com-error_log
        CustomLog logs/www.multiple-one.com-access_log common
    <Directory "/home/multiple-one">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    </VirtualHost>
    
    <VirtualHost *:8080>
        ServerAdmin dennis.r@techoism.com
        DocumentRoot /home/multiple-two
        ServerName www.multiple-two.com
        ErrorLog logs/www.multiple-two.com-error_log
        CustomLog logs/www.multiple-two.com-access_log common
    <Directory "/home/multiple-two">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    </VirtualHost>
    

    Now restrat apache webservice.

    For CentOS/RHEL 5/6
    # /etc/init.d/httpd restart
    For CentOS/RHEL 7
    # systemctl restart httpd
    

    Now you can verify the configuration on your browser by running below domain:

    http://www.multiple-one.com:81/
    

    apache-port1

    http://www.multiple-two.com:8080/
    

    apache-port2

    Name Based Virtual Web Hosting:

    With the name based virtual hosts you can hostare multiple website on same server with a single IP. All the domains on that server will share single IP.

    Steps to configure a port based web hosting

    Before creating an virtual host, you need to create document root where you will keep the new website’s information.

    # mkdir /home/namebase/dennis
    # mkdir /home/namebase/andre
    

    Create a sample index file for testing purpose in both document root.

    # cd /home/namebase/dennis
    # vi index.html
    
    <h1> NAME BASED WEB HOSTING <h1>
    #### Welcome Dennis ####
    
    # cd /home/namebase/andre
    # vi index.html
    
    <h1> NAME BASED WEB HOSTING <h1>
    #### Welcome Andre ####
    

    Now Edit the apache configuration file and make the below changes.

    # vi /etc/httpd/conf/httpd.conf
    

    Add the below lines at the end of the page:

    <VirtualHost 192.168.5.219:80>
        ServerAdmin dennis.r@techoism.com
        DocumentRoot /home/namebase/dennis
        ServerName dennis.namebase.com
        ErrorLog logs/dennis.namebase.com-error_log
        CustomLog logs/dennis.namebase.com-access_log common
    <Directory "/home/namebase/dennis">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    </VirtualHost>
    
    <VirtualHost 192.168.5.219:80>
        ServerAdmin dennis.r@techoism.com
        DocumentRoot /home/namebase/andre
        ServerName andre.namebase.com
        ErrorLog logs/andre.namebase.com-error_log
        CustomLog logs/andre.namebase.com-access_log common
    <Directory "/home/namebase/andre">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    </VirtualHost>
    

    Search for NameVirtualHost in the httpd.conf file and add the below line

    NameVirtualHost 192.168.5.219:80
    

    Now restrat apache webservice.

    For CentOS/RHEL 5/6
    # /etc/init.d/httpd restart
    For CentOS/RHEL 7
    # systemctl restart httpd
    

    Now you can verify the configuration on your browser by running below domain:

    http://dennis.namebase.com/
    

    apache-name1

    http://andre.namebase.com/
    

    apache-name2

    IP Based Virtual Web Hosting:

    To setup IP based virtual hosting, you must have more than one IP address assigned to your server or your Linux machine.

    Steps to configure a IP based web hosting

    If you have single NIC card you can create network configuration file like ifcfg-eth0:1, ifcfg-eth0:2, ifcfg-eth0:3 and so on. You can also attached multiple NIC Cards.

    # ifconfig
    

    Sample Output

    eth0      Link encap:Ethernet  HWaddr 00:0C:29:EE:F1:3D
              inet addr:192.168.5.219  Bcast:192.168.5.255  Mask:255.255.255.0
              inet6 addr: fe80::20c:29ff:feee:f13d/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:65 errors:0 dropped:0 overruns:0 frame:0
              TX packets:55 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:7512 (7.3 KiB)  TX bytes:7737 (7.5 KiB)
    		  
    eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:EE:F1:3D  
              inet addr:192.168.5.220  Bcast:192.168.5.255  Mask:255.255.255.0
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
    
    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:65536  Metric:1
              RX packets:76 errors:0 dropped:0 overruns:0 frame:0
              TX packets:76 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:6604 (6.4 KiB)  TX bytes:6604 (6.4 KiB)
    

    Now creating an virtual host, you need to create document root where you will keep the new website’s information.

    # mkdir /home/namebase/dennis
    # mkdir /home/namebase/andre
    

    Create a sample index file for testing purpose in both document root.

    # cd /home/namebase/dennis
    # vi index.html
    
    <h1> IP BASED WEB HOSTING <h1>
    #### Welcome Dennis ####
    
    # cd /home/namebase/andre
    # vi index.html
    
    <h1> IP BASED WEB HOSTING <h1>
    #### Welcome Andre ####
    

    Now Edit the apache configuration file and make the below changes.

    # vi /etc/httpd/conf/httpd.conf
    

    Add the below lines at the end of the page:

    <VirtualHost 192.168.5.219:80>
        ServerAdmin dennis.r@techoism.com
        DocumentRoot /home/namebase/dennis
        ServerName dennis.namebase.com
        ErrorLog logs/dennis.namebase.com-error_log
        CustomLog logs/dennis.namebase.com-access_log common
    <Directory "/home/namebase/dennis">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    </VirtualHost>
    
    <VirtualHost 192.168.5.220:80>
        ServerAdmin dennis.r@techoism.com
        DocumentRoot /home/namebase/andre
        ServerName andre.namebase.com
        ErrorLog logs/andre.namebase.com-error_log
        CustomLog logs/andre.namebase.com-access_log common
    <Directory "/home/namebase/andre">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    </VirtualHost>
    

    Search for NameVirtualHost in the httpd.conf file and add the below line

    NameVirtualHost 192.168.5.219:80
    

    Now restrat apache webservice.

    For CentOS/RHEL 5/6
    # /etc/init.d/httpd restart
    For CentOS/RHEL 7
    # systemctl restart httpd
    

    Now you can verify the configuration on your browser by running below domain:

    http://dennis.namebase.com/
    

    apache-name1

    http://andre.namebase.com/
    

    apache-name2

    Enjoy it!

    No Responses

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

    The reCAPTCHA verification period has expired. Please reload the page.