CentOS 7でCloudFlare検証を使用してSSLワイルドカード証明書を暗号化して取得しましょう

著者は、Write for DOnationsプログラムの一部として寄付を受け取るためにCode.orgを選択しました。

前書き

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

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

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

  • certbot-dns-cloudflare

  • certbot-dns-route53

  • certbot-dns-google

  • certbot-dns-digitalocean

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

前提条件

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

  • sudo非rootユーザーとファイアウォールを含むthe CentOS 7 initial server setup guideに従うことによってセットアップされた1つのCentOS7サーバー。

  • 完全に登録されたドメイン名。 Namecheapでドメイン名を購入するか、Freenomで無料でドメイン名を取得するか、選択したドメインレジストラを使用できます。

  • Cloudflareアカウント。

  • CloudflareのDNSでドメイン用に設定されたDNSレコードと、構成されたいくつかのサブドメイン。 CloudFlare’s tutorial on setting up a web siteに従ってこれを構成できます。

[[step-1 -—- installing-certbot]] ==ステップ1—Certbotのインストール

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

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

sudo yum install -y epel-release

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

sudo yum install -y certbot

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

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

[。注意]##

別のDNSサービスを使用している場合は、yum searchコマンドを使用して対応するプラグインを見つけることができます。

yum search python2-certbot-dns

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

[[step-2 -—- getting-the-cloudflare-api]] ==ステップ2— CloudFlareAPIを取得する

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

Cloudflareアカウントにログインし、Profile pageに移動します。

Global API Key行のViewボタンをクリックします。

CloudFlare Profile - API Keys

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

CloudFlare Profile - API Keys

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

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

[[step-3 -—- configuring-certbot]] ==ステップ3—Certbotの構成

CertbotにCloudflareの使用方法を伝えるために必要な情報はすべて揃っていますが、それを設定ファイルに書き込んで、Сertbotが自動的に使用できるようにします。

最初に、パラメーターを指定せずにcertbotコマンドを実行して、初期構成ファイルを作成します。

sudo certbot

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

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から実行できるのと同じことをコマンドラインから実行できます。したがって、アカウントを保護するために、構成ファイルを読み取り可能にします。その所有者によってのみ、他の誰もあなたの鍵を取得できないようにします。

sudo chmod 600 /etc/letsencrypt/cloudflareapi.cfg

設定ファイルを用意したら、証明書を取得しましょう。

[[step-4 -—-証明書の取得]] ==ステップ4—証明書の取得

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

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

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

次に、メールアドレスをElectronic Frontier
Foundationと共有するように求められます。

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は証明書を取得します。 次のメッセージが表示されます。

OutputIMPORTANT 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コマンドを使用して、キーと証明書を保持するディレクトリの内容を確認します。

sudo ls /etc/letsencrypt/live/your_domain
Outputcert.pem  chain.pem  fullchain.pem  privkey.pem  README

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.

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

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

  • wwww。example.com

  • api。example.com

  • メール。example.com

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

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

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

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

[[step-5 -—- renewing-certificates]] ==ステップ5—証明書の更新

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

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

次のコマンドを使用して、編集用にcrontabファイルを開きます。

sudo crontab -e

ファイルに次の行を追加して、証明書を毎日更新しようとします。

crontab

30 2 * * * certbot renew --noninteractive
  • 30 2 * * *は、「毎日午前2時30分に次のコマンドを実行する」ことを意味します。

  • certbot renewコマンドは、システムにインストールされているすべての証明書をチェックし、30日以内に期限切れになるように設定されている証明書を更新します。

  • --noninteractiveは、ユーザー入力を待たないようにCertbotに指示します。

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

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

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

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

your_domain.conf’>/etc/letsencrypt/renewal/your_domain.conf

renew_hook = systemctl reload nginx

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

結論

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

Related