序章

Let’s Encrypt は、 Transport Layer Security(TLS)暗号化の無料の証明書を提供する認証局(CA)です。 Certbot と呼ばれるソフトウェアクライアントを提供し、証明書の作成、検証、署名、インストール、および更新のプロセスを簡素化します。

Let’s Encryptは、ワイルドカード証明書をサポートするようになりました。これにより、ドメインのすべてのサブドメインを1つの証明書で保護できます。 これは、単一のサーバーを使用してWebインターフェイス、API、その他のサイトなどの複数のサービスをホストする場合に役立ちます。

Let’s Encryptからワイルドカード証明書を取得するには、CertbotのDNSプラグインのいずれかを使用する必要があります。これには次のものが含まれます。

  • certbot-dns-cloudflare
  • certbot-dns-route53
  • certbot-dns-google
  • certbot-dns-digitalocean

選択するプラグインは、DNSレコードをホストするサービスによって異なります。 このチュートリアルでは、CentOS7のCertbotでCloudFlare 検証を使用して、ドメインのワイルドカード証明書を取得します。 次に、有効期限が切れたときに証明書を更新するように証明書を構成します。

前提条件

このチュートリアルを完了するには、次のものが必要です。

ステップ1—Certbotをインストールする

The certbot パッケージは、デフォルトではCentOSのパッケージマネージャーからは利用できません。 Certbotとそのプラグインをインストールするには、EPELリポジトリを有効にする必要があります。

CentOS 7 EPELリポジトリを追加するには、次のコマンドを実行します。

  1. sudo yum install -y epel-release

インストールが完了したら、インストールできます certbot:

  1. sudo yum install -y certbot

次に、Certbot用のCloudFlareプラグインをインストールします。

  1. sudo yum install -y python2-cloudflare python2-certbot-dns-cloudflare

別のDNSサービスを使用している場合は、 yum search 指図:

  1. yum search python2-certbot-dns

サーバーが証明書を取得する準備ができました。 次に、CloudFlareからAPIキーを取得する必要があります。

ステップ2—CloudFlareAPIを取得する

Certbotがワイルドカード証明書を自動的に更新するには、CloudFlareログインとAPIキーをCertbotに提供する必要があります。

Cloudflareアカウントにログインし、プロファイルページに移動します。

グローバルAPIキー行の表示ボタンをクリックします。

セキュリティ上の理由から、Cloudflareアカウントのパスワードを再入力するように求められます。 それを入力し、CAPTCHAを検証します。 次に、表示ボタンをもう一度クリックします。 APIキーが表示されます。

このキーをコピーします。 次のステップで使用します。

次に、サーバーに戻って、証明書を取得するプロセスを続行します。

ステップ3—Certbotの構成

CertbotにCloudflareの使用方法を指示するために必要な情報はすべて揃っていますが、Сertbotが自動的に使用できるように構成ファイルに書き込みましょう。

最初に実行します certbot 初期構成ファイルを作成するためのパラメーターなしのコマンド:

  1. sudo certbot

次に、で構成ファイルを作成します /etc/letsencrypt CloudFlareのメールアドレスとAPIキーを含むディレクトリ:

  1. sudo vi /etc/letsencrypt/cloudflareapi.cfg

以下を追加して、プレースホルダーをCloudflareログインとAPIキーに置き換えます。

/etc/letsencrypt/cloudflareapi.cfg
dns_cloudflare_email = your_cloudflare_login
dns_cloudflare_api_key = your_cloudflare_api_key

ファイルを保存して、エディターを終了します。 CloudflareのAPIキーを使用すると、コマンドラインからCloudflare UIから実行できるのと同じことを実行できるため、アカウントを保護するために、構成ファイルを所有者だけが読み取り可能にして、他の人がキーを取得できないようにします。

  1. sudo chmod 600 /etc/letsencrypt/cloudflareapi.cfg

構成ファイルを配置したら、証明書を取得しましょう。

ステップ4—証明書を取得する

証明書を取得するには、 certbot コマンドを実行して、必要なプラグイン、使用する資格情報ファイル、および要求の処理に使用するサーバーを指定します。 デフォルトでは、Certbotは ACME APIバージョン1を使用するLet’sEncryptの本番サーバーを使用しますが、Certbotはワイルドカード証明書を取得するために別のプロトコルを使用するため、ACMEv2エンドポイントを提供する必要があります。

