Using MAMP as a local development server

This is the most geeky post we have ever put up on the blog, but having spent the last four hours knee deep in DNS, Apache and everywhere in-between I feel this information needs to be shared.

I will start off by explaining my requirements. I am thinking of buying a Mac Pro to use as my main machine and to replace the Linux server we currently have setup in the corner of the office. If the Mac Pro is going to replace the Linux box it needs to provide us with a web server (as well as file sharing, media sharing etc) and the web server needs to be accessible over the local network to all other machines in the office. Our Linux box is serving us well but I see an opportunity to merge two computers into one; making for a greener office and hopefully it will be a little easier to manage (I am far more comfortable in OSX than ClarkConnect). We call our Linux machine "tux", it has its own DNS server that means we can access it over the network at tux.lan. It works well but one of my issues with it has been having to install websites into sub-directories. For example our local toggle website sits at tux.lan/toggle.uk.com. Because our local sites do not live in a root web folder our local sites can have a number of differences compared to the live sites, these differences mean we have to be careful when deploying changes.

I have spent today looking at ways I can use the standard install of OSX to act as a web server. Although OSX comes with Apache built in, one of the simplest ways to get PHP, MySQL and Apache is to use MAMP. I have been using MAMP for a number of years and love the fact it is expendable and not deeply integrated into the system. If I want to change PHP version I can do so at the touch of a button and if it goes wrong I can copy a new version over the top. So how do we share MAMP over the local network with nice (virtual hostey) URLs like: toggle.uk.com.dev?

You will need:

I am going to assume you have a few things in place:

  1. You are running OSX 10.5.
  2. Your machine is using a fixed IP address on your local network (e.g. 192.168.0.10).
  3. You have installed MAMP and your document root is your OSX sites folder.
  4. You can use Terminal.

The next couple of steps were modified from postpostmodern.com, which is the closest tutorial I could find to what I was trying to achieve. The main difference here is that we want this to work across the local network and not just on the one local machine.

BIND - The hidden OSX DNS server

A DNS server converts a name (google.com) to an IP address (74.125.45.100) - it basically means we do not have to remember IP addresses for the sites we want to visit. We need to enable the DNS server on our OSX machine so when we enter a local URL (toggle.uk.com.dev) it looks for the website on the local server (which lives at 192.168.0.10). I am not a DNS expert but following these steps did the trick for me.

Setup rndc

Open up terminal and get into root: sudo -s
Generate the config file for 'rndc': rndc-confgen > /etc/rndc.conf
Copy a key to the newly created file: head -n 6 /etc/rndc.conf > /etc/rndc.key

Create your DNS zone file

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

;
; 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 192.168.0.10
*.dev. 14400 IN A 192.168.0.10

You will have to replace the IP address (192.168.0.10) with the IP address of your Mac on the local network, remember this should be a fixed IP to ensure it keeps working.

Configure named.conf

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), it is probably best to use the OpenDNS servers. Scroll through this file until you see an options section with the line: directory “/var/named”; - just after this paste the following code:

forwarders {
208.67.222.222;
208.67.220.220;
};

Next we need to add a reference to the zone file we created a minute ago. There is a localhost section already in /etc/named.conf, find it and post the following code below it:

zone "dev" IN {
type master;
file "dev.zone";
};

Getting DNS to start everytime your Mac does

Open the LaunchDaemon plist file (/System/Library/LaunchDaemons/org.isc.named.plist) and look for the first instance of the word 'true' and change it to 'false'. It should appear just under the word 'disabled'. You can now load it using the following terminal command:

sudo launchctl load /System/Library/LaunchDaemons/org.isc.named.plist

The DNS server should now be up and running on your machine, you will need to tell other machines on your network and the Mac itself to look at this IP address for DNS. In system preferences -> network -> add '127.0.0.1' to the list of DNS servers (for the server itself) and for other machines on the network add '192.168.0.10' to the list of DNS servers. The other machines on the network will now look to this IP address when you first type in a URL into your browser.

Setup MAMp

Now that the DNS stuff is out of the way, it’s just a matter of setting that magic directive in your Apache conf file. The directive is called VirtualDocumentRoot. You need to navigate to the MAMP folder (/Applications/MAMP/conf/) and open up httpd.conf in a text editor of your choice. Scroll to the very bottom of the file and add the following code:

NameVirtualHost 192.168.0.10

<VirtualHost 192.168.0.10>
VirtualDocumentRoot /Users/YOURUSERNAME/Sites/%-2+/
</VirtualHost>

The VirtualDocumentRoot is an interpolated path for finding your sites. ‘%-2+’ means everything before the .dev. So: example.dev would load files from /Users/YOURUSERNAME/Sites/example/. If MAMP was running you should now re-start Apache and visiting example.dev in your browser should bring up a 404 (unless of course you have a folder called example in your sites folder). Did it work?

Hitting Problems

I had a number of issues getting this to work across the network and during that time I was running quite a few commands to get rndc to restart without having to reboot my machine every time I made a change. At first I could not get rndc to listen to any of my commands, searching Google once more I found a handy forum tip suggesting the issue might be related to port numbers. I suggest you follow these few extra steps to resolve the issue:

Open up terminal and run the following: sudo nano /etc/rndc.conf - look for the options at the top of the file and make sure the default port is set to 54. Save (ctrl-x, Y, Enter) and you should now be able to use the the command line to interface with rndc.

Useful commands

Whilst working through the steps above I came across a number of handy command line tools that you might find useful too:

nslookup dev : nslookup will interactively find MX and other records. I used this on network machines to see if they correctly identified the Mac as the .dev server. dscacheutil -flushcache : OSX has a tendency to cache DNS, this flushes the cache so you can check your changes. rndc reload : This reloaded any changes I made to my DNS zone files.

So that brings me to the end of this tutorial, having successfully tested this on my Mac laptop I am now one step closer to making a decision about the Mac Pro. How did you get on?

By Pixie Admin Last updated: 15 December 2009, 22:14

Comments

Gravatar Image #1 Tim Plumb 1912 days ago.

Great article Scott. I've been using VirtualHostX (http://clickontyler.com/virtualhostx/) to automatically configure and maintain my Apache httpd.conf settings so I can now host multiple sites locally with MAMP. For example I've Pixie running under http://pixie.web. The beauty is that each site is treated as it's own domain so root relative links start working again ('/css/styles.css' for example). Before using VirtualHostX I had to remember to change certain paths after uploading the files which was both a pain and prone to error.

Gravatar Image #2 Scott Evans 1911 days ago.

Thanks Tim, glad you found it useful. VirtualHostsX is a great bit of software - the developer also does some great work with his "Simple PHP Framework" - a framework I am considering for use in future Pixie.

Gravatar Image #3 Graeme 1904 days ago.

This is such a great post! Since starting to use MAMP Pro a while back, I was trying to find a simple way to setup name-based virtual hosts. You explained the process very well. My previous kludge was to setup one of my linux boxes to run the DNS for my simple dev network. Now that I am no longer dependent on that I can easily take my development work on the road with me! My preferred structure is: /Users/USERNAME/dev/%-2+/public_html That way I can keep other project-related files and folders in the parent directory whilst the public_html directory clearly defines the web directory.

Gravatar Image #4 Scott Evans 1722 days ago.

If anyone follows these steps and consequently has issues with PHP's $_SERVER['DOCUMENT_ROOT'] variable. You might wish to follow the advice here. Apache does not play well with document roots and virtual hosts.

Comments now closed.