We do the majority of our website development on macs, using MAMP as an easy to run webserver with Apache, PHP and MySQL. This setup is great for local development as the environment MAMP provides is very close to what most web hosts provide and the web server is easy to manage.
This setup works well to develop one site at a time, but when you are working on multiple sites and are jumping between them, it can become annoying to constantly have to change the document root and reboot the server. It is only recently that I’ve been motivated to configure Apache with what is known as virtual hosts to make development much easier. This allows you to run and access multiple sites simultaneously.
Below are the instructions on how to set this up under MAMP, but the instructions are pretty much the same for any Apache installation;
Shut down MAMP and Open MAMP’s httpd.conf file, located at /Applications/MAMP/conf/apache/httpd.conf
Scroll to the very bottom of the config file to find the lines:
The NameVirtualHost * line needs to be uncommented as above. I missed this first time around and was scratching my head for a long time.
This step may not be necessary, but it worked well for me. Add to the bottom of the config file a default entry to match the broadest directory you want to be able to access (this might be the folder you keep all you sites in). In the below example, clank is the name of my machine and/Users/ryan/Sites/ is where I keep all of my websites:
I wanted to access my development sites on my webserver via sub-domains, e.g. site1.clank, site2.clank. So for this step, add in a VirtualHost entry for each website. Some examples would be:
Take care with ending each document root with a slash.
Before the the virtual hosts will take effect, Apache needs to be restarted (just stop and start MAMP). The is only half of the process, so read on.
To be able to view the sites, any machine that wants to access the sites needs to be told where on the network these new subdomains are. On my local machine, I needed to edit the file/etc/hosts to look like the following:
As I’m editing this file on the same machine as the webserver, 127.0.0.1 is the loopback address, so all this is doing is saying to look at the local machine to find site1.clank, etc.
For another machine on the network, the same thing applies, but instead of the loop back address, the host’s IP address is needed. So on our other development machine it looks very similar, but instead of 127.0.0.1, a specific IP is given (in my case it is something like 192.168.1.102). Once the file is saved, the above sites should be accessible.
We use VirtualBox to be able to run Windows to test our sites in the various versions of Internet Explorer. These virtual machines need to be treated as if they were additional machines on the network and therefore the hosts file on Windows needs to be edited as well.