10.

ApacheでSSLを有効化|Let's Encrypt certbotとssl.conf設定・常時HTTPS化

編集
この記事の要点
  • 無料証明書発行ツール certbot を入れて Let’s Encrypt から証明書を取得する
  • certbot certonly --webroot でドキュメントルート経由のドメイン検証を行う
  • /etc/httpd/conf.d/ssl.conf に SSLCertificateFile / KeyFile / ChainFile を Let’s Encrypt のパスで設定する
  • firewalld で 443/tcp を開け、Apache を再起動する
  • .htaccess の RewriteRule で HTTP → HTTPS に常時リダイレクトする
  • 証明書は 90 日で期限切れになるため、certbot renew を cron / systemd timer で自動更新する

前提

  • OS: CentOS 7 系(他ディストリは適宜読み替え)
  • Web サーバ: Apache HTTP Server(httpd 2.4)
  • ドキュメントルート: /var/www/html
  • 取得するドメインの A レコードがこのサーバを向いている
  • 80 番ポートが開放され、外部から到達可能

1. certbot のインストール

Let’s Encrypt の証明書を発行するクライアントである certbot を EPEL から入れます。

yum -y install epel-release
yum -y install certbot python-certbot-apache mod_ssl

mod_ssl は Apache の SSL モジュール本体。これが入っていないと ssl.conf の設定が読まれません。

2. 証明書の発行

certbot certonly --webroot 方式は、ドキュメントルートに認証用ファイルを置いて Let’s Encrypt がそれを読み取りに来る形でドメイン所有を検証します。

certbot certonly --webroot -w /var/www/html/ -d example.com -d www.example.com

初回は対話形式で以下を聞かれます。

  • 証明書切れ通知用のメールアドレス
  • 利用規約への同意
  • Electronic Frontier Foundation からの購読メール送信可否(No で可)

発行に成功すると証明書一式が /etc/letsencrypt/live/<ドメイン名>/ 配下に作成されます。

3. ssl.conf の設定

/etc/httpd/conf.d/ssl.conf を編集し、証明書のパスを Let’s Encrypt のものに差し替えます。

<VirtualHost _default_:443>
    ServerName example.com

    SSLEngine on
    SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
    SSLHonorCipherOrder on

    SSLCertificateFile      /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile   /etc/letsencrypt/live/example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem

    DocumentRoot "/var/www/html"
</VirtualHost>

設定後は文法チェックして反映します。

apachectl configtest
systemctl restart httpd

4. 443 ポートの開放

firewalld を使用している場合、HTTPS が外から届くように 443/tcp を解放します。

firewall-cmd --add-port=443/tcp --zone=public
firewall-cmd --add-port=443/tcp --zone=public --permanent
firewall-cmd --reload

クラウド環境では別途セキュリティグループ / ネットワーク ACLでも 443 を許可する必要があります。

5. .htaccess で常時 HTTPS 化

HTTP(80)でアクセスされた場合に HTTPS(443)へ自動リダイレクトします。/var/www/html/.htaccess を以下の内容で作成。

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

.htaccess を有効化するため /etc/httpd/conf/httpd.conf の対象ディレクトリで AllowOverride All を指定します。

<Directory "/var/www/html">
    AllowOverride All
    Require all granted
</Directory>

設定を反映:

systemctl restart httpd

6. 動作確認

確認項目方法
HTTPS で 200 を返すかcurl -I https://example.com/
HTTP が HTTPS にリダイレクトされるかcurl -I http://example.com/ → 301 と Location が出る
証明書のサーバ名・期限openssl s_client -connect example.com:443 -servername example.com
総合スコアSSL Labs(https://www.ssllabs.com/ssltest/)で A 以上

7. 証明書の自動更新

Let’s Encrypt 証明書は有効期限 90 日です。期限切れになるとブラウザに警告が出るため、cron で自動更新を設定します。

# 動作確認(実際には更新しないテスト)
certbot renew --dry-run

# cron に登録(毎日早朝に試行、必要時のみ更新される)
crontab -e
# 毎日 3:00 に renew、更新時は httpd を再起動
0 3 * * * certbot renew --quiet --post-hook "systemctl reload httpd"

よくあるエラーと対処

症状原因 / 対処
Failed authorization procedure80 番が閉じている or DNS A レコードが向いていない。firewalld と dig example.com を確認
The requested URL ... was not found on this server by ACMEwebroot のパスがドキュメントルートと一致していない
httpd 再起動でエラーapachectl configtest でエラー行を特定。SSLCertificateFile のパス誤りが多い
HTTPS でも保護されていない警告HTML 内に http:// のリソースが混在(mixed content)。すべて https:// もしくは相対パスに修正
証明書が更新されないrenew の cron が動いていない / post-hook で httpd 再読込していない

関連

編集
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の文法チェック

最近更新/作成されたページ