在设置Apache + SSL之前, 需要做:
1、安装Apache, 请参见: Windows环境下Apache的安装与虚拟目录的配置, 下载安装Apache时请下载带有ssl版本的Apache安装程序.
在进行下一步之前, 请确认Apache已经安装并可以正常工作. 并且ssl需要的文件在如下的位置:
[Apache安装目录]/modules/ mod_ssl.so
[Apache安装目录]/bin/ openssl.exe, libeay32.dll, ssleay32.dll
[Apache安装目录]/conf/ openssl.cnf
2、创建SSL证书
打开CMD,进入到Apache安装目录下的bin目录下
执行命令:
openssl genrsa 1024 >server.key
说明:这是用128位rsa算法生成密钥(RSA密钥对的默认长度是1024,取值是2的整数次方,并且密钥长度约长,安全性相对会高点),得到server.key文件。 > 是输出文件的标识符
这种生成方法生成的是没有密钥的私钥文件。当然,Apache提供了加入密钥(Password)的命令,就是加入参数-des3。
如果在执行这条命令时提示找不到/conf/openssl.cnf的话,就在执行这条命令前加一条命令为: set openssl_conf=../conf/openssl.cnf
等密钥server.key生产完毕后进行下一步操作。
3、生产为签署的server.csr
继续在bin目录下执行命令:
openssl req -new -config ../conf/openssl.cnf -key server.key >server.csr
(如果不加-config ../conf/openssl.cnf参数的话,常会报Unable to load config info from /usr/local/ssl/openssl.cnf)
然后就会要求输入一系列的参数:
Country Name (2 letter code) [AU]:CN ISO 国家代码(只支持两位字符)
State or Province Name (full name) [Some-State]:ZJ 所在省份
Locality Name (eg, city) []:HZ 所在城市 Organization Name (eg, company): 公司名称
Organizational Unit Name (eg, section) []: 组织名称
Common Name (eg, YOUR name) []: 申请证书的域名
Email Address []:admin@admin.com 管理员邮箱
Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: 交换密钥
An optional company name []:
注: Common Name 必须和 httpd.conf 中 server name 必须一致, 否则 apache 不能启动(启动 apache 时错误提示为: server RSA certificate CommonName (CN) `Kedou' does NOT match server name!? )
4、签署服务器证书文件 server.crt
继续在 bin 目录,执行命令行 命令:
openssl req -x509 -days 4000 -config ../conf/openssl.cnf -key server.key -in server.csr >server.crt
说明:这是用步骤2,3的的密钥和证书请求生成证书server.crt,-days参数指明证书有效期,单位为天,x509表示生成的为X.509证书。
以上签署证书仅仅做测试用,真正运行的时候,应该将CSR发送到一个CA返回真正的证书。网上有些文档描述生成证书文件的过程比较繁琐,就是因为 他们自己建立了一个CA中心,然后再签署server.csr
用openssl x509 -noout -text -in server.crt 可以查看证书的内容。证书实际上包含了Public Key
5、在bin 目录下,找到
server.crt
server.csr
server.key
三个文件,将此三个文件复制到Apache的conf目录下。
6、配置 httpd.conf.
在Apache的confextra目录下的 httpd_ssl.conf 文件是关于 ssl 的配置,是 httpd.conf 的一 部分。
在 httpd.conf 中, 找到下列行
LoadModule ssl_module modules/mod_ssl.so
Include conf/extra/httpd-ssl.conf
去掉前面的#
将ServerName 后面的80改为443.
保存httpd.conf.
7、在confextra目录下,编辑 httpd_ssl.conf
找到 <VirtualHost _default_:443>,修改以下内容
SSLEngine On
SSLCertificateFile "C:/Program Files/Apache Software Foundation/Apache2.2/conf/server.crt"(选择刚刚创建的证书目录)
SSLCertificateKeyFile "C:/Program Files/Apache Software Foundation/Apache2.2/conf/server.key"
保存后,重启Apache 服务。
可能出现的错误及解决办法:
1,、"Syntax error on line 80 of c:/apache/conf/extra/httpd-ssl.conf:ErrorLog takes one argument,The filename of the error log"或者"Syntax error on line 99 of c:/apache/conf/extra/httpd-ssl.conf:SSLCertificateFile takes one argument,SSL Server Certificate file ('/path/to/file' -PEM or DER encoded)"
解决方法:文件路径加双引号
2、Syntax error on line 76 of C:/apache/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?)."
解决办法:
打开httpd.conf,找到 LoadModule socache_shmcb_module modules/mod_socache_shmcb.so,把前面的注释去掉。