更安全的的连线 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


首页