<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Postpostmodern &#187; DNS</title>
	<atom:link href="http://postpostmodern.com/tag/dns/feed/" rel="self" type="application/rss+xml" />
	<link>http://postpostmodern.com</link>
	<description>Speaking of web development.</description>
	<lastBuildDate>Tue, 08 Jun 2010 18:46:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Smarter MAMP</title>
		<link>http://postpostmodern.com/instructional/a-smarter-mamp/</link>
		<comments>http://postpostmodern.com/instructional/a-smarter-mamp/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 16:08:44 +0000</pubDate>
		<dc:creator>Jason Johnson</dc:creator>
				<category><![CDATA[Instructional]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[BIND]]></category>
		<category><![CDATA[DNS]]></category>
		<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://postpostmodern.com/?p=170</guid>
		<description><![CDATA[Create a whole new site on your Mac just by creating a new folder thanks to BIND and Apache's dynamic virtual hosts.]]></description>
			<content:encoded><![CDATA[<p>This past weekend, I was cleaning up my ~/Sites folder, and I started thinking, &#8220;There&#8217;s got to be a better way!&#8221; And I wasn&#8217;t referring to <a href="http://www.adultswim.com/video/?episodeID=8a25c3921a8130c6011a8249afab001b">baby hammock</a>. I was talking about having to set up all those virtual hosts and whatnot. Well, a little research quickly brought me to the <em>&#8220;how did I not know this before?!&#8221;</em> point. And now, I will share it with you.</p>

<p>(Editor&#8217;s Note: <em>This article turned out to be a little longer than I expected. The whole process is really quite simple. It should only take about 10 minutes, at the most.</em>)</p>

<p>The first thing I didn&#8217;t realize is that Apache supports something called dynamic virtual hosts. That means you don&#8217;t have to create a virtual host in your conf files and restart Apache every time you want to create a new site. This is incredibly handy for development. Combine that with a real DNS server (BIND), and you have a much smarter system. I can now create a whole new site on my MacBook just by creating a new folder! Much simpler than the old way.</p>

<h2>The Old Way?</h2>

<p>To put this in context, let&#8217;s quickly review the old way. For years, I&#8217;ve developed websites on Mac OS X using the standard &#8216;MAMP&#8217; setup: Mac/Apache/MySQL/PHP. When starting a new site, the first thing I do is set up a virtual host for Apache. If you&#8217;re like me, you know this as a 4-step process:</p>

<ol>
<li><p>Create a directory for the site&#8217;s files. Something like:</p>

<p><kbd class="block">mkdir -p ~/Sites/domain.com/public</kbd></p></li>
<li><p>Create a line in /etc/hosts for the domain:</p>

<p><kbd class="block">127.0.0.1 domain.dev</kbd></p></li>
<li><p>Add a few lines to my Apache conf file to configure the virtual host:</p>

<pre><code>&lt;VirtualHost *&gt;
  ServerName domain.dev
  DocumentRoot /Users/jason/Sites/domain.com/public
&lt;/VirtualHost&gt;
</code></pre></li>
<li><p>Restart Apache:</p>

<p><kbd class="block">sudo apachectl graceful</kbd></p></li>
</ol>

<h2>The New Way!</h2>

<ol>
<li><p>Create a directory for the site&#8217;s files. Something like:</p>

<p><kbd class="block">mkdir -p ~/Sites/domain.com/public</kbd></p></li>
</ol>

<p>Done.</p>

<p>Not only that, but I also decided to set up a few TLDs to separate my sites. I use <strong>.ppm</strong> for my personal and freelance sites, <strong>.dev</strong> for my experimental stuff, and <strong>.bsi</strong> for my company work. This allows me to separate sites like so:</p>

<pre><code>Sites
|-- bsi
|   |-- this
|   |-- that
|   `-- theother
|-- dev
|   |-- experiment1
|   |-- experiment2
|   |-- youget
|   `-- thepoint
`-- ppm
    |-- postpostmodern
    `-- littlebeestudio
</code></pre>

<p>These would be:</p>

<ul>
<li>this.bsi</li>
<li>that.bsi</li>
<li>theother.bsi</li>
<li>postpostmodern.ppm</li>
<li>littlebeestudio.ppm</li>
<li>experiment1.dev</li>
<li>experiment2.dev</li>
<li>youget.dev</li>
<li>thepoint.dev</li>
</ul>

<h2>First, let&#8217;s talk about the hosts file.</h2>

<p>The first thing you need for an Apache virtual host is a unique hostname. As can be seen in step 2, above, this is usually accomplished via the /etc/hosts file. The only problem is the hosts file doesn&#8217;t support any wildcards. So, you can&#8217;t say:</p>

<pre><code>127.0.0.1    *.dev
</code></pre>

<p>Instead, you have to have this:</p>

<pre><code>127.0.0.1    postpostmodern.dev
127.0.0.1    littlebeestudio.dev
...
...
</code></pre>

<p>&#8230;and eventually, you end up with a hosts file a mile long.</p>

<h2>Enter BIND — the built-in, but inactive DNS server</h2>

<p><a href="http://en.wikipedia.org/wiki/BIND">BIND (named)</a> comes with Mac OS X. We just need to configure it and turn it on.</p>

<p><em>I should mention here that DNS server stuff falls outside of my comfort zone. I just followed a few articles (on <a href="http://www.macosxhints.com/article.php?story=2005110220352084">macosxhints</a> and <a href="http://ubuntuforums.org/showpost.php?p=2220686&#038;postcount=20">Ubuntu Forums</a>) to get this working. I&#8217;m not sure how it impacts the vulnerability of your Mac from a security standpoint.</em> All I know is that it works and provides a few advantages over the hosts file:</p>

<ul>
<li>You can set up TLDs to resolve to your local IP address. So, anything.dev and anything.test will automatically stay local.</li>
<li>Apparently, since BIND will be caching DNS info, it will make web browsing faster. I haven&#8217;t formally tested this, but if it&#8217;s true, it&#8217;s a nice bonus. It does seem a little quicker.</li>
</ul>

<h3>Setting up rndc</h3>

<p>This creates a configuration file and key for rndc, which controls named.</p>

<p>Get into sudo, and make it stick.</p>

<p><kbd class="block">sudo -s</kbd></p>

<p>Generate the conf file.</p>

<p><kbd class="block">rndc-confgen > /etc/rndc.conf</kbd></p>

<p>Copy the key to the key file.</p>

<p><kbd class="block">head -n 6 /etc/rndc.conf > /etc/rndc.key</kbd></p>

<p>Exit sudo.</p>

<p><kbd class="block">exit</kbd></p>

<h3>Creating your DNS zone files</h3>

<p>DNS zones are created via files in /var/named. Create a new file in there called dev.zone and fill it with this:</p>

<pre><code>;
; BIND data file for dev sites
;
$TTL    604800
@       IN      SOA     dev. root.dev. (
                     2008101920         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
@       IN      NS      dev.
@       IN      A       127.0.0.1
*.dev.  14400   IN      A       127.0.0.1
</code></pre>

<p>Repeat the above for each TLD you want to set up, replacing &#8216;dev&#8217; of course.</p>

<h3>Configuring named.conf</h3>

<p>Now, open /etc/named.conf. The first thing you want to do here is to show named where to get its DNS info (for the rest of the internet), i.e. forwarding servers. Let&#8217;s use OpenDNS. Add these lines in the <strong>options</strong> section of named.conf (after &#8216;directory &#8220;/var/named&#8221;;&#8217;):</p>

<pre><code>forwarders {
    208.67.222.222;
    208.67.220.220;
};
</code></pre>

<p>Now, we just need to let named know about those zone files we created a minute ago. For each of the zone files, create a section like this:</p>

<pre><code>zone "dev" IN {
  type master;
  file "dev.zone";
};
</code></pre>

<p>You&#8217;ll see where to put it. There&#8217;s a &#8216;localhost&#8217; section already there. Just put yours below that.</p>

<h3>Configuring and loading the LaunchDaemon</h3>

<p>Okay. One last thing. Tell Mac OS X to activate named. Open the LaunchDaemon plist file for named (I had to use Textmate because Property List Editor didn&#8217;t let me save a file belonging to root.):</p>

<p><kbd class="block">/System/Library/LaunchDaemons/org.isc.named.plist</kbd></p>

<p>Change &#8216;disabled&#8217; from true to false, and save the file.</p>

<p>Now, load it:</p>

<p><kbd class="block">sudo launchctl load /System/Library/LaunchDaemons/org.isc.named.plist</kbd></p>

<p>If everything went well, your DNS server should be up and running, and your personalized TLDs should resolve to your local machine. Try visiting something.dev and see if it resolves correctly. You might want to comment out all those custom lines from your /etc/hosts file too.</p>

<h2>Now for the Apache Magic!</h2>

<p>Now that the DNS stuff is out of the way, it&#8217;s just a matter of setting that magic directive in your Apache conf file. The directive is called <a href="http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html">VirtualDocumentRoot</a>.</p>

<p>I don&#8217;t know how you have your Apache configured, but personally, I like to keep all of my custom configuration in my own file (the one in /etc/apache2/users). Here is what my /etc/apache2/users/jason.conf file looks like:</p>

<pre><code>DocumentRoot "/Users/jason/Sites/default/public/"

NameVirtualHost 127.0.0.1

&lt;VirtualHost 127.0.0.1&gt;
    VirtualDocumentRoot /Users/jason/Sites/%-1/%-2+/public
&lt;/VirtualHost&gt;
</code></pre>

<p>The DocumentRoot directive is for the default site — the site that comes up when you visit http://localhost. The NameVirtualHost should be the reverse IP for your local machine. Finally, the VirtualDocumentRoot is an interpolated path for finding your sites. &#8216;%-1&#8217; means the last part of the domain name (the TLD). &#8216;%-2+&#8217; means everything before that. So, http://example.dev/ would load files from /Users/jason/Sites/dev/example/public. If you want a different scheme, read more about <a href="http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html#interpol">directory name interpolation on Apache&#8217;s web site</a>.</p>

<p>Now, restart Apache!</p>

<p><kbd class="block">sudo apachectl graceful</kbd></p>

<p>You should be good to go!</p>

<p>Please let me know your thoughts and corrections in the comments.</p>

<ins>

<h2>Update – 28 Oct, 2008</h2>

<p>As Brian Toth mentioned in the comments, it&#8217;s probably also necessary to add 127.0.0.1 in your DNS settings in the Network System Pref pane.</p>

</ins>
]]></content:encoded>
			<wfw:commentRss>http://postpostmodern.com/instructional/a-smarter-mamp/feed/</wfw:commentRss>
		<slash:comments>43</slash:comments>
		</item>
	</channel>
</rss>
