Virtual Host

What is virtual host?

According to Wikipedia: Virtual hosting is a method for hosting multiple domain names on a computer using a single IP address. This allows one machine to share its resources, such as memory and processor cycles, to use its resources more efficiently.

Setup for Windows Platform

At first let me tell you my setup, I have windows in my C: drive and I have installed XAMPP in my D: drive. For windows platform (with XAMPP) you need to do 3 things. Thing 1: go to your XAMPP installation drive then in my case it is D:, and open the httpd-xampp.conf file. In my case it is located here D:\xampp\apache\conf\extra. Thing 2: Add these lines of code in the httpd-xampp.conf

<VirtualHost *:80>
	DocumentRoot "D:/xampp/htdocs/project/path"
	ServerName yourhostname.local

NOTE: One thing to note here is, the default host (localhost) must be the last host defined in this file, Otherwise worlds will be at WAR!!!. So your final look of the httpd-vhosts.conf file should be something similar to this

NameVirtualHost *:80

<VirtualHost *:80>
	DocumentRoot "D:/xampp/htdocs/project/path"
	ServerName yourhostname.local

<VirtualHost *:80>
	DocumentRoot "D:/xampp/htdocs"
	ServerName localhost

NOTE: If you have installed xampp in your windows root directory then you might be unable to save the edited file. In that case open the notepad in admin mood (Run as administrator) by right clicking on its icon. Then open the file from within notepad by selecting File > Open... Thing 3: You need to register your custom domain (in my case it is yourhostname.local) in the window’s host file. As I have my windows in C: drive so my host file locates here C:\Windows\System32\drivers\etc. Now read again what I said in my last NOTE point and then open the host file using notepad by following that instruction. In that host file, add this line, DON’T delete any existing lines in that file (yeah that’s common sense!).        yourhostname.local

and Save the file. and One More Thing: OK I admit it is now becoming 4 things, so Just restart your apache server very quickly by selecting XAMPP control panel. If it fails to restart for some unknown reason just restart your PC.

Setup for Linux Platform

For Linux, it is quite straight forward. Step 1: Add a sites entry in this directory /etc/apache2/sites-available. If you don’t know how just copy the default as yourhostname.local

$ sudo cp default yourhostname.local

Step 2: Open your newly created site file (In this case it is yourhostname.local) with any linux editor, I’ll use VIM.

$ sudo vi yourhostname.local

Then add the following lines of code then save and close.

<VirtualHost *:80>
    ServerName yourhostname.local
    DocumentRoot /home/username/yourproject/path
   <Directory /var/username/yourproject/path>
       DirectoryIndex index.php
       AllowOverride All
       Order allow,deny
       allow from all

Step 3: Register the host in the /etc/host file

$ sudo vi /etc/host

Step 4: Enable the new host with this command

$sudo a2ensite yourhostname.local

Step 5: Restart apache server. Command for this is

$ sudo /etc/init.d/apache2 restart


$ sudo service apache reload

That’s it for now. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s