This is a simple guide on how to setup name-based virtual hosts in Apache on Mac OS X 10.5 (Leopard). This tutorial is intended as a supplement to my article, How to setup Apache, PHP, & MySQL on Mac OS X 10.5 (Leopard).
What are virtual hosts used for?
In the case of this tutorial we will create a virtual host to let us access a subfolder on our local web server at a custom address like http://application/ or http://application.local/ instead of having to type in a longer URL like http://localhost/~shortname/application
Preparation
This tutorial assumes you either followed my tutorial about How to setup Apache, PHP, & MySQL on Mac OS X 10.5 (Leopard) or already have similar setup and are somewhat familiar with what you’re doing. Please make backups of any files you modify.
Open up the Terminal application in /Applications/Utilities
I will be using the GNU nano text editor in my examples since it already comes with the Mac, but if you’d like to use TextMate or BBEdit just replace nano with mate or bbedit respectively and remove any nano specific options (e.g. +n or -w).
Whenever I ask you to type something into Terminal you have to hit the return key for the command to go through. Alternatively, for you lazy ones out there, you can just copy and paste these commands.
Setup a Name-Based Virtual Host
You will first need to select a name for your virtual host, in this case I want to create a virtual host to test my local copy of www.authorwars.com but don’t want to use http://authorwars/ because I want that URL to go to the live version of my website so I will be using http://authorwars.loc/ so I remember I’m working on the local or development copy and not my live site. Alternatively, you could use port-based virtual hosts (e.g. http://localhost:81/), but I’m not going to get into that now. Be careful not use something like mywebsite.com as a name because web browsers will check your host file first and go to your local machine instead of the live website located at mywebsite.com.
Go to the Terminal window and type:
sudo nano -w /private/etc/hosts
When you put sudo in front of a command you will need to enter your root/administrator password if prompted to do so.
Hold control v until you reach the end of the file and place this text at the end, replacing authorwars.loc with whatever name you’d like to use for your virtual host:
127.0.0.1 authorwars.loc
You can add more hosts on new lines by using the same format.
Now you will close and save the file: hit control x to exit, then type y, and hit return to save your changes.
Now, type the following into Terminal:
nano -w vhosts-temp.conf
Copy and paste the text below into Terminal:
# # Virtual Hosts # # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at # <URL:http://httpd.apache.org/docs/2.2/vhosts/> # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # NameVirtualHost *:80 # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ServerName or ServerAlias in any <VirtualHost> block. # # <VirtualHost *:80> # ServerAdmin webmaster@dummy-host.example.com # DocumentRoot "/www/docs/dummy-host.example.com" # ServerName dummy-host.example.com # ServerAlias www.dummy-host.example.com # ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log" # CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log common" # </VirtualHost> # # <VirtualHost *:80> # ServerAdmin webmaster@dummy-host2.example.com # DocumentRoot "/www/docs/dummy-host2.example.com" # ServerName dummy-host2.example.com # ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log" # CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log common" # </VirtualHost> # <VirtualHost *:80> DocumentRoot "/Users/shortname/Sites" ServerName localhost </VirtualHost> <VirtualHost *:80> DocumentRoot "/Users/shortname/Sites/authorwars" ServerName authorwars.loc </VirtualHost>
Remember to replace all instances of shortname with your actual shortname. An easy way to find yours is to look at the text before each command you type into Terminal in my case it reads ariadoss:~ ariadoss$ meaning ariadoss is my Mac’s shortname. Also, remember to replace authorwars with your application’s folder and authorwars.loc with the name you chose for your virtual host.
Close and save the file like I explained earlier.
Go back to Terminal and type:
sudo mv vhosts-temp.conf /private/etc/apache2/extra/httpd-vhosts.conf
If you find later you need to add more virtual hosts go to Terminal and type:
sudo nano -w /private/etc/apache2/extra/httpd-vhosts.conf
Go to the end of the file and simply paste new entries at the end of the configuration file like so:
<VirtualHost *:80> DocumentRoot "/Users/shortname/Sites/application" ServerName application.loc </VirtualHost> <VirtualHost *:80> DocumentRoot "/Users/shortname/Sites/application" ServerName application.loc </VirtualHost>
Again, remember to replace all instances of shortname and application with the proper information.
Make sure to add these new virtual hosts to the end of the /private/etc/hosts file. To edit this file use the command below:
sudo nano -w /private/etc/hosts
Put new entries on a new line like so:
127.0.0.1 authorwars.loc
127.0.0.1 application.loc
127.0.0.1 application.loc
Now, we will need to enable virtual hosts in Apache.
In Terminal type:
sudo nano -w +461 /private/etc/apache2/httpd.conf
Your cursor will now be at the beginning of line 461, which should look like this:
#Include /private/etc/apache2/extra/httpd-vhosts.conf
Uncomment that line by removing the pound/hash sign (#). If you don’t see this text try searching for it by hitting control w.
Finally, restart Apache using the command below (type it into Terminal):
sudo apachectl restart
You’ll want to restart the Apache service either from System Preferences or the command line whenever you make a change to a configuration file. To restart Apache from the System Preferences go to System Preferences and click on the Sharing icon, then select Web Sharing and make sure it’s checked, the light will turn green and it will say Web Sharing: On.
Now open up your favorite web browser and go to (again, remember to replace authorwars.loc with whatever name it was you chose for your virtual host):
http://authorwars.loc/
And your done! ^_^
Last modified: May 7, 2011













About


Jun 30, 2009
I am new to this and currently working on a photography website. which I already have set up in my htdocs folder in MAMP.
I would like to start work on another web site and I would also like to set up virtual hosts.
Can I set up multiple hosts while I have a website stored in htdocs, or do I have to move that site somewhere else while I set up Vhosts?
Great tutorial by the way it’s very clear all the others I looked at can get confusing.