※弊社記事はたぶんにPRが含まれますので
話半分で受け取ってください。

無料でSSL/TLSが始められる認証局「Let’s Encrypt」を導入してみた

MacBookのキーボード

 今までサーバ証明書にはStartSSLを使っていたのですが、なんだかんだあって2016/10/21以降にStartSSLで発行された証明書が使えなくなったので、前から気になってた「Let’s Encrypt」を導入してみることにしました。なんだかんだは、なんだかんだです!

 「Let’s Encrypt」はSSL/TLSを利用する際に必要になるサーバ証明書を無料で発行してくれる認証局です。ただ無料で利用できるというだけではなく、専用ソフトを使うことで証明書の更新も自動行ってくれます。すごい!

導入方法

 Let’s Encryptの導入は以下のページにまとめられてるものそのままでいけました。

 そんなわけで、ここには個人的なメモを残しておきたいとおもいます。

backportsを設定する

 うちはDebian 8 Jessieを使ってるので、sources.listのリポジトリにbackportsを追加します。

$ sudo vim /etc/apt/sources.list

でsources.listを開いて、ファイルの適当な場所(だいたいは最後のほうとか)に以下の文句を追加します。

#backports
deb http://ftp.debian.org/debian jessie-backports main

 保存したらupdateを実行しておきます。

$ sudo apt-get update

certbotをインストール

 certbot(Let’s Encryptのクライアント)をインストールします。

$ sudo apt-get install certbot python-certbot-apache -t jessie-backports

 Apacheを使ってない場合は「python-certbot-apache」が省略できます。うちはNginx使ってるので省略しました。

テスト実行

 certbotがインストールされてるか確かめるため、コマンドを実行してみます。(まあでも、インストールされてることがわかってれば別にこの項目は飛ばしてもいいんじゃないかという気もします。)

$ certbot

 特に問題がない場合は、TUIが開いて

No names were found in your configuration files.
You should specify ServerNames in your config files in order to allow for accurate installation of your certificate.
If you do use the default vhost, you may specify the name manually.
Would you like to continue?

という文句が表示されるらしいんですが、うちの場合、TUIには

Saving debug log to /var/log/letsencrypt/letsencrypt.log

 と表示されるだけで、その後、CUIに

Certbot doesn't know how to automatically configure the web server on this system. However, it can still get a certificate for you. Please run "certbot certonly" to do so. You'll need to manually configure your web server to use the resulting certificate.

 という文句が出現。「『certbot certonly』を実行してね」ってことのようなので、次のステップへ。

サーバ証明書の取得

 サーバ証明書を取得するため、certbotを起動します。コマンド中の「example.jp」は、証明書を取得しようとするサーバのドメイン名に置き換えてください。

$ certbot certonly --standalone -d example.jp -d www.example.jp

 TUIが開き、メールアドレスの入力(Enter email address)、利用規約への同意(Please read the Terms of Service…)と続きます。

ウェブサーバの停止

 ここでウェブサーバ(ApacheとかNginxとか)が起動した状態で作業をしてると以下のようなエラーが出てきます。

The program nginx (process ID 22726) is already listening on TCP port 80. This will prevent us from binding to that port. Please stop the httpd program temporarily and then try again.

 この場合は、一度ウェブサーバを終了させ、再度、「certbot certonly …」のコマンドを実行してください。
 サーバ証明書取得の作業は10分もあれば終わるので、10分程度のダウンタイムが許せるならサーバを落としてから作業を始める方がスムーズかもです。

 ちなみにNginxの場合は「nginx -s stop」で終了、「nginx」で起動。「nginx -s reload」で再起動だぞ。しばらく使ってないとどうやるんだったか忘れちゃうぞ。

証明書の取得完了

 証明書の取得が完了すると以下のようなメッセージが表示されます。

Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/example.jp/fullchain.pem. Your cert will
   expire on 2017-02-08. To obtain a new or tweaked version of this
   certificate in the future, simply run certbot again. 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

 メッセージ中の「example.jp」は入力したドメイン名、「2017-02-08」の部分は取得した証明書の有効期限が入ります。

証明書の場所

 証明書の場所は以下の通りです。ウェブサーバの設定にこれらの場所を指定してあげて、ウェブサーバを再起動してください。

  • サーバ証明書(公開鍵)
    /etc/letsencrypt/live/ドメイン名/cert.pem
  • 中間証明書
    /etc/letsencrypt/live/ドメイン名/chain.pem
  • サーバ証明書と中間証明書が結合されたファイル
    /etc/letsencrypt/live/ドメイン名/fullchain.pem
  • 秘密鍵
    /etc/letsencrypt/live/ドメイン名/privkey.pem

 実際にはこれらはシンボリックリンクで、本体は「/etc/letsencrypt/archive/ドメイン名」の中に保存されるのですが、まあ、ただ使う分にはシンボリックリンクの場所だけわかってれば無問題。

 Nginxの場合は、「ssl_certificate」に「fullchain.pem」、「ssl_certificate_key」に「privkey.pem」を設定すればおk。

server {
    listen 443 default ssl;
    ssl on;
    ssl_certificate      /etc/letsencrypt/live/ドメイン名/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/ドメイン名/privkey.pem;
    (以下略)
}

 以上であります! やってみれば意外と簡単。しかも、1回設定してしまえばあとは自動で証明書の更新してくれます! 安い! 簡単! すごい! backportsとか設定しなくても使えるようになったらもっといいと思いますが、それはもうちょっと先になりそうですね。

参考

薄桜鬼SSL ~sweet school life~ (限定版) – PS Vita

  • メーカー:アイディアファクトリー
  • カテゴリ:Video Game
  • 発売日:2014/03/27

関連する記事