Apache httpd开启SSL

https://my.oschina.net/xpbug/blog/197454

必要软件

  1. apache httpd 2.4(windows)

  2. perl 5 (windows)

  3. openssl (windows)

安装软件

因为手上没有linux的空余机器,所以一切操作都是在windows平台上。安装以上三个软件的win版,略。

配置httpd

首先用httpd搭建一个简单的网站。在apache的目录下,找到httpd.conf文件,打开文件,找到下面的内容,并把注释去掉:

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

 

找到httpd-vhosts.conf文件,打开,并向其中添加一个虚拟网站

<VirtualHost *:80>
   ServerAdmin joey
   DocumentRoot "D:/www/test0"
   ServerName www.test0.com
   ErrorLog "logs/errlog"
   CustomLog "logs/accesslog" common
   <Directory "D:/www/test0">
	Order allow,deny
	Allow from all
	</Directory>
</VirtualHost>

 

在网站目录下面创建一个index页面, index.html

<html>
<body>test0</body>
</html>

 

重启httpd,访问网站http://www.test0.com, 可以看到test0页面。(注意,如果www.test0.com无法加入DNS,则可以修改本地的hosts文件)。

生成SSL证书

SSL中存在三种证书,顶级证书,服务器证书和客户端证书。

  • 顶级证书是权威证书颁发机构所持有的证书,权威机构使用顶级证书给服务器证书或客户端证书进行签名。

  • 服务器证书是我们最常见的,访问多有https的网站,都会收到一个服务器证书,如果证书是有有名的权威机构颁发,则浏览器就会帮我们处理证书。如果证书是由不知名机构颁发,浏览器会跳出窗口,问我们是否信任此证书。

  • 客户端证书常见于网银。像招商银行的客户端,可以使用电子证书。之所以存在客户端证书,原因是服务器想验证客户的合法性。这时候,信任是双向的。

一般互联网上,只需要服务器证书,信任是单向的,服务器并不对客户做验证。

安装完openssl以后,其目录存在于C:\OpenSSL-Win32. 现在让我们来生成各种证书。

  1. 打开cmd命令台,在运行openssl的命令之前,先设置环境变量

    >set OPENSSL_CONF=c:\openssl-win32\bin\openssl.cfg
  2. 生成顶级证书。这部分应该是有权威机构做的,这里我冒充一下权威机构。

     

    >CA.pl -newca

    在/demoCA/目录下会生成CA的必须文件。这其中包含顶级证书。

  3. 服务器商需要为自己生成公钥密钥,并制作一个证书申请。这里还是我来冒充一下。

    >openssl.exe genrsa -des3 -out server.key 1024
    >openssl.exe req -new -key server.key -out server.csr

    注意,当遇到下面一行的时候,一定要填写网站的域名,比如今天的例子www.test0.com.

  4. 服务商把证书申请server.csr发送给权威机构,权威机构进行签名。将server.csr重命名为newreq.pem,然后执行下面命令

    >CA.pl -sign

    生成的newcert.pem就是证书,将newcert.pem重命名为server.crt,然后发还给服务商。

  5. 生成客户端证书,这部分并不在本实验中,可以忽略,但我把要执行的命令列在这里,以便于以后查找。

  6. >openssl.exe genrsa -des3 -out client.key 1024
    >openssl.exe req -new -key client.key -out client.csr
    >openssl.exe ca -in client.csr -out client.crt
    #生成客户端可安装文件。
    >openssl.exe pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.pfx

上面的步骤中,将获得的server.key和server.crt复制出来,待会用。

在win32上,apache不支持加密的private key,所以还需要额外步骤,去除加密。请看最后面的部分。

开启httpd的SSL功能

修改httpd.conf文件,将其中两行的注释去掉

LoadModule ssl_module modules/mod_ssl.so

# Secure (SSL/TLS) connections
Include conf/extra/httpd-ssl.conf

 

打开httpd-ssl.conf文件,进行编辑

NameVirtualHost *:443

<VirtualHost *:443>
   ServerAdmin joey
   DocumentRoot "D:/www/test0"
   ServerName www.test0.com:443
   ErrorLog "logs/errlog"
   CustomLog "logs/accesslog" common
   
   SSLEngine on
   SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5
   SSLCertificateFile "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/conf/server.crt"
   SSLCertificateKeyFile "C:/Program Files (x86)/Apache Software Foundation/Apache2.2/conf/server.key"
   
   <Directory "D:/www/test0">
	Order allow,deny
	Allow from all
	</Directory>
</VirtualHost>

 

注意,SSLCertificateFile和SSLCertificateKeyFile必须指向之前我们生成的key和证书。

重启httpd

无法重启httpd,并在error.log中发现这么一句话:

SSLPassPhraseDialog builtin is not supported on Win32

原因是因为Win32不支持SSLPassPhraseDialog命令,我们需要把此命令个注释掉,并需要把server.key的PassPhrase也给去掉。步骤如下

  1. 重命名server.key为server.key.org

  2. 运行命令 >openssl rsa -in server.key.org -out server.key

  3. 将产生的new key替换到原来的位置。

  4. 编辑httpd-ssl.conf,把 #SSLPassPhraseDialog  builtin注释掉。

再重启一下httpd. 还有错误,error.log中有这么一句话:

Syntax error on line 62 of C:/Program Files (x86)/Apache Software Foundation/Apache2.2/conf/extra/httpd-ssl.conf:

SSLSessionCache: Invalid argument: size has to be >= 8192 bytes

原因是命令吧(X86)给当做86来解析了,需要做下修改

#修改前
SSLSessionCache        "shmcb:C:/Program Files (x86)/Apache Software Foundation/Apache2.2/logs/ssl_scache(512000)"
#修改后
SSLSessionCache        "shmcb:C:/PROGRA\~2/Apache Software Foundation/Apache2.2/logs/ssl_scache(512000)"

 

再次重启,成功。

验证https

在浏览器中输入网址https://www.test0.com, 浏览器会询问你是否信任当前证书。因为这个证书不是权威机构发放的。选择信任。我们可以查看证书,证书里面的签发机构是我。

接下来几天的计划

下一步,将使用tomcat搭建集群,使用apache做负载平衡。http://my.oschina.net/xpbug/blog/197680

接着,会给tomcat制作登录页面。然后只保留登录页面的https连接,其它页面使用http.

然后,会开启tomcat认证和授权。

接着,会搭建第二个tomcat网站,并开启两个网站之间的SSO。

最后,会着手性能优化,如何增加cache。

posted @ 2016-10-18 15:52  mydddfly  阅读(2732)  评论(0编辑  收藏  举报