Windows系統中如何配置Apache的SSL加密訪問

系統事件日誌提示:ssl裏的server.crt文件不存在或者爲空。該怎麼辦?

http://www.server110.com/apache/201404/9462.html


到Apache的網站上下載包含有SSL服務的安裝包。

如果你已經安裝過了Apache,在安裝含有SSL服務的Apache服務器之前需要卸載已經安裝過的。到控制面板裏面直接卸載就可以了。 注意:Apache在卸載的時候不會刪除httpd.conf這個配置文件和htdocs文件夾下面的內容。如果你確實不需 要,你需要手工刪除。

在Windows的環境下面安裝含有SSL的Apache和不含有 Apache的方法和過程一致。

這裏關鍵說安裝以後的服務配置。
httpd.conf文件,如果需要Apache服務器支持SSL,這裏需要載入幾個模塊。

去掉下面兩行之前的#符號
LoadModule ssl_module modules/mod_ssl.so
Include conf/extra/httpd-ssl.conf

LoadModule ssl_module modules/mod_ssl.so的作用是載入SSL模塊
apache_ssl_01.jpg  

Include conf/extra/httpd-ssl.conf 的作用是告訴Apache的SSL模塊到那裏去找配置文件
apache_ssl_02.jpg 

重新啓動服務器,測試一下看有沒有什麼問題。
顯然在重新啓動服務器的時候報了一個錯誤,說服務器無法正常啓動。這時候我們可以看看日誌文件了。
[Fri Aug 15 15:33:08 2008] [notice] Parent: Received restart signal -- Restarting the server.
[Fri Aug 15 15:33:08 2008] [notice] Child 740: Exit event signaled. Child process is ending.
Syntax error on line 99 of D:/Dkits/Apache2.2/conf/extra/httpd-ssl.conf:
SSLCertificateFile: file 'D:/Dkits/Apache2.2/conf/server.crt' does not exist or is empty
[Fri Aug 15 15:33:08 2008] [warn] (OS 995)The I/O operation has been aborted because of either a thread exit or an application request.  : winnt_accept: Asynchronous AcceptEx failed.
[Fri Aug 15 15:33:09 2008] [notice] Child 740: Released the start mutex
[Fri Aug 15 15:33:10 2008] [notice] Child 740: All worker threads have exited.
[Fri Aug 15 15:33:10 2008] [notice] Child 740: Child process is exiting

上面說在D:/Dkits/Apache2.2/conf/server.crt路徑下找不到server.crt這個文件。

那麼,這個文件在那裏呢?
這個文件需要我們用openssl這個程序來生成的。生成這個 server.crt文件需要2個文件,一個是openssl.exe 另一個是openssl.cnf。

一般來說openssl.exe 在你Apache的安裝路徑下的bin文件夾裏面,openssl.cnf在Apache安裝路徑下的conf文件裏面。找不到?如果找不到就用 windows的查找搜索一下了。這個時候你需要將openssl.exe和openssl.cnf放在同一個文件夾裏面。

一般的需要將找到的openssl.cnf文件拷貝到bin文件裏面。

用openssl生成KEY 生成自簽名文件

首先生成csr和pem文件。
命令是:openssl req -config openssl.cnf -new -out server.csr -keyout server.pem
運行結果如下:
apache_ssl_03.jpg

D:\Dkits\Apache2.2\bin>openssl req -config openssl.cnf -new -out server.csr -keyout server.pem
Loading 'screen' into random state - done
Generating a 1024 bit RSA private key
.........++++++
.........++++++
writing new private key to 'server.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:New Hampshire
Locality Name (eg, city) []:Manchester
Organization Name (eg, company) [Internet Widgits Pty Ltd]:CUIST
Organizational Unit Name (eg, section) []:IT Section
Common Name (eg, YOUR name) []:localhost
Email Address []:[email protected]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

D:\Dkits\Apache2.2\bin>

其中有幾個地方需要注意
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
用於輸入和重複輸入密碼的,密碼長度不能小於4位

Common Name (eg, YOUR name) []:localhost
用於輸入客戶端能夠訪問SSL的地址:這裏SSL服務器是在本地運行的,因此你可以輸入localhost如果你的SSL地址運行網站主機上,那麼這裏需 要輸入例如www.cuist.com了。

An optional company name []:留空,如果輸入將會報錯。
apache_ssl_04.jpg

其他的提示輸入部分,可以輸入也可以留空,最重要的就是Common Name (eg, YOUR name) []:這個變量,關係到你的SSL服務能不能夠運行。

爲Apache創建沒有密碼保護的key 下面需要爲Apache創建一個沒有密碼保護的key了。

命令是openssl rsa -in server.pem -out server.key

運行結果如下
apache_ssl_05.jpg 

在這裏你需要輸入密碼,這個密碼是你在上一步輸入過的。如果沒有錯誤,你將會看到writing RSA key這句話後面沒有任何內容和錯誤提示。

創建X.509證書 現在需要爲Apache創建X.509證書了。
命令是:openssl x509 -in server.csr -out server.crt -req -signkey server.key -days 365

結果如下:
apache_ssl_06.jpg

