企业环境中,在安全级别要求较高的公司,经常需要搭建基于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.178linux.com/26527
评论列表(2条)
写的不错,思路清晰,能有一些代码高亮显示就更好了。
@马哥教育:嗯嗯,下次注意