The original problem:
Since I jump from project to project a lot, I have lots of project folders.
Summary of the solution:
use dnsmasq, and a mass vhost setup.
Problem in detail:
Normally, you’d have to:
- create a subdirector for your project
- all the standard setup stuff for your project.
- Setup an apache VHOST
- Edit your hosts file
What I wanted to accomplish was avoiding fiddling with the vhost and hosts file, each and every time I setup a new project.
I wanted to map all http://<wildcard>.localhost.com to /www/hosts/wildcard.localhost.com/www
The solution I found that works for me:
- Dnsmasq, which will auto resolve *.localhost.com to 127.0.0.1
- Install
- OSX: $> sudo port install dnsmasq
- Debian/Ubuntu: $> sudo apt-get install dnsmasq
- Dnsmasq Setup
- Editing the /etc/dnsmasq.conf file (with the appropriate permissions)
- $> sudo nano /etc/dnsmasq.conf
- Add the following lines
address=/localhost.com/127.0.0.1
address=/.localhost.com/127.0.0.1
listen-address=127.0.0.1
- Setup your OS
- Make sure your OS is pointing to 127.0.0.1 as a DNS server.
- Restart dnsmasq, so that the dnsmasq changes take effect
- Apache, setup a dynamic mass vhost setup file.
# get the server name from the Host: header
UseCanonicalName Off
# splittable logs
LogFormat “%{Host}i %h %l %u %t \”%r\” %s %b” vcommon
CustomLog logs/access_log vcommon
<Directory /www/hosts>
# ExecCGI is needed here because we can’t force
# CGI execution in the way that ScriptAlias does
Options FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
RewriteEngine On
# a ServerName derived from a Host: header may be any case at all
RewriteMap  lowercase  int:tolower
## deal with normal documents first:
# allow Alias /icons/ to work – repeat for other aliases
RewriteCond  %{REQUEST_URI}  !^/icons/
# allow CGIs to work
RewriteCond  %{REQUEST_URI}  !^/cgi-bin/
# do the magic
RewriteRule  ^/(.*)$  /www/hosts/${lowercase:%{SERVER_NAME}}/www/$1
## and now deal with CGIs – we have to force a handler
RewriteCond  %{REQUEST_URI}  ^/cgi-bin/
RewriteRule  ^/(.*)$  /www/hosts/${lowercase:%{SERVER_NAME}}/cgi-bin/$1  [H=cgi-script]
Make a directory
$> mkdir -p /www/hosts
$> cd /www/hosts
Now for each domain you’re going to have repeat below:
Lets call say you want to start a project called sandbox.
$> mkdir –p sandbox.localhost.com/www
sandbox is the subdomain we are using.
Restart apache, so that the conf file will puck up the new domain.
In your browser, navigat to http://sandbox.localhost.com
You should see an empty folder…  the setup portion is not done.  You are now able to start developing your site in your new document root, /www/hosts/sandbox.localhost.com that maps to sandbox.localhost.com
NOTES:
mobile devices won’t be able to connect to this site… so you’ll either have to setup your router to use dnsmasq (dd-wrt), or another method.
Addition notes:
setup instructions for dnsmasq is available here:
http://inspirix.wikispaces.com/dnsmasq-on-OSX
http://www.justincarmony.com/blog/2011/07/27/mac-os-x-lion-etc-hosts-bugs-and-dns-resolution/
ignore the stuff below this... as it's more for my own sake... but it's here to share.
sudo port install dnsmasq
sudo cp�/opt/local/etc/dnsmasq.conf /etc/dnsmasq.conf
sudo mkdir -p /System/Library/StartupItems/DNSMASQ
3.) sudo mkdir -p /System/Library/StartupItems/DNSMASQ
4.) sudo nano /System/Library/StartupItems/DNSMASQ/DNSMASQ
#!/bin/sh
. /etc/rc.common
if [ "${DNSMASQ}" = "-YES-" ]; then
ConsoleMessage "Starting DNSMASQ"
/opt/local/sbin/dnsmasq
fi
5.) sudo nano /System/Library/StartupItems/DNSMASQ/Startup\ Parameters.plist
{
Description = "Local DNSMASQ Server";
Provides = ("DNS Masq");
OrderPreference = "None";
Messages =
{
start = "Starting DNSMASQ";
stop = "Stopping DNSMASQ";
};
}
6.) Add line to /etc/hostconfig
DNSMASQ=-YES-
7.) sudo chmod +x /System/Library/StartupItems/DNSMASQ/DNSMASQ
8.) sudo /System/Library/StartupItems/DNSMASQ/DNSMASQ
9.) In network settings make sure 127.0.0.1 is in your dns server list. If you do:
dig google.com
You should see something like this near the bottom (if not, your network settings need to
be applied.. try this to flush dns cache: dscacheutil -flushcache ):
;; SERVER: 127.0.0.1#53(127.0.0.1)
# test it.
ping somewhere.localhost.com
: PING somewhere.dev (127.0.0.1): 56 data bytes
: 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.031 ms
add the line to /etc/dnsmasq.conf
$> sudo nano -w /etc/dnsmasq.conf ## or your favourite editor, as root.
address=/localhost.com/127.0.0.1
Clear your dns cache.
dscacheutil -flushcache
Troubleshooting:
You may need to setup your network connection to point your DNS to localhost first.
sudo kill -HUP `cat /opt/local/var/run/dnsmasq.pid`
my vhosts Magic
Alias /phpmyadmin /opt/local/www/phpmyadmin
<directory "/www">
Options -Indexes FollowSymLinks
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
<directory "/opt/local/www/phpmyadmin">
Options -Indexes FollowSymLinks
AllowOverride AuthConfig FileInfo
Order allow,deny
Allow from all
UseCanonicalName Off
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog logs/access_log vcommon
# include part of the server name in the filenames
VirtualDocumentRoot /www/hosts/%1/www