ホームページSSL化

最近(2022年)の新しいブラウザではSSL通信に対応していないホームページを開くと警告が表示されます。
SSLとは『Secure Sockets Layer(セキュア ソケット レイヤー)』の略で、WebブラウザとWebサーバーとの通信を暗号化しユーザーが入力する個人情報等を暗号化して送受信することです。
暗号化することで悪意のある第三者が個人情報等を窃取・改ざんを行おうとしても情報は暗号化されており窃取も改ざんもできなくユーザーの個人情報を安全に送信できるシステムです。
代表的なブラウザの警告としては下記のようなものがあります。
・Microsoft Edge
・Mozilla Firefox
・Google Chrome
ブラウザのアドレスの前に警告文や警告アイコンが表示されているのがわかります。
このような警告が表示されていると「このホームページは大丈夫だろうか?」と考えてしまう人います。
通信を暗号化しているだけなので個人情報等を入力・送信しなければ特に問題はないのですが・・・
しかし、人の思い込みや感情は時として行動を制限させてしまうことがあります。
せっかく作ったホームページもSSLに対応していないという理由だけで訪問者が来なかったりしたら困ります。
そこで皆さんのお役に少しでもなればと思いホームページのSSL化について覚え書きを残しておきます。

概要

◆前提条件
・サーバーOSはCentOS Stream 8
・固定グローバルIPアドレス
・自宅サーバー
・ホスト名(ドメイン名):www.vm-system.net
◆Webサーバー構成
WebサーバーにはApache(アパッチ)を使いLet's Encryptより取得した認証証明書を利用してSSL通信(暗号化通信)を行います。

認証証明書

◆Let's Encrypt より認証証明書を取得
Let's Encrypt とは認証証明書を無料で発行しているプロジェクトで実績もあり多くのブラウザ・サーバー等で信頼済として扱われています。
証明書の有効期間は90日間になるが Snapd からインストールした Certbot にはタイマーが付属されているため自動で有効期間の更新ができます。
◆snapd のインストールと有効化
EPELからインストールするためのリポジトリを追加します。
[root@vm-system]# dnf -y install epel-release
他リポジトリとのパッケージの競合をさけるため、enabled=0 として普段は読まないようにしておきます。
[root@vm-system]#sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/epel.repo
snapd をインストールします。
[root@vm-system]#dnf --enablerepo=epel -y install snapd
snapd のシンボリックの作成と有効化をします。
[root@vm-system]#ln -s /var/lib/snapd/snap /snap
[root@vm-system]#echo 'export PATH=$PATH:/var/lib/snapd/snap/bin' >c/profile.d/snap.sh
[root@vm-system]#systemctl enable --now snapd.service snapd.socket
◆Certbot のインストール
Snap からインストールします。
[root@vm-system]#snap install certbot --classic
シンボリックの作成。
[root@vm-system]#ln -s /snap/bin/certbot /usr/bin/certbot
◆認証証明書の取得
Let's Encrypt でホームページ用の証明書を取得します。
初回は以下のようにいくつかの質問に回答します。
[root@vm-system]#certbot certonly --standalone -d www.vm-system.net
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Enter email address (used for urgent renewal and security notices)
# 受信可能なメールアドレスを指定
(Enter 'c' to cancel):**********@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. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 利用条件に同意する
(Y)es/(N)o: y
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# ニュースやサポート情報のメールを受け取る
(Y)es/(N)o: y
# 回答が終わると証明書の作成がはしまり、下記のように『Congratulations!』と証明書へのパスが表示される
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.vm-system.net/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.vm-system.net/privkey.pem
Your cert will expire on 2019-01-16. To obtain a new or tweaked>
*** 以下略 ***
2回目以降はメールアドレス等の基本情報は登録されているので証明書発行だけになります。
上記では、
・fullchain.pem
: 証明書と中間証明書を連結したファイル
・privkey.pem
: 秘密鍵
二つのファイルが作成されているのが分かります。
◆認証証明書の確認
証明書は /etc/letsencrypt/live/www.vm-system.net に作成されますので確認します。
www.vm-system.netの部分は自分(自社)のホスト名(ドメイン名)に変えて下さい。
[root@vm-system]# ls /etc/letsencrypt/live/www.vm-system.net
READMEcert.pemchain.pemfullchain.pemprivkey.pem
先ほどの二つのファイルの他に、
・cert.pem
: 証明書
・chain.pem
: 中間証明書
二つのファイルが作成されているのが分かります。

関連ソフトのインストール

Apache(アパッチ)をSSL対応にするためには、mod_sslが必要です。
また、mod_sslを使うためには、OpenSSLが必要です。
つまり、関連ソフトとしてmod_sslとOpenSSLをインストールします。
◆mod_sslのインストール
dnfでインストールを行うと依存関係でOpenSSLも一緒にインストールされますのでdnfでインストールします。
[root@vm-system]#dnf -y install mod_ssl
もし、OpenSSLも一緒にインストールされなかった場合は単独でインストールします。
[root@vm-system]#dnf -y install openssl

apache の設定

◆ssl.confの設定
[root@vm-system]#vi /etc/httpd/conf.d/ssl.conf
*** 途中略 ***

##
## SSL Virtual Host Context
##

<VirtualHost _default_:443>

# General setup for the virtual host, inherited from global configuration
* コメント解除&書き換え ホームページデータ保存先
DocumentRoot "/var/www/html"
* コメント解除&書き換え ホスト名
ServerName vm-system.net:443

*** 途中略 ***

# Point SSLCertificateFile at a PEM encoded certificate.If
# the certificate is encrypted, then you will be prompted for a
# pass phrase. Note that restarting httpd will prompt again. Keep
# in mind that if you have both an RSA and a DSA certificate you
# can configure both in parallel (to also allow the use of DSA
# ciphers, etc.)
# Some ECC cipher suites (http://www.ietf.org/rfc/rfc4492.txt)
# require an ECC certificate which can also be configured in
# parallel.
* 書き換え 証明書の保存先
SSLCertificateFile /etc/letsencrypt/live/www.vm-system.net/cert.pem

# Server Private Key:
# If the key is not combined with the certificate, use this
# directive to point at the key file. Keep in mind that if
# you've both a RSA and a DSA private key you can configure
# both in parallel (to also allow the use of DSA ciphers, etc.)
# ECC keys, when in use, can also be configured in parallel
* 書き換え 秘密鍵の保存先
SSLCertificateKeyFile /etc/letsencrypt/live/www.vm-system.net/privkey.pem

# Server Certificate Chain:
# Point SSLCertificateChainFile at a file containing the
# concatenation of PEM encoded CA certificates which form the
# certificate chain for the server certificate. Alternatively
# the referenced file can be the same as SSLCertificateFile
# when the CA certificates are directly appended to the server
# certificate for convenience.
* 書き換え 中間証明書の保存先
SSLCertificateChainFile /etc/letsencrypt/live/www.vm-system.net/chain.pem

*** 以下略 ***

◆設定のチェック

[root@vm-system]#httpd -t
* Syntax OK になっていればOK
Syntax OK

◆apacheの再起動

[root@vm-system]#systemctl restart httpd
◆ポートの開放確認
SSLは443番ポートを使用するのでポート開放確認をします。
[root@vm-system]#netstat -nat

*** 途中略 ***

tcp00 0 :::443:::*LISTEN

*** 以下略 ***

表示確認

https://ドメインにアクセスして正常に表示される事を確認します。
SSL通信はいまや必須と言っても過言ではありません。皆さんも是非挑戦してみて下さい。