HTTPS 配置与动态 Web 内容部署指南

HTTPS 配置与动态 Web 内容部署指南

在这里插入图片描述

一、HTTPS 基础概念

HTTPS(Hypertext Transfer Protocol Secure)是 HTTP 与 SSL/TLS 的结合,通过加密传输保障数据安全:

  • HTTP:明文传输,默认端口 80
  • HTTPS:加密传输,默认端口 443
  • 核心原理:通过 SSL/TLS 协议建立安全连接,使用证书验证身份并协商会话密钥

HTTPS 握手过程

  1. 客户端(Client)发起请求
    • 发送ClientHello:包含支持的 SSL/TLS 版本、加密算法列表,及随机数random_c(32 字节)
  2. 服务器(Server)响应
    • 发送ServerHello:确定使用的版本和加密算法,及随机数random_s(32 字节)
    • 发送ServerCertificate:包含服务器证书和公钥
  3. 客户端后续操作
    • 发送ClientKeyExchange:用服务器公钥加密预主密钥pre_master并发送
  4. 会话密钥生成
    • 服务器用私钥解密pre_master
    • 双方使用random_c + random_s + pre_master生成会话密钥,用于后续数据加密传输

二、HTTPS 证书配置(自签名 CA 模式)

前提准备

在 DNS 服务器的正向解析文件中添加 CA 和 Web 服务器的记录:

vim /var/named/zhang3.com   #编辑正向解析文件
# 添加以下内容

ca  IN  A  192.168.100.10    # CA服务器IP
web IN  A  192.168.100.20    # Web服务器IP

#重启DNS服务

[root@zhangyiwei ~]# systemctl restart named

1. 搭建私有 CA 服务器(主机 CA:192.168.100.10)

步骤 1:生成 CA 私钥
# 生成私钥(权限限制为077,仅root可读写)
[root@zhangyiwei named]# (umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem)
Generating RSA private key, 2048 bit long modulus
..........................................................................................................+++
........+++
e is 65537 (0x10001)
步骤 2:生成 CA 自签名证书
[root@zhangyiwei named]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 365

执行后需输入以下信息:

  • 国家(Country Name):CN
  • 省份(State or Province):HB
  • 城市(Locality):WH
  • 组织名称(Organization):LQ
  • 组织单位(Organizational Unit):linux
  • 服务器主机名(Common Name):ca.example.com
  • 邮箱(Email Address):root@example.com
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) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:LQ
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:ca.example.com
Email Address []:root@example.com
步骤 3:创建 CA 所需目录和文件
# 证书索引数据库
[root@zhangyiwei ~]# touch /etc/pki/CA/index.txt

# 证书序列号文件(初始值为01)
[root@zhangyiwei ~]# touch /etc/pki/CA/serial
[root@zhangyiwei ~]# echo 01 > /etc/pki/CA/serial

2. 配置 Web 服务器(主机 WEB:192.168.100.10)

步骤 1:生成 Web 服务器私钥
# 创建存放证书的目录
[root@zhangyiwei-2 ~]# mkdir /etc/httpd/ssl

# 生成私钥(限制权限)[root@zhangyiwei-2 yum.repos.d]# mkdir /etc/httpd/ssl
[root@zhangyiwei-2 ~]# (umask 077;openssl genrsa -out /etc/httpd/ssl/httpd.key)
Generating RSA private key, 2048 bit long modulus
...........................+++
..........................+++
e is 65537 (0x10001)
(umask 077; openssl genrsa -out /etc/httpd/ssl/httpd.key)
步骤 2:生成证书签署请求(CSR)
[root@zhangyiwei-2 ~]# openssl req -new -key /etc//httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csr -days 365

输入信息(除 Common Name 外与 CA 一致):

ssl req -new -key /etc//httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csr -days 365
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) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:LQ
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:web.example.com
Email Address []:root@example.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:    
An optional company name []:

步骤 3:将 CSR 发送给 CA 服务器
[root@zhangyiwei ~]# scp -p /etc/httpd/ssl/httpd.csr root@ca.example.com:/etc/pki/CA/

3. CA 服务器签署 Web 证书

# 对CSR进行签名,生成Web证书
[root@zhangyiwei ~]# openssl ca -in /etc/pki/CA/httpd.csr -out/etc/pki/CA/certs/httpd.crt -days 365

4. Web 服务器获取签署后的证书

[root@zhangyiwei-2 ~]# scp root@ca.example.com:/etc/pki/CA/certs/httpd.crt /etc/httpd/ssl/

5. 配置 Apache 支持 HTTPS

步骤 1:安装 SSL 模块
[root@zhangyiwei-2 ~]# yum -y install mod_ssl
已加载插件:fastestmirror, langpacks
源 'aa' 在配置文件中未指定名字,使用标识代替
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 mod_ssl.x86_64.1.2.4.6-88.el7.centos 将被 安装
--> 解决依赖关系完成

