Skip to content

Instantly share code, notes, and snippets.

@benfoxall
Created May 20, 2011 09:46
Show Gist options
  • Save benfoxall/982644 to your computer and use it in GitHub Desktop.
Save benfoxall/982644 to your computer and use it in GitHub Desktop.
apache vhosts

Using default OSX apache httpd to serve ~/Sites/foo at http://foo.local

By configuring dns and using virtual hosts - you can automatically serve folders in your ~/Sites directory under a local domain.

point *.local to localhost

Wildcard DNS is a better option that editing your /etc/hosts file for every site, there are a few ways to do this.

set up a local dns

http://mikeferrier.com/2011/04/04/setting-up-wildcard-dns-on-localhost-domains-on-osx/

use a dns server

in our office - *.ben.dev points to my machine.

point an external domain back at 127.0.0.1

If you have a spare domain kicking about, you can point it back to 127.0.0.1 (for example ping foo.lvh.me) and use that.

Configure your virtual host

edit /etc/apache2/extra/httpd-vhosts.conf, remove examples and add:

<VirtualHost *:80>
    ServerName local
    ServerAlias *.local
    UseCanonicalName Off
    VirtualDocumentRoot /Users/ben/Sites/%1
    DirectoryIndex index.html
    
    <Directory /Users/ben/Sites>
        Options Indexes SymLinksIfOwnerMatch
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>    

(changing /Users/ben/Sites/%1 to your own)

enable vhosts in main apache config

uncomment this line in /etc/apache/http.conf

#Include /private/etc/apache2/extra/httpd-vhosts.conf

restart apache

sudo apachectl restart

See how much easier this is

mkdir ~/Sites/test
echo "<h1>Howdy</h1>" > ~/Sites/test/index.html
open http://test.local/
@carlwood
Copy link

Thanks Ben, this is rad.

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