俺、サービス売って家買うんだ

Swift, Kotlin, Vue.js, 統計, GCP / このペースで作ってればいつか2-3億で売れるのがポっと出来るんじゃなかろうか

Amazon Linux上のApacheにLet’s EncryptのSSL証明書でSSL設定をする

f:id:ie-kau:20161028035549p:plain


GoogleがSSL対応をページ表示順位の指標に入れたことをアナウンスしているのですが、数年間対応しないまま運営していたサイトがあったのでその対応を行ったメモです。

webmasters.googleblog.com (最近のことと思ってたけど、2014年のアナウンスだったのね。。)

SSL証明書はLet's Encryptのものを利用しています。 letsencrypt.org

目次

  1. 各種バージョン
  2. EC2でHTTPSのポートを有効にする
  3. certbot-autoと証明書の生成
  4. Apacheの設定変更
  5. 証明書の自動更新
  6. その他

1. 各種バージョン

Amazon Linux AMI release 2016.09
Apache 2.2.3

2. EC2でHTTPSのポートを有効にする

最初にやらないといけないことを忘れて事故りましたすみません。すみません。
EC2のセキュリティグループから対象のグループを選んで443ポートのインを開けましょう。

f:id:ie-kau:20161028030928p:plain

3. certbot-autoと証明書の生成

こちらのサイトを見ながら進めてみます。
nginxの人はこのサイトだけでOKかと。

knowledge.sakura.ad.jp

certbot-autoをインストールする

sudo curl https://dl.eff.org/certbot-auto -o /usr/bin/certbot-auto
sudo chmod 700 /usr/bin/certbot-auto

証明書を作る

certbot-auto certonly \   # 証明書の作成
    --webroot \  # 既存のウェブサーバを使うモードを選択
    -w [path to DocumentRoot] \   # ドキュメント・ルートのパス
    -d  [ドメイン] \   # 認証するドメイン名
    --email <メール>@<アドレス>  # メールアドレス登録(証明書期限切れの通知用)
FATAL: Amazon Linux support is very experimental at present...
if you would like to work on improving it, please ensure you have backups
and then run this script again with the --debug flag!

実験中って言われるけどそのまま突き進んでみた。

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/ドメイン/fullchain.pem. Your cert
   will expire on 2017-01-25. To obtain a new or tweaked version of
   this certificate in the future, simply run certbot-auto again. To
   non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
 - If you lose your account credentials, you can recover through
   e-mails sent to <メール>@<アドレス>.
 - 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

これでOK。以下のディレクトリに色々入る。

/etc/letsencrypt/

4. Apacheの設定変更

SSL/TLS対応

いつもどおり怖いので、ここらへんを参考にする。

docs.aws.amazon.com

SSLの設定が書いてあるところに適宜。

vi /etc/httpd/conf.d/ssl.conf

脆弱性をかわすためSSLv2, SSLv3は使わない方針にする。

# 利用するプロトコル
SSLProtocol -SSLv2 -SSLv3 +TLSv1 +TLSv1.1 +TLSv1.2

# 暗号化方式
SSLHonorCipherOrder on
SSLCipherSuite ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA

# サーバー証明書
SSLCertificateFile   /etc/letsencrypt/live/ドメイン/cert.pem

# 秘密鍵
SSLCertificateKeyFile   /etc/letsencrypt/live/ドメイン/privkey.pem

httpからhttpsへのリダイレクト

既にインデックスされているhttp側にアクセスが有った場合は、ページの評価を引き継ぐため301リダクレイトさせる。

  • 301 → 恒久的
  • 302 → 一時的

なので間違えないように。

RewriteRule ^(.*)$ https://ドメイン/$1 [R=301,L]

で、リスタート。

sudo apachectl restart

ここまでで設定を間違ってなければhttpsでアクセスできるはず。

5. 証明書の自動更新

Let’s Encryptの証明書は3ヶ月で期限が切れるので自動更新のコマンドをcronに仕込む。
さくらインターネットの記事を参考にnginxのところをApacheに変えただけ。

50 3 * * 0 certbot-auto renew --post-hook "sudo apachectl restart" 1 > /dev/null 2 > /dev/null

とは言え不安なので、有効期限が切れる少し前に確認のメールが飛ぶような設定にはしてあります。

6. その他

今回はCPUに余裕があったからそこまで問題ではなさそうだったけど、本来ならCPU使用率の推移はしばらく監視する。

関連記事

www.ie-kau.net www.ie-kau.net