您的位置:首页 > 理论基础 > 计算机网络

apache配置https协议

2017-06-06 20:21 316 查看
安装openssl有两种方式,第一种直接下载安装包,装上就可运行;第二种可以自己下载源码,自己编译。下面对两种方式均进行详细描述。

一、下载和安装openss

方法一:直接使用openssl安装包

Window 的openssl的安装包的下载地址为:

http://slproweb.com/products/Win32OpenSSL.html

一般在安装openssl之前还需要vs的一些插件,该地址中也提供了相关插件的下载。如下图即为openssl的安装及其vs插件在下载网页的截图。

配置https协议:

1、安装好apache环境,注意要装ssl版本的。这里装在c:/apache目录下。

2、生成服务器证书

1)在DOS命令下进入apache/bin目录

2)在windows环境下需先设置Openssl环境变量:
执行命令

[plain] view plain copy

set OPENSSL_CONF=..\conf\openssl.cnf

请在执行之前确保 openssl.cnf 存在,否则会出现:WARNING: can't open config file: /usr/local/ssl/openssl.cnf 信息提示。
还有在windows系统下.cnf 默认会被当成快捷方式,看不到扩展名。

3)生成私钥文件:
执行命令

[plain] view plain copy

openssl genrsa 1024>server.key

说明:这是用128位rsa算法生成密钥,得到server.key文件。 > 是输出文件的标识符

这种生成方法生成的是没有密钥的私钥文件。当然,Apache提供了加入密钥(Password)的命令,就是加入参数-des3。命令为:

[plain] view plain copy

openssl genrsa 1024 -des3 > server.key

使用上述命令生成私钥文件是需要输入密钥的,运行的时候会让你输入并确认你的密钥。但是在Windows环境下会导致以下错误:错误:Apache启动失败,错误提示是:Init: SSLPassPhraseDialog builtin is not supported on Win32 (key file .....)

原因是window下的apache不支持加密的私钥文件。

注:生成的证书中RSA密钥对的默认长度是1024,取值是2的整数次方。建议使用4096以上。

4)生成证书请求文件。
执行命令

[plain] view plain copy

openssl req -new -key server.key > server.csr

说明:这是用步骤3的密钥生成证书请求文件server.csr, 这一步会有很多参数,需要一一输入。

按提示输入一系列的参数:
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):SW_TECH公司名称

Organizational Unit Name (eg, section) []:SW_TECH组织名称

Common Name (eg, YOUR name) []:kedou.com申请证书的域名

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时错误提示为:RSA server certificate CommonName (CN) `Kedou' does NOT match server name!? )

5)签署服务器证书文件。
执行命令行

[plain] view plain copy

openssl req -x509 -days 365 -key server.key -in server.csr > server.crt

说明:这是用步骤3,4的的密钥和证书请求生成证书server.crt,-days参数指明证书有效期,单位为天,x509表示生成的为X.509证书。

以上签署证书仅仅做测试用,真正运行的时候,应该将CSR发送到一个CA返回真正的证书。网上有些文档描述生成证书文件的过程比较繁琐,就是因为 他们自己建立了一个CA中心,然后再签署server.csr

用openssl x509 -noout -text -in server.crt 可以查看证书的内容。证书实际上包含了Public Key

3、配置httpd.conf

打开httpd.conf文件,移除注释的行:

[plain] view plain copy

Include conf/extra/httpd-ssl.conf

LoadModule ssl_module modules/mod_ssl.so

打开httpd-ssl.conf,修改如下:

[plain] view plain copy

<VirtualHost _default_:443>

SSLEngine On

SSLCertificateFile ../bin/server.crt

SSLCertificateKeyFile ../bin/server.key

#SSLCertificateChainFile ../bin//ca.crt // 暂未启用

#......

DocumentRoot "c:/apache/htdocs"

ServerName www.kedou.com:443

</VirtualHost>

4、重启apahce服务,访问https://localhost,完工!

===========出现错误汇集==========

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,把前面的注释去掉。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: