Pages

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 hosting2. 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 172.16.215.130, I decided to add two more IP addresses in the same network.

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 restartlynx 172.16.215.131lynx 172.16.215.132

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 downsudo 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/hosts127.0.0.1 localhost smallco.com bigco.com

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