apacheにSSLを導入する方法

最近、ApacheBasic認証を使いたいページをSSL化することがあったのでメモしておきます。 Basic認証でhttpだと丸見えですからね・・・。今回はオレオレ証明書で実装します。サービス用途にはしっかりとした認証局に署名してもらうようにしてください。

事前準備

  • opensslをインストールする

  • mod_sslをインストールする

  • openssl.cnfをベースにopenssl-ca.cnf、openssl-server.cnfを作成

この3つだけは事前に実施しておいてください。

証明書作成

認証局のCA証明書を作成
$ sudo CADAYS="-days 3650" SSLEAY_CONFIG="-config /etc/pki/tls/openssl-ca.cnf" /etc/pki/tls/misc/CA -newca

以下、パスフレーズの入力や証明書のSubjectの入力があります。 証明書のSubjectはあらかじめ/etc/httpd/conf.d/ssl.confに設定を入れておけば2度手間はなくなります。今回は都度入れる事としています。

CA certificate filename (or enter to create) ←EnterでOK
Making CA certificate ...
Generating a 2048 bit RSA private key
...................................................+++
........................+++
writing new private key to '/etc/pki/CA/private/./cakey.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) []:Tokyo
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 []:

(略)

Write out database with 1 new entries
Data Base Updated

CSR作成

続いてCSRの作成です。 こちらもパスフレーズや各種パラメータを入力する箇所があります。

sudo DAYS="-days 3650" SSLEAY_CONFIG="-config /etc/pki/tls/openssl-server.cnf" /etc/pki/tls/misc/CA -newreq
Generating a 2048 bit RSA private key
................................................+++
..............+++
writing new private key to 'newkey.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) []:Tokyo
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 []:

(略)

Request is in newreq.pem, private key is in newkey.pem

Tips:証明書の作成に失敗した場合

証明書の作成に失敗した場合、同じコマンドを叩いても何も出力がなく再作成ができません。 その場合は、/etc/pki/CA/を消す必要があるので注意してください。

$ sudo CADAYS="-days 3650" SSLEAY_CONFIG="-config /etc/pki/tls/openssl-ca.cnf" /etc/pki/tls/misc/CA -newca
$

$ ls /etc/pki/CA/
certs  crl  index.txt  newcerts  private
$ sudo cp -r /etc/pki/CA/ /etc/pki/CA.org/
$ ls -ld /etc/pki/CA
drwxr-xr-x. 6 root root 4096  6月 16 00:00 2014 /etc/pki/CA
$ ls -ld /etc/pki/CA.org
drwxr-xr-x 6 root root 4096  6月 16 00:00 2014 /etc/pki/CA.org
$ sudo rm -rf /etc/pki/CA
$ ls -ld /etc/pki/CA
ls: cannot access /etc/pki/CA: そのようなファイルやディレクトリはありません

認証局で署名する

先ほど作成したCSRを元に自身の認証局で署名します。

$ sudo SSLEAY_CONFIG="-config /etc/pki/tls/openssl-server.cnf" /etc/pki/tls/misc/CA -sign
Using configuration from /etc/pki/tls/openssl-server.cnf
Enter pass phrase for /etc/pki/CA/private/cakey.pem:
Check that the request matches the signature
Signature ok
Certificate Details:

(略)

Signed certi
ficate is in newcert.pem

※(2014/07/09追記)TXT_DB errorが出た場合は/etc/pki/CA/index.txtを消すかrevokeしてやり直してください。

サーバ証明書と鍵ファイルを生成

まず鍵ファイルを作成します。今回、2048bitで作成しております。

$ pwd
/etc/pki/tls/certs
$ sudo make server.key
umask 77 ; \
        /usr/bin/openssl genrsa -aes128 2048 > server.key
Generating RSA private key, 2048 bit long modulus
.....................................................+++
..........................................................+++
e is 65537 (0x10001)
Enter pass phrase:
Verifying - Enter pass phrase:

続いてサーバ証明書を作成します。

$ sudo make server.crt
umask 77 ; \
        /usr/bin/openssl req -utf8 -new -key server.key -x509 -days 365 -out server.crt -set_serial 0
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) []:Tokyo
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 []:

鍵ファイルとサーバ証明書の設定

先ほど作成した鍵ファイルとサーバ証明書を指定します。 その後、httpdを再起動します。

$sudo service httpd restart

パスが間違っているとhttpdが起動しませんので注意してください。

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

#   Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate.  If
# the certificate is encrypted, then you will be prompted for a
# pass phrase.  Note that a kill -HUP will prompt again.  A new
# certificate can be generated using the genkey(1) command.
SSLCertificateFile /etc/pki/tls/certs/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.)
SSLCertificateKeyFile /etc/pki/tls/certs/server.key

おまけ httpアクセスをhttpsへリダイレクトする方法

今回、/var/www/html/graph/配下にBasic認証をかけてSSLアクセスのみ許可するよう実装を考えていたため以下も合わせて実施しました。logはレベル0で新たにrewite_logに書き出すようにしてます。

LoadModule rewrite_module modules/mod_rewrite.so

<IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteLog "/var/log/rewrite_log"
      RewriteLogLevel 0

      RewriteCond %{SERVER_PORT} !^443$
      RewriteRule ^/graph/(.*)?$ https://%{HTTP_HOST}/graph/$1 [L,R]
</IfModule>

どうでしょうか?できましたかね。割と簡単ですが、だからこそサッとできないといけない実装ですね。また細かいtipsがあれば書いておきたいと思います。

できるPRO Apache Webサーバー 改訂版 Version 2.4/2.2/2.0対応 (できるPROシリーズ)

できるPRO Apache Webサーバー 改訂版 Version 2.4/2.2/2.0対応 (できるPROシリーズ)

サーバ構築の実際がわかる Apache[実践]運用/管理 (Software Design plus)

サーバ構築の実際がわかる Apache[実践]運用/管理 (Software Design plus)

シェアして頂けると嬉しいです。

もし参考になったよという方がいれば是非お願いします。
モチベーション維持の観点で非常に励みになります。

このエントリーをはてなブックマークに追加