天天看點

Windows搭建Apache+SSL Https伺服器

所需的環境參考上一篇文章,簡單地搭建了一個Apache+PHP的伺服器,這個apache是自帶SSL的,

http://blog.csdn.net/wlmnzf/article/details/50229407

因為看到網上的各種教程不全還有很多錯誤,自己繞了好多彎最後參考了官方文檔,才最終解決,是以這裡綜合各方大神所寫的文章,改正和完善細節後再發一次。這裡不得不說一下,官方文檔雖然是英文的平時還特别低調,顯得相當高冷,可是它卻是我們最靠譜,最堅強的後盾。

1.修改兩個配置檔案,一個為conf/httpd.conf,另一個為conf/extra/httpd-ssl.conf

在httpd.conf中

a. 删掉以下語句前的’#’

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

b. httpd-ssl.conf中把相應選項改成如下,有’#’的删掉

SSLCertificateFile "c:/Apache24/conf/server.crt"
SSLCertificateKeyFile "c:/Apache24/conf/server.key"
SSLCACertificateFile "c:/Apache24/conf/ca.crt"
SSLVerifyClient require
SSLVerifyDepth  
           

2.生成各種證書

進入Apache24\bin目錄,在對應檔案夾下打開cmd

這裡需要一個openssl的配置檔案openssl.cnf,如果他不在 conf檔案夾下,可以去網上下一個,建議把它放到bin目錄下,這樣子敲指令比較友善

a. 首先要生成伺服器端的私鑰(key檔案):

set OPENSSL_CONF=openssl.cnf
  openssl genrsa -des3 -out server.key 
           

b. 生成server.csr ,Certificate Signing Request(CSR),生成的csr檔案交給CA簽名後形成服務端自己的證書.螢幕上将有提示,依照其訓示一步一步輸入要求的個人資訊即可.

c. 對用戶端也作同樣的指令生成key及csr檔案

openssl genrsa -des3 -out client.key 
openssl req -new -key client.key -out client.csr -config openssl.cnf
           

d. CSR檔案必須有CA的簽名才可形成證書.可将此檔案發送到verisign等地方由它驗證,要交一大筆錢,何不自己做CA呢.

openssl req -new -x509 -keyout ca.key -out ca.crt -config openssl.cnf
           

e. 在bin目錄下建立一個demoCA檔案夾,進入它

建立newcerts檔案夾,不需要進入

建立index.txt

建立serial,打開後輸入01儲存即可

f. 用生成的CA的證書為剛才生成的server.csr,client.csr檔案簽名:

openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key -config openssl.cnf

openssl ca -in client.csr -out client.crt -cert ca.crt -keyfile ca.key -config openssl.cnf
           

這裡如果第二條指令出現下面這樣的錯誤,進入demoCA,然後打開index.txt.attr,把它修改成unique_subject = no就可以了

Windows搭建Apache+SSL Https伺服器

g. 生成一個ca.pfx,打開IE浏覽器-》工具-》Internet選項-》内容-》證書,按照提示導入,這裡要輸入剛才生成 .pfx 過程中輸入的密碼

openssl pkcs12 -export -in ca.crt -inkey ca.key -out ca.pfx

.

以上操作生成了

client使用的檔案有:ca.crt,client.crt,client.key

server使用的檔案有:ca.crt,server.crt,server.key

把 ca.crt,server.crt,server.key複制到conf根目錄下去

打開Apache Service Monitor ,點選重新開機或者啟動,走你…..

然而并沒什麼卵用,apache爆炸了,可是我們并不需要報警,接下來我們将一步步解決這些錯誤,本人行走江湖多年,能力不高,是以錯誤百出,别的沒能耐,但是對于找錯誤,還是相當有一手的(此處捂着嘴笑),下面貼出logs/error.log的錯誤資訊

AH00558: httpd.exe: Could not reliably determine the server's fully qualified domain name, using fe80::9c8a:3195:d193:8d3c. Set the 'ServerName' directive globally to suppress this message

[Thu Dec  :: ] [ssl:warn] [pid :tid ] AH01873: Init: Session Cache is not configured [hint: SSLSessionCache]

[Thu Dec  :: ] [mpm_winnt:notice] [pid :tid ] AH00354: Child: Starting  worker threads.

[Thu Dec  :: ] [mpm_winnt:notice] [pid :tid ] AH00424: Parent: Received restart signal -- Restarting the server.

AH00526: Syntax error on line  of C:/Apache24/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?).

[Thu Dec  :: ] [mpm_winnt:notice] [pid :tid ] AH00364: Child: All worker threads have exited.
           

各種百度以後得到解決方案

.Syntax error on line  of C:/Apache24/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
取消注釋(删掉前面的"#")

.SSLPassPhraseDialog builtin is not supported on Win32(key file C:/Apache24/conf/server.key)

   這個是說Windows不支援加密密鑰,還記得生成server.key輸入的密碼嗎?沒錯就是它的鍋,我們現在取消掉它,cmd輸入

openssl rsa -in server.key -out server.key

把生成的server.key複制到conf目錄下覆寫
然後打開httpd-ssl.conf 找到SSLPassPhraseDialog  builtin 在前面加上#
           

然後再次點選apache上的啟動試試,然後就SSL走起了。

浏覽器輸入https://127.0.0.1/index.php就可以觀望到自己的https站點了。

參考資料

http://httpd.apache.org/docs//ssl/

http://blog.csdn.net/decajes/article/details/

http://blog.chinaunix.net/uid--id-html

http://impradeep.com/apacheconfextrahttpd-ssl-conf-error/

http://m.blog.csdn.net/blog/kuixinpei/
           

鳴謝

感謝樓上各位大神的幸苦探索

感謝 網易雲音樂 歌單《夜的鋼琴曲 Demo集》--石進 在我煩躁得仿佛萬千草泥馬奔騰而過以及想出門日狗的時候及時阻止了我。

感謝 網易雲音樂 《當你》--林俊傑 的深夜相伴