Apache HTTPD subdomain configuration with Virtual Hosts on a local machine

by oneafrikan on December 6, 2004

I’ve been busy setting up my home desktop to function as a development machine, and have gotten tired of the legacy file structure I’ve been using in the past –

C:httpdocs/sitename

which has a local URL of

http://sitename

. When you have a development, staging and a live version of the site, it get’s quite tricky, especially when you want to start mixing up absolute and relative URL’s, and more importantly you want to keep all the files related to one project in one place – which makes for an easy backup and restore.

So, I’ve decided to upgrade my file structure, partly as a result of GTD, and partly ‘cos I’ve gotten serious about getting some stuff out in the next few months, and know that a good file structure will support that. Of course, an upgraded file structure comes with a need to upgrade my local WAMP (Windows / Apache / MySQL / PHP) server config. Hence this post – think of it as a mini-tutorial.

If you’re thinking this is anal already, then don’t read any further!

OK – all you really need to do is switch off Apache, dive into your “httpd.conf” and “HOSTS” files, do an edit, and then switch the Apache service back on…. easy eh?

Before I go any further, WordPress tends to strip code from posts, so I’m going to have to strip out the really obvious bits and give you the stuff you *have* to change to make it work…

1. Open up your windows service control panel:
Start > Programs > Administrative tools > Services; then right click on Apache and stop.

2. Open up your Apache httpd.conf file, then go to:

C:Program Files/Apache Group/Apache/conf

and open up the file in your favourite text editor and go to the (usually) bottom of the file, and look for the bit about Virtual Hosts.

My local server Virtual Host used to have:

ServerName impistage with the DocumentRoot set to “C:/htstage” and the Directory set to “c:/htstage“.

My local Virtual Host now has it looking like:

ServerName staging.oneafrikan.com with the DocumentRoot set to C:/htstage/oneafrikan.com and the Directory set to C:/htstage/oneafrikan.com.

Can you spot the difference?

3. Open up your HOSTS file to add the changes to your system (so that Windows knows where to look when you ask for that domain):
Look for:

C:/WINNT/SYSTEM3/DRIVERS/ETC/HOSTS

and open up in your fav text editor.
Then add the new virtual hosts to the file (see my edited file below – note the differences) so that:

127.0.0.1 impistage would now become 127.0.0.1 staging.oneafrikan.com.

And that’s it really – fire up Apache again, and Bob should be your uncle…

This is really useful if you want to develop stuff with a structured approach, and if you have more than one site, or set of code you’re dealing with…

3 comments

Blessed you are!

I tried it, it worked.

Dam, I have always thought (still do) that LAMP setup is better, but then again making work on WAMP is even better. Yay!

Now, I can move those 8 sites I have been working on.

Yay!

by Binary 1 on March 28, 2005 at 6:32 am. Reply #

Awesome, thank you!

Just to let everyone know, though, you can add as many vhost as you wish, just add another entry into the hosts file as well (and localhost a.k.a. 127.0.0.1 can be pointed to multiple vhosts).

But, anyways, thanks a bunch once again!

by John on December 16, 2005 at 7:30 am. Reply #

No problem at all – I have about 30 sites running as Vhosts on one machine, so it’s really easy to do that.

;-)

by Gareth Knight on December 16, 2005 at 12:06 pm. Reply #

Leave your comment

Required.

Required. Not published.

If you have one.

Protected with IP Blacklist CloudIP Blacklist Cloud