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

   top »

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

   top »

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

   top »

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

   top »

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

   top »

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

   top »

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.

blog comments powered by Disqus