Let's Encrypt証明書の導入


概要

CentOS6.6 + Apache + Let's Encryptでサイトの通信をSSL化したときのメモ。
cPanelが入っているのでデフォルトを踏襲したやり方を重視しています。

Let's Encrypt

無料・自動で証明書を発行する認証局(総合ポータル)
世間的にはドメイン認証のSSLサーバ証明書が無料に発行できるサービスとして認知度がある。

2019年、更にHTTPSサイトは普及するとの予測記事もあったので貼っておく。もっと増える宣言。
2019年にHTTPSがどれだけ普及するかを予測(Let's Encrypt)
関連記事(GIGAZINE)

実行環境

下記に加えて、OpenSSLも更新した。

  • CentOS release 6.6
  • Apache/2.2.29

今回の流れ

  1. サブドメインを取得(試験用)
  2. サイトを開設
  3. 証明書を取得
  4. サイトの常時SSL化
  5. Mixed Contentsの修正
  6. 本番反映
  7. 証明書更新の自動化

事前準備

Firewalled関連

下記を確認しておく

  • https接続を許可しているか
  • ポート443(https)の接続を許可しているか

Certbotのダウンロード

GitHub(Certbot)
Let's Encryptから証明書を取得し、サーバー上のHTTPSを自動的に有効にするツール。
EFF(電子フロンティア財団)がインターネット全体を暗号化するという取り組みの一部で提供している。

Python2.7をインストール

CentOS6.6のデフォルトである2.6でCertbotを実行するとDeprecation Warningが出る。
オワコンになる前にPython2.7を適用。ソースからインストールする。

# wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz

ビルドしてインストール(/usr/src/Python-2.7.14/)

# ./configure --enable-optimizations
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python2.7... no
(略)
creating Makefile

# make install
gcc -pthread -c -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -o Modules/python.o ./Modules/python.c
(略)

2.7を使うように設定

# python --version
Python 2.7.14

1. サブドメインを取得

同一Webサーバ内にサブドメインでアクセスする試験環境を構築することが目的。
お名前.comのDNSサービスを利用しているため、Aレコードの追加は無料で行える。

test.hogehoge.co.jp

2. サイトを開設

取得したサブドメインで:80(http)および:443(https)についてVirtualHostの設定を行う。
なお、外部から参照されないようにアクセス制限を仕掛ける。

<Directory ${DocumentRoot}>
    Order allow,deny
    Allow from ${IPAddr}
</Directory>

3. 証明書を取得

Certbotを利用して、SSL証明書を取得する

 certbot-auto certonly --webroot -w ${DocumentRoot} -d test.hogehoge.co.jp --email ${MailAddress} 
Option Help
certonly Obtain or renew a certificate, but do not install it
(証明書を取得または更新)
webroot Place files in a server's webroot folder for authentication
(認証をするwebrootフォルダ)
-d DOMAINS Comma-separated list of domains to obtain a certificate for
(認証をするドメインのリスト)
-m EMAIL Email address for important account notifications
(重要なお知らせが届くアドレス)

実行ログが流れ、完了したことが通知される

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/www.hogehoge.co.jp/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/www.hogehoge.co.jp/privkey.pem
   Your cert will expire on 2019-07-22. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto 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

証明書が発行されていることを確認

# ls -la /etc/letsencrypt/live/www.hogehoge.co.jp/
total 12
drwxr-xr-x 2 root root 4096 Apr 23 14:24 ./
drwx------ 4 root root 4096 Apr 23 14:24 ../
lrwxrwxrwx 1 root root   46 Apr 23 14:24 cert.pem -> ../../archive/www.hogehoge.co.jp/cert1.pem
lrwxrwxrwx 1 root root   47 Apr 23 14:24 chain.pem -> ../../archive/www.hogehoge.co.jp/chain1.pem
lrwxrwxrwx 1 root root   51 Apr 23 14:24 fullchain.pem -> ../../archive/www.hogehoge.co.jp/fullchain1.pem
lrwxrwxrwx 1 root root   49 Apr 23 14:24 privkey.pem -> ../../archive/www.hogehoge.co.jp/privkey1.pem
-rw-r--r-- 1 root root  692 Apr 23 14:24 README
ファイル 説明
cert.pem サーバ証明書(公開鍵)
chain.pem 中間証明書
privkey.pem 秘密鍵
fullchain.pem 証明書と中間証明書を連結したファイル

証明書の有効期限を確認する(90日後)。
「notBefore」は証明書の生成日時で「notAfter」が有効期限。

# openssl x509 -in /etc/letsencrypt/live/www.hogehoge.co.jp/cert.pem -noout -dates
notBefore=Apr 23 04:24:11 2019 GMT
notAfter=Jul 22 04:24:11 2019 GMT

4. サイトの常時SSL化

取得したSSL証明書をApacheへ設定する。
Apache 2.4.8 未満 では、fullchain.pemは設定できないため、下記のように設定する。(Let's Encrypt の使い方

httpd.conf
SSLCertificateFile /etc/letsencrypt/live/test.hogehoge.co.jp/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/test.hogehoge.co.jp/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/test.hogehoge.co.jp/chain.pem

再起動で適用となる。

 /etc/init.d/httpd graceful

5. Mixed Contentsの修正

MixedContentsは今回殆どなかったので、パスの修正程度で完了。
HTTPS Checkerと、SSL ServerTestを流し、結果に満足して完了。

7. 証明書更新の自動化

更新方法

cert-autoコマンドを実行する。
renewコマンドは、過去に取得した証明書のうち、有効期限が残り30日未満の証明書を更新する。
プラグインやオプションを明示的に指定しない場合、証明書の発行をした際のオプションが使用される。

取得時に使用したオプション

/etc/letsencrypt/renewal/${DOMAIN}.conf

Cron

  • 証明書を更新する
  • ログをローテーションする(logroteで何故かうまくいかなかった)
  • Apacheを再起動

このcert-autoでは、--post-hockオプションは利用できないようだ。
毎日Apacheを再起動しても問題ないサーバなのでこうしているが、不便である。

  • --post-hock
    証明書の更新が行われた場合にのみ、1度だけ実行されるコマンドを定義する(Apacheの再起動など)
/etc/cron.d/letsencrypt
00 X * * * hoge /usr/bin/certbot-auto renew -m ${MailAddres}
00 X * * * hoge /bin/rm -f /var/log/letsencrypt/letsencrypt.log.20
00 X * * * hoge /etc/init.d/httpd graceful

cert-autoのログは「/var/log/letsencrypt」に配置される。
毎日letsencrypt.log.20を消すので、過去20日分を保有する。

letsencrypt.log
letsencrypt.log.1
letsencrypt.log.2

おわり。