Ask a question »

Configuring Apache and Windows to create a virtual host

September 2015


Configuring Apache and Windows to create a virtual host



Introduction


As a webmaster, upon creating your first site, you may want to install a program like WAMP or EasyPhp. You then put your site in the /www/ directory and it is then accessible via the URL: http://localhost/

Issue


But will happen if you start to develop several sites simultaneously?

You end up with a multitude of sub folders, and URLs to extension, for example:

http://localhost/my_folder1/.../my_folderX/myprojet/index.html

Which will act as the site root url in the development phase.

The problem is that absolute and relative URLs do not work when you host your site (live) and sometimes you may be confronted to legnthy urls to access the site, etc. ...) then this tutorial will be helpful .

The objective of the tutorial


We will make use of short addresses such as: http://myproject.dev/

Prerequisites

  • This tutorial was written for people working under Windows environment, and using Wamp server. But you will probably have no difficulty to implement this method with other software.
  • Assuming you have installed WAMP under C:\wamp\.
  • Assuming you have placed your current project under C:\wamp\www\MyProject\.
  • You need a text editor, such as Notepad.

Set the host for windows

  • Open the file:
    • C:\Windows\System32\drivers\etc\hosts
  • At line 20 you should have something like this:


127.0.0.1       localhost   
::1             localhost


Add line

127.0.0.1       myprojet.dev  

And save the file.

Note that


If you can not edit the file, verify that it is not read-only.
Under Vista, you may encounter more problems with the UAC. Make a copy of the file on your desktop, set the write permissions, make the change, then put the file in read-only, and overwrite the original file.

Configuring Apache

Solution 1


Launch Wamp click on the icon in the taskbar, click on "Apache" and you should see entries of the dropdown the httpd.conf file. Open this file and make the necessary changes.
  • You can find this file in
    • C:\wamp\bin\apache\apacheX.X.X\conf\httpd.conf.


At the end of the file, add this:

1)   
#####   
## localhost   
## MAIN DOMAIN   
#####   
NameVirtualHost localhost    

<VirtualHost localhost>    
    DocumentRoot C:/wamp/www/   
    ServerName localhost   
</VirtualHost>  

2) and

#####   
## myproject.dev   
## DOMAIN of myproject   
#####   
NameVirtualHost myproject.dev    

<VirtualHost myproject.dev>    
    DocumentRoot C:/wamp/www/myproject/   
    ServerName myproject.dev   
</VirtualHost>  

> 




It is essential to properly put the two host definitions. If you do that part 2) then the url http://localhost/ will bring up the folder containing your project. This is not desired, because you need to access the directory /www/.
  • Save the file.
  • Restart your apache server.

Solution 2


Editing the "httpd.conf" file can be tricky when you have not mastered the subject entirely. This file is essential for the proper operation of Apache, a simple error can prevent the server from starting.

To avoid modifying this file, WAMP provides a default alias file ( C:\wamp\alias\ )

In this folder, create a new text file and name it: localhost.conf, then copy the contents:

#####   
## localhost   
## DOMAINE principal   
#####   
NameVirtualHost localhost    

<VirtualHost localhost>    
    DocumentRoot C:/wamp/www/  
    ServerName localhost   
</VirtualHost> 



Also in the same folder, create a new text file and name it: myprojet.dev.conf, then copy the contents:
##### 
# # Myprojet.dev 
# # FIELD of MyProject 
##### 
NameVirtualHost monprojet.dev 

<VirtualHost Myprojet.dev> 
    DocumentRoot C: / wamp / www / MyProject / 
    ServerName monprojet.dev 
</ VirtualHost> 


Save them and then restart the Apache server.

Note that


If an error is made when creating an alias, just delete the concerned file to find the original configuration.

Note that


If everything went well:

Thanks to gryzzly for this tip.
For unlimited offline reading, you can download this article for free in PDF format:
Configuring-apache-and-windows-to-create-a-virtual-host.pdf

See also

In the same category

Original article published by gryzzly. Translated by jak58. - Latest update by Jeff
This document entitled « Configuring Apache and Windows to create a virtual host  » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.