企業環境中,在安全級別要求較高的公司,經常需要搭建基于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均在監聽狀態
最后,測試主機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
如圖,可以看到,使用https已能正常訪問網站,搭建成功!(請忽略chrome關于數字證書的其它警告哈)
以上為自建CA搭建SSL加密網站的詳細描述!我對linux的認識還比較很膚淺,以上可能有不正確的地方,如有錯漏,希望各位能指正,共同進步。
我的QQ:153975050 小斌斌
在此感謝馬哥及馬哥團隊的所有人,在linux的道路上引領我一直前進!
2016-07-28
原創文章,作者:馬哥Net19_小斌斌,如若轉載,請注明出處:http://www.www58058.com/26527
寫的不錯,思路清晰,能有一些代碼高亮顯示就更好了。
@馬哥教育:嗯嗯,下次注意