1.https定义:
HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入SSL层,HTTPS的安全基础是SSL,因此加密的详细内容就需要SSL。它是一个URI scheme(抽象标识符体系),句法类同http:体系。用于安全的HTTP数据传输。https:URL表明它使用了HTTP,但HTTPS存在不同于HTTP的默认端口及一个加密/身份验证(在HTTP与TCP之间)。这个系统的最初研发由网景公司(Netscape)进行,并内置于其浏览器Netscape Navigator中,提供了身份验证与加密通讯方法
2.设置https
1.自定义自签名证书
如果加密的通信非常重要,而经过验证的身份不重要,管理员可以通过生成self-signed certificate来避免与认证机构进行交互所带来的复杂性。使用genkey实用程序(通过crypto-utils软件包分发),生成自签名证书及其关联的私钥。为了简化起见,genkey将在“正确”的位置(/etc/pki/tls目录)创建证书及其关联的密钥。相应地,必须以授权用户(root)身份运行该实用程序。
1.生成自签名证书
[root@test ~]# yum install mod_ssl -y
[root@test ~]# yum install crypto-utils.x86_64 -y
2.调用genkey,同时为生成的文件指定唯一名称(例如,服务器的主机全名) - -days可以指定证书有效期
[root@test ~]# genkey www.westos.com
记录生成的证书(www.westos.com.crt)和关联的私钥(
www.westos.com.key)的位置
继续使用对话框,并选择合适的密钥大小。(默认的2048位密钥为推荐值)
在生成随机数时比较慢,敲键盘和移动鼠标可以加速
拒绝向认证机构(CA)发送证书请求(CSR)
拒绝加密私钥
为服务器提供合适的身份。Common Name必须与服务器的主机全名完全匹配。
(注意,任何逗号都应使用前导反斜线[]进行转义)
做完上述步骤之后产生代码块如下
[root@test ~]# genkey www.westos.com
/usr/bin/keyutil -c makecert -g 1024 -s "CN=www.westos.com, OU=linux, O=westos, L=xi'an, ST=shannxi, C=CN" -v 1 -a -z /etc/pki/tls/.rand.1913 -o /etc/pki/tls/certs/www.westos.com.crt -k /etc/pki/tls/private/www.westos.com.key
cmdstr: makecert
cmd_CreateNewCert
command: makecert
keysize = 1024 bits
subject = CN=www.westos.com, OU=linux, O=westos, L=xi'an, ST=shannxi, C=CN
valid for 1 months
random seed from /etc/pki/tls/.rand.1913
output will be written to /etc/pki/tls/certs/www.westos.com.crt
output key written to /etc/pki/tls/private/www.westos.com.key
Generating key. This may take a few moments...
Made a key
Opened tmprequest for writing
/usr/bin/keyutil Copying the cert pointer
Created a certificate
Wrote 882 bytes of encoded data to /etc/pki/tls/private/www.westos.com.key
Wrote the key to:
/etc/pki/tls/private/www.westos.com.key
2安装证书及其私钥
- 确定已安装mod_ssl软件包
[root@test ~]# yum install mod_ssl -y - 由于私钥是敏感信息,请确保其只被root用户读取
[root@test ~]# ls -l /etc/pki/tls/private/www.westos.com.key
-r——– 1 root root 937 May 15 06:05 /etc/pki/tls/private/www.westos.com.key 编辑/etc/httpd/conf.d/ssl.conf, 将SSLCertificateFile和SSLCertificateKeyFile指令设置为分别指向X.509证书和密钥文件
[root@test ~]# vim /etc/httpd/conf.d/ssl.conf
100 SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
107 SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key重启Web服务器。
[root@test ~]# systemctl restart httpd- 如要进行确认,请使用https协议(https://serverX.example.com)通过Web客户端(如Firefox
)访问Web服务器。Web客户端可能会发出它不认可证书发行者的警告。这种情况适用自签名证书。要求Web客户端绕过证书认证。(对于Firefox,请选择“I Understand the Risks” [我了解风险]、“Add Exception” [添加例外]和“Confirm Security Exception”[确认安全例外]。)
3 网页重写
把所有80端口的请求全部重定向由https来处理
1.编写子配置文件 使得所有80端口的请求全部重定向由https来处理
[root@test ~]# cp -p /etc/httpd/conf.d/news.conf /etc/httpd/conf.d/login.conf ##复制模板
[root@test ~]# vim /etc/httpd/conf.d/login.conf ##编写配置文件
<Virtualhost *:443> ##端口443
ServerName "login.westos.com" ##指定服务器名称。在使用基于名称的虚拟主机的情况下,此处的名称必须与客户端请求完全的匹配
DocumentRoot "/var/www/virtual/login.westos.com/html" ##在<VirtualHost>块内部,指定从中提供内容的目录
CustomLog "logs/login.log" combined
SSLEngine on ##开启https功能
SSLCertificateFile /etc/pki/tls/certs/localhost.crt ##证书
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key # 密钥
</Virtualhost>
<Directory "/var/www/virtual/login.westos.com/html">
Require all granted ##允许所有访问
</Directory>
<Virtualhost *:80> ##端口80
ServerName login.westos.com ##指定服务器名称
RewriteEngine on ##网页重写开启
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
## ^(/.*)$ 客户主机在地址栏中写入的所有字符,不包括换行符
## https:// 被定向的访问协议
## %{HTTP_HOST}$1 客户请求主机
## $1的值就表示^(/.*)$的值
## [redirect=301] 临时重定向 302永久重定向
</Virtualhost>
[root@test ~]# systemctl restart httpd.service ##重启服务
2 测试
在测试主机上做dns解析
[root@foundation48 ~]# vim /etc/hosts
172.25.254.10 www.westos.com news.westos.com money.westos.com login.westos.com
访问http://login.westos.com 会自动跳转到https://login.westos.com 实现网页数据加密传输
访问上边的自动变成如下图:http 变成了https