Mass VHOST wildcard; subdomain to folder

I couldn’t find any information as my search terms were incorrect. My google-fu was off when I was searching.
My objective was to make http://*.localhost.com go to /var/www/*/www, where * is a wildcard.
I’m hoping that this will help others; specifically developed that work on multiple projects.
First, I wanted to wildcard my in my hosts file. I knew I couldn’t do this:

127.0.0.1 *.localhost.com

as that’s now how hosts files work.
DNSMASQ to the rescue, to wildcard the hosts file.
Apache mass vhost, is a technique to setup lots of vhosts based on a pattern.
Some applications such as magento likes to have a proper full domain name.  Also makes testing more realistic as well.
Since I’m on OSX, install it via ports.
The instructions are a mash of: http://inspirix.wikispaces.com/dnsmasq-on-OSX and other sites… you may have to restart dnsmasq after you’ve made the changes.
http://www.justincarmony.com/blog/2011/07/27/mac-os-x-lion-etc-hosts-bugs-and-dns-resolution/

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

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *