<?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>themattharris.com &#187; vhost</title>
	<atom:link href="http://themattharris.com/tag/vhost/feed/" rel="self" type="application/rss+xml" />
	<link>http://themattharris.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 01 Jun 2010 17:30:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How To: MacOS, Apache and Virtual Hosts</title>
		<link>http://www.themattharris.com/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fthemattharris.com%2F2009%2F02%2Fhowto-macos-apache-and-virtual-hosts%2F&amp;seed_title=How+To%3A+MacOS%2C+Apache+and+Virtual+Hosts</link>
		<comments>http://www.themattharris.com/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fthemattharris.com%2F2009%2F02%2Fhowto-macos-apache-and-virtual-hosts%2F&amp;seed_title=How+To%3A+MacOS%2C+Apache+and+Virtual+Hosts#comments</comments>
		<pubDate>Wed, 25 Feb 2009 22:49:46 +0000</pubDate>
		<dc:creator>themattharris</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[leopard]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[macos]]></category>
		<category><![CDATA[vhost]]></category>
		<category><![CDATA[virtual host]]></category>
		<category><![CDATA[vitualhost]]></category>

		<guid isPermaLink="false">http://themattharris.com/?p=209</guid>
		<description><![CDATA[When developing a website I&#8217;m sure many of you are checking the pages work and look the way they should. If you&#8217;re only working on one site this isn&#8217;t too much of problem as you can place the site into your ~/Sites folder, turn on web sharing in Preferences and run the site from your [...]]]></description>
			<content:encoded><![CDATA[<p>When developing a website I&#8217;m sure many of you are checking the pages work and look the way they should.  If you&#8217;re only working on one site this isn&#8217;t too much of problem as you can place the site into your <code>~/Sites</code> folder, turn on web sharing in Preferences and run the site from your local machine.  If you&#8217;re like me though, and work on multiple sites at once, this isn&#8217;t a good idea.  It&#8217;s an inefficient workflow and there is a hugh risk of data loss resulting from the need to move and copy files in and out of the folder.</p>

<p>A better way, which many of my professional colleagues and friends do, is to use MAMP or headdress.  These two products provide a graphical interface to allow you to set up hosts on your computer and point the host to a folder on your computer.  This is great except when you want to start developing with multiple languages, or wish to test SSL or external libraries.  It also costs money as the virtual hosts ability is part of MAMP Pro, not the free MAMP edition.</p>

<p>So, although each of these products has their own strengths, is there a way to do what they do without using them.  Luckily enough there is, and i&#8217;ll talk you through it now.</p>

<h2>Assumptions</h2>

<p>First off, i&#8217;m going to make some assumptions:</p>

<ol>
<li>You&#8217;re using a Mac</li>
<li>You&#8217;re using Mac OS Leopard version 10.5.2 or higher</li>
<li>You know what the terminal is and</li>
<li>You know what the file paths are in MacOS</li>
<li>You&#8217;ve turned MAMP off and reset anything setup by Headdress</li>
</ol>

<h2>What you&#8217;ll need</h2>

<ul>
<li>About 30 minutes of time (although you can do this in 2 minutes if you know your way around the shell)</li>
<li>4 files

<ol>
<li>/etc/hosts</li>
<li>/etc/apache2/extra/httpd-vhosts.conf</li>
<li>/etc/apache2/users/USERNAME.conf (where username is the short username for your logon to your computer)</li>
<li>/etc/apache2/httpd.conf</li>
</ol></li>
<li>A text editor. I use TextMate which means I can go into the terminal and type <code>mate /path/to/file</code> to edit the files. I&#8217;m going to use this notation throughout so remember to substitute <code>mate</code> with your favoured text editor.</li>
</ul>

<h2>Step 1: Allowing Overrides and Options</h2>

<p>Open terminal and navigate to /etc/apache2/users and then list the contents of the folder.</p>

<p><pre class="brush: plain;">cd /etc/apache2/users
ls -l</pre></p>

<p>You will see a <code>.conf</code> file for each account on your computer. The one you want is the one which matches your short username for MacOS. In my case it is <code>matt.conf</code></p>

<p>Edit this file in your favourite editor</p>

<p><pre class="brush: plain;">mate /etc/apache2/users/matt.conf</pre></p>

<p>By default this file will look like this:</p>

<p><pre class="brush: plain;">&lt;Directory &quot;/Users/matt/Sites/&quot;&gt;
    Options Indexes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
&lt;/Directory&gt;</pre></p>

<p>You need to change the <code>Options</code> and <code>AllowOverride</code> lines to <code>All</code>, so the file should now look like:</p>

<p><pre class="brush: plain;">&lt;Directory &quot;/Users/matt/Sites/&quot;&gt;
    Options All
    AllowOverride All
    Order allow,deny
    Allow from all
&lt;/Directory&gt;</pre></p>

<p>Save the file.</p>

<h2>Step 2: Configuring Apache</h2>

<p>Open <code>/etc/apache2/httpd.conf</code> and make the following changes (the line numbers are correct for a fresh install but could be slightly different on your machine. The content remains the same though):</p>

<table>
<tr>
<th>Line</th>
<th>Before</th>
<th>After</th>
<th>Change</th>
</tr>
<tr>
<td>114</td>
<td><code>#LoadModule php5_module libexec/apache2/libphp5.so</code></td>
<td><code>LoadModule php5_module libexec/apache2/libphp5.so</code></td>
<td>ensure the # is not present</td>
</tr>
<tr>
<td>461</td>
<td><code>#Include /private/etc/apache2/ extra/httpd-vhosts.conf</code></td>
<td><code>Include /private/etc/apache2/ extra/httpd-vhosts.conf</code></td>
<td>ensure the # is not present</td>
</tr>
</table>

<p>Save the file.</p>

<h2>Step 3: Configuring your virtual hosts</h2>

<p>Open <code>/etc/apache2/extra/httpd-vhosts.conf</code>. Everything after line 26 is an example of what needs to go in the file and can safely be removed.  Alternatively you can comment the lines out with <code>#</code> if you prefer.</p>

<p>Here&#8217;s an example of what to put here, assuming:</p>

<ul>
<li>My website files are kept in <code>/Users/matt/Sites/themattharris.com</code> (remember this is case sensitive)</li>
<li>I want to test the server with the DNS name <code>www.themattharris.com</code></li>
</ul>

<p>In this case I would put the following entry into the <code>httpd-vhosts.conf</code> file:</p>

<p><pre class="brush: plain;">&lt;VirtualHost *:80&gt;
   DocumentRoot &quot;/Users/matt/Sites/themattharris.com&quot;
   ServerName www.themattharris.com
&lt;/VirtualHost *:80&gt;
</pre></p>

<h2>Step 4: Make the computer think the domain is on the localhost</h2>

<p>Open <code>/etc/hosts</code>. You&#8217;ll see something like:</p>

<p><pre class="brush: plain;">&#35;#
&#35; Host Database
&#35;
&#35; localhost is used to configure the loopback interface
&#35; when the system is booting.  Do not change this entry.
&#35;#
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0 localhost</pre></p>

<p><strong>DO NOT</strong> change any of those lines, they&#8217;re critical to the workings of your computer. What you want to do is instead add lines to the end of the file, like the following for each of your virtual hosts:</p>

<p><pre class="brush: plain;">127.0.0.1      www.themattharris.com</pre></p>

<ul>
<li>127.0.0.1 means my computer even when it&#8217;s not connected a network</li>
<li>The domain name should match the one you used for the ServerName (or ServerAlias if you used that) in <code>/etc/apache2/extra/httpd-vhosts.conf</code></li>
</ul>

<h2>h3. Step 5: Restart Apache</h2>

<p>This can be done through System Preferences by turning Web Sharing off then on again.  Alternatively, a quicker way is to use terminal and type:</p>

<p><pre class="brush: plain;">sudo apachectl graceful</pre></p>

<p>and enter you password when prompted.</p>

<p>All being well if you now point to the domain you setup in hosts you will see the web pages on your local machine. To add more virtual hosts just repeat step 3, 4 and 5.</p>

<h2>Turning a virtual host off</h2>

<p>When you want to visit the live site again you don&#8217;t need to change anything apart from edit <code>/etc/hosts</code> and place a <code>#</code> at the start of the line for the domain you want to turn off.</p>

<p><pre class="brush: plain;">#127.0.0.1      www.themattharris.com</pre></p>

<h2>Troubleshooting</h2>

<p>If you are having problems and the domains are not working open the apache error log (<code>/private/var/log/apache2/error_log</code>) and see what it says.  Quite often you&#8217;ll find you&#8217;ve got the path in the wrong case and Apache couldn&#8217;t find the file.</p>

<p>Let me know how you do with this. In a couple of days I&#8217;ll show how to enable SSL on the virtual hosts and also how to compile the GD graphics library to work with MacOS.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.themattharris.com/feeder/?FeederAction=clicked&amp;feed=Articles+%28RSS2%29&amp;seed=http%3A%2F%2Fthemattharris.com%2F2009%2F02%2Fhowto-macos-apache-and-virtual-hosts%2F&amp;seed_title=How+To%3A+MacOS%2C+Apache+and+Virtual+Hosts/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
