同様の記事があるのは自明なので、完全なる備忘録。
環境は以下。
$ 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
Firewall
ファイアウォールの開き忘れに注意。
sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'
sudo ufw status
以下のように出力されればOK。
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)
Get
初回はたしかメアドとか聞かれたはず。
sudo certbot --nginx -d example.com
sudo certbot --nginx -d example2.com -d xxx.example2.com -d yyy.example2.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;
}
}