参考 「ネコでもわかる!」連載 第6回 「SSL証明書 Let’s Encryptの導入」

1.ApacheへのSSLモジュールmod_sslのインストール

mod_sslが導入済みかどうかは以下のコマンドで確認できる。

# httpd -M

入っていなかったので、インストール。

# yum install mod_ssl
# systmctl restart httpd

勿論、ファイヤーウォールのポートは開放しておくこと。

# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: http https
  ports:
  protocols:

2.vhost.confの修正

ApacheのVirtual Host機能を使っているので、/etc/httpd/conf/conf.d/vhost.confとして、まとめて1つの設定ファイルを作成していた。 今回、これをhttps用に修正するのだが、各サブドメイン毎に分けて作成した方がなにかと便利なので、サブドメイン名.confとして分けて作成し、元のvhost.confは他の拡張子にrename (vhost.conf.org等)しておく。

tako.example.jp 用のtako.example.jp.confファイル、赤字の443ポート部分を追記。

<VirtualHost *:80>
ServerName www.tako.example.jp
DocumentRoot /var/www/html/tako
CustomLog /var/log/httpd/tako.example.jp-access.log common
ErrorLog  /var/log/httpd/ tako.example.jp-error.log
<Directory "/var/www/html/tako">
    AllowOverride all
</Directory>
</VirtualHost>

<VirtualHost *:443>
ServerName www.tako.example.jp:443
DocumentRoot /var/www/html/tako
CustomLog /var/log/httpd/ssl-tako.example.jp-access.log common
ErrorLog  /var/log/httpd/ ssl-tako.example.jp-error.log
<Directory "/var/www/html/tako">
    AllowOverride all
</Directory>
</VirtualHost>

ika.example.jp 用のika.example.jp.confファイル、同じく赤字の443ポート部分を追記。


<VirtualHost *:80>
ServerName www.ika.example.jp
DocumentRoot /var/www/htmli/ika
CustomLog /var/log/httpd/ika.example.jp-access.log common
ErrorLog  /var/log/httpd/ika.example.jp-error.log
<Directory "/var/www/html/ika">
    AllowOverride all
</Directory>
</VirtualHost>

<VirtualHost *:443>
ServerName www.ika.example.jp:443
DocumentRoot /var/www/html/ika
CustomLog /var/log/httpd/ssl-ika.example.jp-access.log common
ErrorLog  /var/log/httpd/ssl-ika.example.jp-error.log
<Directory "/var/www/html/ika">
    AllowOverride all
</Directory>
</VirtualHost>

3.Let’s Encryptのインストール

# yum install certbot python2-certbot-apache

4.サーバ証明書の取得

Certbot を使った SSL/TLS サーバ証明書の取得は以下のコマンドを使用。
-d の後に、サブドメインを入れる。 各サブドメイン毎にこの作業を繰り返す。

# certbot --apache -d www.tako.example.jp
# certbot --apache -d www.ika.example.jp
# certbot --apache -d www.tako.example.jp

問題がなければ、Congratulations! が出てサーバ証明書が無事取得できる。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
Requesting a certificate for www.tako.example.jp
Performing the following challenges:
http-01 challenge for www.tako.example.jp
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/httpd/conf.d/tako.example.jp.conf
Redirecting vhost in /etc/httpd/conf.d/tako.example.jp.conf to ssl vhost in /etc/httpd/conf.d/tako.example.jp.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://www.tako.example.jp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/www.tako.example.jp/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/www.tako.example.jp/privkey.pem
   Your certificate will expire on 2021-04-26. To obtain a new or
   tweaked version of this certificate in the future, simply run
   certbot again with the "certonly" option. To non-interactively
   renew *all* of your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

無事にサーバ証明書が取得できると、tako.example.jp.confファイルに赤字部分が自動的に追記されていた。

