当前位置: 首页 > news >正文

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 一致):

  • 服务器主机名(Common Name):web.example.com
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.comPlease 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.comSSLEngine on                                    # 启用SSLSSLCertificateFile /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 内容的集成,实现加密传输与动态交互功能。

http://www.dtcms.com/a/335485.html

相关文章:

  • Hadoop入门
  • SpringCloud 06 服务容错 Sentinel
  • NY270NY273美光固态闪存NY277NY287
  • 黎阳之光:以动态感知与 AI 深度赋能,引领电力智慧化转型新革命
  • mysql||事务相关知识
  • nertctl使用了解
  • Node.js导入MongoDB具体操作
  • IoT/HCIP实验-5/基于WIFI的智慧农业实验(LwM2M/CoAP+PSK+ESP8266 连接到 IoTDA)
  • python study notes[4]
  • Vue深入组件:Props 详解3
  • 【adb端口5555】烽火hg680-gy_烽火hg680-gc安卓9线刷烧录包 解决用一段时间就提示升级的问题
  • 回溯剪枝的 “减法艺术”:化解超时危机的 “救命稻草”(一)
  • 如何在 Ubuntu 24.04、22.04 或 20.04 Linux 中更改计算机名称
  • 智能化管理:开启海洋牧场新时代
  • 字节 Golang 大模型应用开发框架 Eino简介
  • Vue深入组件:Props 详解2
  • es7.17.x es服务yellow状态的排查查看节点,分片状态数量
  • 42 C++ STL模板库11-容器4-forward_list
  • C++算法竞赛:位运算
  • 线程(基本概念和相关命令)
  • CT01-反转链表(Java)
  • 从零开始:SpringBoot与KingbaseES的完美融合实践
  • 基于飞算JavaAI的可视化数据分析集成系统项目实践:从需求到落地的全流程解析
  • Java 大视界 -- Java 大数据分布式计算在基因测序数据分析与精准医疗中的应用(400)
  • Excel 表格数据自动填充
  • 【线程安全(二) Java EE】
  • 基于飞算JavaAI实现布隆过滤器防止缓存穿透:原理、实践与全流程解析
  • 【电路笔记 通信】AXI4-Lite协议 FPGA实现 Valid-Ready Handshake 握手协议
  • 【计算机网络面试】键入网址到网页显示期间,发生了什么?
  • Tomcat Connector连接器原理