玄箱HG(lenny) Apache2 ssl化

前提条件

  • opensslはインストール済み
  • apache2で既にサイトが稼働済み
  • su 実行済み

opensslの設定

cd /etc/ssl
ls -l
    合計 28
    drwxr-xr-x 2 root root     12288 2011-11-16 00:11 certs
    -rw-r--r-- 1 root root      9374 2010-12-06 00:23 openssl.cnf
    drwx--x--- 2 root ssl-cert  4096 2011-03-19 20:58 private
cp -p openssl.cnf openssl.cnf.org
vi openssl.cnf

編集内容は以下

diff ./openssl.cnf.org openssl.cnf
    172c172
    < # nsCertType			= server
    ---
    > nsCertType			= server
    241c241
    < # nsCertType = sslCA, emailCA
    ---
    > nsCertType = sslCA, emailCA

証明書の作成

場所はどこでもよいと思われる。

cd /usr/lib/ssl/misc
ls -l
    合計 36
    -rwxr-xr-x 1 root root 5875 2011-11-07 06:50 CA.pl
    -rwxr-xr-x 1 root root 5175 2011-11-07 06:50 CA.sh
    -rwxr-xr-x 1 root root  119 2011-11-07 06:50 c_hash
    -rwxr-xr-x 1 root root  152 2011-11-07 06:50 c_info
    -rwxr-xr-x 1 root root  112 2011-11-07 06:50 c_issuer
    -rwxr-xr-x 1 root root  110 2011-11-07 06:50 c_name
    drwxr-xr-x 6 root root 4096 2012-05-26 15:00 demoCA
mkdir demoCA.tmp
cd demoCA.tmp

秘密鍵の作成

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

パスフレーズの削除(apache再起動などしたら、パスフレーズを要求される為)

openssl rsa -in server.key -out server.key

公開鍵の作成

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) [AU]:JP
    State or Province Name (full name) [Some-State]:県名
    Locality Name (eg, city) []:市名
    Organization Name (eg, company) [Internet Widgits Pty Ltd]:
    Organizational Unit Name (eg, section) []:
    Common Name (eg, YOUR name) []:サーバ名
    Email Address []:メールアドレス

    Please enter the following 'extra' attributes
    to be sent with your certificate request
    A challenge password []:
    An optional company name []:
ls -l
    合計 8
    -rw-r--r-- 1 root root 700 2012-05-29 01:09 server.csr
    -rw-r--r-- 1 root root 963 2012-05-29 01:04 server.key

署名付証明書作成

openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
    Signature ok
    subject=/C=JP/ST=県名/L=市名/O=Internet Widgits Pty Ltd/CN=サーバ名/emailAddress=メールアドレス
    Getting Private key
    Enter pass phrase for server.key:
ls -l
    合計 12
    -rw-r--r-- 1 root root 952 2012-05-29 01:11 server.crt
    -rw-r--r-- 1 root root 700 2012-05-29 01:09 server.csr
    -rw-r--r-- 1 root root 963 2012-05-29 01:04 server.key

証明書の配置

mkdir /etc/apache2/ssl
mkdir /etc/apache2/ssl/private
cd /usr/lib/ssl/misc/demoCA.tmp
cp -fpv ./server.crt /etc/apache2/ssl
cp -fpv ./server.key /etc/apache2/ssl/private

SSL接続用バーチャルホスト設定

cd /etc/apache2/sites-availavle
cp -p default-ssl 任意のファイル名
vi 任意のファイル名
    編集内容は以下
    diff default-ssl 任意のファイル名
        →  編集した箇所は、サイトの設定(通常のバーチャルホスト時に設定する項目)+以下
        51,52c53,54
        < 	SSLCertificateFile    /etc/ssl/certs/ssl-cert-snakeoil.pem
        < 	SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
        ---
        > 	SSLCertificateFile    /etc/apache2/ssl/server.crt
        > 	SSLCertificateKeyFile /etc/apache2/ssl/private/server.key

sslモジュールの有効化

a2enmod ssl
    Enabling module ssl.
    See /usr/share/doc/apache2.2-common/README.Debian.gz on how to configure SSL and create self-signed certificates.
    Run '/etc/init.d/apache2 restart' to activate new configuration!

作成したSSL用バーチャルホスト設定を有効化

a2ensite 上記で設定した任意のファイル名
    Enabling site ssl.
    See /usr/share/doc/apache2.2-common/README.Debian.gz on how to configure SSL and create self-signed certificates.
    Run '/etc/init.d/apache2 reload' to activate new configuration!

Apache再起動

/etc/init.d/apache2 restart

外部より、https://~でアクセスし確認!