LetsEncrypt

CentOS6にLet's EncryptでSSL証明書を入れる方法

Let's Encryptは無料のSSL証明書発行サービス。これを使えば無料でWebサイトをSSL化できる。
CentOS6.9にLet's EncryptでSSL証明書を発行してインストールする方法をまとめてみる。

リンク

公式

参考

Nginxに対応

  • [CentOS6][Apache]Let's Encrypt CertBot?でSSL対応 | HappyQuality? https://happyquality.com/2018/02/19/3571.htm

    CertBot?のインストール

    sudo yum install epel-release
    wget https://dl.eff.org/certbot-auto
    chmod a+x certbot-auto

    証明書の更新をcrontabで設定する時にnginxの再起動のためにroot権限で実行する必要があるので/usr/localなどに設置したほうが良いかもしれません。

→nginxを再起動しないでも更新できる、というような情報もあり。

  • NginxでLet’s Encryptを使うためのメモ書き | work.log https://worklog.be/archives/3352

    CentOS6 + Python 2.6 の環境だと certbot-auto を動かす度に、2.6 はサポートを廃止する予定だよと警告が出ますがツール自体は問題なく動作します。(気持ち悪い場合は Python のアップデートを)

    NginxでLet’s Encryptを使う準備
    certbot-auto は Web サーバを無停止で証明書を取得する Webroot プラグイン を利用するので、Nginx に Let’s Encrypt 用の設定を追加します。

→Webrootというプラグインを使えば、サーバーを無停止(再起動は不要?)で、証明書を更新できるのかな?

Apacheに対応

ACMEとは?

  • Let’s Encryptが2018年からワイルドカードに対応ACME v2とは | パソコンたすかるHowTo https://pc-taskal.net/howto/service/lets-encrypt-wildcard-domain-acme-v2

    現在使われているACME v1 は Let’s Encrypt の関連団体が作ったプロトコルのAPIです。(certbot)
    利用者が簡単に証明書を発行してもらってサーバで利用できるように、設定アプリから検証して認証して発行してを自動化出来る仕組みです。
    発行されたら、そのまま設定アプリが自動でサーバの設定までワンストップでしてくれます。
    その新しい仕組みのバーションがACME v2で、インターネットでの相互の接続の世界標準化IETF(Internet Engineering Task Force)に対応したものになり、Let’s Encrypt以外の証明書発行機関も利用できるように、その仕組みが開発されています。

CentOS6でPythonを2.6から2.7にバージョンアップする方法

Python2.7ではなく、Python3にしている事例もあった。

Let's Encryptの導入 作業メモ

CentOS6でインストール

  • Certbot クライアントのインストール - Let's Encrypt 総合ポータル https://letsencrypt.jp/usage/install-certbot.html#CentOS6

    Certbot クライアントが必要とする依存関係にあるパッケージの一部が EPEL (Extra Packages for Enterprise Linux) リポジトリに含まれているため、Certbot クライアントをインストールする前に有効にする必要があります。
    入力するコマンド:

    sudo yum install epel-release

    EPEL リポジトリを有効にした後は、その他の UNIX 系 OS の手順 に従って certbot-auto をインストールしてください。

Certbot クライアント(certbot-auto)をダウンロード・インストール

  • Certbot クライアントのインストール - Let's Encrypt 総合ポータル https://letsencrypt.jp/usage/install-certbot.html#OtherUNIX

    使用している OS のパッケージ管理システムに Certbot パッケージが存在しない場合には、下記の方法で、電子フロンティア財団(EFF)の公式サイトから Certbot クライアントを直接ダウンロードしてください。
    Certbot クライアントをダウンロード・インストールするには、次のコマンドを入力します。
    入力するコマンド:

    wget https://dl.eff.org/certbot-auto
    chmod a+x certbot-auto
    ./certbot-auto

    ※root 権限への昇格が要求されます

Requesting to rerun ./certbot-auto with root privileges...

というエラーメッセージが表示された。
sudoと同じくパスワードを求められたので入力する。

certbot-auto を実行すると、OS 標準のパッケージリポジトリ( apt-get や yum など)を使用して、Certbot クライアントの実行に必要な複数のパッケージ(依存関係にあるパッケージ)が自動的にインストールされ、仮想化された Python 環境(PyPI からダウンロードしたパッケージを動作させるための環境)が構築されます。

