◀ 5.

Apacheのドキュメントルートを変更する方法

▶
この記事の要点
  • 変更するのは DocumentRoot と、同じパスの <Directory> ブロックの 2 か所
  • 設定ファイルは OS で異なる(Ubuntu は sites-available/000-default.conf、RHEL 系は conf/httpd.conf)
  • 新しいディレクトリには Require all granted が必要。ないと 403 Forbidden
  • 反映前に apachectl configtest で文法チェック → 再読み込み
  • RHEL 系は SELinux のラベル、全 OS 共通で親ディレクトリの実行権限が 403 の原因になりやすい

ドキュメントルートとは

ドキュメントルート(DocumentRoot)は、Apache がブラウザに公開するファイルの起点となるディレクトリです。http://example.com/about.html にアクセスすると、Apache は「ドキュメントルート + /about.html」のファイルを返します。

OS ごとに編集する設定ファイルは少し違いますが、考え方は同じで、DocumentRoot と対応する <Directory> を揃えたうえで、Apache を再起動(再読み込み)します。

OS 別の設定ファイルと初期値

環境編集するファイル初期のドキュメントルート
Ubuntu / Debian/etc/apache2/sites-available/000-default.conf/var/www/html
RHEL / CentOS / Rocky / AlmaLinux/etc/httpd/conf/httpd.conf/var/www/html
Windows(Apache Lounge 版)C:\Apache24\conf\httpd.conf${SRVROOT}/htdocs
Windows(XAMPP)C:\xampp\apache\conf\httpd.confC:/xampp/htdocs
macOS(標準同梱)/etc/apache2/httpd.conf/Library/WebServer/Documents

インストール方法によってパスが異なる場合があります。分からないときは apachectl -V(RHEL 系は httpd -V)で表示される HTTPD_ROOT と SERVER_CONFIG_FILE を確認してください。

Ubuntu / Debian での手順

  1. 設定ファイルを開きます。
    sudo vi /etc/apache2/sites-available/000-default.conf
  2. <VirtualHost *:80> 内の DocumentRoot を変更したいパスに書き換え、同じパスの <Directory> を追加します。
    <VirtualHost *:80>
        ServerName example.com
        DocumentRoot /srv/www/myapp
    
        <Directory /srv/www/myapp>
            Options -Indexes +FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
  3. 文法チェックをして再読み込みします。
    sudo apachectl configtest      # Syntax OK と出れば問題なし
    sudo systemctl reload apache2

Ubuntu の /etc/apache2/apache2.conf では、初期状態で /var/www/ 配下だけにアクセス許可(Require all granted)が与えられています。/var/www の外を指定した場合は、上の例のように <Directory> で許可を追加しないと 403 になります。

RHEL / CentOS 系での手順

sudo vi /etc/httpd/conf/httpd.conf

DocumentRoot "/var/www/html" と、その下にある <Directory "/var/www/html"> の 2 か所を新しいパスに変更します。

DocumentRoot "/srv/www/myapp"

<Directory "/srv/www/myapp">
    Options -Indexes +FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
sudo httpd -t                   # 文法チェック
sudo systemctl restart httpd

SELinux が有効(getenforce が Enforcing)な環境では、新しいディレクトリに Web 公開用のラベルを付けないと、パーミッションが正しくても 403 になります。

sudo semanage fcontext -a -t httpd_sys_content_t "/srv/www(/.*)?"
sudo restorecon -Rv /srv/www

Windows での手順

Apache の設定ディレクトリにある httpd.conf を開き、DocumentRoot と <Directory> を変更します。パスの区切りは スラッシュ(/)で書くのが無難です。

DocumentRoot "D:/web/myapp"
<Directory "D:/web/myapp">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

保存後、サービスとして動かしている場合は「サービス」画面から Apache を再起動するか、管理者権限のコマンドプロンプトで httpd.exe -k restart を実行します。XAMPP ならコントロールパネルで Stop → Start です。

macOS での手順

sudo vi /etc/apache2/httpd.conf

他の環境と同様に DocumentRoot "/Library/WebServer/Documents" と対応する <Directory> を書き換え、sudo apachectl configtest → sudo apachectl restart で反映します。Homebrew で入れた Apache の場合、設定ファイルは $(brew --prefix)/etc/httpd/httpd.conf です。

403 / 404 が出るときの原因

  • <Directory> のパスが DocumentRoot と一致していない: 末尾のスラッシュや大文字小文字の違いにも注意
  • 親ディレクトリに実行権限がない: /home/user/www のようにホーム配下を指定すると、/home/user に Apache ユーザーの実行権限(x)がなく 403 になりがち。namei -l /home/user/www/index.html で経路上の権限を一覧できる
  • index ファイルがない: DirectoryIndex(既定は index.html)に該当するファイルがなく、Options -Indexes の場合は 403
  • SELinux: RHEL 系で上記がすべて正しいのに 403 なら、ls -Z でラベルを確認
  • 別の VirtualHost が優先されている: apachectl -S でどの設定がどのホスト名に割り当たっているか確認

確認方法

  • 設定ファイルの文法エラーがないか(apachectl configtest)
  • 新しい公開ディレクトリに index ファイルがあるか(echo ok | sudo tee /srv/www/myapp/index.html などで仮置きして確認)
  • Apache 再起動後に 403 / 404 が出ていないか(curl -I http://localhost/ で 200 が返るか)
  • エラーが出る場合はエラーログを確認(Ubuntu: /var/log/apache2/error.log、RHEL 系: /var/log/httpd/error_log)

Laravel などのフレームワークを公開する場合は、プロジェクト直下ではなく public ディレクトリをドキュメントルートに指定します。プロジェクト直下を公開すると .env などの設定ファイルが外部から見えてしまう危険があります。

関連

Post Share
子ページ

子ページはありません

同階層のページ
  1. インストール方法(Ubuntu)
  2. Apache HTTP Server のインストール方法(CentOS / RHEL)
  3. ディレクトリ構造
  4. httpd.conf(設定ファイル)
  5. ドキュメントルートの変更方法
  6. .htaccess
  7. コマンド一覧
  8. エラー一覧
  9. VPSへの導入例(CentOS編)
  10. SSLの設定
  11. httpd.conf系のバーチャルホストの設定エラー有無確認方法
  12. .htaccess でベーシック認証
  13. configの文法チェック