最終更新日:2016年10月1日
無料SSL/TLS証明書のLet's Encryptが正式版になったので自動更新を含めて試してみた
目次
はじめに
2016年10月1日追記:
Let's Encrypt の実行方法が変更になったので、以下の記事を参考にするようにしてください。なお、この記事の方法も継続して実行できるので、既に設定された方は変更する必要はありません。
追記ここまで。
Let’s Encrypt とは、無償で SSL/TLS 証明書を発行してくれるサービスです。Web 全体を HTTPS化することを目指してサービスを行っており、非常に注目されています。
この Let's Encript は今までパブリックベータとして公開されていたのですが、この度、正式版としてリリースされました。
詳しいことは、以下のパブリックベータ時の記事を参照してください。若干、コマンドが変わっているところがありますが、それは後述します。
- 無料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 の懸念点であった有効期間の短さが解消されますね。
なお、自動更新のスクリプトも数週間後に公開する予定だそうです。この記事では、サンプルスクリプトがあったので、それを元に変更して利用しています。
2016年4月14日追記:
CentOS 6 に Let's Encrypt をインストールしようとしましたが、うまくいきませんでした。解決方法については、後述の追記に記載します。
事前準備
事前に以下のパッケージをインストールしておきます。
# yum -y install httpd openssl mod_ssl # yum -y install git
デフォルトで、CentOS 7 は HTTP(80)/HTTPS(443) ポートを塞いでいるので、ファイアウォールのポートを開放します。
# firewall-cmd --zone=public --add-port=80/tcp --permanent # firewall-cmd --zone=public --add-port=443/tcp --permanent # firewall-cmd --reload
Apache を起動します。
# systemctl start httpd.service
Let’s Encrypt Client のインストール
Let’s Encrypt Client のインストールは簡単で、以下のように行います。最後のコマンドを実行すると、必要なツールが自動でインストールされます。それが嫌な場合は、Let’s Encrypt の採用は見送った方がよいかもしれません。
# git clone https://github.com/letsencrypt/letsencrypt # cd letsencrypt # ./letsencrypt-auto --help
インストールが成功すると、以下のようなガイドが表示されます。ここをよく見れば、いろいろと分かるのではないでしょうか。
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 、公開ディレクトリを /home/c3008847/public_html/websec-room.com として以下のコマンドを実行します。
# ./letsencrypt-auto certonly --webroot -w /home/c3008847/public_html/websec-room.com -d aruse.top
インストールの途中でメールアドレスを聞かれるので、メールアドレスを入力して [了解] を押します。
利用規約に同意する場合は <Agree> を押します。
インストールが終了すると、以下のメッセージが表示されます。/etc/letsencrypt/live/aruse.top/fullchain.pem にサーバー証明書+中間証明書が作成されたことが分かります。同一ディレクトリに、サーバー証明書、中間証明書、秘密鍵も作成されています。
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 なので、サーバー証明書、中間証明書、秘密鍵 の指定が必要です。
# cd /etc/letsencrypt/live/aruse.top/ # ls cert.pem #サーバ証明書 chain.pem #中間証明書 fullchain.pem #サーバ証明書+中間証明書 privkey.pem #サーバ秘密鍵
Apache の設定
SSL の設定ファイルを以下のように変更します。
# 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 の設定を以下のように変更します。
# vi /etc/httpd/conf/httpd.conf NameVirtualHost *:80 NameVirtualHost *:443 <VirtualHost *:80> ServerAdmin root@aruse.top DocumentRoot /home/c3008847/public_html/websec-room.com 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 /home/c3008847/public_html/websec-room.com ServerName aruse.top </VirtualHost>
Apache の設定を反映させます。Apache の再起動は不要です。
# systemctl reload httpd.service
Chrome で https://aruse.top にアクセスし証明書を確認すると、正しい証明書になっていることが分かります。
SSL/TLS 証明書の自動更新
SSL/TLS 証明書の自動更新は renew というサブコマンドを使用します。
以下のような具合です。
./letsencrypt-auto renew
なお、証明書の期限切れが近づくまで証明書は更新されないので、テストで強制的に証明書を最新にしたい場合は、--force-renewal を使用します。
# ./letsencrypt-auto renew --force-renewal
実際に強制的に最新にしたのが以下のものです。時間が変わっていますね。
さらに踏み込んで、スクリプトで自動更新を行うようにします。
その前に、Let’s Encrypt Client を /usr/local/bin に持ってきましょう。
# 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.
以下のようにしてファイルを作成します。
# cd /etc/cron.daily/ # vi letsencrypt_renew
スクリプトは以下のようになります。
#!/bin/bash PATH=$PATH:/usr/local/bin/letsencrypt export PATH letsencrypt-auto renew systemctl reload httpd.service
スクリプトを保存したら、スクリプトを実行できるようパーミッションを変更します。
# chmod 755 letsencript_renew
このスクリプトのコマンドに --force-renewal をつけて実行すると、以下のように時間が最新になります。Apache の再起動は行われていません。
# ./letsencript_renew

追記:CentOS 6 でインストールが失敗する対策方法
CentOS 6 だと、python のバージョンが 2.6 のため Let's Encrypt のインストールがうまくいきません。インストールのメッセージに以下のエラーが含まれます。
No package python-virtualenv available. No package python-pip available.
この問題を解消するには、python 2.7 をインストールする必要があります。インストールは以下のように行います。私の環境では、yum update を行わないでもうまくいきました。
# SCLをインストール $ sudo yum install centos-release-SCL # python2.7 と scl utilsをインストール $ sudo yum update $ sudo yum install scl-utils python27 python27-scldevel # python2.7をbash上で有効化 $ scl enable python27 bash 引用: http://blog.doizaki.com/entry/2016/02/07/152141
また、/etc/cron.daily/letsencrypt_renew を以下のように変更します。
#!/bin/bash PATH=$PATH:/usr/local/bin/letsencrypt:/opt/rh export PATH scl enable python27 'python -V' letsencrypt-auto renew service httpd reload
おわりに
ようやく、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
スポンサーリンク
カテゴリー:ブログ
コメントを残す