When switching between multiple projects, it really is a pain in the ass to go into your Apache config files and start changing directories (the save, and restart your services…etc). That’s why I looked into it more. I, by no means, claim to be anything when it comes to server configurations, security, and such. But if I can get something working a lot quicker on my local working copy, I’m all for it.
Initializing Virtual Hosts
First, open up a text editor. There are three files we’re going to have to manipulate. The first is the main Apache config. If you are using a stack, like WAMP, the file path will look something like:
C:/WAMP/bin/apache/Apache2.2.10/conf/httpd.confOnce you’ve got this file open, look for a line like this (mine was on line 497):
1 2 | # Virtual hosts # Include conf/extra/httpd-vhosts.conf |
And change it to:
1 2 | # Virtual hosts Include conf/extra/httpd-vhosts.conf |
All this is doing is removing the comment symbol from the line that is including that file.
Creating VHost
The next step is to start setting up the file we just included. Again, if you have a stack like WAMP, the directory will look something like:
C:/WAMP/bin/apache/Apache2.2.10/conf/extra/httpd-vhosts.confYou can read this file, but the important part is near the bottom. There are code blocks that read something 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 create as many as we’d like. Once you are finished with a project, you can remove or leave a domain settings. I choose to leave them in case I ever have to revisit that project. I should not that once you include this second file, the ‘localhost’ domain (as said in the Apache guide) is removed. If you’d like to reinstate it, you can recreate it here, and I’ll show you how.
Now, there are some dummy settings here, but you can just remove them, or use them as a template. I’m going to use my local build of my site as an example.
1 2 3 | ServerAdmin justin@jstnjns.com DocumentRoot “W:/JSTNJNS/http“ ServerName jstnjns |
To start, I should not that the beginning tag should match exactly the string that follows ‘Name__________’ on line 19. As you can see, the first thing listed is the ServerAdmin. Just put your email, or remove this line altogether. The second thing is the DocumentRoot, or where the local working copy directory is. Mine is on a second harddrive W:/. And the third, and very important, thing is the local domain we will be using, the ServerName. The above setting allows me to type into the address bar:
jstnjnsor
http://jstnjns/Setting Permissions
Now, if you are using WAMP, there are some initial settings in the httpd.conf file that we are going to need to change. These alterations give us permission to access the files we stated for the ServerName. So, reopen the httpd.conf, and look for the line that resembles:
1 | .… |
Note that there should be two of these, and one just contains “C:/”. This is the base configuration that allows no permissions. We are going to want to change the directory to something more broad. Now, if you are working off the local drive (C:/), then you can reduce it to just that. I, however, am running projects from multiple drives. My directory listing reads like this:
1 | .… |
This broadens it up to an “anything, and everything” approach.
Letting Windows Know What’s Going On
The third file that you have to open is:
C:/Windows/System32/drivers/etc/hostsYou can read the description in the file, but basically it’s what tells Windows that the domains we list in this file will be at the I.P. adress that we designate. 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 localhost ::1 localhost |
We want to add a line for each domain we create.
1 2 3 | 127.0.0.1 localhost ::1 localhost 127.0.0.1 jstnjns |
After adding this line, I should be able to save all the files I altered, restart the Apache service (if you are using WAMP, click on the WAMPSERVER icon in the taskbar and choose “Restart All Services”), and voila! The next time you type in your custom local domain, you’ll have your local working build. I find it also very useful to create a url for utilities, like phpMyAdmin.