Creating multiple virtual hosts/websites in Wampserver


There are have many way to creae multiple virtul host in wampserver. Sometime you have problem with your wampserver by can’t running by type localhost.So now please :
Step 1: Set your hostnames or setup Windows to recognize your local websites
After installing WAMP server you can go to the browser and type http://localhost and it opens up a page. Windows by default recognizes the site localhost as referring to itself. We need to setup windows to recognize our other local websites. To do this we need to edit the “hosts” file. This file is found under the following directory. (Another way to reach this directory is, open the run command or press Windows Key + R, and then type “drivers” and press “OK” then go to “etc”.)
C:/Windows/System32/drivers/etc
After going to this directory open the file named “hosts” in this directory using your favorite texteditor (like Notepad, Editplus, Notepad++).
Note: You may not be able to edit or save this file in Windows Vista or Windows 7. You need administrator rights to edit this file.
Now, you would see a line at the end of the file follows:
   127.0.0.1   localhost
This code tells your computer to connect to your own computer when we try to go to http://localhost
Now we should add our site to this file. Lets say our site name is site1. So we would add an entry for it in the “hosts” file. The updated file would look similar to the following:
   127.0.0.1   localhost
127.0.0.1 site1
Save this file and try going to http://site1
This would take you to the same page as http://localhost
Now our computer knows what http://site1 is.
Note: Make sure you do not add host entries for external sites like google.com or bing.com. If you add these entries, then the computer would no longer be able to go to the actual websites.
Step 2: Create a folder for the website
Wampserver has only one folder or site root to host the website. This folder is typically “C:/wamp/www”. We should create a different folder for each website. We can create the folder in any directory or drive. (If we create the folder outside of “C:/wamp/www” we would need some additional settings in Apache described later).
Add some test file in this folder like “index.html” in say “C:/wamp/www/site1″ directory.
Step 3: Setup Apache to serve multiple sites
We would change the file “httpd.conf” and “httpd-vhosts.conf” for the Apache setting.
Open the file “httpd.conf” by clicking the WAMP server icon and selecting “Apache -> httpd.conf”. This file is typically located at “C:/wamp/bin/apache/Apache2.2.17/conf”.
Don’t add code to this file, its not a clean and easy way. We just want to un-comment one line in this file that include the vhosts file.
#Include conf/extra/httpd-vhosts.conf
Remove the # sign from the front of the line, the changed line should look as follows:
Include conf/extra/httpd-vhosts.conf
Now, we will open the file “httpd-vhosts.conf”, which would be typically located inside the extra directory (“C:/wamp/bin/apache/Apache2.2.17/conf/extra”)
In this file we will add virtual host using the following code:
    ServerAdmin admin@localhost
DocumentRoot "C:/wamp/www"
ServerName localhost
ServerAlias www.localhost.com
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" common
In the above code the ServerName and DocumentRoot are mandatory, other options are optional.
So if we want to have 3 sites on our local machine as follows
localhost -> C:/wamp/www
site1 -> C:/wamp/www/site1
site2 -> C:/site2
    ServerAdmin admin@localhost
DocumentRoot "C:/wamp/www"
ServerName localhost
ServerAlias www.localhost.com
ErrorLog "logs/localhost-error.log"
CustomLog "logs/localhost-access.log" common



DocumentRoot "C:/wamp/www/site1"
ServerName site1



DocumentRoot "C:/site2"
ServerName site2

Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all

I have not used all the options for site1 and site2, but it is a good practice to have them. Also notice that the code for site2 has some extra code for Directory. This is necessary to give the server access to that directory. If we don’t add this, the server will not be able to access the files in this directory and hence the site would not work. We don’t need this for site1 as its inside “C:\wamp\www” directory for which the directory setting are set by defult in wampserver.
Now restart Apache and all your sites should be working. Make sure you add site2 to your hosts file and create the directory for it at C:/site2.
Share on Google Plus

About Elmirakom

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment