Let's Encrypt + nginx + Ubuntu

Posted on 2017/06/05 in tech

めも。殴り書き。

Let's Encrypt

https://letsencrypt.org/

環境

  • Ubuntu 16.04 LTS
  • nginx 1.10.0

一応確認

  • サーバーに対してドメインが振られDNSでの名前解決ができている
  • 80番ポートにアクセスできる

certbotの導入

$ cd /usr/local/bin
$ sudo git clone https://github.com/certbot/certbot
$ cd certbot
$ sudo ./certbot-auto certonly --standalone -t
  • 依存するライブラリを自動でインストールするか途中訊かれるので、yと入力。
  • メールアドレスの入力
  • (A)gree

あとはよくわからないこと聞かれる

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.

とりあえずyにした

次に認証させたいドメインの入力

Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel):example.com

cronでの自動更新設定

Let's Encrypt の証明書の有効期限は3ヶ月のみなので、自動更新できるようにする

下記をnginxの設定ファイルに追記

server {
... 
    location /.well-known/acme-challenge/ { allow all; }
    location / { return 301 https://$host$request_uri; }
...

下記コマンドで証明書の更新ができるか確認

$ sudo ./certbot-auto renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/example.com.conf
-------------------------------------------------------------------------------
Cert not yet due for renewal

The following certs are not due for renewal yet:
  /etc/letsencrypt/live/example.com/fullchain.pem (skipped)
No renewals were attempted.

認証鍵入れたての場合、上記のように更新はスキップされる(有効期限が一ヶ月未満だと更新されるよう)

この表示が確認できたら、cronに証明書を更新してnginxの設定を再読込する設定を書き込む

$ sudo crontab -e
# 毎月1日の午前3時に更新
0 3 1 * * /usr/local/bin/certbot/certbot-auto renew && /bin/systemctl reload nginx

nginxの設定

以下最低限

server {
    listen 443 ssl;
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
}

nginx再起動

$ sudo service nginx restart