Blog
Next Post »

Configuring Virtual Hosts in MAMP on Mac OS X

If you need to easily set-up and maintain a local server environment on your Mac then look no further than MAMP. MAMP comes with Apache, MySQL and PHP and provides a local server environment that is independent of that installed as default by Leopard and best of all it’s free. However, like any server environment, out-of-the-box you’re limited to a single local host. This can prove a little inconvenient if you’re developing more than one site and require more than one local host. The most convenient and cost effective way to overcome this limitation is to use virtual hosts.

So, how are virtual hosts configured under MAMP? Well, there is a Pro version of MAMP which allows easy configuration of virtual hosts , but unlike it’s baby brother, it’s not free. The only real alternative is to get your hands dirty and configure virtual hosts manually. This is not as difficult as it may sound and can be accomplished fairly easily.

The process involves editing just 2 files. The /etc/hosts file and the /Applications/MAMP/conf/apache/httpd.conf file.

1.

What You’ll Need

The /etc/hosts file is hidden and a standard or admin user doesn’t normally have write access to it. To edit this file you can open TextEdit with root privileges from a Terminal window like so.

sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

An alternative is to use an editor like Smultron which has an option from the File menu to Open Hidden.. files and will ask for authentication if required. Smultron is free and whilst no longer under development works under 10.5.8.

2.

Example

In the example I’m going to configure a virtual host for a site that has a root folder of /Users/steve/Sites/MySite1/ and I’ll name the virtual host local.mysite1.

Ensure you stop MAMP’s Apache server before editing either of the files below.

3.

Editing the hosts File

Open the /etc/hosts file. You should see something like this:

# /etc/hosts
##
# 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
::1               localhost
fe80::1%lo0       localhost

We need to add a line to the end of this file for each virtual host we want to configure. The comment line is optional.

# Virtual Hosts
127.0.0.1 local.mysite1

The file should now resemble this.

# /etc/hosts
##
# 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
::1               localhost
fe80::1%lo0       localhost

# Virtual Hosts
127.0.0.1 local.mysite1

Save and close the file.

4.

Editing MAMP’s httpd.conf File

Open /Applications/MAMP/conf/apache/httpd.conf. On or around line 1142 you should see this.

# NameVirtualHost *

We first need to un-comment this line and then add the following block of code immediately after it.

Note that although I only want to add a virtual host for mysite1, beginning on line 1144 I’ve added a virtual host for localhost as the default named server with the ServerName set to localhost and the DocumentRoot and <Directory> set to /Applications/MAMP/bin/mamp. You need to add this if when you type http://localhost in your browser you want the MAMP start page to be displayed. If you choose not to add a localhost configuration then http://localhost will default to display the first virtual host. In this case mysite1. If you do decide to add a localhost configuration in httpd.conf you do not need to add a corresponding entry in /etc/hosts as this is included by default.

For mysite1 you can see we are setting the ServerName to the name of virtual host we configured in the /etc/hosts file. The DocumentRoot and <Directory> are set to the site’s root folder.

NameVirtualHost *

# localhost [must be included as the default named server]
<VirtualHost *>
	ServerName localhost
	DocumentRoot "/Applications/MAMP/bin/mamp"
	<Directory "/Applications/MAMP/bin/mamp">
		Options Indexes FollowSymLinks Includes execCGI
		AllowOverride None
		Order Allow,Deny
		Allow From All
	</Directory>
</VirtualHost>

# mysite1.com
<VirtualHost *>
	ServerName local.mysite1
	DocumentRoot "/Users/steve/Sites/MySite1"
	<Directory "/Users/steve/Sites/MySite1">
		Options Indexes FollowSymLinks Includes execCGI
		AllowOverride None
		Order Allow,Deny
		Allow From All
	</Directory>
</VirtualHost>

Save and close the file and your done.

5.

Testing the Virtual Host

To ensure the virtual host has been configured correctly, restart MAMP’s Apache server and point your browser to http://local.mysite1

I would recommend backing-up the /etc/hosts and /Applications/MAMP/conf/apache/httpd.conf files whenever you make changes and store them on a separate drive or USB stick. If you later re-install the OS or MAMP you’ll lose the changes to the respective files. This way you can easily restore your virtual host configuration.

6.

Changes in MAMP 1.8

MAMP 1.8 released in September 2009 uses a slightly different httpd.conf file as it includes two new aliases for phpMyAdminForPHP4 and phpMyAdminForPHP5. You can still edit the httpd.conf file as described above, but you shouldn’t restore a pre 1.8 httpd.conf file to a 1.8 install as you’ll be unable to access MAMP’s phpMyAdmin.

  Was this post helpful?  

1 Star2 Stars3 Stars4 Stars5 Stars 2 votes | 4.50 average

Loading ...  Loading ...
Email This Post

8 Comments


  1. Mark
    Sep 05, 2010

    I’ve spent what seems like a frustrated eternity trawling through various posts attempting to work out how to install multiple local testing sites with MAMP. This is by far the best post and the code is spot on – I wasted a lot of time with some dud code I picked up elsewhere before finding yours. Thank you Steve – it’s posts like these from people like you that really help newbies like me.


  2. Steve
    Sep 05, 2010

    You’re welcome Mark. I’m glad you found it helpful.

    Regards, Steve.


  3. SlowX
    Mar 15, 2011

    Thanks!
    So many sites just talking about the single-site install.

    Is this method vary with different versions of OSX?
    I’m on 10.6.

    Also, any thoughts on MAMP vs XAMP on a Mac?

    Thanks!


    • Steve
      Mar 15, 2011

      SlowX,

      Is this method vary with different versions of OSX?
      I’m on 10.6….

      I’ve used it on 10.5 and 10.6 though it probably has as much to do with the version of MAMP as that of the OS.

      Also, any thoughts on MAMP vs XAMP on a Mac?…

      I switched to XAMPP for Mac a couple of years back as at the time MAMP hadn’t been updated for a long time. I now use either one or the other on the Macs I have and IMHO there’s not much between them.

      Regards, Steve.


  4. SlowX
    Mar 16, 2011

    BINGO!
    Works great w/ MAMP 1.9.4 on Mac 10.6.6. Thank you SO much!

    The one thing I’d add is that in the hosts file, if you’re working w/ multiple sites add them on the same line w/ a comma, so:
    127.0.0.1 local.mysite1 local.site2 local.site99
    (At least that’s what worked for me)

    Thanks again!

    Also, I just noticed that you’re in Japan… Hope you’re safe, and your family and friends are well. Wising you and your neighbors strength, health and good fortune.


    • Steve
      Mar 17, 2011

      SlowX,

      Gad you’ve got it working and thank you for your kind words.

      Regards, Steve


  5. ann
    Mar 17, 2011

    Interested in purchasing this theme. Could you tell me how I would get rid of the footer and just have Contact Us, News and maybe Recent posts and how would I have the slider so that I could put text on the left hand side and picture on right. I have no experience with wordpress. Thanks in advance


    • Steve
      Mar 18, 2011

      Hi ann,

      You’d do better contacting the developers.

      Good luck, Steve.

Leave a Reply

A native Brit exiled in Japan, Steve spends too much of his time struggling with the Japanese language, dreaming of Fish & Chips and writing the occasional blog post he hopes others will find helpful.

Next Post »

Calendar