どうも、こっこです。
今回はApacheでpublic_htmlを作成して、ユーザー毎のウェブで表示できるディレクトリとして設定する方法を紹介します。
毎度VPSで新しいLinuxサーバーを立ち上げてApacheの設定をするときに「どういう設定していたっけな〜?」と忘れるので、個人の備忘録的な感じで残しておきます。
今回使用しているOS:Fedora32
public_htmlとは?
ブラウザ上で表示させたいhtmlやphp等のソースを公開する場所に設定するには、特別な設定をしていなければ「/var/www/html」以下に置きますよね?
ただここは本番環境の設置場所であり、その本番環境の場所に表示テストでごちゃごちゃとファイルを置きたくないですよね。また、複数ユーザーが扱う1つのサーバーで皆がそれぞれ本番環境のディレクトリにファイルをごちゃごちゃと設置してしまうと管理が大変です。
そのためにユーザーディレクトリに「public_html」というディレクトリを設置して、ユーザー毎のウェブ表示を確認できる機能がApacheには備わっています。
実際には「/home/ユーザー名/public_html」以下にhtmlファイル等を設置することで、「http://ドメインまたはIPアドレス/~ユーザー名/ファイル名」とブラウザでアクセスすることでサーバーへ通信してウェブ表示することができます。
public_htmlで表示できるように設定する
public_htmlディレクトリの作成
ではその設定をしていきましょう。まずはディレクトリの作成から。
[ユーザー名@ドメイン]$ mkdir /home/ユーザー名/pulic_html
ただ、このようにユーザーディレクトリ以下に「public_html」とディレクトリを設置するだけではダメです。設定ファイルでしっかりと設定をする必要があります。
設定ファイルの変更と設定
[ユーザー名@ドメイン]$ vi /etc/httpd/conf.d/userdir.conf
今回説明用で使用しているFedora32では設定ファイルは上記パスにあります。OSまたはApacheのバージョンにより設定ファイルの保管場所が異なるかもしれませんが、「userdir.conf」という設定ファイルをviコマンド等で開きます。
変更前の設定ファイルはこちら
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received.
#
# The path to the end user account 'public_html' directory must be
# accessible to the webserver userid. This usually means that ~userid
# must have permissions of 711, ~userid/public_html must have permissions
# of 755, and documents contained therein must be world-readable.
# Otherwise, the client will only receive a "403 Forbidden" message.
#
<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>
#
# 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
Require method GET POST OPTIONS
</Directory>
この設定ファイルで変更するべきこと
- 17行目:1行まるごとコメントアウト
- 24行目:1行のコメントアウトを外す
- 32行目:1行まるごとコメントアウト
- 33行目:1行挿入して「AllowOverride All」と入力
変更後はこのようになるはずです。
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received.
#
# The path to the end user account 'public_html' directory must be
# accessible to the webserver userid. This usually means that ~userid
# must have permissions of 711, ~userid/public_html must have permissions
# of 755, and documents contained therein must be world-readable.
# Otherwise, the client will only receive a "403 Forbidden" message.
#
<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>
#
# 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
AllOverride All
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Require method GET POST OPTIONS
</Directory>
ここまで入力と変更ができれば保存してviエディタで操作するのは終了です。
終わったらApacheを再起動させておきましょう。
[ユーザー名@ドメイン]$ server httpd restart
403エラーの解決方法
ではここまでできたら表示の確認をしてみましょう。
public_htmlディレクトリ以下に適当なhtmlファイルを設置し、ブラウザでアクセスするためには「http://ドメインまたはIPアドレス/~ユーザー名/ファイル名」の形式でアクセスします。ユーザー名の前の「~」は忘れずに!
アクセスすると403エラーで「You don’t have permission to access this resource.」ということで「アクセスする権限が無いよ」と怒られます。
ユーザーディレクトリとpublic_htmlディレクトリの権限を変更していないために403エラーとなってしまいます。実はその設定をしろと先程のuserdir.confファイルの頭の部分で指示されていました。
かんたんに訳すと「ユーザーディレクトリは権限を711に変更、その下のpublic_htmlは権限を755に変更する必要がある。そうしないと403 Forbiddenエラーの表示になるよ。」とのことです。
そのとおりに権限を変更してあげましょう。
#ユーザーディレクトリの権限を711に変更 [ユーザー名@ドメイン]$ chmod 711 /home/ユーザー名 #その下のpublic_htmlディレクトリの権限を755に変更 [ユーザー名@ドメイン]$ chmod 755 /home/ユーザー名/public_html
この2つのディレクトリの権限変更ができればようやく終わりです。改めて「http://ドメインまたはIPアドレス/~ユーザー名/ファイル名」にアクセスしてブラウザ上で表示ができれば完了です、お疲れ様でした。
まとめ
いかがだったでしょうか?
いろいろと解説をしながら説明をしたので長く感じるかもしれませんが、やることはたった3つで
- public_htmlディレクトリの作成
- Apache設定ファイルの変更
- ディレクトリ2種の権限変更
を行うことでユーザー毎にウェブ表示できるディレクトリを分けてそれぞれの表示テストを行うことが出来ます。
ぜひやってみてくださいね。
ではまた!










