更安全的的連線 Apache + SSL

當你設定好你的 Web 伺服器之後,應該就可以滿足大部份的需求了。但是為了安全考量,你可能會需要一些更安全的加密連線,比方說在傳送密碼驗證訊息時,你會希望中間的傳遞是加密的,這樣就算是被偷走中間的連線封包,看到的也只會是一連串的亂碼而已。

在這篇文章裡,會介紹如何產生 SSL 憑證,並且設定 Apache,該它能夠做連線加密,當然你必需準備好工具,以下是你必需要的環境:

當準備好工具之後,請依以下的過程,就可以輕易的產生出憑證了。

安裝 / 編譯 Apache 並啟用 SSL 功能:

要啟用 SSL 模組功能,主要是在編譯的時候加入 --enable-ssl 參數,而編譯 Apache 的部份請參考 LAMP - Linux + Apache + MySQL + PHP (new window),裡面有更詳細的說明。

安裝 / 編譯 OpenSSL

在此,我是使用 rpm 安裝的,而版本是 openssl-0.9.7a-42.2,你可以使用 rpm 或 apt-get 來安裝。當然,如果你的系統已經安裝 openssl 的話,那就可以省過這一步了。

查詢系統是否已安裝 OpenSSL

1
2
3
root # rpm -qa | grep openssl
openssl-0.9.7a-42.2
root #

如果什麼資訊都沒有的話,那就請你安裝一下吧:

1
root # rpm -ivh openssl-0.9.7a-42.2

1
root # apt-get install openssl

產生 SSL 憑證:

以 www.l-penguin.idv.tw 為例,使用以下命令可以產生 .key 和 .csr 檔:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
root # openssl genrsa -out www.l-penguin.idv.tw.key 1024
Generating RSA private key, 1024 bit long modulus
......++++++
........++++++
e is 65537 (0x10001)
root # 
root # openssl req -new -key www.l-penguin.idv.tw.key -out www.l-penguin.idv.tw.csr
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]:TW
State or Province Name (full name) [Berkshire]:Taipei County
Locality Name (eg, city) [Newbury]:Jhonghe City
Organization Name (eg, company) [My Company Ltd]:l-penguin
Organizational Unit Name (eg, section) []:l-penguin
Common Name (eg, your name or your server's hostname) []:www.l-penguin.idv.tw
Email Address []:steven@ms.ntub.edu.tw
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 
An optional company name []:

root #
root # ls -l
total 8
-rw-r--r-- 1 root root 745 Jun 23 00:13 www.l-penguin.idv.tw.csr
-rw-r--r-- 1 root root 887 Jun 23 00:09 www.l-penguin.idv.tw.key
root #

憑證簽署:

一般來說,要簽署憑證是要向專們授權憑證的組織來做認證,以證明你的憑證是有效的,不過這需要一筆支出才行,但如果你要自行簽署的話也是可以,只是要再讓使用者匯入你的憑證才行。以下我將示範如何自己簽署金鑰:

1
2
3
4
5
6
root # openssl x509 -req -days 365 -in www.l-penguin.idv.tw.csr -signkey 
www.l-penguin.idv.tw.key -out www.l-penguin.idv.tw.crt
Signature ok
subject=/C=TW/ST=Taipei County/L=Jhonghe City/O=l-penguin/OU=l-penguin/CN=www.l-penguin.idv.tw/emailAddress=steven@ms.ntub.edu.tw
Getting Private key
root #

完成之後,你應該就會產生三個檔案:

1
2
3
4
5
root # ls -l
-rw-r--r-- 1 root root 1025 Jun 23 00:19 www.l-penguin.idv.tw.crt
-rw-r--r-- 1 root root 745 Jun 23 00:13 www.l-penguin.idv.tw.csr
-rw-r--r-- 1 root root 887 Jun 23 00:09 www.l-penguin.idv.tw.key
root #

以上,就完成了憑證部份。

設定 Apache 啟用 SSL 憑證:

我現在把憑證放到 /usr/local/httpd/conf/CA 目錄裡,所以之後設定憑證的位置只要對應到正確的目錄就可以了。

1
2
3
4
5
6
7
8
9
10
11
root # ls -l
-rw-r--r-- 1 root root 1025 Jun 23 00:19 www.l-penguin.idv.tw.crt
-rw-r--r-- 1 root root 745 Jun 23 00:13 www.l-penguin.idv.tw.csr
-rw-r--r-- 1 root root 887 Jun 23 00:09 www.l-penguin.idv.tw.key
root #root # <b>vi /usr/local/httpd/conf/httpd.conf</b>
----------------------------------------------------------------------
# 加入以下兩行
SSLCertificateFile /usr/local/httpd/conf/CA/www.l-penguin.idv.tw.crt
SSLCertificateKeyFile /usr/local/httpd/conf/CA/www.l-penguin.idv.tw.key
----------------------------------------------------------------------
root #

進階設定:

一般來說,這個節次你可以跳過去,如果你要做更進階的設定的話,可以參考下設定。有時候,你會想把要加密碼的檔放到不同目錄以便管理或提高安全性,那麼接下來,你要為 SSL 設定一個 Virtual Host,主要目的是在於讓 Apache 知道這個聆聽 Port 443 的 SSL 加密服務要對那些文件傳輸時加密。你可以直接在 httpd.con 編輯。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
<IfDefine SSL>

#設定要 Listen 的 Port
Listen 443
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
SSLPassPhraseDialog builtin
SSLSessionCache dbm:/usr/local/httpd/logs/ssl_scache
SSLSessionCacheTimeout 300
SSLMutex file:/usr/local/httpd/logs/ssl_mutex

#新增一個 Virtual Host
<VirtualHost _default_:443>
DocumentRoot &quot;/data/web&quot;
ServerName www.l-penguin.idv.tw:443
ServerAdmin steven@ms.ntub.edu.tw
ErrorLog /usr/local/httpd/logs/error_log
TransferLog /usr/local/httpd/logs/access_log

SSLEngine on
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
<FilesMatch &quot;.(cgi|shtml|phtml|php3?)$&quot;>
     SSLOptions +StdEnvVars
</FilesMatch>
<Directory &quot;/usr/local/httpd/cgi-bin&quot;>
     SSLOptions +StdEnvVars
</Directory>
SetEnvIf User-Agent &quot;.*MSIE.*&quot; 
         nokeepalive ssl-unclean-shutdown 
         downgrade-1.0 force-response-1.0
CustomLog /usr/local/httpd/logs/ssl_request_log 
          &quot;%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x &quot;%r&quot; %b&quot;

#下面這兩行,主要就是設定私有和公有金鑰的地方。
SSLCertificateFile /usr/local/httpd/conf/CA/www.l-penguin.idv.tw.crt
SSLCertificateKeyFile /usr/local/httpd/conf/CA/www.l-penguin.idv.tw.key
</VirtualHost>
</IfDefine>

重新啟動 Apache

1
2
root # /usr/local/httpd/bin/apachectl stop
root # /usr/local/httpd/bin/apachectl startssl

查看 Apache 是否有監聽 Port 443

1
2
3
root # netstat -ntulp | grep 443
tcp 0 0 :::443 :::* LISTEN 15101/httpd
root #

以上設定,就完成了 Apache 的設定。

使用 Browser 連上加密的網頁:

請在網址列輸入 https 的網址就可以了,比方說我 ssl/ 是有加密的,只要輸入成 https://www.l-penguin.idv.tw/ssl/ 就可以了。

記得,因為你的憑證是自己簽署的,所以在連進網址是會有警告,這時只要接受憑證就可以了。

06/23/2006


首頁