Apache virtual hosting FAQ: How do I configure Apache to run multiple virtual hosts (name-based virtual hosts)?
An Apache name-based virtual host (NameVirtualHost) tutorial: As a quick reminder to myself for something I just did using MAMP on my Mac OS X system, here's how I configured the MAMP Apache server to use name based virtual hosting for two websites that I'm developing at the same time.
Adding host names to /etc/hosts
The two website projects I'm working on are named "drupal7" and "codeme", so I added these entries to my /etc/hosts file:
127.0.0.1 drupal7 codeme
Note that on a Mac OS X system you have to edit that file like this:
sudo vi /etc/hosts
To test that this works I then pinged these new names:
ping drupal7 ping codeme
Since I have my firewall set up I actually just get a "request timeout" message, but that's good enough for me.
Apache name based virtual host (NameVirtualHost) setup
Next, I add the Apache name based virtual host configuration by editing the MAMP Apache conf file:
At the bottom of the file I add these lines, one for my "codeme" project, and another for my "drupal7" project. As you can see from the DocumentRoot entries, these projects are in different folders under the MAMP htdocs folder:
NameVirtualHost 127.0.0.1 <VirtualHost 127.0.0.1> ServerName codeme DocumentRoot /Applications/MAMP/htdocs/codeme SetEnv APPLICATION_ENV "development" <Directory /Applications/MAMP/htdocs/codeme> DirectoryIndex index.php AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost> <VirtualHost 127.0.0.1> ServerName drupal7 DocumentRoot /Applications/MAMP/htdocs/drupal7 SetEnv APPLICATION_ENV "development" <Directory /Applications/MAMP/htdocs/drupal7> DirectoryIndex index.php AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>
After this, I restarted MAMP, and went to the following URLs, and everything worked just fine:
The 8888 port shown is the default MAMP port. You can change that as desired, but it works for me.
Apache name based virtual host (NameVirtualHost) summary
I hope this Apache name based virtual host configuration tutorial has been helpful. As much as anything I wanted to put it out here as a "reminder to self" about how to do this. If this tutorial doesn’t help, I just noticed in 2020 that I wrote another tutorial titled, Apache NameVirtualHost configuration using MAMP on MacOS, and it uses some slightly different techniques in the Apache configuration file.
For more information, please see the Apache NameVirtualHost directive docs.