When switch­ing between mul­ti­ple projects, it really is a pain in the ass to go into your Apache con­fig files and start chang­ing direc­to­ries (the save, and restart your services…etc).  That’s why I looked into it more. I, by no means, claim to be any­thing when it comes to server con­fig­u­ra­tions, secu­rity, and such.  But if I can get some­thing work­ing a lot quicker on my local work­ing copy, I’m all for it.

Ini­tial­iz­ing Vir­tual Hosts

First, open up a text edi­tor.  There are three files we’re going to have to manip­u­late.  The first is the main Apache con­fig.  If you are using a stack, like WAMP, the file path will look some­thing like:

C:/WAMP/bin/apache/Apache2.2.10/conf/httpd.conf

Once you’ve got this file open, look for a line like this (mine was on line 497):

1
2
# Vir­tual hosts
# Include conf/extra/httpd-vhosts.conf

And change it to:

1
2
# Vir­tual hosts
Include conf/extra/httpd-vhosts.conf

All this is doing is remov­ing the com­ment sym­bol from the line that is includ­ing that file.

Cre­at­ing VHost

The next step is to start set­ting up the file we just included.  Again, if you have a stack like WAMP, the direc­tory will look some­thing like:

C:/WAMP/bin/apache/Apache2.2.10/conf/extra/httpd-vhosts.conf

You can read this file, but the impor­tant part is near the bot­tom.  There are code blocks that read some­thing like:

1

Each of these blocks is going to be where we set up new local domains.  The best part is that we get to cre­ate as many as we’d like.  Once you are fin­ished with a project, you can remove or leave a domain set­tings.  I choose to leave them in case I ever have to revisit that project.  I should not that once you include this sec­ond file, the ‘local­host’ domain (as said in the Apache guide) is removed.  If you’d like to rein­state it, you can recre­ate it here, and I’ll show you how.

Now, there are some dummy set­tings here, but you can just remove them, or use them as a tem­plate.  I’m going to use my local build of my site as an example.

1
2
3
Server­Ad­min justin@jstnjns.com
Doc­u­men­t­Root “W:/JSTNJNS/http“
Server­Name jstnjns

To start, I should not that the begin­ning tag should match exactly the string that fol­lows ‘Name__________’ on line 19.  As you can see, the first thing listed is the Server­Ad­min.  Just put your email, or remove this line alto­gether.  The sec­ond thing is the Doc­u­men­t­Root, or where the local work­ing copy direc­tory is.  Mine is on a sec­ond hard­drive W:/.  And the third, and very impor­tant, thing is the local domain we will be using, the Server­Name.  The above set­ting allows me to type into the address bar:

jst­njns

or

http://jstnjns/

Set­ting Permissions

Now, if you are using WAMP, there are some ini­tial set­tings in the httpd.conf file that we are going to need to change.  These alter­ations give us per­mis­sion to access the files we stated for the Server­Name.  So, reopen the httpd.conf, and look for the line that resembles:

1
.…

Note that there should be two of these, and one just con­tains “C:/”.  This is the base con­fig­u­ra­tion that allows no per­mis­sions.  We are going to want to change the direc­tory to some­thing more broad.  Now, if you are work­ing off the local drive (C:/), then you can reduce it to just that.  I, how­ever, am run­ning projects from mul­ti­ple dri­ves.  My direc­tory list­ing reads like this:

1
.…

This broad­ens it up to an “any­thing, and every­thing” approach.

Let­ting Win­dows Know What’s Going On

The third file that you have to open is:

C:/Windows/System32/drivers/etc/hosts

You can read the descrip­tion in the file, but basi­cally it’s what tells Win­dows that the domains we list in this file will be at the I.P. adress that we des­ig­nate.  In our case, we are using 127.0.0.1, our local I.P. address.  The first two lines read like this:

1
2
127.0.0.1   local­host
::1         localhost

We want to add a line for each domain we create.

1
2
3
127.0.0.1   local­host
::1         local­host
127.0.0.1   jstnjns

After adding this line, I should be able to save all the files I altered, restart the Apache ser­vice (if you are using WAMP, click on the WAMPSERVER icon in the taskbar and choose “Restart All Ser­vices”), and voila!  The next time you type in your cus­tom local domain, you’ll have your local work­ing build.  I find it also very use­ful to cre­ate a url for util­i­ties, like phpMyAdmin.