apache How to create virtual host in Apache Virtual Host In WAMP


Example

Assuming that you are working with Windows 7 PC

Step 1: GOTO -> C:\Windows\System32\drivers\etc Where you will find a file named “hosts”, kindly copy it and paste it at the same location. A copy file of hosts will be created there.

Now we need to make some modifications in this file but if you try to edit it with any editor like notepad or notepad++, it will not allow you to save the file.

Now again copy the same file and paste it on your desktop, now you can edit this file easily.

You will find one or many entries like: 127.0.0.1 localhost In that file. Now add another line below that line, for example: 127.0.0.1 myproject1.local By this way you have defined a new sub-domain “myproject1.local” which can work in place of “localhost/myproject1”.

Step 2: Okay, now it’s time to define the root path to access this newly created domain right? GOTO : C:\wamp\bin\apache\Your-Apache-Version\conf\extra Here you will find a file named “httpd-vhosts”. Open it in editor and paste the below lines in it.

<VirtualHost *:80> 
    ServerAdmin webmaster@dummy.example.com 
    DocumentRoot "c:/wamp/www/myproject1/” 
    ServerName myproject1.local 
    ErrorLog "logs/myproject1.local-error.log" 
    CustomLog "logs/myproject1.local.log" common 
</VirtualHost>

Now you are almost there to access the project which resides at “c:/wamp/www/myproject1/”

Step3: GOTO : C:\wamp\bin\apache\your-Apache-Version\conf

Find a file named “httpd.conf”, copy it and paste it at the same location for safety. Open file in editor and find a word “# Virtual hosts”, below you will find a line “Include conf/extra/httpd-vhosts.conf” If it is commented then make it uncommented and restart your wamp-server’s services.

Go to your web-browser and write myproject1.local, you can see the project running now.

Now you might face a problem that your localhost will not work by using localhost as a URL. No Worries…paste this code in “httpd-vhosts” file.

<VirtualHost *:80> 
    ServerAdmin webmaster@dummy.example.com 
    DocumentRoot "c:/wamp/www" 
    ServerName localhost 
    ErrorLog "logs/localhost-error.log" 
    CustomLog "logs/localhost.log" common 
</VirtualHost>

Restart all the services of WAMP, the work is done.

Thanks & cheers Chintan Gor