Ubuntu19.04のnginxでcertbot


- Ubuntu19.04
- Nginx


[https://certbot.eff.org/](https://certbot.eff.org/)で「Nginx on Ubuntu18.04 LTS」を参照しました

```sh
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt update
```

3行目は`'universe' distribution component is already enabled for all sources.`となり、不要でした

また4行目では

> Hit:1 http://security.ubuntu.com/ubuntu eoan-security InRelease
Ign:2 http://ppa.launchpad.net/certbot/certbot/ubuntu eoan InRelease
Hit:3 http://jp.archive.ubuntu.com/ubuntu eoan InRelease      
Hit:4 http://jp.archive.ubuntu.com/ubuntu eoan-updates InRelease         
Hit:5 http://jp.archive.ubuntu.com/ubuntu eoan-backports InRelease       
Err:6 http://ppa.launchpad.net/certbot/certbot/ubuntu eoan Release       
  404  Not Found [IP: 91.189.95.83 80]
Reading package lists... Done
E: The repository 'http://ppa.launchpad.net/certbot/certbot/ubuntu eoan Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

というエラーが出ますが、強引に進めます

```sh
sudo apt-get install certbot python-certbot-nginx
```

認証だけしてnginxの設定は自分で書きます

```sh
sudo certbot certonly --nginx
```

表示される説明にしたがってメールアドレスやドメインを入力します

```
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): admin@example.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
No names were found in your configuration files. Please enter in your domain
name(s) (comma and/or space separated)  (Enter 'c' to cancel): example.com, www.example.com
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for example.com
http-01 challenge for www.example.com
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on ****-**-**. 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"
 - 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.
 - 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

```

上記のなかで`/etc/letsencrypt/live/example.com/fullchain.pem`と`/etc/letsencrypt/live/example.com/privkey.pem`は忘れないようにしてください

## 自動更新

```sh
sudo certbot renew --dry-run
```

## nginxの設定

```nginx:/etc/nginx/sites-available/default
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    location ^~ /.well-known/acme-challenge/ {
        default_type "text/plain";
    }

    location / {
        return 301 https://$server_name$request_uri;
        try_files $uri $uri/ =404;
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com www.example.com; 

    ssl on;
    ssl_certificate      /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/example.com/privkey.pem;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }
}
```

設定を読み込みます

```sh
sudo systemctl reload nginx
```

![Screenshot from 2019-10-28 22-51-28.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/79562/66ca8723-a0fa-b3a9-aafd-5eb9dc92f0b1.png)