天天看點

實作HTTPS(Apache+OpenSSL)

實驗步驟:

1.開啟apache的ssl子產品
#取消以下兩行的注釋
LoadModule ssl_module modules/mod_ssl.so
Include etc/extra/httpd-ssl.conf           
2.CA憑證申請(實驗環境)
#建立存放密鑰和證書檔案的目錄
mkdir /usr/local/apache2/cert && cd /usr/local/apache2/cert           

(1)生成伺服器私鑰,RSA密鑰

openssl genrsa -out ca.key 1024           

(2)生成csr證書檔案,依次輸入國家、地區、城市、組織、組織機關、名字或域名、email等

openssl req -new -key ca.key -out ccku.csr           

(3)設定證書檔案*.crt的有效期等資訊

openssl x509 -req -days 365 -sha256 -in ccku.csr -signkey ca.key -out ccku.crt           
3.修改配置檔案

(1)修改 httpd-ssl.conf檔案,調用證書

#vim /usr/local/apache2/etc/extra/httpd-ssl.conf 
#注釋掉不安全的協定
#添加:
SSLProtocol all -SSLv2 -SSLv3
#修改加密套件
SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
SSLHonorCipherOrder on
SSLCertificateFile cert/ccku.crt
SSLCertificateKeyFile cert/ca.key           

(2)修改apache的主配置檔案,添加虛拟主機

<VirtualHost _default_:443>
        DocumentRoot "/usr/local/apache2/htdocs"
        ServerName localhost:443
        SSLCertificateFile cert/ccku.crt
        SSLCertificateKeyFile cert/ca.key
        SSLCertificateChainFile cert/ccku.crt
</VirtualHost>           
4.驗證

(1)檢查配置檔案文法

apachectl -t           

\#報錯提示:

AH00526: Syntax error on line 83 of /usr/local/apache2/etc/extra/httpd-ssl.conf:
SSLSessionCache: 'shmcb' session cache not supported (known names: ). Maybe you need to load the appropriate socache module (mod_socache_shmcb?).

#解決辦法:修改主配置檔案調用該子產品
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so #取消注釋

重新檢查文法驗證
#apachectl -t
Syntax OK           

(2)重新開機apache,使用https測試

apachectl restart           
5.強制跳轉https
#vim /usr/local/apache2/etc/httpd.conf
#在<Directory "/usr/local/apache2/htdocs">标簽下添加:

    RewriteEngine on
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)?$ https//%{SERVER_PORT}/$1 [R=301,L]           
實作HTTPS(Apache+OpenSSL)
6.關閉https的方法:

1.ssl配置檔案調用

2.虛拟主機

3.跳轉

繼續閱讀