Webサーバ(Apache)の基本
Webサーバは、ブラウザでURLリクエストしたファイルをクライアントにレスポンスします。Webサーバでは決められたフォルダにファイルを配置しておく必要がありますが、この仕組みを Apache(アパッチ) をベースにみていきましょう。
DocumentRoot(ドキュメントルート) とは?
Webサーバに HTML、画像、JS、CSS、PHPなどのコンテンツを配置します。これらコンテンツを配置する最上位のフォルダを、DocumentRootといいます。

XAMPPのDocumentRootはhtdocsフォルダです。フォルダやファイルを作成して公開します。
URL と Webサーバ
DocumentRootの中にコンテンツを配置したら、ブラウザからURLリクエストするとApache(Webサーバ)が対応したファイルを探して起動します。

「 / 」はフォルダを意味する
URLでは「http://」がHTTPプロトコルで、「localhost」がホストといいます。「 / 」はフォルダという決まりがありhttp://localhost/lesson1/sample.php のURL の場合、以下のようになります。
項目 | 説明 |
---|---|
http:// | HTTPプロトコル |
localhost | ホスト localhost |
/ | DocumentRoot(htdocsフォルダ) |
lesson1/ | 「lesson1」フォルダ |
sample.php | 「sample.php」ファイル |
DocumentRoot(htdocsフォルダ)に配置したファイルが、URLで指定されていることを理解しましょう。

開発用ホスト localhost(ローカルホスト)とは
Web の世界ではネットワーク上のPCに名称をつけることができ、その名称のことを「ホスト名(hostname)」といいます。特に自分のPC のことを「ローカル」といい、ホスト名でいうと localhost(ローカルホスト)といいます。

また自分のPCには「ローカルループバックアドレス」という内部的なIPアドレス「127.0.0.1」が割り振られています。通常「127.0.0.1」のホスト名は「localhost」が設定されています。よって Webサーバを起動していれば、http://localhost/ または http://127.0.0.1/ の URL でアクセスすることができます。
DocumentRootの確認
DocumentRoot の場所を把握する
DocumentRootの場所は、OSやWebサーバの設定によって異なるので 利用する開発環境によってDocumentRootのパスを把握しておきましょう。
Apache(Linux)
Linux(Ubuntu)における、ApacheのDocumentRootです。
/var/www/html/
XAMPP(Windows)
XAMPPのDocumentRootです。
¥C:xampp/htdocs/

XAMPP(Mac)
MAMPのDocumentRootです。
/Applications/XAMPP/htdocs/

Apache(Homebrew)
Mac(Homebrew)におけるApacheのDocumentRootです。
/Library/WebServer/Documents/
URLの確認
URLでファイルを指定しますが、ファイル名を指定しない場合、「index.html」や「index.php」を優先的にアクセスされます。XAMPP、MAMPのDocumentRootとURLの関係です。
XAMPP
Webサーバファイルパス | URL |
---|---|
C:xampp/htdocs/ | http://localhost/ |
C:xampp/htdocs/index.html | http://localhost/index.html |
C:xampp/htdocs/index.php | http://localhost/index.php |
C:xampp/htdocs/lesson1/sample1.php/ | http://localhost/lesson1/sample1.php |
MAMP
Webサーバファイルパス | URL |
---|---|
/Application/MAMP/htdocs/ | http://localhost/ |
/Application/MAMP/htdocs/index.html | http://localhost/index.html |
/Application/MAMP/htdocs/index.php | http://localhost/index.php |
/Application/MAMP/htdocs/lesson1/sample1.php/ | http://localhost/lesson1/sample1.php |
Apache の設定ファイル
Apache の DocumentRoot は、Apache の設定ファイルで設定します。例えば「http.conf」といったファイルがあり、ドメインごとに DocumentRoot を変更できるバーチャルドメインを利用した設定が可能です。この設定はサーバの知識が必要になるので、別途学習してみてください。
PHP と動的コンテンツ
PHP は HTML を出力することができるスクリプト言語です。Webサーバが「.php」を判別するとPHPプログラムを実行します。PHPが HTMLを出力した結果を、Apacheがクライアントにレスポンスします。
このようにプログラム処理でHTML生成&出力するコンテンツを動的コンテンツといいます。