依赖关系解决

================================================================================================================================
 Package                     架构                       版本                                       源                      大小
================================================================================================================================
正在安装:
 mod_ssl                     x86_64                     1:2.4.6-88.el7.centos                      aa                     112 k

事务概要
================================================================================================================================
安装  1 软件包

总下载量:112 k
安装大小:224 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : 1:mod_ssl-2.4.6-88.el7.centos.x86_64                                                                        1/1 
  验证中      : 1:mod_ssl-2.4.6-88.el7.centos.x86_64                                                                        1/1 

已安装:
  mod_ssl.x86_64 1:2.4.6-88.el7.centos                                                                                          

完毕!

生成测试网页文件

root@zhangyiwei-2 ~]# cd /var/www/html/
[root@zhangyiwei-2 html]# echo Hello,World > index.html
步骤 2:修改 SSL 配置文件
[root@zhangyiwei-2 ~]# vim /etc/httpd/conf.d/ssl.conf 
# 修改确保以下两行指向正确的证书和私钥路径

SSLCertificateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
步骤 3:配置 HTTPS 虚拟主机
[root@zhangyiwei-2 ~]# vim /etc/httpd/conf.d/httpd-vhosts.conf

# 添加HTTPS虚拟主机配置
<VirtualHost 192.168.100.20:443>
    DocumentRoot "/var/www/html"
    ServerName web.example.com
    SSLEngine on                                    # 启用SSL
    SSLCertificateFile /etc/httpd/ssl/httpd.crt     # 服务器证书
    SSLCertificateKeyFile /etc/httpd/ssl/httpd.key  # 服务器私钥
</VirtualHost>

测试:

[root@zhangyiwei-2 ~]# curl -k https://web.example.com
Hello,World

5. 客户端信任 CA 根证书

步骤 1:下载 CA 根证书
# 在客户端执行,获取CA的根证书
[root@zhangyiwei-2 ~]# scp root@ca.example.com:/etc/pki/CA/cacert.pem /root
步骤 2:导入证书到浏览器(以火狐为例)
  1. 打开设置 → 首选项 → 隐私与安全 → 证书 → 查看证书
  2. 点击 “导入”,选择下载的cacert.pem
  3. 勾选 “信任使用此 CA 标识的网站”,完成导入
步骤 3:访问 HTTPS 站点

在浏览器中访问:https://web.example.com,应显示安全连接。

三、集成动态 Web 内容(Python)

1. 安装所需组件

#mod_wsgi是Apache的Python接口模块

[root@zhangyiwei-2 ~]# yum -y install httpd mod_wsgi
已加载插件:fastestmirror, langpacks
源 'aa' 在配置文件中未指定名字,使用标识代替
Loading mirror speeds from cached hostfile
软件包 httpd-2.4.6-88.el7.centos.x86_64 已安装并且是最新版本
正在解决依赖关系
--> 正在检查事务
---> 软件包 mod_wsgi.x86_64.0.3.4-18.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

================================================================================================================================
 Package                        架构                         版本                                源                        大小
================================================================================================================================
正在安装:
 mod_wsgi                       x86_64                       3.4-18.el7                          aa                        77 k

事务概要
================================================================================================================================
安装  1 软件包

总下载量:77 k
安装大小:198 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : mod_wsgi-3.4-18.el7.x86_64                                                                                  1/1 
  验证中      : mod_wsgi-3.4-18.el7.x86_64                                                                                  1/1 

已安装:
  mod_wsgi.x86_64 0:3.4-18.el7                                                                                                  

完毕!

2. 部署动态 Web 内容

# 创建存放Python脚本的目录
[root@zhangyiwei-2 ~]# mkdir /var/www/wsgi
[root@zhangyiwei-2 ~]# cd /var/www/wsgi
# 将Python编写的动态Web脚本(如webapp.py)上传至此目录

3. 配置虚拟主机(HTTP)

# 编辑配置文件
[root@zhangyiwei-2 ~]vim /etc/httpd/conf.d/httpd-vhosts.conf
# 添加以下内容
<VirtualHost 172.16.30.20:80>
    DocumentRoot "/var/www/wsgi"
    WSGIScriptAlias / "/var/www/wsgi/webapp.py"  # 关联Python脚本
    ServerName py.example.com
</VirtualHost>

4. 配置 DNS 解析

[root@zhangyiwei-2 ~]vim /var/named/zhang3
# 添加解析记录
py  IN  A  192,168.100.20

# 重启DNS服务
[root@zhangyiwei-2 ~]systemctl restart named

在客户端访问:http://py.example.com,验证动态内容是否正常加载。

通过以上步骤,可完成 HTTPS 安全站点的部署及动态 Web 内容的集成,实现加密传输与动态交互功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值