在設置Apache + SSL以前, 須要作:php
安裝Apache, 下載安裝Apache時請下載帶有ssl版本的Apache安裝程序.html
而且ssl須要的文件在以下的位置:apache
安裝好在bin目錄下有一個openssl.exe文件,用來生成證書和密鑰。
1). 生成服務器用的私鑰文件server.keywindows
(注:在Windows操做系統環境下需先設置Openssl環境變量:在DOS命令下進入conf目錄,執行命令行:set OPENSSL_CONF=..confopenssl.cnf;請在執行以前確保openssl.cnf存在,不然會出現:WARNING: can't open config file: /usr/local/ssl/openssl.cnf 信息提示。還有在windows系統下.cnf 默認會被當成快捷方式,看不到擴展名。)安全
[Apache安裝目錄]/modules/ mod_ssl.so服務器
[Apache安裝目錄]/bin/ openssl.exe, libeay32.dll, ssleay32.dll, openssl.cnfpost
[Apache安裝目錄]/conf/ openssl.cnf測試
建立SSL證書(注意,我下載的是PHPStudy裏面自帶了openssl,可是bin目錄下沒有openssl.cnf,須要將conf下的openssl.cnf拷貝一份到bin目錄下,但整體原理同樣)url
打開CMD,進入到Apache安裝目錄下的bin目錄下:spa
若是DOS命令進入目前
步驟一:
執行命令:openssl genrsa 1024 >tian_server.key
(RSA密鑰對的默認長度是1024,取值是2的整數次方,而且密鑰長度約長,安全性相對會高點)。
完成密鑰server.key生產完畢後進行步驟二操做。
步驟二:
生產爲簽署的server.csr
繼續在bin目錄下執行命令:
openssl req -new -config openssl.cnf -key tian_server.key >tian_server.csr
(若是不加-config .openssl.cnf參數的話,常會報Unable to load config info from .../ssl/openssl.cnf)
以後就會要求輸入一系列的參數:
Country Name (2 letter code) [AU]:CN ISO 國家代碼(只支持兩位字符)
State or Province Name (full name) [Some-State]:GD 所在省份
Locality Name (eg, city) []:GZ 所在城市
Organization Name (eg, company): 公司名稱
Organizational Unit Name (eg, section) []: 組織名稱
Common Name (eg, YOUR name) []: 申請證書的域名(建議和httpd.conf中serverName必須一致)
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 []:(建議和httpd.conf中serverName必須一致)
注:Common Name建議和httpd.conf中serverName必須一致,不然有可能致使apache不能啓動(啓動 apache 時錯誤提示爲:server RSA certificate CommonName (CN) `Kedou' does NOT match server name!? )
完成簽署的server.csr配置。
步驟三:
簽署服務器證書文件 server.crt
在 bin/目錄下執行命令:
openssl req -x509 -days 5000 -config openssl.cnf -key tian_server.key -in tian_server.csr >tian_server.crt
說明:這是用步驟 1,2 的的密鑰和證書請求生成證書 server.crt,-days 參數 指明證書有效期,單位爲天,x509 表示生成的爲 X.509 證書。
步驟四:
在bin 目錄下,找到tian_server.crt、tian_server.csr、tian_server.key三個文件,將此三個文件剪切到Apache的conf目錄下。
步驟五:
配置 httpd.conf. 在Apache的conf\extra目錄下的 httpd_ssl.conf 文件是關於 ssl 的配置,是httpd.conf的一 部分。
在 httpd.conf 中下列兩行:去掉前面的「#」
Include conf/extra/httpd-ssl.conf
步驟六:
(注意:相同部分被省略掉)
# General setup for the virtual host
DocumentRoot " D:/PHPStudy/WWW/ "(這個地方網頁根目錄)
ServerName phpssl.com:443
ErrorLog " D:/PHPStudy/Apache/logs/no-robots_error.log.txt" (具體的服務器上的地址)
TransferLog " D:/PHPStudy/Apache/logs/no-robots_access.log.txt" (具體的服務器上的地址)
SSLCertificateFile " D:/PHPStudy/Apache/conf/tian_server.crt"
SSLCertificateKeyFile " D:/PHPStudy/Apache/conf/tian_server.key"
CustomLog " D:/PHPStudy/Apache/logs/ssl_request.log" \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
以上最容易出問題的地方是http_ssl.conf中的日誌等路徑問題,建議按照物理路徑進行設置
步驟八:
調試運行Apache,對於Apache。若是是集成板的如xampp,wampserver,或者phpstudy啓動Apache Server,可能咱們不知道咱們的服務器出現了什麼問題,建議查看 Apache/logs下的日誌,在此基礎上,配合Cmd命令行在bin目錄下運行httpd命令,若是不報錯,說明服務器運行沒問題,不然更具提示修改問題
測試網址:https://localhost:443/ 打開後,
其它網址像:https://localhost/phpMyAdmin/才能打開
運行效果以下
引用:http://my.oschina.net/ososchina/blog/510065