Skip to content

Instantly share code, notes, and snippets.

@trey
Last active October 5, 2015 00:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trey/2722954 to your computer and use it in GitHub Desktop.
Save trey/2722954 to your computer and use it in GitHub Desktop.
The Absolute Least You Need to Do to Use Virtual Hosts on OS X

The Absolute Least You Need to Do to Use Virtual Hosts on OS X

Enable Web Sharing in System Preferences > Sharing. (No need to do this in Mountain Lion. You may need to run sudo apachectl start, though.)

Edit these files:

  • /etc/apache2/httpd.conf
  • /etc/hosts
  • /etc/apache2/users/your-username.conf
    • In Mountain Lion, you'll need to copy Guest.conf to [your-username].conf. Also, be sure to change the first line to be <Directory "/Users/your-username/Sites/"> and then also create the ~/Sites folder (which will, magically, still have the correct folder icon with the compass on it).

(I keep these files in a TextMate project so I can get at them quickly and don't have to remember which files I need.)

In httpd.conf, uncomment this line:

LoadModule php5_module        libexec/apache2/libphp5.so

In hosts, add your local sites in the format (or install Pow):

127.0.0.1	your-site-name.dev

In /etc/apache2/users/your-username.conf, add this line to the very top of the file:

NameVirtualHost *:80

Change None to All in this line (to allow for things like mod_rewrite):

AllowOverride All

Add FollowSymlinks to this line:

Options Indexes MultiViews FollowSymlinks

Add new sites in the same file in this format (or use a wildcard):

<VirtualHost *:80>
    ServerName your-site-name.dev
    DocumentRoot "/Users/your-username/Sites/your-site-name/"
</VirtualHost>

Any time you make changes to any of these files, you'll need to run this command:

sudo apachectl graceful

Check out this script for an easy way to add new sites (and feel free to fork it and adapt it to your uses).

See also

@trey
Copy link
Author

trey commented May 18, 2012

former location

This is a Solutions Log post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment