自建CA搭建SSL加密網站

企業環境中,在安全級別要求較高的公司,經常需要搭建基于SSL加密傳輸的網站,使用https協議訪問web站點,能大大提高網站的安全性。但構建https站點,需要用到證書。內部網站到互聯網上申請費用不菲的證書顯然不符合經濟性。于是,自建內部CA成為我們的首選。

本文以兩臺服務器,分別扮演CA及Web網站的角色,詳細論述自建CA搭建加密網站的過程。

 

實驗環境:

CA:  OS:Centos6.6  IP:172.16.10.10  主機名稱: ca.test.net

Web Server:  OS:Centos7.2  IP:172.16.20.20   主機名稱:  web.test.net

(本文主要描述如何搭建https的網站,因而擬定web server已建好名為web.test.net的虛擬主機站點)

 

整個過程大體可分為:

(1)    為服務器申請數字證書
a.  創建私有CA

b. 在web服務器上創建證書簽署請求

c. CA簽發證書

(2)    配置httpd支持使用ssl,支持使用從CA簽發的證書

(3)    測試主機https訪問,完成SSL服務器搭建

 

下面是詳細的配置過程:

創建私有CA:

登錄CA服務器,執行

#cd  /etc/pki/CA/

##生成ca服務器的私鑰

#(umask 077; openssl genrsa -out private/cakey.pem  2048)

##生成index.txt 及 serial文件

#touch index.txt

#echo 01 > serial

 

##生成CA的自簽證書

[root@www CA]# openssl req -new -x509 -key  private/cakey.pem -out cacert.pem -days 7300

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) []:Nanhai

Locality Name (eg, city) [Default City]:Nanhai

Organization Name (eg, company) [Default Company Ltd]:MageEdu LTD  

Organizational Unit Name (eg, section) []:IT

Common Name (eg, your name or your server's hostname) []:ca.test.net

Email Address []:caadmin@test.net

[root@www CA]#

 

在web服務器上創建證書簽署請求:

轉到web服務器

#cd  /etc/httpd

#mkdir ssl

#cd ssl

##生成web服務器私鑰

##(umask 077; openssl genrsa -out httpd.key 1024)

##生成證書簽署請求

#openssl req -new -key httpd.key -out httpd.csr

其中各參數與之前CA證書上的保持一致,否則CA有可能拒簽該證書

[root@localhost ssl]# (umask 077; openssl genrsa -out httpd.key 1024)

Generating RSA private key, 1024 bit long modulus

..++++++

……………………………..++++++

e is 65537 (0x10001)

[root@localhost ssl]# openssl req -new -key httpd.key -out 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) []:Nanhai

Locality Name (eg, city) [Default City]:Nanhai

Organization Name (eg, company) [Default Company Ltd]:MageEdu LTD

Organizational Unit Name (eg, section) []:IT

Common Name (eg, your name or your server's hostname) []:www.test.net

Email Address []:webmaster@test.net

 

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:

An optional company name []:

[root@localhost ssl]#

 

##將生成的httpd.csr證書請求文件上傳到CA 上

[root@localhost ssl]# scp httpd.csr root@172.16.10.10:/tmp/

httpd.csr                                                                       100%  700     0.7KB/s   00:00   

[root@localhost ssl]#

 

##轉到CA服務器上簽發證書

[root@www CA]# openssl ca -in /tmp/httpd.csr -out certs/web.test.net.crt -days 365

Using configuration from /usr/local/openssl/ssl/openssl.cnf

Check that the request matches the signature

Signature ok

Certificate Details:

        Serial Number: 2 (0x2)

        Validity

            Not Before: Jul 28 16:56:27 2016 GMT

            Not After : Jul 28 16:56:27 2017 GMT

        Subject:

            countryName               = CN

            stateOrProvinceName       = Nanhai

            organizationName          = MageEdu LTD

            organizationalUnitName    = IT

            commonName                = www.test.net

            emailAddress              = webmaster@test.net

        X509v3 extensions:

            X509v3 Basic Constraints:

                CA:FALSE

            Netscape Comment:

                OpenSSL Generated Certificate

            X509v3 Subject Key Identifier:

                08:33:66:A2:B6:20:27:77:78:42:8D:FA:0E:00:49:DE:BE:57:F1:5B

            X509v3 Authority Key Identifier:

                keyid:11:10:82:7A:6A:8C:C7:C7:6F:D0:08:A3:55:4B:CF:BB:3C:2E:C2:9A

 

Certificate is to be certified until Jul 28 16:56:27 2017 GMT (365 days)

Sign the certificate? [y/n]:y

 

 

1 out of 1 certificate requests certified, commit? [y/n]y

Write out database with 1 new entries

Data Base Updated

[root@www CA]#

下面將已簽發的證書回傳到web服務器上

[root@www CA]# scp certs/web.test.net.crt 172.16.20.20:/etc/httpd/ssl/

web.test.net.crt                                 100%    0     0.0KB/s   00:00  

[root@www CA]#

至此,web服務器的證書已成功簽發創建完畢

配置httpd支持使用ssl:

httpd服務器要使用SSL,需要添加ssl的模塊,安裝很簡單:

#yum install mod_ssl

安裝完mod_ssl模塊后,會在系統添加相應的文件,其中比較重要的有:

/etc/httpd/conf.d/ssl.conf              ##ssl模塊配置文件

/usr/lib64/httpd/modules/mod_ssl.so     ##so文件

 

 

支持使用從CA簽發的證書:

#編輯ssl_conf文件

找到<VirtualHost _default_:443>一項,直接修改成web服務器的IP 地址

##可更改為你的主機IP

<VirtualHost 172.16.20.20:443>

 

##下面有幾個重要參數

SSLEngine On  ##啟用

SSLCertificateFile /etc/httpd/ssl/web.test.net.crt   ##重要,證書文件

SSLCertificateKeyFile /etc/httpd/ssl/httpd.key          ##重要,與證書文件匹配的私鑰

##還需要修改匹配的DocumentRoot 及 ServerName

DocumentRoot "/vhosts/web/htdoc"

ServerName web.test.net

其它的參數保持默認值即可

保存退出,重啟httpd

[root@localhost conf.d]# systemctl restart httpd.service

[root@localhost conf.d]#

 

查看監聽端口:80及443均在監聽狀態

監聽443.PNG

 

 

最后,測試主機https訪問,完成SSL服務器搭建

(為避免干擾,web服務器禁用selinux及iptables,客戶機設置hosts文件,指明web.test.net的IP地址為172.16.20.20)

[root@localhost conf.d]# systemctl stop firewalld.service

[root@localhost conf.d]# systemctl disable firewalld.service

rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'

rm '/etc/systemd/system/basic.target.wants/firewalld.service'

[root@localhost conf.d]#

 

在另一臺主機充當客戶端訪問網站,我這里使用win7,chrome瀏覽器,償試訪問 https://web.test.net

證書.PNG

如圖,可以看到,使用https已能正常訪問網站,搭建成功!(請忽略chrome關于數字證書的其它警告哈)

 

以上為自建CA搭建SSL加密網站的詳細描述!我對linux的認識還比較很膚淺,以上可能有不正確的地方,如有錯漏,希望各位能指正,共同進步。

 

我的QQ:153975050  小斌斌

在此感謝馬哥及馬哥團隊的所有人,在linux的道路上引領我一直前進!

                                                            2016-07-28

 

 

 

 

 

 

 

 

 

 

原創文章,作者:馬哥Net19_小斌斌,如若轉載,請注明出處:http://www.www58058.com/26527

(0)
馬哥Net19_小斌斌馬哥Net19_小斌斌
上一篇 2016-07-28 21:51
下一篇 2016-07-29 09:38

相關推薦

  • linux網絡屬性管理

    Linux網絡屬性配置 計算機網絡:TCP/IP:協議棧(使用)ISO,OSI:協議棧(學習) MAC:Media Access Control48bits:ICANN:24bits, 2^24地址塊:2^24 網橋(bridge):MAC地址表靜態指定:動態學習:根據原地址學習; 交換機(switch):多端口網橋; IP(Internet protoco…

    Linux干貨 2017-10-14
  • 磁盤分區和腳本練習

    1、創建一個10G分區,并格式為ext4文件系統 (1)要求其block大小為2048,預留空間百分比為2,卷標為MYDATA,默認掛載屬性包含acl; mke2fs -t ext4 -b 2048 -m 2 -L MYDATA /dev/sdb5 tune2fs -o acl /dev/sdb5 (2)掛載至/etc/mydate目錄,要求掛載時禁止自動運…

    Linux干貨 2017-08-21
  • 基于NFS實現WordPress

    實驗內容: (1)主機IP nfs server IP :192.168.29.120 nfs server IP: 192.168.29.110 (2)要求 nfs server共享/data/web/ 、/data/mysql 兩個目錄 nfs client掛載nfs server共享的/data/web/的文件系統至/var/www/html;部署wo…

    2017-06-13
  • 推薦-tree命令的安裝和使用

    一、前言     tree命令是可以把指定文件夾的所以文件用樹狀羅列出來,呈現目錄形式的一個命令。在Centos 6.5中默認不能直接使用: 輸入type tree命令(type COMMAND:區別是內建命令還是外部命令)提示不存在: 二、安裝       …

    系統運維 2016-03-27
  • CentOS系統詳解(啟動流程、kickstart)&bash腳本編程之while/函數用法

    1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情) 2、為運行于虛擬機上的CentOS 6添加一塊新硬件,提供兩個主分區; (1) 為硬盤新建兩個主分區;并為其安裝grub; (2) 為硬盤的第一個主分區提供內核和ramdisk文件; 為第二個分區提供rootfs; (3) 為rootfs提供bash、ls、cat程序及所依賴的庫文件…

    Linux干貨 2016-11-28
  • 數組,字符串處理,mktemp命令,install命令,bash的環境配置文件,程序包編譯

    數組 變量:存儲單個元素的內存空間 數組:存儲多個元素的連續的內存空間,相當于多個變量的集合。 數組名和索引 索引:編號從0開始,屬于數值索引 注意:索引可支持使用自定義的格式,而不僅是數值格式,即為關聯索引,bash4.0版本之后開始支持。 bash的數組支持稀疏格式(索引不連續) 聲明數組: declare -a ARRAY_NAME(普通數組可以不加聲…

    Linux干貨 2016-08-24

評論列表(2條)

  • 馬哥教育
    馬哥教育 2016-07-29 09:29

    寫的不錯,思路清晰,能有一些代碼高亮顯示就更好了。

    • 馬哥Net19_小斌斌
      馬哥Net19_小斌斌 2016-07-29 22:23

      @馬哥教育嗯嗯,下次注意

欧美性久久久久