3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Posted at

環境

Ubuntu20.4 + vagrant
Apache2.4.41

はじめに

ApacheでWebサイトを公開するとき、基本的なやり方をすれば、Apacheのルートである、/var/www 配下にindex.htmlを配置することになります。しかし、公開の仕方はそれだけではありません。各ユーザーごとに、ホームディレクトリ配下にindex.htmlを配置して、複数のユーザーで公開するといったやり方もできます。

つまり、Apacheのルートで、/var/www/hoge/index.htmlを公開しながら、ユーザーAさんが~user_a/index.htmlを公開したり、ユーザーBさんが~user_b/index.htmlを公開したりするといったことができるようになります。その場合の設定方法について書いてみました。

userdir.confのデフォルト

ユーザーディレクトリを公開するときに設定するコンフィグファイルは、/etc/apahce2/mods-available/userdir.confです。
デフォルトでは次の設定になっています。

/etc/apahce2/mods-available/userdir.conf
<IfModule mod_userdir.c>
       UserDir public_html
       UserDir disabled root

       <Directory /home/*/public_html>
               AllowOverride FileInfo AuthConfig Limit Indexes
               Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
               Require method GET POST OPTIONS
       </Directory>
</IfModule>

userdir.confの設定

ホームディレクトリのユーザーディレクトリ配下に、公開するディレクトリを新規で作成します。次の例では、vagrantユーザーの配下にpublic_htmlディレクトリを作成しています。

#cd ~
#cd vagrant
#mkdir public_html

userdir.confのサンプルをみると、public_htmlになっていますが、ディレクトリの名前は、public_htmlである必要はありません。自分で好きな名前を指定することができます。例えば、hoge_htmlにしたい場合は、コンフィグファイルも次のようにします。

  • UserDir hoge_html  : 公開するフォルダを指定します。
  • UserDir disabled root : ホーム配下を非公開にする設定?。(未確認)
/etc/apahce2/mods-available/userdir.conf
<IfModule mod_userdir.c>
       UserDir hoge_html
       UserDir disabled root

       <Directory /home/*/hoge_html>
               AllowOverride FileInfo AuthConfig Limit Indexes
               Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
               Require method GET POST OPTIONS
       </Directory>
</IfModule>

公開するユーザーをvagrantとtanに限定する場合は、ワイルドカードを指定するのではなく、次のように各ユーザーごとに分けて設定することもできます。セキュリティ的にはこちらの設定の方が強固になりますが、公開するユーザーが増えるたびに、メンテナンスをしなくてはいけないことにはなります。

/etc/apahce2/mods-available/userdir.conf
       <Directory /home/vagrant/hoge_html>
          # 省略
       </Directory>
       <Directory /home/tan/hoge_html>
          # 省略
       </Directory>
</IfModule>

/var/wwwを公開するときは、apache2.confのDirectoryディテクティブで、公開するパスを有効化する設定をしましたが、ユーザーディレクトリを公開するときは、userdir.confで設定します。apache2.confでも同じように設定をしなくてはいけないのかいえば、そのようなことはありません。つまり、userdir.confで設定をすれば、下記の設定を行う必要はありません。

/etc/apache2.conf
<Directory /home/*/>
	Options Indexes FollowSymLinks
	AllowOverride None
	Require all granted
</Directory>

userdir.confを有効化する

userdir.confは、デフォルトで /etc/apahce2/mods-enabledにシンボリックリンクを作成していません。よって、次のコマンドで設定した内容を有効化する必要があります。

#a2enmod userdir

Apacheの再起動

コンフィグファイルを触ったら、Apacheを再起動して反映させます。

#systemctl restart apache2

動作確認

ブラウザを起動して、URLに次のアドレスを指定すれば、画面が表示されます。このとき設定するパスは、~vagrant/public_html/index.htmlのように、public_htmlを加える必要はありません。

http://192.168.33.1/~vagrant/index.html

tanユーザーのディレクトリにアクセスしたい場合は、URLで次のようにします。

http://192.168.33.1/~tan/index.html
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?