[轉]配置Apache服務器支持https協議和SSL證書

創建網站時,特別是支付網站和電子商務網站,爲了讓客戶信賴網站,讓他們不把咱們當作騙子,網站通常會安裝SSL,咱們訪問網站時都是以https開頭的,而不是以http開頭的。下面是在windows環境下配置Apache服務器支持https協議和SSL證書。

1.配置Apache服務器支持openssl

配置Apache服務器支持https協議和SSL證書,最基本的要求是Apache包含openssl模塊。在Windows下面單獨安裝openssl比較麻煩,最好仍是安裝那種綁定openssl的apache。我安裝的是httpd-2.2.25-win32-x86-openssl-0.9.8y.msi,點擊這裏下載。php

安裝完成以後,在Apache安裝目錄的bin目錄下能夠看到 libeay32.dll、openssl.exe、ssleay32.dll 等三個文件,以下圖所示:html

apache 支持 ssl 的文件

再查看一下modules目錄下是否存在 mod_ssl.so,查看 conf目錄下是否存在openssl.cnf。git

若是上面的都存在,能夠肯定Apache 可以支持 openssl 模塊。算法

2.生成相關的證書

   1). 使用DOS命令進入Apache bin目錄

windows DOS命令下進入Apache bin 目錄

   2). 設置openssl環境變量

使用DOS命令在bin目錄下執行命令:set OPENSSL_CONF=..\conf\openssl.cnf,經過此命令設置openssl的環境變量,若是不執行此命令,後面的操做會報錯。apache

DOS命令設置openssl環境變量

   3). 生成server.key

DOS下執行命令:openssl genrsa 4096 > server.key,執行了這個命令,會在bin目錄下生成server.key。註釋:這是使用128位的RSA算法生成的密鑰,還可使用其餘的算法生成密鑰,相關的用法可使用搜索引擎搜索。4096是密鑰的長度,這個值最好使用4096以上的值,必須是2的整數次方。windows

命令生成openssl密鑰

 4). 生成未簽署的server.csr

DOS命令下執行:openssl req -new -key server.key > server.csr,這個命令使用生成的密鑰文件 server.key生成證書請求文件server.csr,執行這一命令以後,後面還要輸入不少參數。瀏覽器

Country Name (2 letter code) [AU]:CN                                                                        ISO國家代碼(只支持兩位字符)
State or Province Name (full name) [Some-State]:Hu Bei                                         所在省份
Locality Name (eg, city) []:Wu Han                                                                               所在城市
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Mark Company    公司名稱
Organizational Unit Name (eg, section) []:IT                                                               組織名稱
Common Name (e.g. server FQDN or YOUR name) []:www.phpmarker.com       申請證書的域名
Email Address []:phpmarker@163.com                                                                        管理員郵箱
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:                                                                                                    交換密鑰 可空
An optional company name []:                                                                                         可空服務器

openssl生成未簽署的server.csr

 5). 簽署服務器證書文件server.crt

DOS命令下執行:openssl req -x509 -days 365 -key server.key -in server.csr > server.crt,這個命令使用第三步和第四步生成的密鑰和證書來生成證書server.crt,-days參數表示證書有效期,單位爲天,x509表示生成的是X.509證書。測試

openssl 簽署服務器證書文件server.crt

 6). 查看證書詳細信息

DOS下執行命令:openssl x509 -noout -text -in server.crt,這個命令能夠查看證書的詳細信息網站

openssl查看證書詳細信息

執行上面全部的命令以後,在Apache的bin目錄下生成了三個文件server.key、server.csr、server.crt。

https相關的密鑰和證書文件

注意:這樣生成的證書只能做爲測試使用,這種證書是不可信賴的,瀏覽器會給出警告的。若是想要真正使用,能夠找相關提供證書的網站提供有關的證書。

3.配置Apache服務器載入openssl相關的配置文件

修改Apache conf目錄下的httpd.conf配置文件找到 LoadModule ssl_module modules/mod_ssl.so,移除前面的#,找到 Include conf/extra/httpd-ssl.conf,移除前面的#

4.修改httpd-ssl.conf文件

httpd.conf文件載入的ssl配置文件爲 conf/extra/httpd-ssl.conf,找到 <VirtualHost _default_:443>,修改裏面的有關配置,個人配置以下:

# General setup for the virtual host
DocumentRoot 「K:/wwwroot/mark1990.com」       網站路徑
ServerName www.phpmarker.com:443                   網站域名
ServerAdmin phpmarker@163.com
ErrorLog 「K:/Apache/logs/error.log」
TransferLog 「K:/Apache/logs/access.log」

SSLCertificateFile 「K:/Apache/conf/server.crt」
SSLCertificateKeyFile 「K:/Apache/conf/server.key」
SSLCertificateChainFile 「K:/Apache/conf/server.crt」

我是將 server.key、server.crt、server.csr移動到 conf 目錄下

5.修改host文件,重啓Apache服務器

進入 Windows\System32\drivers\etc目錄下,向 hosts添加127.0.0.1 www.phpmarker.com,從新啓動Apache服務器,訪問https://www.phpmarker.com/,出現以下界面:

Apache https 證書安裝完成

出現這種界面,由於咱們本身製做的證書,不被瀏覽器所信賴。咱們查看證書詳細狀況:

https 證書

 

 轉載自:http://www.phpmarker.com/226.html

相關文章
相關標籤/搜索