Tuesday, January 10, 2012

Setting Up Multiple Virtual Hosts for XAMPP

Creating virtual hosts on Apache 2.2

After installing XAMPP correctly (In my case i use H drive to install), your windows system will be able to act a a webserver. So that you can locally run your projects by calling the URL in your browser. By default XAMPP sets a folder called 'htdocs' as the document root, and the folder located inside xampp folder. You can change the Document root folder to any other folder if you want. your projects can now call as http://localhost/project1 or http://localhost/project2 and so on..

Now, if you want to assign a domain name for a particular folder (project), then you can make use of VirtualHost in Apache directive. This mainly redirects the url into a specified location that we mentioned.

Advantageous of making virtual hosts

1 - User readability - We doesn't want to type long urls to display a project home page

2 - Can avoid Base URL concatenation - If we are stored the code in a folder before you move the code into server your base url for the site will be different because of the directory structure. But once you upload the script into server there the base url become null and our script will concatenate it will all the links that we are placing in the pages

Steps to make virtual hosts

Consider you have a number of site projects there in your htdocs (or in your Document Root) folder like project1, project2, project3,project4 and so on. You are going to set virtual hosts for 2 different sites
project1 &
project2
and you want to call those projects in your browser as
http://project1.local &
http://project2.local

And importantly, your other projects as well as the XAMPP home page or phpmyadmin still needs to be loaded in the same way it was before.

Step 1: You need to add the host name for those two projects in the file called 'hosts' and it is stored in C:\Windows\System32\drivers\etc directory. after the line
127.0.0.1 localhost
::1 localhost
add two new lines as
127.0.0.1 project1.local
127.0.0.1 project2.local
And save the file.

This line will tell the system that redirect the calls project1.local and project2.local into the same local machine, and obviously it reaches the htdocs directory in xampp folder.

Step 2: Open H:\xampp\apache\conf\httpd.conf and make sure that Virtual hosts configuration file has included and not commented. Search for the string
# Virtual hosts
or
Include "conf/extra/httpd-vhosts.conf".

if the line
Include "conf/extra/httpd-vhosts.conf"
is commented - if there a symbol # in front of it - Remove the comment

Step 3: Open H:\xampp\apache\conf\extra\httpd-vhosts.conf file and the line below
NameVirtualHost *

Now you need to set the directive to manage all default calls into redirect into localhost

DocumentRoot "H:\xampp\htdocs"
ServerName localhost

If we are not set these directive then all the calls that we doesn't included in virtual host file will be redirects into the first virtual host home page that we defined

Below that we are adding the home name related information for each domain


DocumentRoot "H:/xampp/htdocs/project1"
ServerName project1.local

Order allow,deny
Allow from all


This should be copied for the second domain as well


DocumentRoot "H:/xampp/htdocs/project2"
ServerName project2.local

Order allow,deny
Allow from all

Step 4: Restart your Apache service or just restart your machine itself to make confirms that the Apache has read the settings

Now if you open a browser and calls http://project1.local it will redirect into the folder

H:/xampp/htdocs/project2.

No comments:

Post a Comment