次のコマンドを実行して、ドメインのワイルドカード証明書を取得します。

  1. sudo certbot certonly --cert-name your_domain --dns-cloudflare --dns-cloudflare-credentials /etc/letsencrypt/cloudflareapi.cfg --server https://acme-v02.api.letsencrypt.org/directory -d "*.your_domain" -d your_domain

緊急の更新およびセキュリティ通知を受け取る必要がある電子メールアドレスを指定するように求められます。

Output
... Plugins selected: Authenticator dns-cloudflare, Installer None Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): your email

次に、利用規約に同意するよう求められます。

Output
------------------------------------------------------------------------------- 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

次に、電子メールアドレスを電子フロンティア財団と共有するように求められます。

Output
------------------------------------------------------------------------------- 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 EFF and our work to encrypt the web, protect its users and defend digital rights. ------------------------------------------------------------------------------- (Y)es/(N)o: N

次に、Certbotが証明書を取得します。 次のメッセージが表示されます。

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

これで、ワイルドカード証明書ができました。 Certbotがダウンロードしたものを見てみましょう。 使用 ls キーと証明書を保持するディレクトリの内容を表示するコマンド:

  1. sudo ls /etc/letsencrypt/live/your_domain
Output
cert.pem chain.pem fullchain.pem privkey.pem README

The README ファイルには、これらのファイルに関する情報が含まれています。

$ cat /etc/letsencrypt/live/your_domain/README

次のような出力が表示されます。

README
This directory contains your keys and certificates.

`privkey.pem`  : the private key for your certificate.
`fullchain.pem`: the certificate file used in most server software.
`chain.pem`    : used for OCSP stapling in Nginx >=1.3.7.
`cert.pem`     : will break many server configurations, and should not be used
                 without reading further documentation (see link below).

We recommend not moving these files. For more information, see the Certbot
User Guide at https://certbot.eff.org/docs/using.html#where-are-my-certificates.

ここから、ワイルドカード証明書を使用してサーバーを構成できます。 通常、必要なファイルは次の2つだけです。 fullchain.pemprivkey.pem.

たとえば、いくつかのWebベースのサービスを構成できます。

  • wwww。example.com
  • api。example.com
  • mail。example.com

これを行うには、ApacheやNginxなどのWebサーバーが必要です。 これらのサーバーのインストールと構成はこのチュートリアルの範囲を超えていますが、次のガイドでは、サーバーを構成して証明書を適用するために必要なすべての手順を説明します。

Nginxについては、次のチュートリアルをご覧ください。

Apacheについては、次のチュートリアルを参照してください。

次に、証明書を自動的に更新する方法を見てみましょう。

ステップ5—証明書を更新する

Let’s Encryptは、90日間有効な短期間の証明書を発行します。 有効期限が切れる証明書を確認して自動的に更新するために、cronタスクを設定する必要があります。

毎日更新チェックを実行するcronタスクを作成しましょう。

次のコマンドを使用して、 crontab 編集用ファイル:

  1. sudo crontab -e

次の行をファイルに追加して、証明書を毎日更新してみてください。

crontab
30 2 * * * certbot renew --noninteractive
  • 30 2 * * * 「毎日午前2時30分に次のコマンドを実行する」という意味です。
  • The certbot renew コマンドは、システムにインストールされているすべての証明書をチェックし、30日以内に期限切れになるように設定されている証明書を更新します。
  • --noninteractive ユーザー入力を待たないようにCertbotに指示します。

証明書を更新した後、Webサーバーをリロードする必要があります。 The renew commandには、証明書が更新される前または後にコマンドまたはスクリプトを実行するためのフックが含まれています。 ドメインの更新構成ファイルでこれらのフックを構成することもできます。

たとえば、Nginxサーバーをリロードするには、更新構成ファイルを開きます。

  1. sudo vi /etc/letsencrypt/renewal/your_domain.conf

次に、下に次の行を追加します [renewalparams] セクション:

/etc/letsencrypt/renewal/ <^>your_domain<^>。conf
renew_hook = systemctl reload nginx

これで、Certbotは、更新された証明書をインストールした後、Webサーバーを自動的に再起動します。

結論

このチュートリアルでは、Certbotクライアントをインストールし、DNS検証を使用してワイルドカード証明書を取得し、自動更新を有効にしました。 これにより、ドメインの複数のサブドメインで単一の証明書を使用し、Webサービスを保護できます。