2018年1月21日日曜日

[Debian9] SSL/TLS証明書「Let's Encrypt」の導入記録


【試した環境】
Google Cloud Platform(GCP)[VMインスタンス]上で実行
OS : Linux(Debian 9.2)

今回は [Debian 9] で [SSL/TLSの証明書(Let's Encrypt)] を [Apache] インストール環境へ導入した時の記録を残しておきたいと思います。

前提条件

[Apache] が事前にインストールされている
[Let's Encrypt はドメイン名が必要(IP直での証明書の取得はできません)

Certbot クライアントのインストール

まず初めにCertbot クライアント(旧・Let's Encrypt クライアント)をインストールします。
OSごとにインストールの方法が違います。今回は「Debian 8 (jessie)」に従いインストールします。(今回は「Debian 9 (stretch)」ですが、まだ資料がないので)
Certbot クライアントのインストール - Let's Encrypt 総合ポータル

リポジトリ [backports] から入手する必要なないようで、以下を実行。
$ sudo apt-get install certbot python-certbot-apache

これで [certbot] が使えるようになりました。

certbotで証明書の取得

[certbot] コマンドで [--apache]オプションを指定すると、設定ファイルから自動的に該当する箇所を検出して https 用の設定をしてくれます。
$ sudo certbot --apache

このコマンドで初回に聞かれることは以下のとおり
No names were found in your configuration files. Please enter in your domain
name(s) (comma and/or space separated)  (Enter 'c' to cancel):tadamatuapps.com,www.tadamatuapps.com
ドメインを答えます。複数ある場合は[,(カンマ)]か[(スペース)]で。

Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):xxxxx@gmail.com
連絡用のメールアドレスを答えます。期限切れなど連絡を受け取れます。

-------------------------------------------------------------------------------
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-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A
最後は同意を求められます。

で、結果は以下のようなエラーが・・・
An unexpected error occurred:
The request message was malformed :: Error creating new authz :: DNS name does not have enough labels
Please see the logfiles in /var/log/letsencrypt for more details.
予期せぬエラーとな・・・

念のためもう一度実行してみる
$ sudo certbot --apache
Client with the currently selected authenticator does not support any combination of challenges that will satisfy the CA.
うーん、やっぱりエラーに。

いろいろ調べた結果、2018年1月11日にTLS-SNI検証を無効化したことによるものだと理解をしました。
Let’s Encrypt ACME TLS-SNI検証を無効化
以下のように実行すると良いようです。
$ sudo certbot --authenticator standalone --installer apache

しかし以下のようなエラーが出ました。
-------------------------------------------------------------------------------
The program apache2 (process ID 10724) is already listening on TCP port 80. This
will prevent us from binding to that port. Please stop the apache2 program
temporarily and then try again.
-------------------------------------------------------------------------------

コマンドのエラーではなく、単純にApacheのプロセスが残っていたようなので、プロセスを止めます。
$ sudo service apache2 stop

再度コマンドを実行。
$ sudo certbot --authenticator standalone --installer apache

ドメインを聞かれます。
No names were found in your configuration files. Please enter in your domain
name(s) (comma and/or space separated)  (Enter 'c' to cancel):tadamatuapps.com

Please choose whether HTTPS access is required or optional.
-------------------------------------------------------------------------------
1: Easy - Allow both HTTP and HTTPS access to these sites
2: Secure - Make all requests redirect to secure HTTPS access
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 1
HTTPSのオプションを指定します。
1:Easy - HTTPとHTTPSの両方のアクセスを許可する
2:Secure - HTTPSアクセスを保護するためにすべての要求をリダイレクトする

-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://tadamatuapps.com
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=tadamatuapps.com
-------------------------------------------------------------------------------
やっと成功!

さてもう一つwwwサブドメインつきのドメイン名を設定したかったので、もう一度実行。
次はパラメータにドメインをつけて。
$ sudo certbot --authenticator standalone --installer apache -d www.tadamatuapps.com

We were unable to find a vhost with a ServerName or Address of www.tadamatuapps.com.
Which virtual host would you like to choose?
(note: conf files with multiple vhosts are not yet supported)
-------------------------------------------------------------------------------
1: 000-default-le-ssl.conf        | tadamatuapps.com      | HTTPS | Enabled
2: default-ssl.conf               |                       | HTTPS |
3: 000-default.conf               |                       |       | Enabled
-------------------------------------------------------------------------------
Select the appropriate number [1-3] then [enter] (press 'c' to cancel):
複数はサポートされていません的なメモが。
一旦 [c] を入力し、処理をキャンセル。

上記に移動して、[default-ssl.conf] を001としてコピー。
# cd /etc/apache2/sites-available/
# cp default-ssl.conf 001-default-ssl.conf

再度、certbotを実行。
$ sudo certbot --authenticator standalone --installer apache -d www.tadamatuapps.com

-------------------------------------------------------------------------------
1: default-ssl.conf.bk            |                       | HTTPS |
2: 000-default-le-ssl.conf        | tadamatuapps.com      | HTTPS | Enabled
3: default-ssl.conf               |                       | HTTPS |
4: 000-default.conf               |                       |       | Enabled
5: 001-default-ssl.conf           |                       | HTTPS |
-------------------------------------------------------------------------------
Select the appropriate number [1-5] then [enter] (press 'c' to cancel): 5

成功しました。
-------------------------------------------------------------------------------
Congratulations! You have successfully enabled https://www.tadamatuapps.com
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=www.tadamatuapps.com
-------------------------------------------------------------------------------

最後にApacheの再起動。
$ sudo service apache2 restart

確認

証明書が作成されているか確認。
# cd /etc/letsencrypt/live/tadamatuapps.com
# ls
cert.pem  chain.pem  fullchain.pem  privkey.pem
#cd //etc/letsencrypt/live/www.tadamatuapps.com
# ls
cert.pem  chain.pem  fullchain.pem  privkey.pem

設定したドメインにブラウザでHTTPSアクセスすると・・・
https://tadamatuapps.com/tool/sslcheck/
うまく動作しました!

自動更新(Renew)設定

「Let's Encrypt」は3ヶ月ごとに更新が必要です。
最後に自動的に更新(Renew)されるように対応をします。
まずは任意の場所に自動実行するためのシェル(sh)を作成します。
$ sudo vi cerbotrenew.sh
#!/bin/sh
# 一旦サーバを止める
service apache2 stop;
# 証明書の再発行
certbot renew;
# サーバを起動
service apache2 start

これをcronに登録します。
$ sudo crontab -e
### Let's Encrypt ### 毎週月曜
0 3 * * 1 sudo sh /home/tadamatu0001/sh/cerbotrenew.sh

【参考】

Let's Encrypt 総合ポータル
debian で無料のSSL/TLS証明書(Let's Encrypt) を使う

0 件のコメント:

コメントを投稿