MeWrite Docs

403 Forbidden

Apacheでアクセス権限がない場合に発生するエラー

概要

403 Forbidden は、Apacheがリクエストを理解したが、アクセスを拒否した場合に発生するエラーです。ファイル権限、ディレクトリ設定、.htaccessの制限などが原因です。

エラーメッセージ

ブラウザ:

Forbidden
You don't have permission to access this resource.

Apacheエラーログ:

[error] [client 192.168.1.1] AH01630: client denied by server configuration
[error] [client 192.168.1.1] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex
[error] [client 192.168.1.1] AH00035: access to /secret/ denied

原因

  1. ファイル/ディレクトリ権限: Apacheユーザーが読み取れない
  2. Directory設定: Require all deniedなどで制限されている
  3. DirectoryIndex未設定: ディレクトリにインデックスファイルがない
  4. .htaccess制限: deny from allなどの設定
  5. SELinux: セキュリティコンテキストの問題
  6. Options設定: Indexesが無効でインデックスファイルがない

解決策

1. ファイル権限を確認・修正

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# 現在の権限を確認
ls -la /var/www/html/

# ディレクトリ権限を修正(755推奨)
sudo chmod 755 /var/www/html
sudo chmod -R 755 /var/www/html/

# ファイル権限を修正(644推奨)
sudo find /var/www/html -type f -exec chmod 644 {} \;

# 所有者をApacheユーザーに変更
sudo chown -R www-data:www-data /var/www/html/  # Ubuntu/Debian
sudo chown -R apache:apache /var/www/html/       # CentOS/RHEL

2. Apache設定でアクセスを許可

/etc/apache2/sites-available/000-default.conf (Ubuntu):

1
2
3
4
5
6
7
8
9
<VirtualHost *:80>
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

/etc/httpd/conf/httpd.conf (CentOS):

1
2
3
4
5
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

3. DirectoryIndexを設定

1
2
3
<Directory /var/www/html>
    DirectoryIndex index.html index.php index.htm
</Directory>

または、ディレクトリ一覧表示を有効化:

1
2
3
<Directory /var/www/html>
    Options +Indexes
</Directory>

4. .htaccessを確認

問題のある.htaccess:

1
2
# これがあるとすべてアクセス拒否
deny from all

修正版:

1
2
3
4
5
# Apache 2.4形式
Require all granted

# または特定IPのみ許可
Require ip 192.168.1.0/24

5. AllowOverrideを有効化

.htaccessを有効にする:

1
2
3
<Directory /var/www/html>
    AllowOverride All
</Directory>

6. SELinuxの確認と修正

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# SELinux状態を確認
getenforce

# Webコンテンツ用のコンテキストを設定
sudo chcon -R -t httpd_sys_content_t /var/www/html/

# 書き込み可能なディレクトリの場合
sudo chcon -R -t httpd_sys_rw_content_t /var/www/html/uploads/

# 永続的に設定
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?"
sudo restorecon -Rv /var/www/html/

7. mod_rewrite関連の問題

.htaccess:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# mod_rewriteが有効か確認
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # 存在しないファイルはindex.phpへ
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>

mod_rewriteを有効化:

1
2
3
4
5
6
7
# Ubuntu/Debian
sudo a2enmod rewrite
sudo systemctl restart apache2

# CentOS/RHEL
# /etc/httpd/conf/httpd.conf で LoadModule rewrite_module を確認
sudo systemctl restart httpd

8. 特定ファイルへのアクセス制限

1
2
3
4
5
6
7
8
9
# .htで始まるファイルを保護
<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

# 設定ファイルを保護
<FilesMatch "\.(env|ini|log|conf)$">
    Require all denied
</FilesMatch>

9. ログを確認

1
2
3
4
5
6
7
# エラーログを確認
sudo tail -f /var/log/apache2/error.log    # Ubuntu/Debian
sudo tail -f /var/log/httpd/error_log      # CentOS/RHEL

# アクセスログを確認
sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/httpd/access_log

10. 設定をテスト・再起動

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 設定ファイルの構文チェック
sudo apachectl configtest
sudo apache2ctl -t

# 設定を再読み込み
sudo systemctl reload apache2
sudo systemctl reload httpd

# 再起動
sudo systemctl restart apache2
sudo systemctl restart httpd

Apache 2.2 から 2.4 への移行

1
2
3
4
5
6
7
# Apache 2.2(古い形式)
Order deny,allow
Deny from all
Allow from 192.168.1.0/24

# Apache 2.4(新しい形式)
Require ip 192.168.1.0/24

Apache の他のエラー

最終更新: 2025-12-08