# インストール手順
## HTTP-01, DNS-01 共通
1. 公式ページのインストール手順通り ppa を追加し、`certbot` を apt でインストールする
    - https://certbot.eff.org/lets-encrypt/ubuntuxenial-apache
    - サーバのディストリビューションに合った手順が用意されているはずなので、それを見ること

## HTTP-01 を利用する場合

- 以下の 2 つをやっておく
    - [[../certbot インストール検証#Domain Validation fileを設置するディレクトリを作成]]
    - [[../certbot インストール検証#Domain Validation fileにCAからアクセスできるようにするための設定]]

## DNS-01 を利用する場合

1. ワイルドカード証明書を発行する場合は、追加で `python3-certbot-dns-route53` をインストールする
    - 2019/02/10 現在、apt でインストールした certbot-dns-route53 は必ずコケるので、以下のように pip 経由でインストールする
        ```bash
        # apt でインストールしてしまった場合、それを削除する
        $ sudo apt purge python3-certbot-dns-route53
        $ sudo apt autoremove --purge
        # pip と certbot_dns_route53 をインストールする
        $ sudo apt install python3-pip
        $ sudo -H pip3 install certbot_dns_route53
        ```
        - see: https://github.com/certbot/certbot/issues/5234#issuecomment-455888297
2. certbot-dns-route53 のドキュメントを見ながら AWS アカウントを作成し、アクセスキー・シークレットを取得する
    - https://certbot-dns-route53.readthedocs.io/en/stable/
    - IAM 上のユーザ名は `<ホスト名>-dns01-challenge` にする
    - ポリシーの設定は、既存の `saber-dns01-challenge` からコピーして利用するのがよい
3. `/root/.aws/config` を作成し、`chmod go-rwx -R /root/.aws` する
    - certbot コマンド類は root で実行されるため

# インストール後の各種構造
## ディレクトリ構造

`/etc/letsencrypt` にすべて収まる。  
既に発行された証明書、証明書に関する再発行時の設定ファイルなどが入っている。

## 自動更新
- `/etc/cron.d/certbot`
    - どのディストリビューションにインストールしても存在するファイル
    - が、中身はディストリビューションごとに違うっぽい
        - ex.) インストール対象が Ubuntu 14.04 の場合(systemd がそもそも用意されてない)
            ```cron:/etc/cron.d/certbot
            0 */12 * * * root test -x /usr/bin/certbot && perl -e 'sleep int(rand(3600))' && certbot -q renew
            ```
        - ex.) インストール対象が Ubuntu 16.04 の場合(systemd が実行されている可能性がある)
            ```cron:/etc/cron.d/certbot
            0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew
            ```
    - systemd がインストールされているディストリビューションでは、systemd が実行されている場合は systemd の timer に任せるような構造になっている
        - `/lib/systemd/system/certbot.timer`
        - `/lib/systemd/system/certbot.service`
- 証明書が更新されたときに自動的に実行されるコマンドの指定方法
    1. 証明書発行時に `--post-hook` 引数で指定する
    2. 証明書設定ファイル内のパラメータで指定する
        ```toml:/etc/letsencrypt/renewal/[ドメイン名].conf
        [renewalparams]
        post_hook = service apache2 reload
        ```

# 発行方法
## HTTP-01
1. 発行するドメイン名に対して http でアクセスすると、これから certbot を実行するホストへアクセスされるように A or CNAME を設定する
2. 以下のコマンドを叩く
    ```bash
    $ sudo certbot certonly --webroot -w /var/www/acme-domain-validation/ -d [発行するドメイン名] --post-hook 'service apache2 reload'
    ```
    - SAN 証明書を発行したい場合は、`-d` オプションを複数指定する
    - `--post-hook` オプションは証明書を設定するデーモンに応じて、適宜書き換える
        - デーモンが証明書を再度読み込みなおせるようなコマンドをセットする

## DNS-01(ワイルドカード証明書)

1. 以下のコマンドを叩く
    ```bash
    $ sudo -H certbot certonly --dns-route53 --server https://acme-v02.api.letsencrypt.org/directory -d [発行するドメイン名] --post-hook 'service apache2 reload'
    ```
    - ワイルドカード証明書の場合は、 `-d '*.hoge.weseek.co.jp'` のように指定する
    - `--post-hook` オプションは証明書を設定するデーモンに応じて、適宜書き換える
    - `sudo` に `-H` をつける理由
        - `$HOME` が切り替わらないので Route53 のエントリを操作するための credentials が読めない

# 発行した証明書の利用方法
## Apache

- VirtualHost 配下に以下のように指定する
    ```
    SSLCertificateFile /etc/letsencrypt/live/[発行したドメイン名]/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/[発行したドメイン名]/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/[発行したドメイン名]/chain.pem
    ```
    または
    ```
    SSLCertificateFile /etc/letsencrypt/live/[発行したドメイン名]/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/[発行したドメイン名]/privkey.pem
    ```

## nginx

TBD

# 発行した証明書一覧の取得方法

```
$ sudo certbot certificates
```
- certbot コマンドを利用して発行した証明書の一覧、格納されている場所が出力される
- 出力例
    ```
    $ sudo certbot certificates
    Saving debug log to /var/log/letsencrypt/letsencrypt.log

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Found the following certs:
      Certificate Name: growi.org
        Domains: growi.org
        Expiry Date: 2019-05-04 02:38:12+00:00 (VALID: 83 days)
        Certificate Path: /etc/letsencrypt/live/growi.org/fullchain.pem
        Private Key Path: /etc/letsencrypt/live/growi.org/privkey.pem
      Certificate Name: weseek.co.jp
        Domains: *.weseek.co.jp weseek.co.jp
        Expiry Date: 2019-05-05 15:04:16+00:00 (VALID: 85 days)
        Certificate Path: /etc/letsencrypt/live/weseek.co.jp/fullchain.pem
        Private Key Path: /etc/letsencrypt/live/weseek.co.jp/privkey.pem
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ```


---

## Page Navigation

- Canonical URL: https://tips.weseek.co.jp/Tips/Let's%20Encrypt/certbot%20インストール手順・基本構造
- Permalink: https://tips.weseek.co.jp/5fc0ad108b05ce0048ff5a01
- Parent: [Let's Encrypt](/65069da24603e7ff39a6b3d8.md)
- Children: 0 total
- Total descendants: 0
- Siblings: 4 total
  - [RateLimits](/5fc0ad4f8b05ce0048ff5a0b.md)
  - [SecurityConsiderations](/5fc0aa258b05ce0048ff5970.md)
  - [certbot インストール検証](/5fc0acd08b05ce0048ff59ef.md)
  - [証明書の無効化・削除](/5fc0adad8b05ce0048ff5a1f.md)
- Last updated: 2020-11-27T07:38:56.368Z by syunsuke
- Full page listing (all children regardless of count): https://tips.weseek.co.jp/_api/v3/page-listing/children?id=5fc0ad108b05ce0048ff5a01
