Skip to main content

Name Based Virtual Hosting in apache2

In name based virtual hosting we run several web sites in one IP address.To do that request from browser should come in HTTP/1.1 protocol.

To demonstrate this process I used this scenario. Imagine we have three web sites called as www.bict,lk , www.ucsc.lk and www.scs.lk. And our apache server runs in 192.168.1.1IP address and 8080 port. Following document roots are used.

www.bict.lk    /var/opt/www.bict.lk/htdocs
www.ucsc.lk   /var/opt/www.ucsc.lk/htdocs
www.scs.lk   /var/opt/www.scs.lk/htdocs

In an each htdocs directory you should create separate index.html files.

note : You can change those document root in /etc/apache/sites-available/default file. And it has default document root as /var/www . To edit that use
$vi /etc/apache/sites-available/default    command.

There should be a index files (index.html) relevant to each web site. To make easy understand I explain this step by step .

step 1.
     Edit the ports.conf file
    $vi /etc/apache2/ports.conf
    In this file you should give  listening port number. Following I     show the whole file and edited part in blue color. If you wish to run your virtualhost in different port no need to edit NameVirtualHost here.
------------ port.conf-----------
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default
# This is also true if you have upgraded from before 2.2.9-3 (i.e. from
# Debian etch). See /usr/share/doc/apache2.2-common/NEWS.Debian.gz and
# README.Debian.gz

NameVirtualHost *:80
Listen 8080

<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

Step2.

       Create a text file in /etc/apache2/sites-available directory.You can use any name that you wish. All virtual hosting in particular IP and port should be keep in one file. I named this file as myhosting.

-----myhosting--------
#
#  Example.com (/etc/apache2/sites-available/myhosting)
#


NameVirtualHost 192.168.1.1:8080
<VirtualHost 192.168.1.1:8080>
        ServerAdmin sashika@localhost
        ServerName  www.ucsc.lk
        ServerAlias www.ucsc.lk

        # Indexes + Directory Root.
        DirectoryIndex index.html
        DocumentRoot /var/opt/www.ucsc.lk/htdocs/

        # Logfiles
        ErrorLog  /var/opt/www.ucsc.lk/logs/error.log
        CustomLog /var/opt/www.ucsc.lk/logs/access.log combined
</VirtualHost>

<VirtualHost 192.168.1.1:8080>
        ServerAdmin sashika@localhost
        ServerName  www.bict.lk
        ServerAlias www.bict.lk

        # Indexes + Directory Root.
        DirectoryIndex index.html
        DocumentRoot /var/opt/www.bict.lk/htdocs/
        
        # Logfiles
        ErrorLog  /var/opt/www.bict.lk/logs/error.log
        CustomLog /var/opt/www.bict.lk/logs/access.log combined
</VirtualHost>

<VirtualHost 192.168.1.1:8080>
    ServerAdmin sashika@ubuntu.com
    ServerName www.scs.lk
    ServerAlias www.scs.lk

 # Indexes + Directory Root.
    DirectoryIndex index.html
    DocumentRoot /var/opt/www.scs.lk/htdocs
</VirtualHost>



All three web sites are defined under different <VirtualHost> tags in same myhosting file. In red color I represent what should be equal in each hosting site. IP and Port address should be same in each site.

note : Don't repeat NameVirtualHost 192.168.1.1:8080 more than one time .

In blue color shows what are the parameters should be changed in each sites. Creating log files are not required and it is a optional.

Note: In my example I create log entries in /var/opt/www.ucsc.lk/logs . This folder structure should be there when you try this as it is. (and /var/opt/www.bict.lk/logs). Or comment both ErrorLog and CustomLog.

If you willing to create your own log file read this article will help you.

Step3.

Create Symlinks.
a2ensite myhosting  is the command to create Symlinks. When it works properly a file called myhosting should be created inside the sites-enabled directory. This is the place (sites-enabled) where daemon looks for whenever user requests for particular site from server.

Whenever you want to disable your virtual hosting simply use                   a2dissite myhosting command then file in sites-enabled will disappear.

Step4.
Update hosts file.

$vi /etc/hosts   is the command to change DNS. As I show in blue color you should give the relevant configuration.
-------------hosts-----------
127.0.0.1    localhost
127.0.1.1    sashika-Laptop

192.168.1.1     www.bict.lk
192.168.1.1     www.ucsc.lk
192.168.1.1     www.scs.lk

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Step 5
Restart the server.
 /etc/init.d/apache2 restart

Step 6
Give the IP address to your machine.
ifconfig eth0 192.168.1.1

Then type two urls, www.bict.lk:8080  , www.ucsc.lk:8080  www.scs.lk:8080 and see the result.
 That's all about NameBasedVirtual hosting in apache2.

 But there is some problem when user type IP address for url like this; 192.168.1.1:8080 . Then what site should web server gives to the client, It is a question since server has three different sites which are using same IP.

In situation like this sever sends very first site which are in configuration file. In our example server sends index of www.ucsc.lk because it is the first VirtualHost site in our myhosting file. If you want to change it, simply change the order. Thats all.

Comments

Popular posts from this blog

IP based vertual hosting in apache2

I think most of the people used ubuntu as their linux OS. There will be a little bit conflict when you are trying to use virtual hosting in apache2 according to centOS. In this article I will try to explain how it should done in ubuntu apache2. Anyway there is a few more things to know before you start. 1. httpd.conf  file is equal to apache2.conf in /etc/apache2/ 2. In apache2.conf includes some other conf file so that necessary settings should be done those files. e.g. changing port numbers should be done in ports.conf I will explain step by step vertual hosting using apacge2. I take /etc/var/www as a DocumentRoot any way if you want to change it, Change the default file which locate in /etc/apache2/sites-available/default file. To easy access create a folder called www.bict.lk (any choice)  and create htdocs and logs two folders inside www.bict.lk. Don't forget to create index.html file in htdocs folder. (html file which shows www.bict.lk ). These folders should be cre

SSL Certificate

SSL stands as secure socket layer, of the main protocol which is used to secure the data transmission.  In this article I am trying to discuss how this SSL certificate is used in client server architecture. First of all you should know the difference of "Authentication" and "Authorization".  Authentication means validate the correct user, for instance most of times when you tried to log to the system, it will ask user name and password to log it. That is called as authentication. Only user who is given correct user name and password allow to log to the system. When you log to a particular site what are the things you are allowed to do and not to do, in other words the privileges that you have in that site called as Authorization. These two concepts are used when client (your browser) requests from server through SSL protocol.  You may wonder what is the reason of having such a system. This all about trust. Once you type your favorite site, www.facebook