如果沒有錯誤,將會看到簽名已經成功,後面有簽名的詳細信息和內容。

現在你再看看opensel.exe所在的文件夾下面是不是多了4個文件了,分別 是:server.pem,server.key,server.csr,server.crt
apache_ssl_07.jpg  

其實我們並不需要所有的這4個文件,我一向喜歡偷懶,就把這4個文件直接拷貝到conf文件夾下面保存。

重新啓動錯誤 如果重新啓動沒有錯誤,但是還是不能訪問。

這時候你需要檢查日誌文件,有一種可能是443端口的衝突,如果你的機器已經安裝了IIS,那需要將IIS的web服務,FTP,SMTP服務全部關掉再 重新啓動。

如果既沒有日誌輸出,也不能訪問,那是因爲配置文件沒有被正確的載入,這時候你需要先在httpd.conf文件裏面註釋掉這句話Include conf/extra/httpd-ssl.conf,然後重新啓動Apache的服務。等服務成功啓動以後,再打開這句話,再重新啓動服務。這時候你就 能看到日誌的輸出了。否則不管你重新啓動多少次,Apache還是隻會載入老的httpd-ssl.conf文件,不會重新讀取httpd- ssl.conf的。

如果在日誌裏面有警告
[Fri Aug 15 16:15:10 2008] [warn] RSA server certificate CommonName (CN) `localhost' does NOT match server name!?
[Fri Aug 15 16:15:10 2008] [notice] Child 3664: Released the start mutex
[Fri Aug 15 16:15:10 2008] [warn] RSA server certificate CommonName (CN) `localhost' does NOT match server name!?
那是因爲你輸入的SSL服務爲localhost但是你Apache在安裝的時候服務卻不是localhost。這時候你需要改幾個地方。

httpd-ssl.conf文件中的
apache_ssl_08.jpg  

# ServerAdmin: Your address, where problems with the server should be
# e-mailed.  This address appears on some server-generated pages, such
# as error documents.  e.g. [email protected]
#
ServerAdmin [email protected]

將ServerAdmin 變量中[email protected]的 改成admin@localhost

保存上述兩個配置文件後重新啓動Apache服務器,如果沒有日誌輸出,則需要先在httpd.conf文件裏面註釋掉這句話Include conf/extra/httpd-ssl.conf,然後重新啓動Apache的服務。等服務成功啓動以後,再打開這句話,再重新啓動服務。

在輸出的日誌裏面應該不會包含有任何的警告信息了。
[Fri Aug 15 16:29:13 2008] [notice] Parent: Received restart signal -- Restarting the server.
[Fri Aug 15 16:29:13 2008] [notice] Child 3908: Exit event signaled. Child process is ending.
[Fri Aug 15 16:29:13 2008] [notice] Apache/2.2.9 (Win32) mod_ssl/2.2.9 OpenSSL/0.9.8h PHP/5.2.6 configured -- resuming normal operations
[Fri Aug 15 16:29:13 2008] [notice] Server built: Jun 13 2008 04:04:59
[Fri Aug 15 16:29:13 2008] [notice] Parent: Created child process 2976
[Fri Aug 15 16:29:14 2008] [notice] Child 3908: Released the start mutex
[Fri Aug 15 16:29:14 2008] [notice] Child 2976: Child process is running
[Fri Aug 15 16:29:14 2008] [notice] Child 2976: Acquired the start mutex.
[Fri Aug 15 16:29:14 2008] [notice] Child 2976: Starting 64 worker threads.
[Fri Aug 15 16:29:14 2008] [notice] Child 2976: Starting thread to listen on port 443.
[Fri Aug 15 16:29:14 2008] [notice] Child 2976: Starting thread to listen on port 80.
[Fri Aug 15 16:29:15 2008] [notice] Child 3908: All worker threads have exited.
[Fri Aug 15 16:29:15 2008] [notice] Child 3908: Child process is exiting
apache_ssl_10.jpg

#   General setup for the virtual host
DocumentRoot "D:/Dkits/Apache2.2/htdocs"
ServerName CUIST_PC02.rochester.cuist.local:443
ServerAdmin [email protected]
ErrorLog "D:/Dkits/Apache2.2/logs/error.log"
TransferLog "D:/Dkits/Apache2.2/logs/access.log"

將ServerName CUIST_PC02.rochester.cuist.local:443變量中的 CUIST_PC02.rochester.cuist.local:443改成localhost:443
將ServerAdmin 變量中[email protected]的 改成admin@localhost

httpd.conf文件中的
apache_ssl_09.jpg  

HTTP服務啓動在80端口,SSL服務啓動在443端口。

驗證 在HTTP能正常訪問的地址下面加上S變成https。

如果http://localhost/phpMyAdmin/index.php變化成https://localhost/phpMyAdmin /index.php

這時候應該能夠看到一個證書警告的信息
apache_ssl_11.jpg


同意這個證書,看看是不是能夠看到web頁面了呀,如果能,則說明SSL已經被正確的配置到Apache服務裏面來了。可以輕鬆一下了喔。。




轉載請註明原文地址:http://www.server110.com


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章