DocumentRootディレクティブ:Webサーバで公開するコンテンツを配置するディレクトリ

DocumentRoot ディレクティブは Apache が Web サーバとして外部に公開するコンテンツを配置するディレクトリを設定するのに使用します。このディレクトリはドキュメントルートと呼ばれます。ここでは Apache の DocumentRoot ディレクティブの使い方について解説します。

(Last modified: )

DocumentRootディレクティブの使い方

DocumentRoot ディレクティブでは Apache サーバが外部に公開する Web ページを配置するディレクトリを設定します。書式は次のとおりです。

DocumentRoot ディレクトリ

記述できる場所は httpd.conf, VirtualHost です。

ディレクトリは絶対パス、又は ServerRoot ディレクティブで設定したディレクトリからの相対パスで指定します。最後にスラッシュ(/)は記述しないで下さい。ここで設定したディレクトリをドキュメントルートと呼びます。

※ ServerRoot ディレクティブについては「ServerRootディレクティブ:相対パスの起点となるApacheがインストールされているディレクトリ」を参照されてください。

例えば d:/pg/Apache/Apache24/htdocs ディレクトリに設定する場合には次のように記述します。上が相対パスで指定した場合、下が絶対パスで指定した場合です。

DocumentRoot htdocs
DocumentRoot d:/pg/Apache/Apache24/htdocs

httpd.conファイルでの記述

httpd.conf ファイルにはデフォルトで次のように記述されています。

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "${SRVROOT}/htdocs"

デフォルトの設定では Apache がインストールされたディレクトリの中にある htdocs ディレクトリがドキュメントルートに設定されています。

ドキュメントルート(DocumentRoot)(1)

ブラウザで指定されたURLに対してどのファイルがクライアントへ送信されるのか

ユーザーがブラウザを使って Web サーバへアクセスした場合、入力された URL のパス部分を Apache のドキュメントルートに付け加えた場所にあるファイルがクライアントへ返されます。

例えばブラウザから http://www.example.com/index.html のようにルートにある index.html ファイルへアクセスした場合、 ドキュメントルートのディレクトリの中にある index.html ファイルがクライアントへ返されます。

同じようにブラウザから http://www.example.com/sub/hello.html へアクセスした場合、ドキュメントルートの中の sub ディレクトリの中にある hello.html ファイルがクライアントへ返されます。

ドキュメントルート:d:/pg/Apache/Apache24/htdocs/

http://www.example.com/index.html
>> d:/pg/Apache/Apache24/htdocs/index.html

http://www.example.com/sub/hello.html
>> d:/pg/Apache/Apache24/htdocs/sub/hello.html

ドキュメントルートを変更する

それでは実際にドキュメントルートを変更してみます。ドキュメントルートのディレクトリは Apache をインストールしたディレクトリの下だけでなく自由に設定することができます。今回は d:\contents というディレクトリを作成して DocumentRoot ディレクティブに設定します。

httpd.conf ファイルを開き、次のように記述されている箇所を検索してください。

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "${SRVROOT}/htdocs"

次のように変更しました。

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot d:/contents

※ Winows の場合でもディレクトリの区切りはスラッシュ(/)です。また最後にスラッシュは記述しないでください。

また httpd.conf には元々のドキュメントルートに対して次の設定が行われています。

<Directory "${SRVROOT}/htdocs">
  Options Indexes FollowSymLinks
  AllowOverride None
  Require all granted
</Directory>

今回この部分も次のように変更しました。

<Directory d:/contents>
  Options Indexes FollowSymLinks
  AllowOverride None
  Require all granted
</Directory>

httpd.conf ファイルを保存します。そのあとで Apache を再起動して設定ファイルを読み込みなおしてください。

次にドキュメントルートにファイルを配置します。 d:\contnts ディレクトリに次のように index.html ファイルを設置しました。

ドキュメントルートを変更する(1)

ブラウザから http://localhost/index.html へアクセスします。新しくドキュメントルートに設定したディレクトリに設置した index.html ファイルがクライアントへ返されて次のようにブラウザに表示されました。

ドキュメントルートを変更する(2)

-- --

Apache の DocumentRoot ディレクティブの使い方について解説しました。

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。