环境介绍:
1.RedHat 6.3 X64 English
2.Apache 2.4 with openssl
花了差不多2个小时才搞定。具体步骤如下:
- 从www.apachelounge.com/download/下载包含openssl的软件包。我测试时使用包含openssl 0.98的那个版本。
- 下载后,解压缩,查看文件中的Readme文件。
- 下载Microsoft Visual C++ 2010 SP1并安装
- 拷贝Apache文件到C盘根目录
- 根据网上找到的资料进配置。看下面的配置过程。
1,编辑c:\apache24\conf\httpd.conf文件
中去掉下面两行之前的#符号
LoadModule ssl_module modules/mod_ssl.so
Include conf/extra/httpd-ssl.conf
2, 创建新的证书。
这个文件需要我们用openssl这个程序来生成的。生成这个 server.crt文件需要2个文件,一个是openssl.exe 另一个是openssl.cnf。
一般来说openssl.exe 在你Apache的安装路径下的bin文件夹里面,openssl.cnf在Apache安装路径下的conf文件里面。找不到?如果找不到就用 windows的查找搜索一下了。这个时候你需要将openssl.exe和openssl.cnf放在同一个文件夹里面。一般的需要将找到的openssl.cnf文件拷贝到bin文件里面。
用openssl生成KEY
生成自签名文件
首先生成csr和pem文件。
openssl req -config openssl.cnf -new -out server.csr -keyout server.pem
运行结果如下:
下载 (210.54 KB)
2009-10-30 08:15
其中有几个地方需要注意
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
用于输入和重复输入密码的,密码长度不能小于4位
Common Name (eg, YOUR name) []:localhost
用于输入客户端能够访问SSL的地址:这里SSL服务器是在本地运行的,因此你可以输入localhost。其余的都可以跳过。
An optional company name []:留空,如果输入将会报错。
其他的提示输入部分,可以输入也可以留空,最重要的就是Common Name (eg, YOUR name) []:这个变量,关系到你的SSL服务能不能够运行。
为Apache创建没有密码保护的key
下面需要为Apache创建一个没有密码保护的key了。
openssl rsa -in server.pem -out server.key
运行结果如下
下载 (54.77 KB)
2009-10-30 08:18
在这里你需要输入密码,这个密码是你在上一步输入过的。如果没有错误,你将会看到writing RSA key这句话后面没有任何内容和错误提示。
创建X.509证书
现在需要为Apache创建X.509证书了。
openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 365
结果如下:
下载 (79.91 KB)
2009-10-30 08:25
如果没有错误,将会看到签名已经成功,后面有签名的详细信息和内容。
现在你再看看opensel.exe所在的文件夹下面是不是多了4个文件了,分别 是:server.pem,server.key,server.csr,server.crt
下载 (62.49 KB)
2009-10-30 08:25
其实我们并不需要所有的这4个文件,我一向喜欢偷懒,就把这4个文件直接拷贝到证书保存的文件目录下面保存。
4,重新启动错误
如果重新启动没有错误,但是还是不能访问。
这时候你需要检查日志文件,有一种可能是443端口的冲突,如果你的机器已经安装了IIS,那需要将IIS的web服务,FTP,SMTP服务全部关掉再 重新启动。
(skype也会占用433端口)
如果既没有日志输出,也不能访问,那是因为配置文件没有被正确的载入,这时候你需要先在httpd.conf文件里面注释掉这句话Include conf/extra/httpd-ssl.conf,然后重新启动Apache的服务。等服务成功启动以后,再打开这句话,再重新启动服务。这时候你就 能看到日志的输出了。否则不管你重新启动多少次,Apache还是只会载入老的httpd-ssl.conf文件,不会重新读取httpd- ssl.conf的。
如果在日志里面有警告
[Fri Aug 15 16:15:10 2008] [warn] RSA server certificate CommonName (CN) `localhost' does NOT match server name!?
[Fri Aug 15 16:15:10 2008] [notice] Child 3664: Released the start mutex
[Fri Aug 15 16:15:10 2008] [warn] RSA server certificate CommonName (CN) `localhost' does NOT match server name!?
那是因为你输入的SSL服务为localhost但是你Apache在安装的时候服务却不是localhost。这时候你需要改几个地方。
httpd-ssl.conf文件中的
下载 (80.2 KB)
2009-10-30 08:27
保存上述两个配置文件后重新启动Apache服务器,如果没有日志输出,则需要先在httpd.conf文件里面注释掉这句话Include conf/extra/httpd-ssl.conf,然后重新启动Apache的服务。等服务成功启动以后,再打开这句话,再重新启动服务。
在输出的日志里面应该不会包含有任何的警告信息了。
下载 (317.93 KB)
2009-10-30 08:29
我当时配置完ssl后启动Apache,在SSL日志中,报错如下:
[Mon Apr 01 16:32:08.583752 2013] [mpm_winnt:notice] [pid 4212:tid 396] AH00456: Server built: Feb 23 2013 13:07:34
[Mon Apr 01 16:32:08.583752 2013] [core:notice] [pid 4212:tid 396] AH00094: Command line: 'C:\\Apache24\\bin\\httpd.exe -d C:/Apache24'
[Mon Apr 01 16:32:08.598754 2013] [mpm_winnt:notice] [pid 4212:tid 396] AH00418: Parent: Created child process 4168
[Mon Apr 01 16:32:09.957889 2013] [mpm_winnt:notice] [pid 6036:tid 336] AH00364: Child: All worker threads have exited.
[Mon Apr 01 16:32:10.192913 2013] [ssl:warn] [pid 4168:tid 336] AH01873: Init: Session Cache is not configured [hint: SSLSessionCache]
[Mon Apr 01 16:32:10.195913 2013] [mpm_winnt:notice] [pid 4168:tid 336] AH00354: Child: Starting 64 worker threads.16:35 2013/4/1
[Mon Apr 01 16:32:26.773571 2013] [mpm_winnt:notice] [pid 4212:tid 396] AH00424: Parent: Received restart signal -- Restarting the server.
AH00526: Syntax error on line 76 of C:/Apache24/conf/extra/httpd-ssl.conf:
SSLSessionCache: 'shmcb' session cache not supported (known names: ). Maybe you need to load the appropriate socache module (mod_socache_shmcb?).
[Mon Apr 01 16:32:28.780772 2013] [mpm_winnt:notice] [pid 4168:tid 336] AH00364: Child: All worker threads have exited.
在网上找了一圈,都没有结果。我注意到上面的一个错误信息是:
AH00526: Syntax error on line 76 of C:/Apache24/conf/extra/httpd-ssl.conf:
在C:\Apache24\conf\extra\httpd-ssl.conf中找到了配置信息:
#SSLSessionCache "dbm:c:/Apache24/logs/ssl_scache"
SSLSessionCache "shmcb:c:/Apache24/logs/ssl_scache(512000)"
SSLSessionCacheTimeout 300
看来报错和这个有关。我抱着尝试的心里,把上面的内容都注销掉,再尝试重新启动。可以启动了。太好了。但报错如下:
[Mon Apr 01 16:55:44.014281 2013] [core:error] [pid 5184:tid 396] (OS 11001)不知道这样的主机。 : AH00547: Could not resolve host name test.abc.com -- ignoring!
这个我知道,在创建证书和httpd-conf,httpd-ssl.conf文件中,我都是用test.abc.com。但没有dns进行解析。导致Apache解析失败。那就在本地hosts文件添加以下吧。
在hosts中添加如下记录:
127.0.0.1 test.abc.com
再访问试试,一切ok。但https只能使用https://127.0.0.1 or https://test.abc.com.这点要注意。