How to set up Apache Virtual Hosts

Published on February 3rd, 2013 by admin

Virtual hosts are a way to host multiple domain names on one machine. This makes it much easier to create content, organize projects and deploy to the remote server.

  • Apache installed and running.
  • A pure text editor like Notepad or Notepad++

What is a virtual host?

Virtual hosts are a way to host multiple domain names on one machine.

Uses of virtual hosting:

  • Shared web hosting:
  • Institution extranet:
  • Site development (we’re going to focus on this)

Site development with Virtual Hosts

Cleans up the URL access point to the project

Virtual hosts make it possible to specify a unique domain name for each of your projects. This eliminates the need to type the path in the URL.

Without virtual hosting:

http://localhost/myproject/site/

With virtual hosting:

http://www.myproject.local

Simplifies file URL paths.

A virtual host also simplifies the way you reference files; especially in regards to absolute paths.

Without virtual hosting:

<img src = “/myproject/site/images/myimage.jpg” alt = “” />
<a href = “/myproject/site/about.html” />Link</a>

With virtual hosting:

<img src = “/images/myimage.jpg” alt = “” />
<a href = “/about.html”>Link</a>

Provides flexibility for organizing projects

Because it cleans up the URL access point and simplifies the file and image path URLS, you can organize your projects anyway you want and still access the project from http://myprojectt.local

Below you can see an example of a project organized into different folders.
/www/myproject/sql
/www/myproject/PSD
/www/myproject/Documents
/www/myproject/site

Setting up Virtual Hosts on your WAMP machine

To do this requires three steps:

  1. modify the .hosts file
  2. modify the httpd.conf file
  3. modify the httpd-vhosts.conf file
  4. restart wamp

.hosts file

You can find this file in C:\Windows\System32\drivers\etc
At the bottom of this file add the ip address and the domain.

	127.0.0.1		myproject.local
	127.0.0.1		www.myproject.local

httpd.conf

You can find this file in C:\wamp\bin\apache\Apache2.2.17\conf\

Uncomment the line that says:

Include conf/extra/httpd-vhosts.conf

httpd-vhosts.conf

You can find this file in C:\wamp\bin\apache\Apache2.2.17\conf\extra

Make sure that the following is uncommented

NameVirtualHost *:80

Above the examples add the following, you’ll only have to do this once.

<VirtualHost *:80>
	DocumentRoot "C:\www"
	ServerName localhost
	<Directory \>
		AllowOverride All
		Allow from all
	</Directory>
</VirtualHost>

For each new project that you create add the following code to httpd-vhost.conf and provide values for the following:

  • DocumentRoot
  • ServerName
  • ServerAlias
<VirtualHost *:80>
	DocumentRoot "C:\www\myproject\site"
	ServerName myproject.local
	ServerAlias www.myproject.local
	<Directory />
		AllowOverride All
		Allow from all
	</Directory>
</VirtualHost>

Restart Wamp

Restart your wamp server.

See it in action

In your browser, type http://myproject.local (or whatever project url you provided) and see what happens!

Leave a Reply




Popular Posts

Posts By Category

Supported by