Debian9 + Apache に"Let's Encrypt"をセットアップしてみた!!

WEBページのSSL対応はもはや避けては通れなくなってきていて、GoogleのSEOにもSSL対応是非が影響することが明確にうたわれている。参考

一方、立ち上げたばかりのサービスでに対して、最初から数万円のSSL証明書は買えない!!ということで、無料で発行される"Let's Encrypt"を適応してみたので、その手続きを備忘録としてまとめた。

"Let's Encrypt"について知りたい方は、オフィシャルサイトを参照してほしい。一応、日本語まとめサイトもあるので、合わせて読むと理解が深まると思う。

今回はConoHa VPS の "Debian9" への設定方法をまとめる。CentOS系はそこそこ情報があったのだけれど、Debian系は少なかったので…



環境

クライアントPC

  • MacOSX 10.13.4

サーバー

  • Debian9
  • Apache/2.4.25 (Debian)
  • certbot 0.10.2

サーバーへSSH接続できることを前提とする。

また、今回はroot権限でそのまま作業を行うが、セキュリティ確保の為にはroot認証は無効化することを推奨する。

サーバーの環境構築とSSH設定、root無効化などの詳細に関しては、
ConoHa VPS の Debian9 に Djangoベースのサービスデプロイしてみた!!〜その①〜
とか
SSH conf を簡単に接続する方法まとめた!
を参照

設定方法

作業手順は大きく以下の3ステップとなる

  1. Let's Encryptツールのインストール
  2. apacheのインストール
  3. SSL認証情報の設定
  4. WEBサーバーの設定
  5. WEBサーバーの再起動


Step1. "Let's Encrypt"ツール(certbot)のインストール

まず、この作業はroot権限の元で行うこと

$ apt-get update 
$ apt-get install -y certbot
$ certbot --version
certbot 0.10.2

Step2. apacheのインストール

以下のコマンドでapacheをインストール

apt-get install -y apache2

<チェック>
適当なブラウザにWEBサーバーのIPアドレスを入力し、apache初期画面が表示されることを確認する。この結果、webサーバーが正常にインストール〜起動していることがわかる
apache初期画面

Step3. SSL認証情報の設定

以下のコマンドを実行して、SSL認証ファイルを生成する

# certbot certonly --webroot -w <ドキュメントのルートPATH> -d <ドメイン名>
$ certbot certonly --webroot -w /var/www/html -d hogehoge.net

この結果、以下のような表示になれば成功

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):hoge@hoge.jp    # ←入力

-------------------------------------------------------------------------------
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      # ←入力
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for hogehoge.net
Using the webroot path /var/www/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges
Generating key (2048 bits): /etc/letsencrypt/keys/0000_key-certbot.pem
Creating CSR: /etc/letsencrypt/csr/0000_csr-certbot.pem

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/hogehoge.net/fullchain.pem. Your cert will
   expire on 2018-07-24. 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"
 - If you lose your account credentials, you can recover through
   e-mails sent to hoge@hoge.jp.
 - 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


このメッセージ中で重要な部分は、IMPORTANT NOTES:以降にある証明書や秘密鍵などに関する情報。ココには、それらの鍵ファイルを出力したパスが載っていて、この例の場合は、/etc/letsencrypt/live/hogehoge.net/ディレクトリ内に各種鍵が格納されていることがわかる。これら鍵ファイルは次ステップでサーバー設定に必要となる。

Step4. WEBサーバーの設定

以下のコマンドでWEBサーバーのSSL設定ファイルを編集

# オリジナルのファイルを別名でコピーしておくと安心
$ cp /etc/apache2/sites-available/default-ssl.conf /etc/apache2/sites-available/default-ssl.conf.org
$ nano /etc/apache2/sites-available/default-ssl.conf

ファイルが開いたら、以下の部分を編集(加筆、修正)する。場合によっては、行の先頭に#が付与されている場合も考えられるが、この場合、#はコメントアウトの意味なので削除!!

ServerName hogehoge.net
DocumentRoot /var/www/html

SSLEngine on
SSLCipherSuite ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:EC$
SSLHonorCipherOrder on
SSLOptions +StrictRequire

SSLCertificateFile /etc/letsencrypt/live/hogehoge.net/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hogehoge.net/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/hogehoge.net/chain.pem

Step5. WEBサーバーの再起動

順番に以下のコマンドを実行して、設定を反映&再起動!!
毎回、apacheの再起動を促されるが、最後に一回実行すればいい!!

$ a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
  systemctl restart apache2
$ a2enmod ssl
Considering dependency setenvif for ssl:
Module setenvif already enabled
Considering dependency mime for ssl:
Module mime already enabled
Considering dependency socache_shmcb for ssl:
Enabling module socache_shmcb.
Enabling module ssl.
See /usr/share/doc/apache2/README.Debian.gz on how to configure SSL and create self-signed certificates.
To activate the new configuration, you need to run:
  systemctl restart apache2


$ a2ensite default-ssl.conf
Enabling site default-ssl.
To activate the new configuration, you need to run:
  systemctl reload apache2

最後に再起動!!

$ service apache2 restart

まとめ

この結果、ブラウザへ https://hogehoge.net を入力すると、無事SSL通信できていることがわかる!!

ConoHaVPS_ssl2

<補足>
以下のサイトに自分のドメイン名をセットすると、SSL証明のテストを行なって、判定してくれる

# https://www.ssllabs.com/ssltest/analyze.html?d=<ドメイン名>
https://www.ssllabs.com/ssltest/analyze.html?d=hogehoge.net

今回の結果、【B】判定だった。原因はRSA暗号が"2048bit"であることらしい。【A】判定を目指して、4096bitを今後の課題とする

ConoHaVPS_ssl1

以上、今日はここまで!!



参考サイト:
https://www.itzgeek.com/how-tos/linux/how-to-install-lets-encrypt-on-centos-debian-ubuntu-running-apache-web-server.html