mod_sslのインストール

# yum install mod_ssl

秘密鍵の作成

AES(Advanced Encryption Standard)で128ビットの鍵を生成する。

# cd /etc/httpd/conf
# openssl genrsa -aes128 2048 > server.key

パスフレーズを聞かれるので適当に入力 password

公開鍵の作成

生成した秘密鍵をもとに作成
Common Nameに *.example.com を指定すればワイルドカードになる?

# openssl req -new -key server.key > server.csr
Enter pass phrase for server.key:パスフレーズを入力
Country Name (2 letter code) [XX]:JP
State or Province Name (full name) []:Tokyo
Locality Name (eg, city) [Default City]:Shinjyuku-ku
Organization Name (eg, company) [Default Company Ltd]:self
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:example.com
Email Address []:test@example.com

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

デジタル証明書の作成

100年有効な証明書を作成します。

# openssl x509 -in server.csr -days 36500 -req -signkey server.key > server.crt
Signature ok
subject=/C=JP/ST=Tokyo/L=Shinjyuku-ku/O=self/CN=example.com/emailAddress=test@example.com
Getting Private key
Enter pass phrase for server.key:パスフレーズ

Apacheの設定

書き換えた部分のみ記載

# vi /etc/httpd/conf.d/ssl.conf
DocumentRoot "/home/example/public_html"
ServerName example.com:443
SSLCertificateFile /etc/httpd/conf/server.crt
SSLCertificateKeyFile /etc/httpd/conf/server.key

Apache起動時のパスフレーズ解除

このままではApacheの起動のたびにパスフレーズの入力が必要になります。
以下でパスフレーズの入力が不要になります。
実行する前にバックアップを取っておきましょう。

# cp server.key server.key.org
# openssl rsa -in server.key -out server.key

以下は昔書いた内容です。


前提

Apacheは --enable-ssl を付けてインストールしている必要があります。

準備

# cd /usr/local/apache2/
# mkdir -p ssl/{work,key,cert}
# chmod 700 ssl/key
# DOMAIN_NAME='example.localhost'
# cd ssl
# RAND_VALUE=`vmstat | tail -n 1`
# echo $RAND_VALUE | openssl md5 > work/${DOMAIN_NAME}.rand
# chmod 700 work/${DOMAIN_NAME}.rand

秘密鍵の生成

# openssl genrsa -rand work/${DOMAIN_NAME}.rand -des3 2048 > key/${DOMAIN_NAME}_key.pem

任意に決めたパスフレーズを入力します。
パスフレーズは忘れてはいけません。

# chmod 700 key/${DOMAIN_NAME}_key.pem

CSRの生成

# openssl req -new -key key/${DOMAIN_NAME}_key.pem -out work/${DOMAIN_NAME}_csr.pem

さきほどのパスフレーズを入力します。

入力例

Country Name (2 letter code) [XX]:JP
State or Province Name (full name) []:Tokyo
Locality Name (eg, city) [Default City]:Shibuya
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:system1
Common Name (eg, your name or your server's hostname) []:example.localhost
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
# chmod 700 work/${DOMAIN_NAME}_csr.pem

証明書の生成

例では10年間有効。

# openssl req -x509 -in work/${DOMAIN_NAME}_csr.pem -key key/${DOMAIN_NAME}_key.pem -out cert/${DOMAIN_NAME}_crt.self.pem -days 3650

さきほどのパスフレーズを入力します。

# chmod 700 cert/${DOMAIN_NAME}_crt.self.pem

パスフレーズの削除

このままだとApache起動時にパスフレーズの入力を求められるので、秘密鍵からパスフレーズを削除します。

# openssl rsa -in key/${DOMAIN_NAME}_key.pem -out key/${DOMAIN_NAME}_key.nopass.pem

さきほどのパスフレーズを入力します。

Apacheの設定変更

# vi /usr/local/apache2/conf/httpd.conf
Listen 443
LoadModule ssl_module modules/mod_ssl.so
# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile "/usr/local/apache2/ssl/cert/example.localhost_crt.self.pem"
    SSLCertificateKeyFile "/usr/local/apache2/ssl/key/example.localhost_key.nopass.pem"
    
    ServerAdmin test@example.com
    DocumentRoot "/home/example/public_html"
    ServerName example.localhost
    ErrorLog "logs/error_log"
    CustomLog "logs/access_log" combined
</VirtualHost>
# /etc/rc.d/init.d/httpd stop
# /etc/rc.d/init.d/httpd start

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS