OSX(Lion)で、VirtualDocumentRoot を設定したのでメモ

WEBアプリケーションを開発するとき、localhost:8080とかlocalhost:9000とかで頑張っていたんですが、
進行中の開発モノと、メンテナンス中のサイトを交互に作業してると、URLの切替が段々うざったくなってきたんで、
ホスト名だけですっきりブラウズ出来るようにしたくなってきました。

http://hoge.localhost とか http://fuga.localhost
をサーバの再起動なくブラウザでアクセスしたい、というのがゴール

参考にしたところ
http://d.hatena.ne.jp/y-kawaz/20110725/1311610727:VirtualDocumentRootを使って複数ドメインをスッキリ管理(Apache)

1.apacheの設定変更
1)httpd.confを編集。
httpd-vhosts.confを有効にするため、以下の行のコメントを外します。
> sudo vi /etc/apache2/httpd.conf

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

2)httpd-vhosts.confを編集。
既存のexampleはコメントアウトします。
>sudo vi /etc/apache2/extra/httpd.conf

#
# ServerAdmin webmaster@dummy-host.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 webmaster@dummy-host2.example.com
# 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
#

で以下を追加して最終的に以下に

NameVirtualHost *:80

ServerName *
VirtualDocumentRoot "/Users/ksaka-ew/Vhosts/%0/htdocs"

Options All
AllowOverride All
Allow from all

3)apache再起動
>sudo apachectl restart

4)サイトに応じたディレクトリを作る
>mkdir -p /Users/ksaka-ew/Vhosts/hoge.localhost/htdocs
ディレクトリの配置場所はどこでもOKです。
hoge.localhostの部分はアクセスしたいホスト名を入れます。

2.ホスト名を引けるようにする
hoge.localhost127.0.0.1で引きたい。
1)hostsに下記行書き加えてエントリを追加
>sudo /etc/hosts

127.0.0.1 hoge.localhost

2)DNSキャッシュをクリアしてhostsを読込み
>dscacheutil -flushcache

これで終わりです。
/Users/ksaka-ew/Vhosts/hoge.localhost/htdocsの下にindex.htmlを置くなどして
>curl hoge.localhost
とやるなり、ブラウザで http://hoge.localhost/index.html にアクセスしてみてください。