タイトル: Webサーバの設定
SEOタイトル: Apache Web サーバ設定完全ガイド
| この記事の要点 |
|
インストール
# Ubuntu / Debian
sudo apt update
sudo apt install -y apache2
sudo systemctl enable --now apache2
# RHEL / Rocky / AlmaLinux
sudo dnf install -y httpd
sudo systemctl enable --now httpd
# 動作確認
curl -I http://localhost/
# HTTP/1.1 200 OK
# Server: Apache/2.4.x (Ubuntu)
ディレクトリ構成
| パス (Debian系) | 役割 |
|---|---|
/etc/apache2/apache2.conf | 本体設定(全体) |
/etc/apache2/ports.conf | Listen ポート定義 |
/etc/apache2/sites-available/ | VirtualHost 定義(無効化済み含む) |
/etc/apache2/sites-enabled/ | 有効化された VirtualHost (シンボリックリンク) |
/etc/apache2/mods-available/ | 利用可能モジュール |
/etc/apache2/mods-enabled/ | 有効化されたモジュール |
/etc/apache2/conf-available/ | 追加設定スニペット |
/var/www/html/ | 既定 DocumentRoot |
/var/log/apache2/ | access.log / error.log |
RHEL 系では /etc/httpd/conf/httpd.conf、/etc/httpd/conf.d/*.conf、/etc/httpd/conf.modules.d/、ドキュメントルートは /var/www/html/。
有効化/無効化コマンド (Debian系)
# モジュール
sudo a2enmod rewrite # mod_rewrite 有効化
sudo a2enmod ssl headers proxy proxy_http
sudo a2dismod status # 無効化
# サイト
sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
# 追加設定
sudo a2enconf security
sudo a2disconf serve-cgi-bin
# 変更を反映
sudo systemctl reload apache2
基本 VirtualHost 例 (HTTP)
# /etc/apache2/sites-available/example.com.conf
ServerName example.com
ServerAlias www.example.com
ServerAdmin admin@example.com
DocumentRoot /var/www/example.com/public
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
sudo a2ensite example.com.conf
sudo apache2ctl configtest
sudo systemctl reload apache2
HTTPS 化 (Let's Encrypt)
# Certbot で証明書取得 + Apache 設定自動更新
sudo apt install -y certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com
# 自動更新確認
sudo systemctl status certbot.timer
sudo certbot renew --dry-run
HTTPS VirtualHost 例
ServerName example.com
DocumentRoot /var/www/example.com/public
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Protocols h2 http/1.1
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
AllowOverride All
Require all granted
# HTTP → HTTPS リダイレクト
ServerName example.com
Redirect permanent / https://example.com/
AllowOverride と .htaccess
AllowOverride はディレクトリ毎の .htaccess 上書きをどこまで許すか制御します。
| 値 | 意味 |
|---|---|
None | .htaccess を無効化(性能最良・推奨) |
All | 全ディレクティブを上書き可(柔軟だが遅い) |
AuthConfig | Basic 認証だけ許可 |
FileInfo | RewriteRule / AddType / Redirect 許可 |
Indexes | DirectoryIndex / Options Indexes |
Limit | Require / Order / Allow / Deny |
# .htaccess 例: 拡張子なし URL ルーティング (Laravel 等)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
# .htaccess 例: Basic 認証
AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
# パスワードファイル生成
# sudo htpasswd -c /etc/apache2/.htpasswd alice
リバースプロキシ (mod_proxy)
# Node.js / Laravel Octane / Spring Boot 等を 8080 で待たせて Apache が前段
ServerName api.example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/api.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/api.example.com/privkey.pem
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
RequestHeader set X-Forwarded-Proto https
Listen ポート
# /etc/apache2/ports.conf
Listen 80
Listen 443
Listen 443
# 追加ポート
Listen 8080
運用コマンド
# 構文検証
sudo apache2ctl configtest # Debian 系
sudo httpd -t # RHEL 系
# Syntax OK
# 設定一覧表示
apache2ctl -S # VirtualHost 構造
apache2ctl -M # ロード済モジュール
# 再起動 / リロード
sudo systemctl reload apache2 # 無停止反映
sudo systemctl restart apache2 # 完全再起動
# ステータス
sudo systemctl status apache2
ss -lntp | grep -E ':80|:443'
# ログ
sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/error.log
Apache vs Nginx の比較
| 項目 | Apache | Nginx |
|---|---|---|
| モデル | prefork / worker / event MPM | 非同期イベント駆動 (高並列に強い) |
| 設定 | 柔軟(.htaccess あり) | シンプル(再読込必須) |
| 動的コンテンツ | mod_php 等で同一プロセス内 | FastCGI/uwsgi にリバプロ |
| 静的配信性能 | 標準 | 圧倒的に速い |
| 用途 | 共有ホスト・レガシー連携 | リバプロ・LB・モダン Web |
FAQ
Q: configtest が「AH00558: Could not reliably determine the server's fully qualified domain name」と出る
A: 警告のみで動作に影響なし。ServerName localhost を apache2.conf に追記すれば消えます。
Q: 設定変更が反映されない
A: sites-enabled/ のシンボリックリンクを確認、もしくは a2ensite 忘れ。apache2ctl -S で読まれている VirtualHost を確認。
Q: HTTPS 化したのに HTTP でアクセスできてしまう
A: HTTP の VirtualHost で Redirect permanent を入れる。または HSTS ヘッダ送出と組み合わせる。