How to set up Virtual Hosts on Mac OS X El Capitan 10.11 El Capitan & Mac OS X 10.10 Yosemite
This is a simple guide on how to set up name-based virtual hosts in Apache on Mac OS X 10.10 Yosemite. This tutorial is intended as a supplement to my article, How to setup Apache, PHP & MySQL on Mac OS X 10.10 Yosemite. If you know what you’re doing this guide should also work for Leopard, Snow Leopard, Lion, Mountain Lion, and Mavericks with minor changes as the setup hasn’t changed much since I originally wrote about this in 2009.
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.loc/ instead of having to type in a longer URL like http://localhost/~username/application
This tutorial assumes you either followed my tutorial about How to set up Apache, PHP, & MySQL on Mac OS X 10.10 Yosemite or already have a similar setup and are somewhat familiar with what you’re doing. Remember to backup any files you modify.
Open up the Terminal application in /Applications/Utilities or if you prefer use iTerm.
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 BBEdit or TextMate just replace nano with bbedit or mate respectively and remove any nano-specific options (e.g. -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.
Set up 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 copies of Ariadoss.com, HogwartsLive.com, and AuthorWars.com but don’t want to use the .com version of the domain names because I want those URLs to go to the live version of my website. Instead, I will be using domains like http://ariadoss.loc/ so I remember I’m working on the local (hence the .loc) copy and not my live site. Alternatively, you could use a subdomain like http://local.ariadoss.com/ but some codebases won’t run properly on a subdomain. 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 /etc/hosts
When you put sudo in front of a command you will need to enter your administrator/user 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 ariadoss.loc with whatever name you’d like to use for your virtual host:
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 to edit the virtual hosts:
sudo nano -w /private/etc/apache2/extra/httpd-vhosts.conf
Remove or comment out the virtual host entries and place your virtual hosts at the end of the file like so:
# Virtual Hosts
# Required modules: mod_log_config
# 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
# for further details before you try to setup virtual hosts.
# You may use the command line option '-S' to verify your virtual host
# 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.
# ServerAdmin email@example.com
# DocumentRoot "/usr/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
# ServerAdmin firstname.lastname@example.org
# DocumentRoot "/usr/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
Remember to replace all instances of username with your actual username. An easy way to find yours is to look at the text before each command you type into Terminal in my case it reads “Danilo-MacBook-Pro:~ danilo$” meaning danilo is my username. Also, remember to replace ariadoss with your application’s directory/folder name and ariadoss.loc with the name you chose for your virtual host.
Close and save the file like I explained earlier.
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:
Again, remember to replace all instances of username and ariadoss, hogwartslive, and authorwars with your different application names.
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:
Now, we will need to enable virtual hosts in Apache.
Type the following into Terminal:
sudo nano -w /etc/apache2/httpd.conf
Hit control w and search for:
Uncomment that line by removing the pound/hash sign (#) in front of the “Include” text.
Close and save.
Finally, restart Apache using the command below (enter it into Terminal):
sudo apachectl restart
You’ll want to restart the Apache service whenever you make a change to a configuration file.
Now open up your favorite web browser and go to (again, remember to replace ariadoss.loc with whatever name it was you chose for your virtual host):
And your done! ^_^
Originally published at http://danilo.ariadoss.com on June 2, 2009. Last updated November 17, 2015.