Tuesday, May 31, 2016

IP-based virtual hosts VS name-based virtual hosts

We setup two virtual hosts as explained in the previous blog post. Now, we can configure it based on two techniques,

1. IP-based virtual hosting
2. Name-based virtual hosting

IP-based Virtual Hosting:

In this case, we are assigning multiple IP addresses for the very same network interface we have in our server machine. Then we can give a unique IP address for each of our virtual host. Let's try it. In our previous blog post, we have already created two virtual hosts called "smallco.com" and "bigco.com". We will use them in this exercise.

First of all, let's assign two IP aliases to our network interface. Since my network interface already has the IP address, I decided to add two more IP addresses in the same network.

sudo ifconfig eth0:0 up
sudo ifconfig eth0:1 up

Now, we need to edit our "/etc/hosts" file to map these IP addresses with our virtual host names.

sudo vim /etc/hosts

Inside this file, append the following two lines.  smallco.com www.smallco.com  bigco.com www.bigco.com

Now, it's time to edit our website configuration files.

sudo vim smallco.com.conf

Edit the virtual host tag to look like the following.


Follow a similar approach for the other virtual host we have in our server with the appropriate IP address. Then, we can restart the server and try to access the websites from their associated IP addresses.

sudo service apache2 restart

Name-based Virtual Hosting:

In order to try name-based virtual hosting, revert all the changes we made for the IP-based virtual hosting about. We have to remove the entries in "/etc/hosts" file and website configuration files where we added an IP address instead of the * mark. Finally, let's bring down the IP aliases.

sudo ifconfig eth0:0 down
sudo ifconfig eth0:1 down

Now we are ready to do name-based virtual hosting. In this, the only thing we have to put is the virtual host names in our "/etc/hosts" file but this time for the same IP address.

sudo vim /etc/hosts   localhost smallco.com bigco.com

Now, restart the apache server and try to access the websites using their virtual host names.

sudo service apache2 restart
lynx smallco.com
lynx bigco.com

That's all folks!

No comments:

Post a Comment