当前位置: 首页 > 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.lryc.cn/news/624132.html

相关文章:

  • Pycharm Debug详解
  • mysql建库规范
  • Grid系统概述
  • 佳文赏读 || (CVPR 2025新突破) Robobrain:机器人操作从抽象到具体的统一大脑模型(A Unified Brain Model)
  • 基于Python的旅游推荐系统 Python+Django+Vue.js
  • SVN客户端下载与安装
  • 在鸿蒙中实现深色/浅色模式切换:从原理到可运行 Demo
  • 力扣第463场周赛
  • C++---迭代器删除元素避免索引混乱
  • 轻松配置NAT模式让虚拟机上网
  • LeetCode热题100--104. 二叉树的最大深度--简单
  • JavaScript性能优化实战(四):资源加载优化
  • 记SpringBoot3.x + Thymeleaf 项目实现(MVC架构模式)
  • 【Unity3D实例-功能-拔枪】角色拔枪(二)分割上身和下身
  • TDengine IDMP 运维指南(1. 部署规划)
  • 大模型算法岗面试准备经验分享
  • 母猪姿态转换行为识别:计算机视觉与行为识别模型调优指南
  • Java试题-选择题(10)
  • AMBA-AXI and ACE协议详解(四)
  • 计算机毕业设计java的小天鹅酒店月子会所管理小天鹅酒店母婴护理中心管理系统设计小天鹅酒店产后护理会所信息化管理平台
  • 物联网软件开发过程中,数据流图(DFD),用例图,类图,活动图,序列图,状态图,实体关系图(ERD),BPMN(业务流程建模)详解分析
  • 嵌入式练习项目——————抓包获取天气信息
  • Python大模型应用开发-核心技术与项目开发
  • C++编程实战:高效解决算法与数据结构问题
  • Linux817 shell:until,nfs,random
  • React 第七十节 Router中matchRoutes的使用详解及注意事项
  • Next.js跟React关系(Next.js是基于React库的全栈框架)(文件系统路由、服务端渲染SSR、静态生成SSG、增量静态再生ISR、API路由)
  • Vue 与 React 深度对比:设计哲学、技术差异与应用场景
  • 每日Java面试系列(15):进阶篇(String不可变的原因、性能问题、String三剑客、自定义不可变设计、组合优于继承等相关问题)
  • FreeRTOS源码分析八:timer管理(一)