我的环境配置:PHP(5.3.8)+Apache(2.2.8)+mysql(5.6)
1.安装Apache服务时,选择安装带有ssl服务的.(如apache_2.2.8-win32-x86-openssl-0.9.8g )
下载地址:http://archive.apache.org/dist/httpd/binaries/win32/

2.Apache+PHP安装完成之后,在项目根目录新建index.php文件,写入phpinfo();[前提:已配置好Apache解析PHP]
浏览器输入localhost://index.php,搜索openssl

3.下载openssl.exe
下载地址:http://slproweb.com/products/Win32OpenSSL.html
(1)再次访问index.php,可以看到

(2)openssl安装完成后,将openssl加入环境变量(PATH )[后续操作方便]
4.接下来是Apache中ssl的配置
打开cmd,进入到Apache/bin目录,执行以下命令
(1)设置OPENSSL_CONFIG配置
set OPENSSL_CONF=..\conf\openssl.cnf

(2)生成服务端的key文件
openssl genrsa -out server.key 1024

此命令执行后,会在Apache/bin目录下生成server.key文件
(3)生成签署申请
openssl req -new -out server.csr -key server.key

完成后,会在bin目录下生成server.csr文件,其中 Common Name [] 需要与配置文件中的ServerName一致,否则apache启动时将会报错。
(4)生成CA的key文件
openssl genrsa -out ca.key 1024

此命令完成后,会在目录bin下生成ca.key文件
(5)生成CA自签署证书(有效期365天)
openssl req -new -x509 -days 365 -key ca.key -out ca.crt

完成后,会在目录bin下生成ca.crt文件,此处填写的信息与2.3步中类似。
(6)生成CA的服务器签署证书
openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key

此处如果报如下错误.../XXX目录不存在..

根据提示,手动创建相关目录:
在bin下新建demoCA文件夹
bin/demoCA
在demoCA下新建index.txt
bin/demoCA/index.txt
在demoCA下新建serial.txt,其内容为01,重命名删除.txt后缀
bin/demoCA/serial
在demoCA下新建newcerts文件夹
bin/demoCA/newcerts
完成后,会在bin目录下生成server.crt文件。demoCA目录的最终结构如下

5.httpd.conf对ssl进行设置(Apache/conf/httpd.conf)
#LoadModule ssl_module modules/mod_ssl.so (去掉前面的#号)
#Include conf/extra/httpd-ssl.conf (去掉前面的#号)
#LoadModule proxy_module modules/mod_proxy.so (去掉前面的#号)
#LoadModule proxy_http_module modules/mod_proxy_http.so (去掉前面的#号)
6.httpd-ssl.conf设置
SSLCertificateFile "xxx/conf/key/server.crt"
SSLCertificateKeyFile "xxx/conf/key/server.key"
SSLCACertificateFile "xxx/conf/key/ca.crt"
#SSLVerifyClient require (去掉前面的#号,进行客户端验证时需要)
#SSLVerifyDepth 1 (去掉前面的#号,把10改为1,进行客户端验证时需要)
7.重启Apache,访问 https://localhost
(1)在Apache重启成功后,如果浏览器页面出现''你的链接不安全(XXX证书失效或者不受信任)"等错误;
再次打开cmd,进入到Apache/bin目录执行
openssl pkcs12 -export -in ca.crt -inkey ca.key -out ca.pfx
在bin下面生成一个ca.pfx的文件,中间有个输入密码的过程,要记住这个密码。打开浏览器证书管理那块,按照提示导入生成的ca.pdf文件,这里要输入刚才生成 .pfx 过程中输入的密码。导入之后,打开浏览器输入https://localhost/index.php 显示phpinfo页面,表示成功
火狐浏览器证书导入(现在设置中找到证书管理的入口)

[ 如果是线上部署的话,可以购买阿里云服务器+阿里云的ssl证书, 阿里有 ssl证书配置的方法
请移步阿里云社区查看 ]
感兴趣可以看看这篇文章,写的很好 http://www.barretlee.com/blog/2016/04/24/detail-about-ca-and-certs/
以上自己本地测试OK,如有不同意见可以提出.欢迎指正!!!
网友评论