现在的位置: 首页 > 综合 > 正文

原文转载:Apache2中的SSL设定

2013年10月07日 ⁄ 综合 ⁄ 共 7123字 ⁄ 字号 评论关闭

还没自己弄过HTTPS的设置.今天做一个HTPPS测试环境,在网上看了一下.

CSDN:http://blog.csdn.net/cangzhubai/archive/2010/01/19/5214749.aspx写的也不错.

 

最好看到一篇日文的设置方法,觉得比较实用.把原文贴过来了.谢谢原作者!!!

本文来自:http://kajuhome.com/apache_ssl.shtml

 

■ sslとは・・・(HTTPS : Hypertext Transfer Protocol Security)暗号化通信

SSL(Secure Sockets Layer)は簡単に言うと、データを暗号化してやり取りするやり方の決まりです。
SSLを使用すると送信するデータが暗号化されるので、プライバシーに関わる情報を第三者に見られずにやり取りすることができます。
ApacheでSSLを利用するには、モジュールを組み込む必要があります。
このモジュールを組み込み、Apacheでデータの暗号化を行い通信する構築方法を紹介します。


■ sslモジュールのインストール

# yum -y install mod_ssl


■ CA用秘密鍵(ca.key)の作成

乱数データ(rand.dat)を使用してCA用の鍵を作成



# openssl genrsa -des3 -out /etc/httpd/conf/ca.key -rand rand.dat 1024


0 semi-random bytes loaded

Generating RSA private key, 1024 bit long modulus

......++++++

....................++++++

e is 65537 (0x10001)

ca用のパスワードを入力


Enter pass phrase for /etc/httpd/conf/ca.key:

確認の為、上と同じパスワードを入力


Verifying - Enter pass phrase for /etc/httpd/conf/ca.key:


■ CA用証明書(ca.crt)の作成

CA用証明書を作成



# openssl req -new -x509 -days 365 -key /etc/httpd/conf/ca.key -out /etc/httpd/conf/ca.crt


CA用のパスフレーズを入力


Enter pass phrase for /etc/httpd/conf/ca.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) [GB]:JP



都道府県を入力


State or Province Name (full name) [Berkshire]:Chiba



市町村を入力


Locality Name (eg, city) [Newbury]:Yachiyo



組織名を入力


Organization Name (eg, company) [My Company Ltd]:Private_CA



組織内ユニット名を入力


Organizational Unit Name (eg, section) []:Admin



サーバ名(ホスト名)を入力


Common Name (eg, your name or your server's hostname) []:fedora.kajuhome.com



管理者メールアドレスを入力


Email Address []:webmaster@kajuhome.com


■ サーバ用秘密鍵(server.key)の作成

サーバ用秘密鍵の作成(鍵長1024ビット)



# openssl genrsa -des3 -out /etc/httpd/conf/server.key -rand rand.dat 1024


0 semi-random bytes loaded

Generating RSA private key, 1024 bit long modulus

..........++++++

.......++++++

e is 65537 (0x10001)

サーバ用のパスワードを入力


Enter pass phrase for /etc/httpd/conf/server.key:

確認の為、上と同じパスワードを入力


Verifying - Enter pass phrase for /etc/httpd/conf/server.key:


■ 署名要求書(server.csr)の作成

CAに送るデジタル証明書のリクエストファイル作成



# openssl req -new -key /etc/httpd/conf/server.key -out /etc/httpd/conf/server.csr


サーバ用のパスフレーズを入力


Enter pass phrase for /etc/httpd/conf/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) [GB]:JP



都道府県を入力


State or Province Name (full name) [Berkshire]:Chiba



市町村を入力


Locality Name (eg, city) [Newbury]:Yachiyo



組織名を入力


Organization Name (eg, company) [My Company Ltd]:HomeServer



組織内ユニット名を入力


Organizational Unit Name (eg, section) []:Self



