Create Virtual Hosts in Apache wamp xamp
When we install WAMP or XAMP for development purpose, it has one Virtual Host by default that is localhost. all projects or websites we create are sub-folders in that single Virtual Host.
and can can only put our projects inside default folder www for WAMP and htdocs for XAMP.
when we access our projects our urls are something like the following
1 2 3 |
localhost/project1 localhost/project2 localhost/project3 |
the main domain in all above URLs is localhost. all the 3 projects are parts of that single domain.
What is Virtual Host
Virtual hosting is a method using which we can share resources of one single server to host multiple projects or website with separate domain names.
if we want to have separate domain name for each or some of our projects let say for above 3 projects we want domain names as
1 2 3 |
project1.com project2.com project3.com |
now all these domain names are different from each other and they hold separate domain names.
It is easy to setup, just note few points.
How to setup Virtual Hosts in Apache
I will be demonstrating the steps assuming you have installed WAMP server, for XAMP and MAMP there will be little paths differences.
to setup one or more Virtual Hosts we have to edit only 2 files
- c:\wamp\bin\apache\apache2.2.23\conf\extra\httpd-vhosts.conf
- c:\Windows\System32\drivers\etc\hosts
Step 1- Editing httpd-vhosts.conf
Goto c:\wamp\bin\apache\apache2.2.23\conf\extra\
Open httpd-vhosts.conf in notepad or other editor and paste the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#FOR PROJECT-1 ServerName project1.com DocumentRoot "${INSTALL_DIR}/project1" <directory "${INSTALL_DIR}/project1"> Options +Indexes +Includes +FollowSymLinks +MultiViews AllowOverride All Require local #FOR PROJECT-2 ServerName project2.com DocumentRoot "${INSTALL_DIR}/project2" <directory "${INSTALL_DIR}/project2"> Options +Indexes +Includes +FollowSymLinks +MultiViews AllowOverride All Require local #FOR PROJECT-3 ServerName project3.com DocumentRoot "d:/xamp/htdocs/project3" <directory "d:/xamp/htdocs/project3"> Options +Indexes +Includes +FollowSymLinks +MultiViews AllowOverride All Require local |
Using these Virtual Hosts we can also put our project source code in different folder (other than default wwww)
After saving changes, restart WAMP server.
Step 2- Editing hosts file
Goto C:\Windows\System32\drivers\etc\
open notepad as an administrative, and open hosts file
At the end write the following 3 lines one for each project Domain Name
1 2 3 |
127.0.0.1 project1.com 127.0.0.1 project2.com 127.0.0.1 project3.com |
Now you will be able to access each project as a separate Domain Name
1 2 3 4 5 6 7 8 |
Listen 0.0.0.0:80 Listen [::0]:80 Listen 0.0.0.0:81 Listen [::0]:81 Listen 0.0.0.0:82 Listen [::0]:82 |
Isn’t it easy, let me know if you still have problem
Comments
[…] have also written a detailed blog on Virtual Hosts If you have setup custom virtual host for your website you can write environment variables in that […]