Apache で ~/public_html を公開

ユーザ毎のウェブディレクトリ - Apache HTTP サーバ
mod_userdir - Apache HTTP サーバ


~/public_html を公開する設定にしておけば,

http://ドメイン名/~ユーザ名/

にアクセスしたときに,各ユーザの ~/public_html の中身を見に行くようになる.

設定を有効にするには,/etc/conf.d/apache2 の APACHE2_OPTS に -D USERDIR を加えて apache2 を restart すればよい.以下は /etc/apache2/httpd.conf のデフォルトの設定.上の方法で USERDIR が宣言されてさえいれば,設定が自動的に有効になる.

<IfDefine USERDIR>
    LoadModule userdir_module                modules/mod_userdir.so
</IfDefine>

#
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received.
# enable by adding -D USERDIR to /etc/conf.d/apache2
#
<IfModule mod_userdir.c>
    UserDir public_html

#
# Control access to UserDir directories.  The following is an example
# for a site where these directories are restricted to read-only.
#
    <Directory /home/*/public_html>
        AllowOverride FileInfo AuthConfig Limit Indexes
        Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
        <Limit GET POST OPTIONS PROPFIND>
            Order allow,deny
            Allow from all
        </Limit>
        <LimitExcept GET POST OPTIONS PROPFIND>
            Order deny,allow
            Deny from all
        </LimitExcept>
    </Directory>

# Enable this additional section if you would like to make use of a
# suexec-enabled cgi-bin directory on a per-user basis.
#
#<Directory /home/*/public_html/cgi-bin>
#    Options ExecCGI
#    SetHandler cgi-script
#</Directory>

</IfModule>

また,

UserDir disabled ユーザ名 ...

でユーザディレクトリを公開するユーザを制限することができる.そこで,

UserDir public_html
UserDir disabled root

としておくと,http://ドメイン名/~root/ にアクセスしても 404 Not Found となる.(とはいえ,~/public_html は other users から見えるようになっていないと 403 Forbidden となるので,そもそも /root が 600 の場合は http://ドメイン名/~root/ にアクセスしても 403 Forbidden となるだろう.)


また,ここで,

AllowOverride FileInfo AuthConfig Limit Indexes

と設定されているので,各ユーザで .htaccess を使ってディレクトリ以下のサーバ設定を変更することができる.ただし,.htaccess が other users に見えるようになっていなければ,.htaccess が置かれているディレクトリ以下にアクセスしても 403 Forbidden となる.