I manage a number of different websites and most of the development has been done offline on a Windows 7 machine. I use the LAMP stack for all my sites and have always found the easiest method of setting up an equivalent WAMP stack on a Windows 7 machine was using XAMPP by Apache Friends. This has always worked fine but was a bit clunky when I wanted to change which website I was working on. It meant killing the Apache processes (httpd.exe) in the Windows task manager, editing the Apache conf file to change which site I was working on and then re-starting Apache. And when crap programs like Skype keep grabbing port 80 restarting Apache is always a pain in the butt.
There had to be an easier way so this week I took an hour out of my day to work out what it was. I already had Apache installed on my mini file server that runs Ubuntu so it was just a matter of getting that to work properly to serve up different sites. And that was (surprisingly) simple.
Edit the Hosts File on the Server
First step was to edit the hosts file on the linux machine with
sudo nano /etc/hosts
And then add entries for the sites I wanted to be served up locally. So, my hosts file ended up looking something like this:
127.0.0.1 localhost 127.0.0.1 local-site-1 127.0.0.1 local-site-2
Create Sites Available Files
I then had to create files for each site to sit in Apache’s sites available directory (/etc/apache2/sites-available/)
These files are pretty simple and in my case looked like this:
<VirtualHost *:80> ServerName local-site-1 DocumentRoot "/srv/path/to/local/site/1/html_root/" </VirtualHost>
Just change the server name to your local site name and the DocumentRoot to the path where the files for the site reside. In my case DocumentRoot is a SAMBA directory that is accessible from my windows machines (so I can edit the files from my dev machines). Name each file sensibly (in my case I named them local-site-1 and local-site-2).
Enable Sites and Reload Apache
Enabling the new sites is simple, just use the following command:
sudo a2ensite /etc/apache2/sites-available/local-site-1
Then reload the apache configuration with:
sudo /etc/init.d/apache2 reload
Edit the Windows Hosts File
The final step is to edit the hosts file on the Windows machines you want to access the local sites. On Windows 7 this can be found in:
%systemroot%\system32\drivers\etc\
I opened this in Notepad and changed it to look like this:
127.0.0.1 localhost 192.168.2.3 local-site-1 192.168.2.3 local-site-2
Note that 192.168.2.3 is the IP address of my file server.
Now if I want to access one of these local sites in a browser I just need to type local-site-1 into the address bar and hey presto I see the local copy of the website served up by my file server. I love increased productivity!
Potential Improvement
One potential improvement to this process is to remove the need to edit Windows hosts files by installing a DNS server (like dmasq) that will resolve the local site URL’s into an IP address. Of course this would require changing the DNS settings on the Windows machines.