Certbot

提供: ArchWiki
2018年3月25日 (日) 18:26時点におけるKusakata.bot (トーク | 投稿記録)による版 (Pkg/AUR テンプレートの更新)
ナビゲーションに移動 検索に移動

Let’s Encrypt はフリーかつ自動化されたオープンな認証局です。ACME プロトコルを利用しています。

公式クライアントは Certbot という名前で、コマンドラインから有効な X.509 証明書を取得できます。また、手動で CSR 作成を行うミニマルなクライアントを acme-tiny でインストールすることができます。スクリプトで使用するのに適したクライアントとして simp_le-gitAURletsencrypt-cliAUR も存在します。

インストール

certbot パッケージをインストールしてください。

公式クライアントを用いて発行された証明書はプラグインを使ってウェブサーバーに自動的に設定・インストールできます:

設定

証明を作成・インストールする方法は Certbot のドキュメント を参照してください。

プラグイン

警告: プラグインを使用すると設定ファイルが上書きされます。先に設定ファイルのバックアップを作成することを推奨します。

Nginx

certbot-nginx プラグインは nginxserver ブロックを自動的に設定します:

# certbot --nginx

証明書を更新するには:

# certbot renew

nginx の設定ファイルに手を加えずに証明書を変更するには:

# certbot --nginx certonly

詳しい情報は Nginx on Arch Linux を見てください。インストールされた証明書の状態を有効に保つ方法は#自動更新を見てください。

サーバーブロックの管理

サーバーブロックを手動で管理する場合、以下のようにサーバーブロックで使うことができます:

/etc/nginx/sites-available/example
server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2; # Listen on IPv6
  ssl_certificate /etc/letsencrypt/live/domain/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/domain/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf;
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
  ..
}

詳しくは nginx#TLS/SSL を見てください。

別の設定ファイルを作成してサーバーブロックから include して使用することも可能です:

/etc/nginx/conf/001-cerbot.conf
ssl_certificate /etc/letsencrypt/live/domain/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/domain/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
/etc/nginx/sites-available/example
server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2; # Listen on IPv6
  include conf/001-certbot.conf;
  ..
}

Webroot

ノート:
  • Webroot 方式では Certbot で認証するためにポート 80 で HTTP 接続できるようにする必要があります。
  • サーバーの名前は DNS と一致している必要があります。
  • http://domain.tld/.well-known の読み取りを許可するためにホストのパーミッションを修正する必要がある場合があります。

webroot 方式を使う場合、Certbot クライアントは /path/to/domain.tld/html/.well-known/acme-challenge/ の中でチャレンジ・レスポンス認証を行います。

手動でインストールする場合と比べて、自動で証明書を更新したり楽に管理することができます。ただし、#プラグインを使う時のように自動設定・自動インストールはできません。

ACME チャレンジリクエストのマッピング

.well-known/acme-challenge に対する HTTP リクエストを全てひとつのディレクトリ (例: /var/lib/letsencrypt) にマッピングすることで管理が簡単になります。

Certbot やウェブサーバー (http ユーザーで動作する nginxApache など) が書き込みできるパスを使用してください:

# mkdir -p /var/lib/letsencrypt/.well-known
# chgrp http /var/lib/letsencrypt
# chmod g+s /var/lib/letsencrypt
nginx

location ブロックを記述したファイルを作成してサーバーブロックから読み込むようにしてください:

/etc/nginx/conf.d/letsencrypt.conf
location ^~ /.well-known/acme-challenge/ {
  allow all;
  root /var/lib/letsencrypt/;
  default_type "text/plain";
  try_files $uri =404;
}

サーバー設定の例:

/etc/nginx/servers-available/domain.conf
server {
  server_name domain.tld
   ..
  include conf.d/letsencrypt.conf;
}
Apache

/etc/httpd/conf/extra/httpd-acme.conf ファイルを作成してください:

/etc/httpd/conf/extra/httpd-acme.conf
Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/"
<Directory "/var/lib/letsencrypt/">
    AllowOverride None
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Require method GET POST OPTIONS
</Directory>

/etc/httpd/conf/httpd.conf で上記ファイルを読み込んでください:

/etc/httpd/conf/httpd.conf
Include conf/extra/httpd-acme.conf

証明書の取得

公開パスとして /var/lib/letsencrypt/ を使用して domain.tld の証明書を取得するには:

# certbot certonly --email email@example.com --webroot -w /var/lib/letsencrypt/ -d domain.tld

(サブ)ドメインを追加するときは、登録済みの全てのドメインを一緒に指定します:

# certbot certonly --email email@example.com --webroot -w /var/lib/letsencrypt/ -d domain.tld,sub.domain.tld

登録している証明書を(全て)更新するには:

# certbot renew

証明書の更新方法については#自動更新も参照。

手動

ウェブサーバーのプラグインが存在しない場合、以下のコマンドを使ってください:

# certbot certonly --manual

DNS の TXT レコードを利用して認証したい場合、以下のコマンドを使ってください:

# certbot certonly --manual --preferred-challenges dns

上記のコマンドで自動的にドメインが認証されて秘密鍵と証明書のペアが作成されます。秘密鍵と証明書は /etc/letsencrypt/archive/your.domain/ に保存され、/etc/letsencrypt/live/your.domain/ からリンクが張られます。

証明書が取得できたら手動でウェブサーバーを設定して、シンボリックリンクが存在するディレクトリの秘密鍵・証明書・証明書チェーンを参照してください。

ノート: 上のコマンドを複数回実行したり証明書を更新したりするとファイル名の末尾に数字が付加されたファイルが /etc/letsencrypt/archive/your.domain/ に作られます。Certbot は /etc/letsencrypt/live/your.domain/ のシンボリックリンクを自動的に最新のファイルに更新するため、証明書を更新するたびに手動でウェブサーバーの設定を更新する必要はありません。

高度な設定

自動更新

systemd

certbot.service ユニットを作成:

/etc/systemd/system/certbot.service
[Unit]
Description=Let's Encrypt renewal

[Service]
Type=oneshot
ExecStart=/usr/bin/certbot renew --quiet --agree-tos

プラグインを使わずにウェブサーバーの設定を自動で管理する場合、ウェブサーバーを手動でリロードして証明書が更新されるたびにリロードが必要です。ExecStart コマンドに --deploy-hook "systemctl reload nginx.service" を追加することでリロードさせることができます [1]。適宜 nginx.service のかわりに httpd.service を使ってください。

ノート: タイマーを追加する前に、サービスが正しく動作すること、何も入力が要求されないことを確認してください。

それから、タイマーを追加することで証明書を更新できます (更新の必要がない証明書は自動的にスキップされます)。

/etc/systemd/system/certbot.timer
[Unit]
Description=Daily renewal of Let's Encrypt's certificates

[Timer]
OnCalendar=0/12:00:00
RandomizedDelaySec=1h
Persistent=true

[Install]
WantedBy=timers.target

certbot.timer起動有効化してください。

参照