How to use virtual host in WAMP server in PC

Website development in local computer or PC has lots many configuration. Developers who are using WAMP (Apache, MySQL and PHP bundle for windows) server can develop the website by putting the project files into the WAMP installation folder which is most of the time:

c:\wamp\www\folder_name\

and you need to access your test site as:

http://localhost/folder_name

where the folder name is your project folder name where index.php file exists. As a developer you must have thought of accessing your site as a general URL and also keeping your project folder and files outside the WAMP directory. To accomplish this you need to setup the virtual host into the WAMP server and this blog describe details steps involved to accomplish the goal.

Setting up virtual host in WAMP server involves the below steps:

Step 1: Enable the Virtual host configuration in Apache's httpd.conf file. Open the httpd.conf file usually located at "C:\wamp\bin\apache\apache2.4.9\conf" and find the below settings:

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

Initially the virtual host inclusion is commented. You need to uncomment it i.e remove the # from the Include command. So after the change the above two lines will look like:

# Virtual hosts
 Include conf/extra/httpd-vhosts.conf

Save the file. In this step you have actually enabled virtual host feature in WAMP's Apache server.

Step 2: Update the virtual hos configuration file httpd-vhosts.conf which is located under folder "C:\wamp\bin\apache\apache2.4.9\conf\extra". Here you need to add the entry of your custom host accordingly. Lets assume that your host name is example.dev and your project root folder is "d:\projects\example\www\". So the configuration should be added to the file is:

 <VirtualHost *:80>
     DocumentRoot "d:/projects/example/www"
     ServerName example.dev
     ServerAlias www.example.dev
     <Directory  "d:/projects/example/www">
        AllowOverride All
        Require all granted
     </Directory>
 </VirtualHost>

Now this entry will enable the virtual host to redirect to your custome folder which is outside of the wamp installation directory and you can access your web root by typing example.dev in the browser address bar.

Step 3: Change the windows host file located at "C:\Windows\System32\drivers\etc". open notepad application in administrator mode and open the file "hosts" located in the above mentioned directory. Enter the below lines into it and then save it:

127.0.0.1       example.dev
::1             example.dev

 

Step 4: Restart all services in WAMP and now you should be able to access the index.php located in your project root directory by typing the example.dev in the browser's address bar.

Watch the complete video guide on YouTube.

Last but not the least, How to access localhost?

Now since you have enabled the virtual host, you also need to enter the local host details in the httpd-vhosts.conf which is located under folder "C:\wamp\bin\apache\apache2.4.9\conf\extra". Below will be the entry for localhost:

<VirtualHost *:80>
     DocumentRoot "c:/wamp/www"
     ServerName localhost
     ServerAlias localhost
     <Directory  "c:/wamp/www">
        AllowOverride All
 	Require local
     </Directory>
</VirtualHost>


Note: Change your project's host name and root folder based on your need but don't forget to follow the steps mentioned.

 

Comments