Setting up a
Virtual Host in OS X

Setting up a Virtual Host is useful during web development so that you can easily access a running project via a custom, root level domain in your browser.

Edit httpd-vhosts.conf

Locate the /private/etc/apache2/extra directory and then sudo vim httpd-vhosts.conf or use whatever text editor to open the file. Add one of the following sections as necessary.

Basic version

1<VirtualHost *:80>
2    DocumentRoot "/Users/danielsellergren/Sites/website-directory"
3    ServerName dev.danielsellergren.com
4</VirtualHost>

Custom version

 1<VirtualHost *:80>
 2    DocumentRoot "/Users/danielsellergren/Sites/website-directory"
 3    ServerName dev.danielsellergren.com
 4    <directory "/Users/danielsellergren/Sites/website-directory/">
 5        Options Indexes FollowSymLinks
 6        AllowOverride All
 7        Order allow,deny
 8        Allow from all
 9    </directory>
10</VirtualHost>

Set the options as you need them. This example allows for clean URLs in Drupal. You should of course also replace the username danielsellergren with yours, and replace website-directory with the directory you want to serve from this Virtual Host.

Edit hosts file

The hosts file is a mapping of hostnames to IP addresses, so here we want to set your development hostname to the localhost IP so you can access it via web browser.

Use sudo vim /private/etc/hosts and add the line:

1127.0.0.1                dev.danielsellergren.com

Restart Apache

Finally, restart the server enter the command sudo apachectl restart.

Additional Reading