NginxでLet’s Encryptを簡単に使う

SSLとTLS

まず、暗号化に関して知識が不足なので調べてみた。

何気なくSSLって言っていたけど随分前からTLSの時代だった。広義としてSSLやSSL/TLSと言っているが、正確にはTLSと言う必要ある。

そういえば、とあるキャンプで小学生に難しすぎる質問をしていたような….。

あの説明内容の一部がディフィー・ヘルマン鍵共有だったみたい。

TLS化

ブログをTLS化をしようと調べたところ下記ページを見つけた。

letsencryptをUbuntu16.04 + Nginxで使ってみた – Qiita

Nginxを止めたくないと思って、いろいろ調べてみたところNginx用のプラグインが公式にあった。まだ、アルファ版なのでNginxのconfigはバックアップしてから実行するべきらしい。

ユーザーガイド – Let’s Encrypt 総合ポータル

User Guide — Certbot 0.16.0.dev0 documentation

[メモ] certbotとletsencryptとcertbot-auto

初めたばかりの時に、サイトによってコマンドが違っていて困った。動けばいいという発想もあるが気になったので調べてみた。ドキュメントを漁ったところ以下の記述を発見した。

The certbot script on your web server might be named letsencrypt if your system uses an older package, or certbot-auto if you used an alternate installation method. Throughout the docs, whenever you see certbot, swap in the correct name as needed.

User Guide — Certbot 0.16.0.dev0 documentation

簡単に書くなら

  • 古いシステムを使っているなら letsencrypt
  • 別のインストール方法を使用したなら certbot-auto

らしいが、日本語のドキュメントを読んでみると

「Let’s Encrypt クライアント」の名称は、「Certbot クライアント」に変更されました。
それに伴って、letsencrypt-auto が certbot-auto になるなど、ファイル名・ディレクトリ名・コマンド名・URLなども変更されました。

ユーザーガイド – Let’s Encrypt 総合ポータル

つまり、最新版はドキュメントにあるcertbotではなくcertbot-autoになった。

実際、リポジトリの中身もcertbot-autoが存在している。

CertbotのNginxプラグインを導入

まず、configのバックアップをする.

sudo cp -R /etc/nginx/ ~/

次にcertbotをgitやwgetなどでGithubから落としてくる.

certbot/certbot: Certbot, previously the Let’s Encrypt Client, is EFF’s tool to obtain certs from Let’s Encrypt, and (optionally) auto-enable HTTPS on your server. It can also act as a client for any other CA that uses the ACME protocol.

git clone https://github.com/certbot/certbot.git

nginxプラグインを実行する。

sudo ./certbot-auto --nginx

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

どのサイトをTLS化するか聞かれる。

Which names would you like to activate HTTPS for?
-------------------------------------------------------------------------------
1: koyama.me
2: blog.koyama.me
-------------------------------------------------------------------------------
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 2
Obtaining a new certificate
Performing the following challenges:
tls-sni-01 challenge for blog.koyama.me
Waiting for verification...
Cleaning up challenges
Deployed Certificate to VirtualHost /etc/nginx/sites-enabled/blog for set(['blog.koyama.me'])

httpからhttpsへリダイレクトするか聞かれる。

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
The appropriate server block is already redirecting traffic. To enable redirect anyway, uncomment the redirect lines in /etc/nginx/sites-enabled/blog.

-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://blog.koyama.me

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=blog.koyama.me
-------------------------------------------------------------------------------

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/yourPath/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/yourPath/privkey.pem
Your cert will expire on 2017-11-26. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again with the "certonly" option. 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

これだけでサイトがTLS対応される。素晴らしすぎる。

httpをhttpsに飛ばしたいのでnginxの設定ファイルにある以下のコードのコメントアウトを外してnginxにconfigを適用させる。

# Redirect non-https traffic to https
if ($scheme != "https") {
    return 301 https://$host$request_uri;
} # managed by Certbot

Nginxは再起動する前にConfigのチェックをする。

sudo nginx -t

よりセキュアを目指す

これに加えてよりセキュアなサーバにしていく。

Arunekoさんの記事がとても分かりやすかったので参考にさせて頂いた。

NginxでHTTP2を有効にする – Qiita

証明書更新の自動化

これをcronに登録しておけばOK

sudo /yourPath/certbot-auto renew --nignx

返信を残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です