VirtualDocumentRoot使い方

自分用メモ。

VirtualDocumentRootの使い方

test.localhostを例に出して、

・前準備
自前のDNSに*.test.localhostワイルドカード使ってIPを登録。

Apacheの設定

LogFormat "%{Host}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" vcombined
NameVirtualHost 192.168.3.11:80

<VirtualHost 192.168.3.11:80>
    ServerName          hontai.test.localhost
    ServerAlias         *.test.localhost
    VirtualDocumentRoot /home/%0/htdocs
    CustomLog           /var/log/httpd/access_log vcombined

    <Directory /home/*/htdocs>
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

って感じで設定して、

# mkdir -p /home/hoge.test.localhost/htdocs

って感じで設定すれば、

http://hoge.test.localhost/でアクセスできるようになる、と。

増やしたい時は、

# mkdir -p /home/hage.test.localhost/htdocs
# mkdir -p /home/huge.test.localhost/htdocs

って作れば、
http://hage.test.localhost/
http://huge.test.localhost/
でアクセスできる、と。

ログは1つに集約されてしまうけど、後でgrepしてわけてあげればいいかなと。

ちなみに%0にはFQDNが入ります。
それ以降の変数には分解されたドメインが入る感じ。

%0 … hoge.test.localhost
%1 … hoge
%2 … test
%3 … localhost

が入る。
最後に、このディレクティブを使いたい時は、コンパイルオプションに--enable-vhost-aliasをつけること。