apache下部署ssl证书|Apache下部署ssl协议的步骤介绍

更新时间:2019-09-28    来源:apache    手机版     字体:

【www.bbyears.com--apache】

https(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的 http 通道,简单讲是 http 的安全版。即 http 下加入 ssl 层,https 的安全基础是 ssl,因此加密的详细内容就需要 ssl。
个人网站用 ssl 证书,国外这几年开始比较流行起来,尤其是 Google 对使用了 ssl 证书的网站收录良好,越来越多的网站开始使用 https 访问了。但是国内的一众搜索引擎对 ssl 就不那么友好了,而且使用 ssl 后会拖慢网站浏览速度。
不管怎样,安全性越来越受到重视总归不是一件坏事。
那么在 Apache 里配置 ssl 是怎样的,我们一步一步来。


一、使用 OpenSSL 生成证书请求
生成 rsa 私钥(2048位)

openssl genrsa -out teddysun.pem 2048

Generating RSA private key, 2048 bit long modulus
.........................+++
................................................................+++
e is 65537 (0x10001)
生成 csr 证书请求

openssl req -new -key teddysun.pem -out teddysun.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) []:Shanghai  // 省
Locality Name (eg, city) [Default City]:Shanghai  // 城市
Organization Name (eg, company) [Default Company Ltd]:Teddysun // 组织或公司名
Organizational Unit Name (eg, section) []:  // 不填
Common Name (eg, your name or your server"s hostname) []:*.teddysun.com // 此处演示是通配符域名,一般带 www 前缀即可
Email Address []:admin@teddysun.com  // 邮箱地址

Please enter the following "extra" attributes
to be sent with your certificate request
A challenge password []:  // 不填
An optional company name []:  // 不填
查看生成完毕的文件

ll *.pem *.csr

-rw-r--r-- 1 root root 1058 Jan 22 14:30 teddysun.csr
-rw-r--r-- 1 root root 1675 Jan 22 14:25 teddysun.pem
将上述 2 个文件下载到本地,用记事本打开 teddysun.csr,里面的完整内容就是证书请求了。

二、签发证书

如果需要默认受信任的证书,则需要花钱购买了。比如
https://www.namecheap.com/security/ssl-certificates/comodo/positivessl.aspx
只需 9 美元/年。
利用第一步生成的 csr 文件,即可申请证书,过程略,网上有一堆图文教程。

另外,证书可以自签,但是使用的时候需要安装根证书,否则便不受信任。
下面说说自签名证书步骤

生成根证书 rsa 私钥(2048位)

openssl genrsa -out ca.pem 2048

Generating RSA private key, 2048 bit long modulus
...............................................+++
.........+++
e is 65537 (0x10001)
利用私钥创建根证书

openssl req -new -x509 -days 3650 -key ca.pem -out ca.crt

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) []:Shanghai
Locality Name (eg, city) [Default City]:Shanghai
Organization Name (eg, company) [Default Company Ltd]:Teddysun
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server"s hostname) []:teddysun.com Root CA
Email Address []:admin@teddysun.com
至此,有效期为 3650 天的根证书 ca.crt 就创建完毕了。
查看生成完毕的文件

ll *.pem *.csr *.crt

-rw-r--r-- 1 root root 1411 Jan 22 15:14 ca.crt
-rw-r--r-- 1 root root 1679 Jan 22 14:48 ca.pem
-rw-r--r-- 1 root root 1041 Jan 22 14:40 teddysun.csr
-rw-r--r-- 1 root root 1675 Jan 22 14:25 teddysun.pem
利用根证书签发 ssl 证书

openssl ca -in teddysun.csr -out teddysun.crt -cert ca.crt -keyfile ca.pem
这一步出错了,错误信息如下

Using configuration from /etc/pki/tls/openssl.cnf
/etc/pki/CA/index.txt: No such file or directory
unable to open "/etc/pki/CA/index.txt"
140292081481544:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen("/etc/pki/CA/index.txt","r")
140292081481544:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:
提示缺少文件,那我们就生成一个空文件

touch /etc/pki/CA/index.txt
继续执行

openssl ca -in teddysun.csr -out teddysun.crt -cert ca.crt -keyfile ca.pem
又出错了,错误信息如下

Using configuration from /etc/pki/tls/openssl.cnf
/etc/pki/CA/serial: No such file or directory
error while loading serial number
140375536244552:error:02001002:system library:fopen:No such file or directory:bss_file.c:398:fopen("/etc/pki/CA/serial","r")
140375536244552:error:20074002:BIO routines:FILE_CTRL:system lib:bss_file.c:400:
还是缺少文件,这里生成一个内容为 01 的文件

echo "01" > /etc/pki/CA/serial
再次执行

