Apacheでhttpサーバをhttpsサーバにする
はじめに
Apache でwebサーバを構築すると、何もしなければ「http 80/tcp」の通信になり、セキュリティ的に推奨されていないものになってしまう。
そこてで、今回はあらかじめ作成されている「httpサーバ」に「自己証明書」を作成し、「httpsサーバ」にする。
※「httpサーバ」は作成されているものとする。
https化に必要なモジュールをインストールする。
Apache で httpsサーバを構築するには、「mod_ssl」モジュールが必要になる。
[root@web01 ~]# dnf -y install mod_ssl
メタデータの期限切れの最終確認: 1:39:30 時間前の 2020年11月15日 10時57分53秒 に実施しました。
依存関係が解決しました。
======================================================================================================================
パッケージ アーキテクチャー バージョン リポジトリー サイズ
======================================================================================================================
インストール中:
mod_ssl x86_64 1:2.4.37-21.module_el8.2.0+494+1df74eae AppStream 132 k
完了しました!
サーバの秘密鍵を作る
秘密鍵を作成。
[root@web01 ~]# openssl genrsa -aes128 2048 > server.key
Generating RSA private key, 2048 bit long modulus (2 primes)
....+++++
.......................................................................+++++
e is 65537 (0x010001)
Enter pass phrase: #パスフレーズを入力
Verifying - Enter pass phrase: #パスフレーズを再入力
証明書署名要求(CSR)を作成する
基本的にすべての項目を[Enterキー]でスキップする。
[root@web01 ~]# openssl req -utf8 -new -key server.key -out server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
ーーーー
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
サーバ証明書を発行する
今回は、無料で証明書を発行したいため自分自身で証明書を作る。
先ほど作成した証明書署名要求「server.csr」をもとに、サーバ証明書「server.crt」を有効期限:10年間で作成する。
[root@web01 ~]# openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 3650
Signature ok
subject=C = XX, L = Default City, O = Default Company Ltd
Getting Private key
SSLモジュールの設定
証明書・鍵の移動
先ほど作成したサーバ証明書「server.crt」とサーバ秘密鍵「server.key」を管理しやすいように移動させる。
[root@web01 ~]# mkdir /etc/httpd/conf/ssl
[root@web01 ~]# mv server.key /etc/httpd/conf/ssl/
[root@web01 ~]# mv server.crt /etc/httpd/conf/ssl/
SSLモジュールの設定変更
証明書と鍵の場所を加える
[root@web01 ~]# vi /etc/httpd/conf.d/ssl.conf
# 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/pki/tls/certs/localhost.crt #コメントアウト
SSLCertificateFile /etc/httpd/conf/ssl/server.crt# 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/pki/tls/private/localhost.key #コメントアウト
SSLCertificateKeyFile /etc/httpd/conf/ssl/server.key