アクセス名(http://<アクセス名>)を入力


Common Name (eg, your name or your server's hostname) []:kajuhome.com



管理者メールアドレスを入力


Email Address []:webmaster@kajuhome.com





Please enter the following 'extra' attributes

to be sent with your certificate request

何も入力しないでエンターキー押下


A challenge password []:

何も入力しないでエンターキー押下


An optional company name []:


■ サーバ用秘密鍵(server.key)からのパスフレーズ削除

オリジナルをバックアップ



# cp /etc/httpd/conf/server.key /etc/httpd/conf/server.key.bak




server.keyからのパスフレーズ削除


# openssl rsa -in /etc/httpd/conf/server.key.bak -out /etc/httpd/conf/server.key


サーバ用のパスフレーズを入力


Enter pass phrase for /etc/httpd/conf/server.key.bak:

writing RSA key


■ サーバ用証明書(server.crt)の作成


■ 事前準備

  • サーバ用証明書を作成するには、mod_ssl付属の"sign.sh"シェルスクリプトが必要になります。
    mod_ssl: The Apache
    Interface to OpenSSL

    よりmod_sslを取得し、対象のシェルスクリプトを取り出します。
mod_sslモジュールは日々バージョンアップしています。

最新版は『www.modssl.org
』で確認してください。





mod_sslを取得


# wget http://www.modssl.org/source/mod_ssl-2.8.28-1.3.37.tar.gz


--15:12:37-- http://www.modssl.org/source/mod_ssl-2.8.28-1.3.37.tar.gz

=> `mod_ssl-2.8.28-1.3.37.tar.gz'

www.modssl.org をDNSに問いあわせています... 195.30.6.168

www.modssl.org[195.30.6.168]:80 に接続しています... 接続しました。

HTTP による接続要求を送信しました、応答を待っています... 200 OK

長さ: 754,277 [application/x-tar]



100%[====================================>] 820,417 89.12K/s ETA 00:00



15:12:47 (80.68 KB/s) - `mod_ssl-2.8.28-1.3.37.tar.gz' を保存しました [820417/820417]





ダウンロードしたファイルを展開


# tar zxvf mod_ssl-2.8.28-1.3.37.tar.gz


mod_ssl-2.8.28-1.3.37/ANNOUNCE

mod_ssl-2.8.28-1.3.37/CHANGES





mod_ssl-2.8.28-1.3.37/pkg.sslsup/mkcert.sh

mod_ssl-2.8.28-1.3.37/pkg.sslsup/sslsup.patch


■ サーバ用証明書の作成

パスの移動



# cd /etc/httpd/conf/




上記で取得したmod_ssl付属のsign.shスクリプトでサーバ用デジタル証明書を作成


# /root/mod_ssl-2.8.28-1.3.37/pkg.contrib/sign.sh server.csr


CA signing: server.csr -> server.crt:

Using configuration from ca.config

CA用のパスフレーズを入力


Enter pass phrase for ./ca.key:

Check that the request matches the signature

Signature ok

The Subject's Distinguished Name is as follows

countryName :PRINTABLE:'JP'

stateOrProvinceName :PRINTABLE:'Chiba'

localityName :PRINTABLE:'Yachiyo'

organizationName :T61STRING:'HomeServer'

organizationalUnitName:PRINTABLE:'Self'

commonName :PRINTABLE:'kajuhome.com'

emailAddress :IA5STRING:'webmaster@kajuhome.com'

Certificate is to be certified until Oct 4 06:29:19 2005 GMT (365 days)

"y"を入力してエンターキー押下


Sign the certificate? [y/n]:y






"y"を入力してエンターキー押下


1 out of 1 certificate requests certified, commit? [y/n]y


Write out database with 1 new entries

Data Base Updated

CA verifying: server.crt <-> CA cert

server.crt: OK


■ ブラウザインポート用のバイナリDERフォーマット(ca.der)の作成

CA証明書(PEMフォーマット)からバイナリDERフォーマットで作成



# openssl x509 -inform pem -in /etc/httpd/conf/ca.crt -outform der -out /etc/httpd/conf/ca.der


■ 作成した各ファイルの所有権変更

所有権の変更



# chmod -R 400 /etc/httpd/conf/server.* /etc/httpd/conf/ca.*


■ ssl設定ファイルの変更

ssl設定ファイル変更



# 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 test

# certificate can be generated with `make certificate' under

# built time. Keep in mind that if you've both a RSA and a DSA

# certificate you can configure both in parallel (to also allow

# the use of DSA ciphers, etc.)

SSLCertificateFile /etc/httpd/conf/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/httpd/conf/server.key


■ apacheの再起動

apache(httpd)を再起動する



# /etc/rc.d/init.d/httpd restart


httpdを停止中: [ OK ]

httpd を起動中: [ OK ]


■ クライアントへのCA証明書インストール

クライアントより[https
://サーバのアドレス/]でアクセスすると
下記の画面が表示されます。

上記の「証明書の表示」をクリックすると下記の画面が表示されます。

  • Intenet Explorerでは、この方法では警告画面から証明書をインポートできません。

以下の手順を行ない、クライアントからダウンロード出来る様に追加します。

ca.derを公開ページのトップへコピーします



# cp /etc/httpd/conf/ca.der /var/www/html/




パーミッションの変更


# chmod 440 /var/www/html/ca.der




所有者・グループの変更


# chown apache:apache /var/www/html/ca.der

任意のホームページより、上記ca.derにリンクを貼り、ダウンロードできる様にします。
当サイトの証明書はここよりダウンロード
できます。

←こんな感じ
で・・・
(意図的にダウンロードできない様にしています。理由は更新履歴をご参照下さい。)

商用などで使用する場合は、きちんと証明
機関

に発行(有料)してもらってください。

  1. 証明書のダウンロード
    • 一度、適当なフォルダにダウンロードして、ダウンロードした「ca.der」を右クリックより証明書のインストールを選択します。
    • 下記画面で「保存」をクリックし、適当なフォルダにダウンロードします。

  2. 証明書のインポート
    • ダウンロードした「ca.der」を右クリックして、「証明書のインストール」を選択すると、画面が表示されるので「次へ」をクリックし
      ます。

    • 下記の様に選択し、「次へ」をクリックします。

    • 「完了」ボタンをクリックします

    • インポートの完了

  • これで、「https://サーバのアドレス/]でアクセスしても、警告ウィンドウが表示されなくなります。


■ 公開前の準備

ホームページを外部に公開するに当たって、ルータの設定が必要です。
ルータのポート開閉は、ご自分のルータ取扱説明書をご覧ください。

  • ルータの設定でポート番号443番(https: http protocol
    over TLS/SSL)

    を開けてください。
  • ポートの開閉状態は、ポート
    チェック【ポート開放確認】

    で確認できます。

    • 「ホスト名」に自分のホスト名またはグローバルIPを入力
    • 「ポート番号」に443を入力
    • 「ポートチェック」ボタンを押下
    • 「ホスト=xxxxxxxxxx
       ポート=443
       にアクセスできました。」と表示する事を確認します。

DNS名で接続するには事前にドメイン名の取得を行っている必要があります。(以下は代表的なものであり、また、当サイトが使用させて頂いておりま
す)

抱歉!评论已关闭.