※Python 環境の仮想化 virtualenv (英文) により、PyPI パッケージによる OS メイン空間の汚染を防いでいます。

※Certbot クライアントを実行すると、パッケージ管理システム(yum など)を使用したパッケージのインストール・アップデートが、確認画面の表示無しで自動的に行われます。
厳格にパッケージのバージョンを管理しているサーバの場合にはご注意ください。
自動的にインストール・アップデートされるパッケージは、「python」「python-virtualenv」「python-pip」「python-devel」「python-tools」「tcl」「tix」「tk」「tkinter」「gcc」「augeas-libs」「openssl」「ca-certificates」「dialog」「libffi-devel」「openssl-devel」「redhat-rpm-config」「keyutils-libs-devel」「krb5-devel」「libcom_err-devel」「libselinux-devel」「libsepol-devel」「zlib-devel」「mod_ssl」です(2016年05月18日 に CentOS 6.7 で確認)。
なお、左記のパッケージ一覧は環境により異なる場合があり、将来的に変更される可能性があります。

Complete!
Creating virtual environment...
Installing Python packages...
Installation succeeded.
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): 

メールアドレスの登録が要求されたが、とりあえずキャンセルにしてみる。

An e-mail address or --register-unsafely-without-email must be provided.

というメッセージが表示された。

補足説明:
certbot は、OS 標準のパッケージ管理システムからインストールした Certbot パッケージ(旧・Let's Encrypt パッケージ)を実行する際に使用するコマンドです。
wget https://dl.eff.org/certbot-auto または git clone https://github.com/certbot/certbot コマンドでインストールした Certbot クライアントを使用する場合には、クライアントをダウンロードしたディレクトリにある certbot-auto(ファイル名)を実行します。

CentOS6の場合は、「certbot-auto」でインストールしたので、後者になる

cd /home/user
./certbot-auto

という形でコマンドを使うことになる。

テスト

$ ./certbot-auto --help
Requesting to rerun ./certbot-auto with root privileges...
[sudo] password for user:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 certbot-auto [SUBCOMMAND] [options] [-d DOMAIN] [-d DOMAIN] ...

Certbot can obtain and install HTTPS/TLS/SSL certificates.  By default,
it will attempt to use a webserver both for obtaining and installing the
certificate. The most common SUBCOMMANDS and flags are:

obtain, install, and renew certificates:
   (default) run   Obtain & install a certificate in your current webserver
   certonly        Obtain or renew a certificate, but do not install it
   renew           Renew all previously obtained certificates that are near
expiry
   enhance         Add security enhancements to your existing configuration
  -d DOMAINS       Comma-separated list of domains to obtain a certificate for

 --apache          Use the Apache plugin for authentication & installation
 --standalone      Run a standalone webserver for authentication
 --nginx           Use the Nginx plugin for authentication & installation
 --webroot         Place files in a server's webroot folder for authentication
 --manual          Obtain certificates interactively, or using shell script
hooks

  -n               Run non-interactively
 --test-cert       Obtain a test certificate from a staging server
 --dry-run         Test "renew" or "certonly" without saving any certificates
to disk

manage certificates:
   certificates    Display information about certificates you have from Certbot
   revoke          Revoke a certificate (supply --cert-path or --cert-name)
   delete          Delete a certificate

manage your account with Let's Encrypt:
   register        Create a Let's Encrypt ACME account
 --agree-tos       Agree to the ACME server's Subscriber Agreement
  -m EMAIL         Email address for important account notifications

More detailed help:

 -h, --help [TOPIC]    print this message, or detailed help on a topic;
                       the available TOPICS are:

  all, automation, commands, paths, security, testing, or any of the
  subcommands or plugins (certonly, renew, install, register, nginx,
  apache, standalone, webroot, etc.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

メールアドレスの登録

$ su
# ./certbot-auto
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): test@example.com

Google翻訳
/var/log/letsencrypt/letsencrypt.logにデバッグログを保存する
選択されたプラグイン:オーセンティケータapache、インストーラapache
電子メールアドレスを入力してください(緊急更新およびセキュリティ通知に使用されます)(キャンセルするには 'c'を入力してください):

→オプション無しだとデフォルトでApacheが対象になるようだ。
→Nginxにする場合は、プラグインで指定できるようだ。

./certbot-auto --nginx

cf. Nginx プラグイン - Let's Encrypt 総合ポータル https://letsencrypt.jp/docs/using.html#nginx

利用規約への同意

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

Google翻訳
利用規約をご覧ください
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf
ACMEサーバーに登録するには、同意する必要があります。
https://acme-v02.api.letsencrypt.org/directory

お知らせメールの受取り

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

Google翻訳
Let's Encryptプロジェクトの創設パートナーであるElectronic Frontier FoundationとCertbotを開発する非営利団体と電子メールアドレスを共有してもよろしいですか?
ウェブの暗号化、EFFニュース、キャンペーン、デジタルの自由をサポートする方法についての電子メールをお送りします。

ドメインの登録

No names were found in your configuration files. Please enter in your domain
name(s) (comma and/or space separated)  (Enter 'c' to cancel): c

Google翻訳
設定ファイルには名前が見つかりませんでした。
あなたのドメイン名を入力してください(カンマおよび/またはスペースを区切ってください)(キャンセルするには 'c'を入力してください):

とりあえず、いったんキャンセルにしてみた。
登録するドメイン(サブドメインも含む)を用意しておく必要がある。

Please specify --domains, or --installer that will help in domain names autodiscovery, or --cert-name for an existing certificate name.
IMPORTANT NOTES:
- 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.

Google翻訳
ドメイン名の自動検出に役立つ--domains、--installer、または既存の証明書名の--cert-nameを指定してください。
重要なメモ:

  • アカウントの認証情報が、Certbot設定ディレクトリの/etc/letsencryptに保存されています。
    今すぐこのフォルダの安全なバックアップを作成する必要があります。
    この構成ディレクトリには、Certbotで取得した証明書と秘密鍵も含まれているため、このフォルダの定期的なバックアップを行うことが理想的です。

Nginxプラグインを利用する

デフォルトでやるとApache向けになるみたいなので、Nginx向けのやり方でもう一度やってみる。

$ ./certbot-auto --nginx
Requesting to rerun ./certbot-auto with root privileges...
[sudo] password for user: 
Saving debug log to /var/log/letsencrypt/letsencrypt.log
The nginx plugin is not working; there may be problems with your existing configuration.
The error was: NoInstallationError("Could not find a usable 'nginx' binary. Ensure nginx exists, the binary is executable, and your PATH is set correctly.",)

Google翻訳
nginxプラグインが動作していません。 既存の設定に問題がある可能性があります。
エラー:NoInstallationError?( "使用可能な 'nginx'バイナリが見つかりませんでした。nginxが存在し、バイナリが実行可能であり、PATHが正しく設定されていることを確認してください。

Nginxのパスを指定する必要あり?
Nginxの実行ファイルでいいのだろうか?

$ whereis nginx
nginx: /usr/local/nginx


ここまでやって根本的に間違っているような気がした。
「certbot --nginx 使い方」で検索したら、以下の参考情報がヒットした。


「let's encrypt webroot ディレクトリ」でGoogle検索

  • Let’s Encryptの使い方。standaloneとwebroot - Qiita https://qiita.com/f_uto/items/4178a9fdd657b78672ea

    -w は、webrootのディレクトリを指定。この中にファイルを書き込みます。そのファイルをhttp(80)でアクセスするのでアクセスできる状態としておくこと。

    -dは、ドメイン名。複数指定可能。複数指定するときは、-d xxx.sample.com -d xxx2.sample.comと書いていく。

「let's encrypt webrootのディレクトリを指定」でGoogle検索

ここを熟読したら、Let's Encryptの使い方について、だんだんと分かってきた。

例えば、

certbot certonly --webroot -w /var/www/example/ -d www.example.com -d example.com -w /var/www/other -d other.example.net -d another.other.example.net

と指定した場合、指定した全てのドメイン名(www.example.com, example.com, other.example.net, another.other.example.net)に使用できる1枚の SSL/TLS サーバ証明書が発行されます。
ドメイン使用権者の認証の際には、ドメイン名 www.example.com と example.com に対してはドキュメントルートディレクトリ /var/www/example/ が使用され、ドメイン名 other.example.net と another.other.example.net に対してはドキュメントルートディレクトリ /var/www/other が使用されます。

Webroot プラグインは ${webroot-path}/.well-known/acme-challenge に一時ファイル(ワンタイムトークン)を作成し、Let's Encrypt の認証サーバから HTTP リクエスト(名前解決には DNS のAレコードを使用)で取得することにより、認証を行います。

※Webroot プラグインを使用する場合、Webサーバの設定が、隠しディレクトリ(ドット "." から始まる名前のディレクトリ名)内のファイルにインターネット経由でアクセスできるように構成されている必要があります。もし、ウェブサーバの設定によって /.well-known に対して特別な扱いがされている場合には、/.well-known/acme-challenge 内のファイルに外部からアクセス可能にするために設定を変更する必要があるかもしれません。

※複数のドメイン名に対応するサーバ証明書には、サブジェクトの代替名(SAN : Subject Alternative Name)という仕組みが用いられています。詳しくは 1枚で複数のドメイン名に対応する証明書は発行できますか? をご覧ください。

要するに、

  1. -wオプションでWebサイトのルートディレクトリを指定する。
  2. -dオプションでWebサイトのドメイン名を指定する。
  3. -wと-dの組合せを延々と指定していけば、複数のドメイン名に適用できるSSL証明書を作れる。
  4. certbot-autoはWebサーバーとしての機能も持っているため、自分から外(認証局CA)にアクセスしに行く。
  5. certbot-autoがSSL証明書を作成する作業で使うフォルダ(ディレクトリー)として、Webサイトのルートディレクトリ内に「${webroot-path}/.well-known/acme-challenge」という隠しディレクトリが用意されていなければならない?(これは自動的に作られるのかな?)
  6. 隠しディレクトリの「${webroot-path}/.well-known/acme-challenge」は、インターネット経由でアクセスできる=パーミッションが公開されてる必要があるみたい?

crontabで更新を自動化する作業は、とりあえず後回しでOK

「/.well-known/acme-challenge "パーミッション"」でGoogle検索

  • Let's Encrypt 証明書をサーバー無停止で新規発行する(Nginx編) - @dobassy 技術ブログ https://blog.exlair.net/entry/le-webroot-plugin-with-nginx

    Let's Encrypt 証明書作成の準備をする (Nginx環境)
    前回記事でも "webrootの仕組み" として少し記載しました。
    /path/to/doc_root/ で設定した部分に、スクリプトが /.well-known/acme-challenge/xxxxxx(←ランダム文字) という構造でファイルを作成し認証しているようです。従って、WordPress?をリバースプロキシしているnginxに対しては、以下のコンフィグを適用する必要があります。
    ですので、Let's Encrypt スクリプトがアクセスするためのパスを以下の通り定義します。

       server_name exlair.hatenablog.com;
       ...(中略…各自のコンフィグ)...
       location ^~ /.well-known/acme-challenge {
           root /path/to/doc_root/;
       }

    新規発行したいドメインの VirtualHost? 設定に適用します。ここでは、exlair.hatenablog.com に適用するイメージを記載しています。
    /path/to/doc/root/ 部分は何でも構いません。用途が限定的なので、現在稼働しているサービスに依存しない場所へ新規作成したほうが運用しやすいと個人的には思います。別の新規ドメイン作成時にも同じものが流用できるので。
    パーミッションは、Webページが表示できる最低限のレベルで問題ありません(スクリプト実行時には結局、rootでランダム文字のディレクトリが生成されるため)。

Nginxのルーティングの設定で、「/.well-known/acme-challenge」にアクセスしてきたら「/path/to/doc_root/」へ転送するように設定してやればOKみたい。
=自分で好きな「/path/to/doc_root/」を用意してOKと。
複数のドメインで共用するなら、共通のディレクトリを作り、そこを「root /path/to/doc_root/;」みたいな形で指定してやる。


Let's Encryptの使い方で参考になったサイト その1

Nginxの設定ファイルの書き方で参考になったサイト

次は

  1. ワイルドカードの使い方
  2. 証明書の自動更新(クーロンの使い方)
    を調べてみる。

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-12-10 (月) 15:38:20 (1759d)