openssl ca -in teddysun.csr -out teddysun.crt -cert ca.crt -keyfile ca.pem

Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Jan 22 07:35:14 2015 GMT
            Not After : Jan 22 07:35:14 2016 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = Shanghai
            organizationName          = Teddysun
            commonName                = *.teddysun.com
            emailAddress              = admin@teddysun.com
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                E8:7F:9E:BE:12:25:22:48:A2:49:AE:D5:CB:A6:7B:24:EE:A6:E2:5B
            X509v3 Authority Key Identifier:
                keyid:C7:0C:EF:F2:73:8C:CB:01:7A:8F:9C:30:A7:80:37:FA:E7:B6:88:02

Certificate is to be certified until Jan 22 07:35:14 2016 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
至此,就签发了一个默认有效期为 365 天的通配符域名 *.teddysun.com 的 ssl 证书了。
如果想签发更长有效期的证书,比如 730 天的,则命令如下:

openssl ca -in teddysun.csr -out teddysun2.crt -days 730 -cert ca.crt -keyfile ca.pem
最长不能超过根证书的有效期。

查看最终生成完毕的文件

ll *.pem *.csr *.crt

-rw-r--r-- 1 root root 1411 Jan 22 15:14 ca.crt
-rw-r--r-- 1 root root 1679 Jan 22 14:48 ca.pem
-rw-r--r-- 1 root root 4594 Jan 22 15:35 teddysun.crt
-rw-r--r-- 1 root root 1041 Jan 22 14:40 teddysun.csr
-rw-r--r-- 1 root root 1675 Jan 22 14:25 teddysun.pem
分别解释一下这 5 个文件。
ca.crt 是签发完成的根证书文件
ca.pem 是根证书的私钥文件
teddysun.crt 是签发完成的域名证书文件
teddysun.csr 是证书请求文件
teddysun.pem 是签发域名的私钥

如果要使用自签名的证书,需要在本机导入根证书,步骤如下:
点击“安装证书”,下一步,将所有的证书放入下列存储,浏览,选择“收信任的根证书颁发机构”,下一步,完成。

 

安装好根证书后,再打开后,就是这个样子的:

 

 

自签名证书则是这个样子的:

 

 

 

三、在 Apache 下部署 ssl 证书

 

首先需要合并一下证书文件,用记事本打开 ca.crt 文件,全选,复制,再用记事本打开 teddysun.crt ,在其内容的最后,回车,粘贴 ca.crt 的全部内容,保存。
当然,如果是在 namecheap 申请到的证书,发到你邮箱的时候都都打包好了,证书也合并好了,直接使用即可。

 

在安装完 LAMP 环境后,默认是没有加载 https 配置的,这里需要修改一下配置文件 /usr/local/apache/conf/httpd.conf ,找到下面的
#Include conf/extra/httpd-ssl.conf ,将前面的 # 去掉,保存。
将合并后的证书 teddysun.crt 和私钥 teddysun.pem 复制到 /usr/local/apache/conf/ 下。
再修改配置文件 /usr/local/apache/conf/extra/httpd-ssl.conf ,sample 内容如下:

# port
Listen 443
#   SSL Cipher Suite:
#   List the ciphers that the client is permitted to negotiate.
#   See the mod_ssl documentation for a complete list.
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5

SSLPassPhraseDialog  builtin

SSLSessionCache        "shmcb:/usr/local/apache/logs/ssl_scache(512000)"
SSLSessionCacheTimeout  300

##
## SSL Virtual Host Context
##


DocumentRoot /data/www/default
ServerName teddysun.com
ServerAlias www.teddysun.com
ErrorLog "/usr/local/apache/logs/lamp_error_log"
TransferLog "/usr/local/apache/logs/lamp_access_log"

SSLEngine on

SSLCertificateFile /usr/local/apache/conf/teddysun.crt
SSLCertificateKeyFile /usr/local/apache/conf/teddysun.pem
#SSLCertificateChainFile /usr/local/apache/conf/alphassl.pem
#SSLCACertificateFile /usr/local/apache/conf/root.pem

CustomLog "/usr/local/apache/logs/lamp_ssl_request_log"
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x "%r" %b "%{Referer}i" "%{User-Agent}i""

BrowserMatch "MSIE [2-5]"
nokeepalive ssl-unclean-shutdown
downgrade-1.0 force-response-1.0


上述配置都修改完毕后,注意防火墙要放行 443 端口。查看防火墙状态:

/etc/init.d/iptables status


然后再重启 Apache ,命令:

/etc/init.d/httpd restart
好了,现在就可以愉快地用 https 访问网站了。

本文来源:http://www.bbyears.com/jiaocheng/69912.html

热门标签

更多>>

本类排行