Alba HK

MAMP and Virtual hosts on Mac OS

I’ve just spent a few hours setting up a new local development environment for development and thought I would write down the steps both for my own reference and hopefully for the benefit of others using MAMP.

Development Environment

We are using a Mac OS X environment for development. Basically all our projects are under version control (SVN) and we use MAMP to get the basic web development stack up and running with as little drama as possible. We use the command-line or svnX for SVN and Textmate for code. It’s relatively problem-free to setup and most importantly gets out of our way so we can get on with real work rather than faffing about with configurations and Yak shaving.

Multiple Projects under MAMP

Often we’ve got multiple projects underway and while MAMP is great out of the box for a single site, shutting down and restarting it with a new DocumentRoot was not a satisfactory solution for switching between projects, not to mention embarrassingly low-tech.

Below is a solution to get multiple virtual hosts running locally on the same MAMP instance locally so we can access our projects at http://project1/ and http://project2/ at the same time and without a MAMP restart. This solution is clearly in the category of bleedingly obvious setup tweaks and we are not the first person to do it this way, however this little setup has saved us a truckload of time and more importantly our flow is not interrupted when switching between projects.

Modify /etc/hosts file

In OS X (and basically all flavours of *NIX-based operating systems) there is a config file such as:

/etc/hosts

Open this file up in a text-editor and you should see a few lines of options such as:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost

Modify the local hosts file and add one line for each new host name you require. For example I would add the following lines:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
127.0.0.1 project1
127.0.0.1 project2

Once you’ve saved this file, try pinging the new hostnames from Terminal like so (hit ctrl-C to terminate the pinging):

Computer1:~ User1$ ping project1
PING project1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.148 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.132 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.136 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.133 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.150 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.131 ms
^C
--- project1 ping statistics ---
6 packets transmitted, 6 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.131/0.138/0.150/0.008 ms

Now that this is done we need to get Apache involved.

Modify Apache httpd.conf file

We’ll let Apache know about the new hostnames. We will setup a Virtual Host for each of our projects. This can be done by modifying Apache’s configuration file which is located in the following location for default MAMP installations:

/Applications/MAMP/conf/apache/httpd.conf

At the very end of the file should be a section on Virtual Hosts. You’ll need to uncomment the following line:

NameVirtualHost *

and add the following nested entry for each virtual host you have:

<VirtualHost *>
  DocumentRoot /Users/User1/project1
  ServerName project1
  <Directory "/Users/User1/project1">
    Options All
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

Test it out

It is necessary to restart Apache (or MAMP) in order to pick up the new changes. After restarting, we can navigate to the following urls and see the respective projects in a browser:

http://project1/

http://project2/

If you’ve got any questions, please leave a comment below.

Leave a Reply

Your email address will not be published.