Apache 2.4.9 構築 7 - SSL/TLS暗号化通信

Contents


通信を暗号化してセキュアな環境でサイトを閲覧できるようにしたいと思います。


モジュールの確認

拡張モジュールmod_sslをインストールする必要があります。Apacheをソースでインストールしている場合はインストール時に同時に組み込む必要があります。

拡張モジュールmod_sslが有効になっていることを確認します。

# /usr/local/httpd-2.4.9/bin/httpd -M | grep ssl
ssl_module (shared)

上記でモジュールが表示されない場合はモジュールが有効になっていない場合があるので、httpd.confをで確認して有効にします。

# vi /usr/local/httpd-2.4.9/conf/httpd.conf

#LoadModule ssl_module modules/mod_ssl.so
LoadModule ssl_module modules/mod_ssl.so

 

秘密鍵/公開鍵/サーバ証明書

HTTPSでは秘密鍵と公開鍵、サーバ証明書が必要であり、以下のファイルを用意します。

server.key 秘密鍵
server.csr 公開鍵とサーバ証明書を発行するために必要な情報
server.crt サーバ証明書

デフォルトの配置先ディレクトリで鍵を作成します。

# cd /usr/local/httpd-2.4.9/conf

 

server.keyの作成

# openssl genrsa -des3 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
......+++
.....................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:(パスフレーズ入力)
Verifying - Enter pass phrase for server.key:(パスフレーズ再入力)

 

server.csrの作成

# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key:(パスフレーズ入力)
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) []:(都道府県名)
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) []:(FQDNなどサーバ固有の名称)
Email Address []:(メールアドレス)

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

 

server.crtの作成(自己証明書)

# openssl x509 -in server.csr -days 365 -req -signkey server.key -out server.crt
Signature ok
subject=(省略)
Getting Private key
Enter pass phrase for server.key:(パスフレーズ入力)

 

設定ファイル

httpd.confを見ると以下の記述があり、#Include conf/extra/httpd-ssl.conf行のコメントを外してデフォルトで用意されている設定ファイルhttpd-ssl.confを使用することができます。

# Secure (SSL/TLS) connections
#Include conf/extra/httpd-ssl.conf
#
# Note: The following must must be present to support
#       starting without SSL on platforms with no /dev/random equivalent
#       but a statically compiled-in mod_ssl.
#
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

今回は#Include conf/extra/httpd-ssl.conf行はそのままにして、httpd-ssl.confを別途用意した外部ファイル取り込み用のディレクトリにコピーして修正します。

# cp /usr/local/httpd-2.4.9/conf/extra/httpd-ssl.conf /usr/local/httpd-2.4.9/conf.d/ssl.conf

# vi /usr/local/httpd-2.4.9/conf/httpd.conf

#追加
Include conf.d/ssl.conf

Apache2.4.9のhttpd-ssl.confでデフォルトで有効になっている内容。(抜粋)

# vi /usr/local/httpd-2.4.9/conf.d/ssl.conf

Listen 443

SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5

SSLSessionCache        "shmcb:/usr/local/httpd-2.4.9/logs/ssl_scache(512000)"
SSLSessionCacheTimeout  300

<VirtualHost _default_:443>

#   General setup for the virtual host
DocumentRoot "/usr/local/httpd-2.4.9/htdocs"
ServerName www.example.com:443
ServerAdmin you@example.com
ErrorLog "/usr/local/httpd-2.4.9/logs/error_log"
TransferLog "/usr/local/httpd-2.4.9/logs/access_log"

#   SSL Engine Switch:
#   Enable/Disable SSL for this virtual host.
SSLEngine on

SSLCertificateFile "/usr/local/httpd-2.4.9/conf/server.crt"

SSLCertificateKeyFile "/usr/local/httpd-2.4.9/conf/server.key"

<FilesMatch "\.(cgi|shtml|phtml|php)$">
    SSLOptions +StdEnvVars
</FilesMatch>
<Directory "/usr/local/httpd-2.4.9/cgi-bin">
    SSLOptions +StdEnvVars
</Directory>

BrowserMatch "MSIE [2-5]" \
         nokeepalive ssl-unclean-shutdown \
         downgrade-1.0 force-response-1.0

CustomLog "/usr/local/httpd-2.4.9/logs/ssl_request_log" \
          "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

</VirtualHost>

必要に応じて上記の内容を修正します。

 

構文チェックとApache再起動

# /etc/init.d/httpd configtest
Syntax OK
# /etc/init.d/httpd restart
(省略)
Enter pass phrase:(パスフレーズ入力)

OK: Pass Phrase Dialog successful.
                                                           [  OK  ]

私の環境ではhttpd-ssl.conf(ssl.conf)をデフォルトでIncludeしてApache再起動するとエラーがでました。

# /etc/init.d/httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: AH00526: Syntax error on line 73 of /usr/local/httpd-2.4.9/conf.d/ssl.conf:
SSLSessionCache: 'shmcb' session cache not supported (known names: ). Maybe you need to load the appropriate socache module (mod_socache_shmcb?).

httpd-ssl.conf(ssl.conf)を見るとキャッシュ(SSLSessionCache)を使うようになっているので、エラーにも出ているmod_socache_shmcbモジュールを有効にします。

# vi /usr/local/httpd-2.4.9/conf/httpd.conf

LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

 

Apache起動時のパスフレーズ入力を省略

Apache起動時にパスフレーズの入力を求められるので解除します。

# cd /usr/local/httpd-2.4.9/conf

# cp -p  server.key server.key.org
# openssl rsa -in server.key -out server.key
Enter pass phrase for server.key:(パスフレーズ)入力
writing RSA key

Apache再起動してパスフレーズ入力を求められないことを確認します。

 

最後に

ブラウザでHTTPSアクセス(https://~)可能なことを確認します。自己証明書なので警告がでますがそのまま接続許可すればHTTPSで通信がされます。

参考サイト:
http://www.nina.jp/server/slackware/httpd/ssl.conf.html
https://jp.globalsign.com/support/csr/04.html?service=ssl

参考