AmazonLinux2でSSL暗号化通信 (FreenomとLet's Encryptを使って)


チュートリアル: Amazon Linux 2 で SSL/TLS を使用できるように Apache ウェブサーバーを設定する
https://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/SSL-on-an-instance.html#letsencrypt

準備:https://でアクセスできるようする

AmazonLinux2にrootユーザーでログインする。

AmazonLinux2
login as: ec2-user
[ec2-user ~]$ sudo -i
[root ~]#

Apacheが実行されていることを確認する。
(enabledなら有効、disabledなら無効)

AmazonLinux2
[root ~]# systemctl is-enabled httpd

ソフトウェアパッケージが最新の状態であることを確認するため、
インスタンスの全てのソフトウェアパッケージを更新する。

AmazonLinux2
[root ~]# yum update -y

Apache モジュール mod_ssl をインストールして SSL/TLS サポートを追加します。

AmazonLinux2
[root ~]# yum install -y mod_ssl

Apache を再起動します。

AmazonLinux2
[root ~]# systemctl restart httpd

パブリックDNSアドレスの先頭にhttps://を付けてウェブブラウザでURLを実行します。
https://ec2-~.compute.amazonaws.com

ブラウザからのセキュリティ警告を無視してサイトに進んでください。


ちゃんとアクセスできれば成功です。

Freenomで無料ドメインを取得

Freenomにアクセスします。
https://www.freenom.com/ja/index.html
取得したいアドレス名を入力して[利用可能状況をチェックします]をクリックします。

使用するドメイン名を選択して、[チェックアウト]をクリックします。

[Use DNS]をクリックしてIP AddressにAmazonLinux2インスタンスのパブリックIPを入力します。
Periodの項目から使用期間を選択します。(1~12か月まで無料で使用できます。)
[Continue]をクリックします。

自分のメールアドレスを入力して[Verify My Email Address]をクリックします。

Freenomからメールが送られて来るので、アカウント登録をしましょう。
登録が完了すると、ここで取得したアドレスを入力するとAmazonLinux2インスタンスのアドレスへリダイレクトされるようになります。

Let's Encryptから無料のSSL証明書を取得

Extra Packages for Enterprise Linux (EPEL) 7 パッケージをダウンロードします。これは、Certbot が必要とする依存関係を提供するために必要です。

AmazonLinux2
[root ~]# wget -r --no-parent -A 'epel-release-*.rpm' http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/

リポジトリパッケージをインストールします。

AmazonLinux2
[root ~]# rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm

EPELの有効化

AmazonLinux2
[root ~]# yum-config-manager --enable epel*

EPEL が有効になっていることを確認

AmazonLinux2
[root ~]# yum repolist all
epel/x86_64                                           Extra  enabled: 12,721+175
epel-debuginfo/x86_64                                 Extra  enabled:      2,872
epel-source/x86_64                                    Extra  enabled:          0
epel-testing/x86_64                                   Extra  enabled:      828+2
epel-testing-debuginfo/x86_64                         Extra  enabled:        118
epel-testing-source/x86_64                            Extra  enabled:          0

Apacheのメイン設定ファイルを編集します。

AmazonLinux2
[root ~]# vim /etc/httpd/conf/httpd.conf

「listen 80」ディレクティブを見つけてその後ろに次の行を追加します。このとき、サンプルドメイン名を、設定する実際の共通名およびサブジェクト代替名拡張子 (SAN) に置き換えます。
([A]キーをクリックしてINSERTモードに切り替え、listen 80の下に以下のコードを記述。[Esc]キーでINSERTモードを終了し、[*wq]と入力して[Enter]キーを押して保存)
※ServerNameとServerAliasには自分がFreenomで取得したアドレス名を入力してください。

Listen 80
<VirtualHost *:80>
    DocumentRoot "/var/www/html"
    ServerName "ponta.ml"
    ServerAlias "www.ponta.ml"
</VirtualHost>

Apacheを再起動します。

AmazonLinux2
[root ~]# systemctl restart httpd

Certbotパッケージと依存関係をインストールします。

[root ~]# yum install -y certbot python2-certbot-apache

Crebotを実行します。

AmazonLinux2
[root ~]# certbot

連絡先住所(メールアドレス)を入力し、Enter キーを押します。

AmazonLinux2
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]

aを入力してEnter(規約に同意します。)

AmazonLinux2
(A)gree/(C)ancel: a

Let's EncryptパートナーにEmailを公開しますか?
公開する場合はy、したくない場合はnを入力してEnter。

AmazonLinux2
(Y)es/(N)o:

入力を空白にしたままEnterキーを押します。

AmazonLinux2
Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: ponta.ml
2: www.ponta.ml
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel):

HTTPでページにアクセスした時に、HTTPSにリダイレクトしますか?
リダイレクトする場合は2、しない場合は1を入力してEnter。

AmazonLinux2
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

これで、Freenomで取得したアドレスにhttpsでアクセス出来るようになりました。