Configuring Apache and Windows to create a virtual host


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.
Published by jak58 - Last update on July 7, 2010 02:38 PM by jak58
This document entitled « Configuring Apache and Windows to create a virtual host  » from Kioskea.net (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the licence, as this note appears clearly.
Suggestions
  •  Configuring Apache and Windows to create a virtual host
  •  Windows 7 - Create a shortcut to clear the clipboard » Tips : [Windows 7]Create a shortcut to clear the clipboard To create a shortcut to clear the contents of the clipboard: On an empty space of your desktop> Right-Click>New > Shortcut In the field below "Type location of the item" enter the...
  •  [Windows 7] Creating a shortcut to Add/Remove Program » Tips : Create a shortcut to Add/Remove Program To create a desktop shortcut point to the "Add/Remove Program": Right Click on an empty area of your Desktop Select >New > Shortcut In the "Type the location of the item, type: rundll32.exe...
  •  [Windows 7 ]Creating a shortcut the Firewall settings » Tips : Creating a shortcut the Firewall settings To create a desktop shortcut to the Firewall settings Right Click on an empty area of your Desktop Select >New > Shortcut In the "Type the location of the item", type: rundll32.exe...
  •  [Windows 7] Create a new Library » Tips : [Windows 7] Create a new Library To create a new library: First go to Libraries Right Click > New > Library Once the Library is created , rename it to your convenience You can optimize the library for a certain type of data and include...
  •  [Windows 7]Create a shorcut to Notification Area Icons » Tips : [Windows 7]Create a shorcut to Notification Area Icons To create a shortcut allowing you to rapidly access the Notification Area Icons: Right-click on an empty space of your desktop > New > Shortuct In the "Type the location of the Item"...
PHP - date after function
Automate a popup