Configuring Virtual Host Support in MAMP's Free Edition

Technical

I recently wrote about my installation of the free version of MAMP on my development machine. Yesterday I was looking at the default homepage of my install - a page shipped with the application itself - and noticed, in the text promoting their commercial, Pro version, a line item indicating support for multiple virtual hosts in that paid version. I thought that was interesting. The free version may not publicize virtual host support and it may not come with any fancy-schmancy GUI to configure it, but it's still Apache. When I installed the free version, the first thing I did was configure virtual hosts. I like having multiple web roots. I like elegance. I didn't want to specify individual, non-standard port numbers for each web root and I didn't want to have to access separate sites as subdirectories of my primary web root. I wanted to be able to access each site at its own nice, clean, elegant URI. In short, I wanted virtual hosts. So I created some.

The key to virtual hosts, like so many things with Apache, is the httpd.conf file. In a MAMP install, the configuration file is located in /Applications/MAMP/conf/apache. To configure support for virtual hosts, open the httpd.conf file in any text editor and ensure that the NameVirtualHost directive exists and is uncommented. Technically, that's it. Support for virtual hosts is now enabled. To be useful, though, virtual host blocks will have to be created for each site that should run independently. In the spirit of following through with things, I'll describe the way I configured my virtual hosts.

First, because I'm anal this way and like to keep truly custom settings segregated from my main configuration, I created a new folder to house external configuration files. It can be anywhere and be named anything, but I chose to follow the Linux naming convention and create a folder named conf.d since its contents would be loaded each time Apache is started. The new folder was created in a structure I have in my home folder for storing application data that is uniquely related to me. The full path on my machine is ~/Application Data/MAMP/conf.d.

In my case, the primary reason for the existence of this folder is to hold several custom .conf files that define the virtual hosts I need. Since I wanted my machine to respond to http://localhost, but had enabled virtual host support, I needed a virtual host defined to listen for that host name. I also needed virtual hosts configured for two sites that I'm working on in my spare time. I created those in the conf.d folder as localhost.conf, site1.conf and site2.conf, respectively. Sample contents of these files are available at the end of this post.

Now that I had virtual host support enabled and several named hosts configured, I just needed to let Apache know where to find them. For that, the Include directive. I added the following line to the bottom of my httpd.conf file:

Include "/Users/myusername/Application Data/MAMP/conf.d/*.conf"

This directive tells Apache to read every file at that path ending in .conf and load the contents into its runtime configuration. The wildcard, the asterisk (*), could be used without the .conf extension to tell Apache to simply load every file in that path, but I prefer to use both components because it gives me an easy way to exclude configurations. By renaming a file whatever.conf.old, the file is excluded from Apache's runtime. That can be very handy when trying to track down a problem in the configuration or when simply trying to trim the fat from a bloated configuration.

Sample localhost Configuration File (localhost.conf)


   ServerName localhost
   DocumentRoot /Applications/MAMP/bin/mamp
   DirectoryIndex index.htm index.php index.cfm

Sample Site Configuration File (site1.conf)


   ServerName site1.localmachine.net
   ServerAlias *.site1.localmachine.net site1
   DocumentRoot /Users/myusername/Sites/site1/www
   DirectoryIndex index.htm index.php index.cfm

In the sample VirtualHost blocks, pay attention to part that reads *:80. It's important that that text exactly match the text that follows the NameVirtualHost directive in httpd.conf. If it doesn't, Apache will not start. If my NameVirtualHost directive read 192.168.10.40:8080, then that string would have to be echoed in my VirtualHost block definition.

That's all there is to it. Virtual hosts are now supported and configured. Happy developing.

tags:
Mac, Apache, mamp
Chad said:
 
It's also worth mentioning that several VirtualHost definitions may be stored within a single file rather than one per host. I suppose it's just a matter of preference.
 
posted 602 days ago
Add Comment Reply to: this comment OR this thread
 
 
Right. For me, the "need" for multiple files is an extension of that anal nature I mentioned. :-)

In the same vein, it may also be worth noting that separate conf files can contain things other than virtual host definitions. In the past I've included global rewrite rules in an external file, for example.
 
posted 602 days ago
Add Comment Reply to: this comment OR this thread
 
Christopher said:
 
Just wondering, all the other tutorials I've read concerning this require the editing of the hosts file. But I don't think you've mentioned it.

Is it always necessary?
 
posted 568 days ago
Add Comment Reply to: this comment OR this thread
 
 
Depends. I don't need my hosts file because I run an internal DNS server that handles name resolution for me. I have each virtual host listening for a name that's resolved by my name server. Barring that, yes, you'll probably need to edit your hosts file to handle name resolution for you.
 
posted 568 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Rob  Wilkerson