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
<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.
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
Post a Comment