Apache

無料SSL証明書「Let’s Encrypt」をAmazon EC2で使用する

ssl-secure
o_wani
この記事は作成から7年以上経過しているため、内容が古くなっている可能性があります。

無料で利用できるSSL証明書のLet's Encryptが気になっていたので、試してみることにしました。このブログで導入してみましたが、カンタンにURLがHTTPSに切り替わりましたのでその手順を書いておきます。所要時間は約15分です。

GoogleもHTTPSの方が好みのようで、SEOに直接関係があるとは言っていませんが、悪い影響は考えられません。1年くらい前になりますが、こんな発表もありました。

『グーグル、HTTPSページを優先的にインデックスすることを発表、既存HTTPに影響なし』
http://www.sem-r.com/google-2010/20151219103538.html

これを見て、「SSL証明書」導入して、このブログもHTTPS通信にしないとダメだなぁと思っていましたが、内容をよく見てみると

URLの両方のバージョンを発見した場合は、後者を優先的にインデックスしていく

というだけで、直接的なSEOへの影響は無いようです。それでも、やはりSSL証明書を取得したいとずっと思っていました。今日、その時がやってきたのです。


[今回の作業環境]
・Amazon Linux(CentOS6と近い構成)
・Apache 2.4
・対象ドメイン:sample.com (以下はこのドメインで作業)
※ドメインは各自適宜変更して使用


1.certbotのインストール

/home/ec2-userにてcertbotgitでインストールする。

$ git clone https://github.com/certbot/certbot
Cloning into 'certbot'...
remote: Counting objects: 40932, done.
remote: Compressing objects: 100% (41/41), done.
remote: Total 40932 (delta 15), reused 4 (delta 4), pack-reused 40887
Receiving objects: 100% (40932/40932), 11.28 MiB | 10.57 MiB/s, done.
Resolving deltas: 100% (29140/29140), done.
Checking connectivity... done.

2.コマンドで設定ファイルの作成

ディレクトリを移動して、コマンドを実行する。

$ cd certbot/
$ ./certbot-auto certonly --standalone -d sample.com
WARNING: 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!

Amazon Linuxでは、--debugオプションが必要となるそうです。

$ ./certbot-auto --debug certonly --standalone -d sample.com
Bootstrapping dependencies via Amazon Linux...
yum is /usr/bin/yum
...
...
...
Complete!
Creating virtual environment...
Installing Python packages...
Installation succeeded.
Requesting root privileges to run certbot...
  /home/ec2-user/.local/share/letsencrypt/bin/letsencrypt --debug certonly --standalone -d sample.com
Version: 1.1-20080819
Version: 1.1-20080819

メールアドレスinfo@sample.comを入力して「OK」

Alt text

規約への同意「agree」

Alt text

Apacheを停止させる必要があるとのこと。。「OK」

Alt text

...
...
MisconfigurationError: At least one of the (possibly) required ports is already taken.


IMPORTANT NOTES:
 - If you lose your account credentials, you can recover through
   e-mails sent to info@sample.com.
 - 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.

うまくできなかったので、起動中のwebサーバを停止して再度実施する。

$ sudo /etc/init.d/httpd stop
Stopping httpd:                                            [  OK  ]
$ ./certbot-auto --debug certonly --standalone -d sample.com
Requesting root privileges to run certbot...
  /home/ec2-user/.local/share/letsencrypt/bin/letsencrypt --debug certonly --standalone -d sample.com
Version: 1.1-20080819
Version: 1.1-20080819

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/sample.com/fullchain.pem. Your cert will
   expire on 2017-01-03. 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 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

これで、証明書が生成される。次にapacheの設定が必要。

追記:2017/01/10時点では、apacheの再起動は必要なかった。
追記:2018/02/20 --standalone:このオプションはそもそもWebサーバ(Apache や nginx など)が動作していない環境において、 必要なオプションでした。

3.mod_sslのインストール

SSLを使用するために必要なmod_sslのインストールです。(インストール済の場合は不要)

$ sudo yum install mod24_ssl

4.iptablesの修正

HTTPS通信で使用するport番号:443を開放するためiptablesの修正をします。
/etc/sysconfig/iptables

...
...
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
...
...

iptables修正後は、iptableの再起動が必要です。

$ sudo /etc/init.d/iptables restart

5.apacheの設定

設定ファイルの追加をします。
/etc/httpd/conf.d/ssl.conf
以下の2点は自分の環境に置き換えて、適宜変更。
ドキュメントルート:/var/www/html/sample.com
ドメイン:sample.com

<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/sample.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/sample.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/sample.com/chain.pem

    <Directory "/var/www/html/sample.com/">
        AllowOverride All
    </Directory>

    ServerAdmin root@sample.com
    DocumentRoot /var/www/html/sample.com
    ServerName sample.com
</VirtualHost>

Webサーバの再起動です。

$ sudo /etc/init.d/httpd restart

追記2017.01.12)/etc/httpd/conf/httpd.confは修正不要。

6.HTTPSで表示

これでhttpsにアクセスできるようになりました。

Alt text

次回は、このSSL証明書の自動更新の設定をします。

おまけ

httpにアクセスしてきたら、httpsにリダイレクトします。
以下のようなVirtualHostを設定している箇所にmod_rewrieの記述を追加します。

<VirtualHost *:80>
    DocumentRoot /var/www/html/sample.com
    ServerName sample.com

    RewriteEngine on
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
....
....

設定を有効にするには、Webサーバの再起動が必要です。

これでひと通り作業完了です。無料でSSLが使えるようになりました。
参考サイトは以下です。

Let’s Encrypt 総合ポータル

Let’s Encrypt

無料で使えるSSL!会社でも自宅でもみんなで let’s encrypt!

無料SSL証明書の Let’s Encrypt が公開されたので実際に試してみた

Let’s EncryptのSSL証明書で、安全なウェブサイトを公開

STAFF
o_wani
o_wani
スタッフ
大学卒業後、15年間WEB業界で働く。現在はマネジメントに従事していますが、ChatGPTの登場に触発され、このブログを再開。AIをパートナーに、自分で手を動かして実装する楽しさと喜びを再発見中。時代が変わりつつある中でも、陳腐化しない情報発信も目指しています。
記事URLをコピーしました