Linux:https静态网站搭建案例

介绍https

整个实验是在http实验基础上进行的

因为http协议在传输的时候采用的是明文传输,有安全隐患,所以出现了https(安全套接字层超文本传输协议)

HTTPS并不是一个新协议,
而是HTTP+SSL(TLS)。原本HTTP先和TCP(假定传输层是TCP协议)直接通信,而加了SSL后,就变成HTTP先和SSL通信,再由SSL和TCP通信,相当于SSL被嵌在了HTTP和TCP之间。

SSL协议提供的服务:

1)认证用户和服务器,确保数据发送到正确的客户机和服务器
2)加密数据以防止数据中途被窃取
3)维护数据的完整性,确保数据在传输过程中不被改变。

1, 客户端浏览器向服务器端发送如下信息:
(1)客户端支持的SSL /TLS协议的版本号。
(2)Cipher Suite(密钥算法套件)。
(3)客户端产生的随机数,稍后用于生成"对话密钥"。

2,为了实现https通信,需要安装 mod_ssl 包,主配置文件在

 /etc/httpd/conf.d/ssl.conf

该文件下面的2个重要参数:后面访问https服务器需要提供证书,这2个证书需要生成

SSLCertificateFile /etc/pki/tls/certs/localhost.crt      服务器颁发的证书文件路径
SSLCertificateKeyFile /etc/pki/tls/private/localhost.key   服务器私钥文件

https通信过程

服务器端向客户端发送如下信息:

1. 确认使用的加密通信协议版本,如果浏览器与服务器支持的版本不一致,服务器关闭加密通信。
2. 确认使用的加密方法。
3. 服务器证书。
4. 服务器生成的随机数,稍后用于生成"对话密钥"
5. 客户端用服务器传过来的信息验证服务器的合法性。如果合法性验证没有通过,通讯将断开;如
果合法性验证通过,则可以知道认证服务器的公开密钥的是真实有效的数字证书认证机构,并且服
务器的公开密钥是值得信赖的。(此处认证机关的公开密钥必须安全地转交给客户端。使用通信方
式时,如何安全转交是一件很困难的事,因此,多数浏览器开发商发布版本时,会事先在内部植入
常用认证机关的公开密钥。)
4. 客户端随机产生一个用于后面通讯的对称密钥,然后用服务器的公钥对其加密,然后将加密后的对
称密钥传给服务器。

要求

创建一个ip 192.168.190.103/24 基于https协议访问的静态网站,documentroot 为 /var/www/html/index.html ,访问的结果显示为https

步骤1、
创建一个ip

[root@localhost f1]# nmcli connection modify ens160 +ipv4.addresses 192.168.190.103/24 ipv4.method manual 
[root@localhost f1]# nmcli c up ens160 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)

2,新建.conf 文件

在 /etc/httpd/conf.d/ 创建一个 https_test.conf 配置文件,在这个文件中写入:

<virtualhost 192.168.107.190:443>    https 服务器ip 端口为443
    servername 192.168.107.190    服务器主机名
    documentroot /var/www/html     默认访问的主目录
    sslengine on     启动 ssl
    sslcertificatefile /etc/pki/tls/certs/https_test.crt    颁发证书的文件所在
    sslcertificatekeyfile /etc/pki/tls/certs/https_test.key   服务器证书私钥
</virtualhost>

3,密钥和证书生成

私钥:

[root@localhost f1]# openssl genrsa -aes128 2048 > /etc/pki/tls/certs/https_test.key
解释: 以aes128格式生成 一个2048 位的rsa私钥文件

证书生成:

解释:用刚刚生成的私钥自己申请一个证书
[root@localhost f1]openssl req -x509 -key /etc/pki/tls/certs/https_test.key 
Enter pass phrase for /etc/pki/tls/certs/https_test.key:        输入密码不低于4位
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]:zg      随便输入
State or Province Name (full name) []:cq   随便
Locality Name (eg, city) [Default City]:    可以回车跳过
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

证书输出到文件
[root@localhost f1]# openssl req -x509 -key /etc/pki/tls/certs/https_test.key -out /etc/pki/tls/certs/https_test.crt
Enter pass phrase for /etc/pki/tls/certs/https_test.key:   输入密码
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]:zg
State or Province Name (full name) []:cq
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

4:重启服务

[root@localhost f1]# systemctl restart httpd
Enter TLS private key passphrase for 192.168.107.190:443 (RSA) : ****     输入设置的密码则启动成功

5,客户端测试

[root@localhost ~]# curl https://192.168.190.103:443 -k          用https协议访问服务器 需要加上 -k 跳过不安全连接的提示
https       

在浏览器中访问:显示不是安全的连接,可以直接点击advanced 跳过就可以看到内容了。
因为自己创建颁发的证书是没有权威性的,浏览器会提示这个证书不安全。

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员Fy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值