openssl生成默认证书和密钥

window下

安装目录为 c:\httpd-2.2.34-win32-openssl-1.0.2\conf

步骤1:生成密钥

命令:openssl genrsa 1024 > server.key

说明:这是用128位rsa算法生成密钥,得到server.key文件

openssl命令path为httpd-2.2.34-win32-openssl-1.0.2\bin

步骤2: 生成证书请求文件

命令:openssl req -config c:\httpd-2.2.34-win32-openssl-1.0.2\conf\openssl.cnf -new -key server.key > server.csr

说明:这是用步骤1的密钥生成证书请求文件server.csr, 这一步提很多问题,一一输入

步骤3: 生成证书

命令:openssl req -config c:\httpd-2.2.34-win32-openssl-1.0.2\conf\openssl.cnf -x509 -days 365 -key server.key -in server.csr > server.crt

说明:这是用步骤1,2的的密钥和证书请求生成证书server.crt,-days参数指明证书有效期,单位为天

把得到的server.key和server.crt文件拷贝到apache的对应目录

阿里云申请证书

准备

备案过的域名,一个SSL证书(免费的就行)。

申请证书

登录:【阿里云控制台】,【产品与服务】,【SSL证书(应用安全)】,【购买证书】。

默认会有一个Symantec 免费版 SSL出现,你可以用这个或者继续购买

购买:品牌(Symantec) 证书类型 (这儿很鸡贼,你要先选一下增强型 OV SSL,然后免费型 DV SSL才会出现),价格为0,购买。购买完成后它就会出现在证书【未签发】里面。

域名验证:点击申请,填写相关信息,最后验证并提交审核。我选的是一个二级域名验证的额,一级的话还没试。

如果一切正常,10 分钟左右,申请的证书就会审核通过。验证通过后,你就可以在【已签发】里下载证书了。

下载证书

我用的apache,所以选择apache。下载后的证书包括三个

1808143_jse.codingsoho.com_chain.crt   # 根证书链
1808143_jse.codingsoho.com_public.crt  # 公钥
1808143_jse.codingsoho.com.key    # 私钥

apache配置

配置Apache下httpd.conf文件

httpd.conf

#LoadModule ssl_module modules/mod_ssl.so
#Include conf/extra/httpd-ssl.conf

删除行首的配置语句注释符号“#”

有的版本下需要打开下面这个库

LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

httpd-ssl

修改apache下httpd-ssl文件

打开apache安装目录下conf/extra目录中的httpd-ssl.conf文件,在配置文件中查找以下配置语句:

将前面生成的证书文件放到对应的位置

SSLCertificateFile "${SRVROOT}/conf/server.crt"
SSLCertificateKeyFile "${SRVROOT}/conf/server.key"
SSLCertificateChainFile "${SRVROOT}/conf/server-ca.crt"

它们的对应路径如下

SSLCertificateFile "c:/httpd-2.2.34-win32-openssl-1.0.2/conf/server.crt"
SSLCertificateKeyFile "c:/httpd-2.2.34-win32-openssl-1.0.2/conf/server.key"

阿里云下载的密钥和证书

  • 服务器证书公钥 (1808143_jse.codingsoho.com_public.crt) 替换 server.crt
  • 服务器证书私钥 (1808143_jse.codingsoho.com.key) 替换 server.key
  • 证书 (1808143_jse.codingsoho.com_chain.crt) 替换 server-ca.cer

对应代码如下

 SSLCertificateFile "c:/httpd-2.2.34-win32-openssl-1.0.2/conf/1808143_jse.codingsoho.com_public.crt"
 SSLCertificateKeyFile "c:/httpd-2.2.34-win32-openssl-1.0.2/conf/1808143_jse.codingsoho.com.key"
 SSLCertificateChainFile "c:/httpd-2.2.34-win32-openssl-1.0.2/conf/1808143_jse.codingsoho.com_chain.crt"

把以下代码加入任意位置,指定ssl加密协议(optional)

SSLProtocol all -SSLv2 -SSLv3
SSLCipherSuite AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL

保存退出,并重启Apache。

虚拟机配置 - 443端口

文件conf\extra\httpd-ssl.conf添加默认的配置

