自建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 []:[email protected]

[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 []:[email protected]

 

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 [email protected]:/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              = [email protected]

        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.178linux.com/26527

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

相关推荐

  • grep的用法(CentOS7)及有关正则表达式的使用

    http://www.cnblogs.com/wzhuo/p/6659352.html

    Linux干货 2017-04-08
  • Shell脚本编程基础中() (()) [ ] [[ ]] 的使用

    Shell脚本编程基础中() (()) [ ] [[ ]] 的使用 () 生成子进程,括号内的命令将会在子进程中运行,父进程不能够读取在子进程中创建的变量 例: 新建个脚本文件,写入 则执行结果为 $()相当于 ` `  ,返回括号内命令执行结果 (( )) 用作四则运算和逻辑运算,并且支持多个表达式 例: 当 (( )) 加$,则是将获得表达式值,赋值给左…

    2017-11-26
  • 日志系统

    日志管理 发送日志命令,通知用户:logger  -p  local7.info “this is a test log” Local#.info 为自定义到文件或目录,或用户的级别 日志远程存储需要启动514端口,在centos7上需要取消注释 Uptime可以查看开机时间 rsyslog特性:CentOS6和7 Ü多线程 ÜUDP…

    Linux干货 2018-02-01
  • OPenSSL

    OPenSSL   OpenSSL 是一个安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用.   SSL是Secure Sockets Layer(安全套接层协议)的缩写,可以在Internet上提供秘密性传输。Netscape公司在推出第一个Web浏览器的同时,提出了SSL协议标准。…

    Linux干货 2016-11-07
  • 马哥教育网络班21期+第6周课程练习

    VIM基础知识 常用模式: 编辑模式–>命令模式 输入模式 末行模式:内置的命令行接口 模式转换: 编辑模式–>输入模式 i:在光标所在处前转换为输入模式; a:在光标所在后面转入输入模式; o:在当前光标所在行的下方打开一个新行,并转为输入模式; I:在当前光标所在行的行首输入; A:在当前光标所在行的行尾输入; O:在当前光标所在行的上方…

    Linux干货 2016-08-15
  • 学习宣言

    别人笑我太疯癫,我笑他人看不穿。不见五陵豪杰墓,无花无酒锄作田。

    Linux干货 2016-10-24

评论列表(2条)

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

    写的不错,思路清晰,能有一些代码高亮显示就更好了。

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

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