はじめに
無償で使用できる SSL/TLS 証明書の Let’s Encrypt は、以前から簡単に SSL/TLS 証明書を取得できたのですが、仕組みがバージョンアップしてさらに簡単になっていましたのでその手順をご紹介します。
以前、正式版リリース時に行った以下の設定方法は今も有効ですのでご心配なく。新しい方法は、環境によって動作しないことがあるようなので以下の方法があるということは知っておいてもよいかもしれません。
今回は、CentOS 7 + Apache に Let’s Encrypt の SSL/TLS 証明書を設定します。
事前準備
SSL/TLS 証明書をインストールする前に、以下の事前設定を行っておきましょう。
まずは以下のパッケージをインストールします。
# yum -y install httpd openssl mod_ssl
そして下記のように、Apache のバーチャールホスト名の設定を行います。この設定が行われていないと、後で SSL/TLS 証明書のインストールができないのでご注意ください。なお、今回のサーバー名は www.aruse.top とします。
# vi /etc/httpd/conf/httpd.conf NameVirtualHost *:80 <VirtualHost *:80> ServerAdmin root@www.aruse.top DocumentRoot /home/c3008847/public_html/websec-room.com ServerName www.aruse.top </VirtualHost>
環境によって、ファイアウォールが通信をブロックしていることがあるので、その場合は通信を許可するようにしてください。設定方法は、以前の記事を参照してください。
SSL/TLS 証明書のインストール
Certbot というサイトにアクセスして、OS と Web サーバーを選択すると、インストール手順が表示されます。
今回は、CentOS 7 + Apache なので、以下のように選択しました。
以下のコマンドで EPEL リポジトリと Certbot のパッケージをインストールします。
# yum -y install epel-release # yum -y install python-certbot-apache
そして以下のコマンドを実行すると、ウィザードが立ち上がります。
# certbot --apache
ウィザードが起動すると、以下のように HTTPS にするサーバー名を尋ねてくるのでチェックを入れたまま<了解>を選択します。
メールアドレスを入力して<了解>を選択します。
利用規約に同意して<Agree>を選択します。
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/www.aruse.top/fullchain.pem. Your cert will expire on 2016-12-30. 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 lose your account credentials, you can recover through e-mails sent to xxxxx@gmail.com. - Your account credentials have been saved in your Certbot 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 Certbot so making regular backups of this folder is ideal.
なんと!たったこれだけのステップで SSL/TLS 証明書のインストールができてしまいました!驚きの進化ぶりですね!
Apache などの設定
Apache 2.4.8 未満の場合は、 サーバー証明書、中間証明書、秘密鍵 が必要で、Apache 2.4.8 以上の場合や Nginx の場合は、サーバー証明書+中間証明書、秘密鍵が必要になります。
CentOS 7の Apache はデフォルトで Apache 2.4.6 なので、サーバー証明書、中間証明書、秘密鍵 の指定が必要です。
以下の場所にインストールされた SSL/TLS 証明書が保存されています。
# cd /etc/letsencrypt/live/www.aruse.top/ # ls cert.pem #サーバ証明書 chain.pem #中間証明書 fullchain.pem #サーバ証明書+中間証明書 privkey.pem #サーバ秘密鍵
ssl.conf の設定を行います。
# 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/www.aruse.top/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/www.aruse.top/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/www.aruse.top/chain.pem
httpd.conf の設定を行います。
# vi /etc/httpd/conf/httpd.conf NameVirtualHost *:80 NameVirtualHost *:443 <VirtualHost *:80> ServerAdmin root@www.aruse.top DocumentRoot /home/c3008847/public_html/websec-room.com ServerName www.aruse.top </VirtualHost> <VirtualHost *:443> SSLEngine on SSLCertificateFile /etc/letsencrypt/live/www.aruse.top/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/www.aruse.top/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/www.aruse.top/chain.pem ServerAdmin root@www.aruse.top DocumentRoot /home/c3008847/public_html/websec-room.com ServerName www.aruse.top </VirtualHost>
Apache を起動して、https://www.aruse.top にアクセスしてみると問題なく設定出来ていることが分かります。
SSL/TLS 証明書の詳細を確認すると、Let’s Encrypt から正しい SSL/TLS 証明書が発行されていることが分かります。
自動更新の設定
Let’s Encrypt の SSL/TLS 証明書の認証期間は90日間しかないので、期限が来る前に自動で更新できるようにする必要があります。
まず、以下のコマンドを実行して自動更新が正しく実行されるか確かめます。
# certbot renew --dry-run ------------------------------------------------------------------------------- Processing /etc/letsencrypt/renewal/www.aruse.top.conf ------------------------------------------------------------------------------- ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates below have not been saved.) Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/www.aruse.top/fullchain.pem (success) ** DRY RUN: simulating 'certbot renew' close to cert expiry ** (The test certificates above have not been saved.)
問題なく実行されたようです。そうしたら、以下のコマンドを cron か systemd のタスクに登録して定期的に実行できるようにします。
# certbot renew --quiet
今回は crontab に、root ユーザーで毎日2回実行するように設定してみます。それは、更新に失敗する可能性があるので、1日に2回実行することが推奨されているためです。
まず、以下のようにユーザーと -e オプションを付けて実行します。
# crontab -u root -e
するとテキストエディタが表示されるので、以下のように設定して保存します。タスク実行の度に毎回メールが飛んでくるのはうっとうしいので、メールの通知を無効にしています。
MAILTO="" 1 4 * * * certbot renew --quiet 1 5 * * * certbot renew --quiet
これで毎日必要があれば SSL/TLS 証明書が更新されるので、問題なく HTTPS のサイトを運用することができます。
おわりに
新しい Let’s Encrypt の SSL/TLS 証明書の設定方法をみてきましたが、いかがだったでしょうか。
Apache などの設定はありますが、SSL/TLS 証明書のインストールがこれだけ簡単になったのは画期的なことだと思います。
常時 HTTPS 化がますます進みますね。
コメント