無料SSL/TLS証明書のLet's Encrypt が正式版になったので自動更新を含めて試してみた
はじめに
Let’s Encrypt とは、無償で SSL/TLS 証明書を発行してくれるサービスです。Web 全体を HTTPS化することを目指してサービスを行っており、非常に注目されています。
詳しいことは、以下のパブリックベータ時の記事を参照してください。若干、コマンドが変わっているところがありますが、それは後述します。大きな特徴としては、無償で有効期間が短く自動更新できることですね。
- 無料SSL証明書の Let’s Encrypt が公開されたので実際に試してみた
- Let’s Encrypt の SSL証明書を CentOS 7 にインストールしてみた
- Let’s Encrypt の SSL証明書を Apache を停止しないでインストールする方法
パブリックベータから変わった点
Let’s Encrypt Client のバージョンが 0.5 になり、コマンドが letsencrypt-auto に統一されました。
残念ながら、Let’s Encrypt の Get Started に書かれているのはバージョンが 0.4 のものなので、まだコマンドが統一されておらずそのままでは動作しません。
なぜ分かったかというと、GitHub に掲載されているコードサンプルが letsencrypt-auto に変わっていたためです。
また、パブリックベータでは未公開であった SSL/TLS 証明書の有効期限を更新するコマンドが公開されました。これで自動で証明書の更新ができるので、Let’s Encrypt の懸念点であった有効期間の短さが解消されますね。
なお、自動更新のスクリプトも数週間後に公開する予定だそうです。この記事では、サンプルスクリプトがあったので、それを元に変更して利用しています。
事前準備
事前に以下のパッケージをインストールしておきます。
|
1 2 |
# yum -y install httpd openssl mod_ssl # yum -y install git |
デフォルトで、CentOS 7 は HTTP(80)/HTTPS(443) ポートを塞いでいるので、ファイアウォールのポートを開放します。
|
1 2 3 |
# firewall-cmd --zone=public --add-port=80/tcp --permanent # firewall-cmd --zone=public --add-port=443/tcp --permanent # firewall-cmd --reload |
Apache を起動します。
|
1 |
# systemctl start httpd.service |
Let’s Encrypt Client のインストール
Let’s Encrypt Client のインストールは簡単で、以下のように行います。最後のコマンドを実行すると、必要なツールが自動でインストールされます。それが嫌な場合は、Let’s Encrypt の採用は見送った方がよいかもしれません。
|
1 2 3 |
# git clone https://github.com/letsencrypt/letsencrypt # cd letsencrypt # ./letsencrypt-auto --help |
インストールが成功すると、以下のようなガイドが表示されます。ここをよく見れば、いろいろと分かるのではないでしょうか。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
Installation succeeded. Requesting root privileges to run letsencrypt... /root/.local/share/letsencrypt/bin/letsencrypt --help letsencrypt-auto [SUBCOMMAND] [options] [-d domain] [-d domain] ... The Let's Encrypt agent can obtain and install HTTPS/TLS/SSL certificates. By default, it will attempt to use a webserver both for obtaining and installing the cert. Major SUBCOMMANDS are: (default) run Obtain & install a cert in your current webserver certonly Obtain cert, but do not install it (aka "auth") install Install a previously obtained cert in a server renew Renew previously obtained certs that are near expiry revoke Revoke a previously obtained certificate rollback Rollback server configuration changes made during install config_changes Show changes made to server config during installation plugins Display information about installed plugins Choice of server plugins for obtaining and installing cert: --apache Use the Apache plugin for authentication & installation --standalone Run a standalone webserver for authentication (nginx support is experimental, buggy, and not installed by default) --webroot Place files in a server's webroot folder for authentication OR use different plugins to obtain (authenticate) the cert and then install it: --authenticator standalone --installer apache More detailed help: -h, --help [topic] print this message, or detailed help on a topic; the available topics are: all, automation, paths, security, testing, or any of the subcommands or plugins (certonly, install, nginx, apache, standalone, webroot, etc) |
SSL/TLS 証明書のインストール
SSL/TLS 証明書のインストールでは、webroot プラグインを利用します。これを利用することで、Web サーバーを再起動することなく証明書をインストールすることができます。
ここでは、ドメイン名を aruse.top 、公開ディレクトリを /var/www/html として以下のコマンドを実行します。
|
1 |
# ./letsencrypt-auto certonly --webroot -w /var/www/html -d aruse.top |
インストールの途中でメールアドレスを聞かれるので、メールアドレスを入力して [了解] を押します。
利用規約に同意する場合は <Agree> を押します。
インストールが終了すると、以下のメッセージが表示されます。/etc/letsencrypt/live/aruse.top/fullchain.pem にサーバー証明書+中間証明書が作成されたことが分かります。同一ディレクトリに、サーバー証明書、中間証明書、秘密鍵も作成されています。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/aruse.top/fullchain.pem. Your cert will expire on 2016-07-12. To obtain a new version of the certificate in the future, simply run Let's Encrypt again. - If you lose your account credentials, you can recover through e-mails sent to xxxxx@gmail.com. - Your account credentials have been saved in your Let's Encrypt configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Let's Encrypt so making regular backups of this folder is ideal. - If you like Let's Encrypt, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le |
なお、Apache 2.4.8 未満の場合は、 サーバー証明書、中間証明書、秘密鍵 が必要で、Apache 2.4.8 以上の場合や Nginx の場合は、サーバー証明書+中間証明書、秘密鍵が必要になります。
CentOS の Apache はデフォルトで Apache 2.4.6 なので、サーバー証明書、中間証明書、秘密鍵 の指定が必要です。
|
1 2 3 4 5 6 |
# cd /etc/letsencrypt/live/aruse.top/ # ls cert.pem #サーバ証明書 chain.pem #中間証明書 fullchain.pem #サーバ証明書+中間証明書 privkey.pem #サーバ秘密鍵 |
Apache の設定
SSL の設定ファイルを以下のように変更します。
|
1 2 3 4 5 6 7 8 |
# vi /etc/httpd/conf.d/ssl.conf #SSLCertificateFile /etc/pki/tls/certs/localhost.crt #SSLCertificateKeyFile /etc/pki/tls/private/localhost.key SSLCertificateFile /etc/letsencrypt/live/aruse.top/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/aruse.top/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/aruse.top/chain.pem |
Apache の設定を以下のように変更します。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# vi /etc/httpd/conf/httpd.conf NameVirtualHost *:80 NameVirtualHost *:443 <VirtualHost *:80> ServerAdmin root@aruse.top DocumentRoot /var/www/html ServerName aruse.top </VirtualHost> <VirtualHost *:443> SSLEngine on SSLCertificateFile /etc/letsencrypt/live/aruse.top/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/aruse.top/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/aruse.top/chain.pem ServerAdmin root@aruse.top DocumentRoot /var/www/html ServerName aruse.top </VirtualHost> |
Apache の設定を反映させます。Apache の再起動は不要です。
|
1 |
# systemctl reload httpd.service |
Chrome で https://aruse.top にアクセスし証明書を確認すると、正しい証明書になっていることが分かります。
SSL/TLS 証明書の自動更新
SSL/TLS 証明書の自動更新は renew というサブコマンドを使用します。
以下のような具合です。
|
1 |
./letsencrypt-auto renew |
なお、証明書の期限切れが近づくまで証明書は更新されないので、テストで強制的に証明書を最新にしたい場合は、--force-renewal を使用します。
|
1 |
# ./letsencrypt-auto renew --force-renewal |
実際に強制的に最新にしたのが以下のものです。時間が変わっていますね。
さらに踏み込んで、スクリプトで自動更新を行うようにします。
その前に、Let’s Encrypt Client を /usr/local/bin に持ってきましょう。
|
1 2 |
# cd /usr/local/bin # git clone https://github.com/letsencrypt/letsencrypt |
それからスクリプトを /etc/cron.daily/ に作成します。これは更新した証明書の有効期間は30日なのですが、失敗することを考えて毎日行うよう公式が推奨しているためです。
Running letsencrypt renew will renew all certificates that are within the renewal window (by default, certificates within 30 days of expiry)
Once you’re happy with your script, you can run it with cron or systemd. We recommend running renewal scripts at least daily, at a random hour and minute. This gives the script many days to retry renewal in case of transient network failures or server outages.
以下のようにしてファイルを作成します。
|
1 2 |
# cd /etc/cron.daily/ # vi letsencrypt_renew |
スクリプトは以下のようになります。
|
1 2 3 4 5 6 |
if ! ./letsencrypt-auto renew --non-interactive > /var/log/letsencrypt/renew.log 2>&1 ; then exit 1 else systemctl reload httpd.service exit 0 fi |
これを --force-renewal をつけて実行すると、以下のように時間が最新になります。Apache の再起動は行われていません。
おわりに
ようやく、Let’s Encrypt も正式版になり本格的に利用することができるようになりましたね。
WordPress.com も独自ドメインは Let’s Encrypt を使用して HTTPS 化するという話なので、これからどんどん利用範囲が広がっていくことが予想されます。
ブログや個人の Web サービスあたりは Let’s Encrypt で十分ではないかと思います。
注意点としては、コマンドの仕様が固まっていない感じがするところでしょうか。
ですが、使用方法も簡単なので積極的に使っていきたいですね。
参考サイト
- Let’s Encrypt サーバー証明書の取得と自動更新設定メモ | あぱーブログ
- ALL about Linux: CentOS6 の cron.daily 実行時刻を CentOS5 以前と同様に設定する
- /etc/crontabと/etc/cron.d設定ファイルの書き方 | server-memo.net
スポンサーリンク
カテゴリー:ブログ
Twitter でも、いろんな情報を発信しています。@fnyaさんをフォロー