I’ve spent a bit of time recently making it easier to spin up new sites locally, the goal being to avoid editing the Apache config and localhosts everytime I setup a new site. Eventually I just want to push a button, type in a site name and have a site generated (based on a template like HTML5 Boiler plate) and be able to hit the site with it’s own domain in all browsers (virtual machines included). So the 1st 2 steps needed for that dream is apache and dns config. I’ve taken a lot of pointers from Jason T Johnson’s excellent post here. I decided to go for a different approach to the DNS side of things, I went with Ghost instead of Bind as I was a little scared of Bind.
Like Jason I have a 3 tier projects folder, the top tier being a sort of category (eg Home, Clientname, ideas), in that folder I have site name typically using it’s public domain and finally under there will be a ‘web’ folder which is the actual site.
So to get this working in Apache automatically I just use this config file entry:
<VirtualHost *> VirtualDocumentRoot /Users/stu/Projects/%-1/%-2+/web </VirtualHost>
This is a Virtual document root, the clever bits being the %-1 and $-2+ wildcards which map to the domain name. In other words http://caracaldigital.com.home now maps to /Users/stu/Projects/home/caracaldigital.com/web. Again the great things about this is all future sites will be mapped to the the above ‘interpolations’, no more editing the conf file and restarting Apache.
To get the above mapping working in the browser, the final step to do either use something like Bind or adding the following line in your hosts file:
Rather than manually opening the hosts file and editing which is a bit long winded you could use a graphical tool like Virtual Host X and host buddy or use a neat little command line tool called Ghost (Here’s a little primer in how to use Ghost). I went the Ghost route because I wanted to further automate the entries, basically having the filesystem dictate what goes in the host file. I made the below applescript to generate the host entries based upon what is in my project folder.
set root to "Macintosh HD:Users:stu:Projects" set unixRoot to POSIX path of root set domain to "" set theUserName to do shell script ("security find-generic-password -gl MAMP | grep \"acct\" | cut -c 19-99 | sed 's/\"//g'") set thePassword to do shell script ("security 2>&1 >/dev/null find-generic-password -gl MAMP | cut -c 11-99 | sed 's/\"//g'") do shell script "ghost empty" user name theUserName password thePassword with administrator privileges tell application "Finder" set clients to every folder of alias root repeat with client in clients if first character of name of client is not "_" then set projects to every folder of client repeat with project in projects set projectSubFolders to every folder of project repeat with projectSubFolder in projectSubFolders if name of projectSubFolder is "web" then set domain to name of project & "." & name of client do shell script "ghost add " & domain user name theUserName password thePassword with administrator privileges end if end repeat end repeat end if end repeat end tell do shell script "ghost export >> '" & unixRoot & "/hosts.txt'" user name theUserName password thePassword with administrator privileges
You’ll notice I’m using the keychain for admin level permission credentials in lines 5 & 6, I borrowed this technique from Apache & MySql – No Password. On the last line I’m exporting all the hosts to a file which I somehow intend to use in my Windows virtual machine, not thought of a way yet but I’ve a few ideas, I’ll be putting the solution in some future post. Finally, as I mentioned earlier, I’m going to wrap all this in a ‘generate new site’ applescript which will take the site domain name as a parameter, and maybe some other options.