0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Apache HTTP ServerでSSL通信する

Last updated at Posted at 2021-03-25

目的

linux環境に構築したApache HTTP Serverに対し、SSL通信を行います。
SSLで使用する証明書は自己証明書を作成して使用します。
Webサーバの構築手順はこちらをベースにします。

環境

Iaas:AWS EC2
OS:RHEL-7.6_HVM_GA-20190128-x86_64-0-Hourly2-GP2(コミュニティ AMI)
Apache:2.4.6

ApacheにSSLモジュールを追加する

1. SSLモジュールをインストール

yumコマンドでインストールします。
完了のメッセージが表示されれば成功です。

$ sudo yum -y install mod_ssl
・
・
インストール:
  mod_ssl.x86_64 1:2.4.6-97.el7_9                                                                                                                                                                              

完了しました!

2. インストールされたか確認

インストール済みのパッケージを検索し、SSLモジュールがあることを確認します。

$ sudo yum list installed | grep mod_ssl
mod_ssl.x86_64                     1:2.4.6-97.el7_9         @rhui-REGION-rhel-server-releases

SSL通信で使用する秘密鍵と証明書を作成する

1.秘密鍵とCSR(証明書発行要求)を作成

opensslコマンドで秘密鍵とCSR(証明書発行要求)を作成します。
コマンド実行後、パスフレーズや国名などの入力を求められますので、任意で値を入力していきます。
※今回は必須項目のみ指定しています

$ openssl req -new -out localhost.csr
Generating a 2048 bit RSA private key
.................+++
........................+++
writing new private key to 'privkey.pem'
[Enter PEM pass phrase:任意のパスフレーズを入力
[Verifying - Enter PEM pass phrase:
-----
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]:JP ←国名を指定します
[State or Province Name (full name) []:Kanagawa ←都道府県名を指定します
[Locality Name (eg, city) [Default City]:Kawasaki ←市区町村名を指定します
[Organization Name (eg, company) [Default Company Ltd]:SampleOrg ←組織名を指定します
[Organizational Unit Name (eg, section) []:System1 ←部門名を指定します
[Common Name (eg, your name or your server's hostname) []:sample.jp ←一般名称を指定します
[Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
[A challenge password []:
[An optional company name []:

lsコマンドで秘密鍵とCSR(証明書発行要求)を作成出来ていれば成功です。

$ls
localhost.csr  privkey.pem

2.作成した秘密鍵を使用し、自己署名した証明書を作成

先程作成した秘密鍵とCSRファイルを使用して自己署名した証明書を作成します。
※コマンドを実行するとパスフレーズが求められますので、秘密鍵とCSRファイルを作成したときに指定したパスフレーズを入力します。

$ openssl x509 -in localhost.csr -out localhost.crt -req -signkey privkey.pem -days 365
Signature ok
subject=/C=JP/ST=Kanagawa/L=Kawasaki/O=SampleOrg/OU=System1/CN=sample.jp
Getting Private key
Enter pass phrase for privkey.pem:指定した任意のパスフレーズ

作成した秘密鍵と証明書をApacheに設定する

SSLモジュールの設定ファイルとなるssl.confを確認します

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

SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key

今回、秘密鍵と証明書をssl.confのデフォルト指定と同じのファイル名にしているので、それぞれの配置先にファイルを移動します。

$ sudo mv privkey.pem /etc/pki/tls/private/localhost.key
$ sudo mv localhost.crt /etc/pki/tls/certs/localhost.crt

http通信を無効化する

SSL通信を使用するため、通常のhttp通信でアクセス出来ないようにhttpd.confのListenディレクティブをコメントアウトしておきます。

$ sudo vi /etc/httpd/conf/httpd.conf

# Listen 80

SELINUXの実行モードをpermissiveに変更する

実行モードがenforcingの場合、指定した証明書等に適切な権限設定をしないとApacheの起動でエラーになります。
そのため、SELinuxポリシーを強制せず、ログに記録するpermissiveに実行モードを変更する。

$ sudo vi /etc/selinux/config
SELINUX=permissive    

$ sudo shutdown -r now

Apacheを再起動する

コマンドを実行するとパスフレーズが求められますので、秘密鍵とCSRファイルを作成したときに指定したパスフレーズを入力します。

$ sudo systemctl restart httpd
Enter SSL pass phrase for ip-172-31-37-255.ap-northeast-1.compute.internal:443 (RSA) : 指定したパスフレーズ

$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2021-03-21 04:22:12 UTC; 7s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 4131 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─4131 /usr/sbin/httpd -DFOREGROUND
           ├─4137 /usr/sbin/httpd -DFOREGROUND
           ├─4138 /usr/sbin/httpd -DFOREGROUND
           ├─4139 /usr/sbin/httpd -DFOREGROUND
           ├─4140 /usr/sbin/httpd -DFOREGROUND
           └─4141 /usr/sbin/httpd -DFOREGROUND

 3月 21 04:22:10 ip-172-31-37-255.ap-northeast-1.compute.internal systemd[1]: Starting The Apache HTTP Server...
 3月 21 04:22:12 ip-172-31-37-255.ap-northeast-1.compute.internal systemd[1]: Started The Apache HTTP Server.

終わりに

以上でブラウザからhttpsプロトコルで通信が可能になり、http通信が不可になります。

参考文献

0
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?