<VirtualHost *:80>
ServerName www.tako.example.jp
DocumentRoot /var/www/html/tako
CustomLog /var/log/httpd/tako.example.jp-access.log common
ErrorLog  /var/log/httpd/ tako.example.jp-error.log
<Directory "/var/www/html/tako">
    AllowOverride all
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www. tako.example.jp
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:443>
ServerName www.tako.example.jp:443
DocumentRoot /var/www/html/tako
CustomLog /var/log/httpd/ssl-tako.example.jp-access.log common
ErrorLog  /var/log/httpd/ssl-tako.example.jp-error.log
<Directory "/var/www/html/tako">
    AllowOverride all
</Directory>
ServerAlias www.tako.example.jp
SSLCertificateFile /etc/letsencrypt/live/www.tako.example.jp/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.tako.example.jp/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateChainFile /etc/letsencrypt/live/www.tako.example.jp/chain.pem
</VirtualHost>

http:// www. tako.example.jp からhttps:// www. tako.example.jp へのリダイレクトは最初の赤字部分に次のように自動的に追加されていて、他に設定は不要のようだ。

RewriteEngine on
RewriteCond %{SERVER_NAME} =www. tako.example.jp
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

証明書関連は次の部分に自動的に追記されている。

ServerAlias www.tako.example.jp
SSLCertificateFile /etc/letsencrypt/live/www.tako.example.jp/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.tako.example.jp/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateChainFile /etc/letsencrypt/live/www.tako.example.jp/chain.pem

詳細は以下の文書を見た方がよいとのこと。
https://certbot.eff.org/docs/using.html#where-are-my-certificates
いい加減な訳で間違っているだろうが、こういうことか?

「privkey.pem」:サーバー証明書用の秘密鍵。
ApacheではSSLCertificateKeyFileデイレクティブ、Nginxではssl_certificate_keyで使用する。

「fullchain.pem」: サーバー証明書を含む全ての証明書。 多くのサーバーソフトで使える。
Apache2.4.8以上ではSSLCertificateFileデイレクティブ、Nginxではssl_certificateに使用する。

「cert.pem」 と 「chain.pem」 (一般的でない):
「cert.pem」 は、自身のサーバー証明書。
「chain.pem」は、中間証明書や、ウェブブラウザによるサーバー検証用の証明書を含んでいる。 ウェブブラウザに使用するときは両方を一緒に使用すること、さもないとブラウザが「信頼できない接続」の表示をだす場合がある。 よくわからないが安易に使うべきではないようだ。
Apache2.4.8未満で「cert.pem」をSSLCertificateFileデイレクティブに、「chain.pem」を、SSLCertificateChainFileに使う。(今回はApache2.4.6なので自動的に、この構成になったようだ)
また、Nginx 1.3.7以上でOCSP stapling を使用するときは、`chain.pem`をssl_trusted_certificateに使用する。

注意:これらのファイルの名前を変えたり、移動させてはならない。
このフォルダから移動させると、certbot が正しく機能しなくなる。

またIncludeされたoptions-ssl-apache.confには、

SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1

の記述があり、利用できるSSL/TLS のバージョンは、マイナス(-) による制限から、残るのはTLSv1.2以降のプロトコルになっていた。

勿論、httpdをrestart すると、http://www.tako.example.jpでもhttps://に無事リダイレクトされSSL接続の設定が完了していた。
それにしても、自動的に適当なconfファイルを見つけ出し、自動追記するやり方には感心した。 初心者にはとても助かる。 多分ファイル名を各サイト名に合わせたのが良かったのかも。

5.サーバ証明書の更新

「Let’s Encrypt」の証明書は、3ヶ月毎に更新されるためcronによる自動更新の設定に挑戦。
いくら説明書を読んでも設定の仕方がよくわからず、諦めて毎日スケジュールにいれることに。 ずさんなやり方がこちら。

# crontab -e
45 4 * * * certbot renew && systemctl restart httpd

すると、/var/spool/cron/rootの設定ファイルが上の内容で出来ていた。
この「certbot renew」でVirtual Hostやマルチサイトで作成した各サイトの証明書が全て更新されるのか、はなはだ不安なのだが、とりあえず試してみることに。