Let's encryptでSSL証明書の取得(ubuntu20.04+nginx+certbot)


同様の記事があるのは自明なので、完全なる備忘録。

環境は以下。

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.1 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

$ nginx -v
nginx version: nginx/1.18.0 (Ubuntu)

ざっくり確認できるように、解説はだいぶ略します。

Install

sudo apt install certbot python3-certbot-nginx

Get

初回はたしかメアドとか聞かれたはず。

sudo certbot --nginx -d example.com

Config

nginxのConfig。私は /etc/nginx/conf.d にあった。

適当にいじりつつ、落ち着いたところ。managed by Certbot がついてるやつはCertbotが勝手に書いてくれたやつを流用してます。
httpにアクセスされたらhttpsにリダイレクトするおまけつきです。

私はGoでアプリケーションサーバを立てて、リバースプロキシとして動かしてるので、locationはそういう仕様です。

default.conf
server {
    listen 80;
    server_name  example.com;
    return 301 https://$host$request_uri;
}
server{
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location / {
        proxy_pass http://127.0.0.1:xxxx;
    }
}