Apacheでユーザー毎にディレクトリを公開する

はまったのでメモ。
環境はCentOS6.4付属のApache 2.2.15。

httpd.conf の修正

httpd.conf を開いて、

  • userdir_moduleモジュールをロードする
LoadModule userdir_module modules/mod_userdir.so
<IfModule mod_userdir.c>
    #
    # UserDir is disabled by default since it can confirm the presence
    # of a username on the system (depending on home directory
    # permissions).
    #
#    UserDir disabled

    #
    # To enable requests to /~user/ to serve the user's public_html
    # directory, remove the "UserDir disabled" line above, and uncomment
    # the following line instead:
    #
    UserDir public_html

</IfModule>
#service httpd restart

ユーザーのディレクトリの権限設定

  • ユーザーのホームディレクトリにその他ユーザーの実行権を与える
#cd /home
#adduser ymaru
#chmod o+x ymaru
#su - ymaru
$mkdir public_html
$cd public_html
$echo "<html>test</html>" > index.html


これでブラウザから

http://example.com/~ymaru/

などでアクセスすれば、さきほど作成したHTMLが見れる。

SELinux を使っている場合

SELinux を使っていると追加で設定が必要になる。

#restorecon -R /home/ymaru
#ls -Z /home
drwx-----x. a     a     unconfined_u:object_r:user_home_dir_t:s0 ymaru/
#chcon -t httpd_user_content_t /home/ymaru