Apache Httpd 2.2 實現https加密通訊
實際生產中CA證書一般是向一些專業認證的國際機構來進行申請的。我們會模擬使用OpenSSL生成的證書,來實現Apache的安全加密通訊,這與實際生產中是類似的。
實驗環境准備
主機A:172.16.0.57-------->httpd服務器
主機B:172.16.0.58--------->CA openssl
接下來,我們分兩個部分進行,在主機B上搭建CA證書環境,然后在主機A上配置證書環境。
主機B上搭建CA證書環境
CA證書環境中,私鑰的名稱以及存放路徑,還有證書的名稱和存放路徑都是有一定規則的,所以,如果不明白的話,可以查看/etc/pki/tls/openssl.cnf
文件
1、構建私鑰文件
[root@localhost ~]#(umask 066;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048 )
Generating RSA private key, 2048 bit long modulus .............................................+++ .........................+++ e is 65537 (0x10001)
2、根據私鑰文件,創建自簽名的根CA證書
證書的名字必須是cacert.pem ,而且存放路徑必須是/etc/pki/CA/cacert.pem
[root@localhost ~]#openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem 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) [XX]:CN State or Province Name (full name) []:shandong Locality Name (eg, city) [Default City]:qingdao Organization Name (eg, company) [Default Company Ltd]:pojun.tech Organizational Unit Name (eg, section) []:Opt Common Name (eg, your name or your server's hostname) []:ca.pojun.tech Email Address []:
3、此時如果我們去查看證書的內容話,是可以查看我們剛剛指定的這些信息的。
[root@localhost ~]#openssl x509 -in /etc/pki/CA/cacert.pem -noout -text Certificate: Data: Version: 3 (0x2) Serial Number: 17076170100312404196 (0xecfabe3b994470e4) Signature Algorithm: sha256WithRSAEncryption Issuer: C=CN, ST=shandong, L=qingdao, O=pojun.tech, OU=Opt, CN=ca.pojun.tech Validity Not Before: Sep 29 03:40:10 2017 GMT Not After : Sep 24 03:40:10 2037 GMT Subject: C=CN, ST=shandong, L=qingdao, O=pojun.tech, OU=Opt, CN=ca.pojun.tech *******************省略了公鑰和簽名信息*************************
3、創建頒發證書必須的兩個文件
如果不提前創建這兩個文件,那么在生成證書的過程中會出現錯誤。
我們將文件創建在配置文件中指定的路徑下面。
touch /etc/pki/CA/index.txt echo 01 > /etc/pki/CA/serial
在主機A上申請證書
1、首先安裝mod_ssl動態模塊
首先在主機A上安裝mod_ssl 模塊 ,然后我們來查看一下這個模塊里面都包含哪些內容
[root@CentOS6 ~]$rpm -ql mod_ssl /etc/httpd/conf.d/ssl.conf # 配置文件 /usr/lib64/httpd/modules/mod_ssl.so # Apache 動態模塊 /var/cache/mod_ssl /var/cache/mod_ssl/scache.dir /var/cache/mod_ssl/scache.pag /var/cache/mod_ssl/scache.sem
2、生成私鑰文件
因為私鑰文件是給Web服務器來使用的,所以私鑰文件可以存放在web服務器的配置目錄下。這樣方便管理 。
# 首先創建一個目錄用來管理生成的私鑰和證書請求文件,可根據自己的實際情況而定 [root@centos6 ~]$ mkdir /etc/httpd/conf.d/ssl # 生成自己的私鑰文件 [root@centos6 ~]$(umask 066; openssl genrsa -out /etc/httpd/conf.d/ssl/httpd.key 1024) Generating RSA private key, 1024 bit long modulus .++++++ ...++++++ e is 65537 (0x10001)
3、生成證書請求文件
生成自己的證書請求文件,這里的請求文件是要傳給主機B(根CA)來申請證書的。其中的域名采用了范域名解析。
也就是說,當我們把所有的環境配置結束之后,就應該使用https://*.a.com的方式來訪問網站,這樣話,服務器就會自動采用加密的方式來處理我們的請求。
# 生成自己的證書申請文件,以 .csr 結尾的文件。 [root@centos6 ssl]$openssl req -new -key /etc/httpd/conf.d/ssl/httpd.key -out /etc/httpd/conf.d/ssl/httpd.csr 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) [XX]:CN State or Province Name (full name) []:shandong Locality Name (eg, city) [Default City]:yantai Organization Name (eg, company) [Default Company Ltd]:pojun.tech Organizational Unit Name (eg, section) []:opt Common Name (eg, your name or your server's hostname) []:*.a.com Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []:
4、將證書請求文件發送給證書頒發機構(主機B)
我們需要將證書請求文件發送給證書頒發機構。
[root@centos6 ssl]$scp /etc/httpd/conf.d/ssl/httpd.csr 172.18.2.77:/etc/pki/CA/ root@172.18.2.77's password: httpd.csr 100% 647 0.6KB/s 00:00
5、在根CA(主機B)頒發證書
我們需要將證書請求文件發送給證書頒發機構。
# 根據主機A提交的證書申請內容,生成證書 [root@localhost ~]#openssl ca -in /etc/pki/CA/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365 *****************中間省略了輸出信息***********************
將生成的證書文件頒發給申請者(主機A)
# 前面我們說過,證書文件都存放在/etc/httpd/conf.d/ssl/目錄下 [root@localhost ~]#scp /etc/pki/CA/certs/httpd.crt 172.18.2.66:/etc/httpd/conf.d/ssl/ The authenticity of host '172.18.2.66 (172.18.2.66)' can't be established. RSA key fingerprint is 00:c0:e5:a6:39:e9:a7:bb:1b:f4:ab:0d:75:9b:38:b0. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '172.18.2.66' (RSA) to the list of known hosts. root@172.18.2.66's password: httpd.crt 100% 3714 3.6KB/s 00:00
同時將根CA的證書也發送給Web服務器(主機A)。這一點很重要。
[root@localhost ~]#scp /etc/pki/CA/cacert.pem 172.18.2.66:/etc/httpd/conf.d/ssl/ root@172.18.2.66's password: cacert.pem 100% 1334 1.3KB/s 00:00
在主機A上配置Web服務器
首先配置Web服務器的私鑰文件和證書文件,編輯”/etc/httpd/conf.d/ssl.conf”
然后將根CA的證書路徑,配置在配置文件中。
配置虛擬機主機站點
在/etc/httpd/conf.d/目錄下創建一個以.conf結尾的文件,並寫上:
在ssl.conf文件中進行配置:
我這里是在windows系統上操作的,需要在windows的hosts文件中添加ip映射才可以
接下來在瀏覽器中添加根證書
最后我們來訪問一下:
好了,這樣子就配置成功了。