Listen 443

# General setup for the virtual host
DocumentRoot "c:/httpd-2.2.34-win32-openssl-1.0.2/htdocs"
ServerName www.codingsoho.com:443
ServerAdmin hordechief@qq.com
ErrorLog "c:/httpd-2.2.34-win32-openssl-1.0.2/logs/error.log"
TransferLog "c:/httpd-2.2.34-win32-openssl-1.0.2/logs/access.log"

我的服务器上起了两个apache(一个2.2 win32 py27,一个2.4 win64 py35),所以在第二个apache上将监听端口换成了444,否则报错 >> https好像只能走在443端口,换其他端口就无法监听https请求,即使在阿里云配置了安全组规则

(OS 10048)通常每个套接字地址(协议/网络地址/端口)只允许使用一次。 : AH00072: make_sock: could not bind to address [::]:443
(OS 10048)通常每个套接字地址(协议/网络地址/端口)只允许使用一次。 : AH00072: make_sock: could not bind to address 0.0.0.0:443
AH00451: no listening sockets available, shutting down

这个配置其实我也没用上,主要的修改还是针对各个具体的应用,如下:

NameVirtualHost *:443
<VirtualHost *:443>
...
 SSLCertificateFile "c:/httpd-2.2.34-win32-openssl-1.0.2/conf/1808143_jse.codingsoho.com_public.crt"
 SSLCertificateKeyFile "c:/httpd-2.2.34-win32-openssl-1.0.2/conf/1808143_jse.codingsoho.com.key"
 SSLCertificateChainFile "c:/httpd-2.2.34-win32-openssl-1.0.2/conf/1808143_jse.codingsoho.com_chain.crt"
</VirtualHost>

如果是阿里云,千万别忘了在安全组里面添加443端口

启动重定向(可选),使用用户HTTP访问自动重定向为HTTPS,直接在http.conf最后配置即可,在httpd.conf文件尾加入如下内容:(代码未验证)

RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [L,R]

443和80可以共存,也就是网站同时支持http和https。碰到ajax的情况,这样处理会减少不少麻烦。

django配置

如果是配置的django项目,同样的原理配置443端口

<VirtualHost *:443>
 ServerName jse.codingsoho.com:443
 ServerAdmin navicester@163.com
 DocumentRoot "C:/virtualenv/jse/jse/"
#
 SSLEngine on
#
 SSLCertificateFile "c:/httpd-2.2.34-win32-openssl-1.0.2/conf/1808143_jse.codingsoho.com_public.crt"
 SSLCertificateKeyFile "c:/httpd-2.2.34-win32-openssl-1.0.2/conf/1808143_jse.codingsoho.com.key"
 SSLCertificateChainFile "c:/httpd-2.2.34-win32-openssl-1.0.2/conf/1808143_jse.codingsoho.com_chain.crt"
#
 <FilesMatch "\.(cgi|shtml|phtml|php)$">
  SSLOptions +StdEnvVars
 </FilesMatch>
 <Directory "c:/httpd-2.2.34-win32-openssl-1.0.2/cgi-bin">
  SSLOptions +StdEnvVars
 </Directory>
#
 BrowserMatch "MSIE [2-5]" \
    nokeepalive ssl-unclean-shutdown \
    downgrade-1.0 force-response-1.0
#
 WSGIScriptAlias / "C:/virtualenv/jse/jse/apache/django.wsgi"
#
 <Directory "C:/virtualenv/jse/jse/">
  Options FollowSymLinks Includes ExecCGI
  AllowOverride None
  Order deny,allow
  Allow from all
 </Directory>
#
 ErrorLog "logs/jse-codingsoho-error.log"
 CustomLog "logs/jse-codingsoho-access.log" common
 Alias /static "C:/virtualenv/jse/static_cdn"
 <Directory "C:/virtualenv/jse/static_cdn">
  Order allow,deny
  Allow from all
 </Directory>
</VirtualHost>

Symantec SSL 证书安全签章

https://csr.chinassl.net/symantec-siteseal.html

阿里云域名地址解析

参考文档

评论

留言请先登录注册! 并在激活账号后留言!