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

(0)
马哥Net19_小斌斌马哥Net19_小斌斌
上一篇 2016-07-28
下一篇 2016-07-29

相关推荐

  • Nginx lnmp环境及https的实现

    一、http事务简明  request: <method> <URL> <VERSION> MHADERS <body> response: <version><status><reason phrase> <HEADERS> … <body&…

    2016-07-27
  • HAProxy浅说

    HAProxy浅说:    HAProxy响应码:        200:请求正常,响应正常,也就是正常响应码     301:配置使用的重定向,以下都是有关于重定向的一些响应码,不做解释     302:    &nb…

    2017-05-18
  • 创建CA 和申请证书

    创建CA 和申请证书1生成私有CA的私钥:(umask 066;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)注:CA的私钥文件必须放在/etc/pki/CA/private/cakey.pem 这个路径里2创建序列号 ,数据库文件touch  /etc/pki/CA/index.tx…

    Linux干货 2017-07-17
  • 文件权限

     本篇博客是对文件权限的简单介绍,将会简述下权限的数字表现形式,还有字母表现形式,还有一些特殊的suid、sgid、sticky的权限介绍,还有对ACL权限的简述。  一、权限的定义     关于权限,百度百科的解释如下:权限(privilege)是指某个特定的用户具有特定的系统资源使用权力,像是文…

    Linux干货 2017-07-29
  • Linux常见小知识点

    什么是Linux? Linux是一款支持多任务,多线程,多用户的类Unix系统 Linux常见的发行版本 Redhat  Linux   Linux发行版中比较重要的一个版本,多用于企业,由Redhat公司提供收费技术支持和更新,其衍生版本有centos为免费版本 Debain linux      系统分为…

    Linux干货 2017-03-26
  • IoC/DIP其实是一种管理思想

    关于IoC的的概念提出来已经很多年了,其被用于一种面象对像的设计。我在这里再简单的回顾一下这个概念。我先谈技术,再说管理。 话说,我们有一个开关要控制一个灯的开和关这两个动作,最常见也是最没有技术含量的实现会是这个样子: 然后,有一天,我们发现需要对灯泡扩展一下,于是我们做了个抽象类: 但是,如果有一天,我们发现这个开关可能还要控制别的不单单是灯泡的东西,我…

    Linux干货 2016-08-15

评论列表(2条)

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

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

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

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