web服务之Apache实现的https访问

本文旨在实践httpd-2.4基于域名的虚拟主机配置,让指定用户访问站点状态信息,并为站点提供https服务。

知识储备



HTTPS协议

HTTPS协议就是“HTTP协议”和“SSL/TLS”协议的结合,HTTP over SSL”或“HTTP over TLS”,对http协议的文本数据进行加密处理后,成为二进制形式传输.

SSL会话简化过程

(1) 客户端发送可供选择的加密方式,并向服务器请求证书;

(2) 服务器端发送证书以及选定的加密方式给客户端;

(3) 客户端取得证书并进行证书验正:

如果信任给其发证书的CA机构:则

(a) 验正证书来源的合法性;用CA的公钥解密证书上数字签名;

(b) 验正证书的内容的合法性:完整性验正

(c) 检查证书的有效期限;

(d) 检查证书是否被吊销;

(e) 证书中拥有者的名字,与访问的目标主机要一致;

(4) 客户端生成临时会话密钥(对称密钥),并使用服务器端的公钥加密此数据发送给服务器,

完成密钥交换;

(5) 服务用此密钥加密用户请求的资源,响应给客户端;

注意:SSL会话是基于IP地址创建;所以单IP的主机上,仅可以使用一个https虚拟主机;

SSL/TLS协议模型

环境准备



1.操作系统及软件

2台 Centos 7.2 x86_64

httpd:httpd-2.4.6-40.el7.centos.x86_64

2.IP地址

172.16.52.51/16 web服务

172.16.52.1/6  CA证书颁发机构

2.提供2个基于域名的虚拟主机

域名 www1.linux.com、www2.linux.com

站点目录:/web/vhosts/www{1,2}

访问日志:/var/log/httpd/www{1,2}/www{1,2}.access_log

错误日志:/var/log/httpd/www{1,2}/www{1,2}.error_log

3.输出www1.linux.com的状态信息,且要求只允许提供账号的用户访问

4.www1不允许10.0.0.0/24网络中的主机访问

5.为www2提供https服务。

注意:关闭防火墙和selinux

安装httpd并配置虚拟主机



1.安装httpd

[[email protected] ~]# yum -y install httpd

2.注释主配置文件的DocumentRoot

[[email protected] ~]# grep "#DocumentRoot" /etc/httpd/conf/httpd.conf 
#DocumentRoot "/var/www/html"

3.配置虚拟主机

[[email protected] conf.d]# cat www1.conf 
<VirtualHost *:80>
    ServerName www1.linux.com
    DocumentRoot "/web/vhosts/www1"
    CustomLog "/var/log/httpd/www1/www1.access_log" combined
    ErrorLog "/var/log/httpd/www1/www1.error_log"
    <Directory "/web/vhosts/www1">
        Options None
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

www2的配置与此并无差别,不在不再赘述

配置站点主页

[[email protected] ~]# cat /web/vhosts/www{1,2}/index.html 
www1 websit http://www1.linux.com
www2 websit http://www2.linux.com

 4.检查并启动服务  

[[email protected] conf.d]# httpd -t
Syntax OK
[[email protected] conf.d]# systemctl start httpd.service

 5.测试 

 [[email protected] conf.d]# curl www1.linux.com
 www1 websit http://www1.linux.com
[[email protected] conf.d]# curl www2.linux.com
www2 websit http://www2.linux.com

 6.输出www1.linux.com的状态信息,且要求只允许提供账号的用户访问

6.1 编辑www1的虚拟主机文件:添加一个<Location></Location>标签

[[email protected] conf.d]# cat www1.conf 
<VirtualHost *:80>
ServerName www1.linux.com
DocumentRoot "/web/vhosts/www1"
CustomLog "/var/log/httpd/www1/www1.access_log" combined
ErrorLog "/var/log/httpd/www1/www1.error_log"
    <Directory "/web/vhosts/www1">
        Options None
        AllowOverride None
        Require all granted
    </Directory>
    <Location "/server-status">
        SetHandler server-status
        AuthType basic
        AuthName "For Adminstrator"
        AuthUserFile "/etc/httpd/conf/.htpasswd"
        Require user tom
    </Location>
</VirtualHost>

6.2 提供账号和密码存储文件

    [[email protected] conf]# htpasswd -c -m /etc/httpd/conf/.htpasswd tom
    New password: 
    Re-type new password: 
    Adding password for user tom

 6.3 测试:

浏览器输入http://www1.linux.com/server-status

 7. www1不允许10.0.0.0/24网络中的主机访问

配置www1虚拟主机,在<Directory></Directory>标签段内添加<RequireAll>段

<Directory "/web/vhosts/www1">
        Options None
        AllowOverride None
        <RequireAll>
            Require all granted
            Require not ip 10.0.0.0/24
        </RequireAll>
    </Directory>

实现虚拟主机 www2 https访问



在172.16.52.1 CA服务器上:

1. 配置CA证书颁发机构

1.1 查看openssl相关文件 

[[email protected] ~]# cd /etc/pki/CA
[[email protected] CA]# ll
total 16
drwxr-xr-x. 2 root root 4096 Jul 24 03:09 certs
drwxr-xr-x. 2 root root 4096 Jul 24 03:09 crl
drwxr-xr-x. 2 root root 4096 Jul 24 03:09 newcerts
drwx------. 2 root root 4096 Jul 24 03:09 private

1.2 使用openssl生成CA私钥 

[[email protected] CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
....................................................................................................................................................+++
.....................................................................................+++
e is 65537 (0x10001)

1.3 使用openssl给CA服务器生成自签名证书

[[email protected] CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem
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) []:Beijing  
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server‘s hostname) []:ca.test.com
Email Address []:[email protected]

 1.4 创建CA相关目录和文件,指定序列号起始数字

[[email protected] CA]# touch index.txt  #新建索引文件
[[email protected] CA]# touch serial     #建立序列号文件
[[email protected] CA]# echo 01 > serial #写入起始序列号

web服务器创建申请证书

 2. 创建申请证书

2.1 在web服务器配置目录创建ssl目录

    [[email protected] ~]# mkdir /etc/httpd/ssl

 2.2 生成httpd 服务私钥

[[email protected] ~]# (umask 077;openssl genrsa -out /etc/httpd/ssl/httpd.key 1024) 
Generating RSA private key, 1024 bit long modulus
..................++++++
..............................++++++
e is 65537 (0x10001)
    
[[email protected] ~]# ll /etc/httpd/ssl/httpd.key 
-rw------- 1 root root 887 Jul 14 15:29 /etc/httpd/ssl/httpd.key

2.3 生成证书签署请求文件

[[email protected] ~]# openssl req -new -key /etc/httpd/ssl/httpd.key -out /etc/httpd/ssl/httpd.csr
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) []:Beijing
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:magedu
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server‘s hostname) []:www1.linux.com
Email Address []:[email protected]

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

2.4 把httpd申请证书发送到CA颁发机构上

[[email protected] ssl]# scp httpd.csr 172.16.52.1:/tmp
[email protected]‘s password: 
httpd.csr                                  100%  696     0.7KB/s   00:00

2.5 在CA端为给客户端签名并颁发正式  

[[email protected] tmp]# openssl ca -in httpd.csr -out httpd.crt -days 3650
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Dec 15 18:13:23 2015 GMT
            Not After : Dec 12 18:13:23 2025 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = Beijing
            organizationName          = magedu
            organizationalUnitName    = ops
            commonName                = www1.linux.com
            emailAddress              = [email protected]
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                D0:C4:3B:E1:C4:59:25:D4:0E:DF:AF:83:9C:48:D6:A8:D9:CC:27:27
            X509v3 Authority Key Identifier: 
                keyid:67:CC:F6:A8:E6:0B:73:CE:6C:A1:6D:B8:A6:99:1F:CA:7A:A3:D3:AB
Certificate is to be certified until Dec 12 18:13:23 2025 GMT (3650 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
    [[email protected] tmp]# ll httpd*
    -rw-r--r-- 1 root root 3830 Dec 16 02:15 httpd.crt
    -rw-r--r-- 1 root root  696 Dec 16 02:12 httpd.csr

 2.6 将生成的证书复制到web服务器上

    [[email protected] tmp]# scp httpd.crt [email protected]:/etc/httpd/ssl

    2.7 web服务器查看收到的证书

[[email protected] ssl]# ll
total 12
-rw-r--r-- 1 root root 3830 Dec 16  2015 httpd.crt
-rw-r--r-- 1 root root  696 Jul 14 16:01 httpd.csr
-rw------- 1 root root  887 Jul 14 16:00 httpd.key


3. web服务器配置ssl模块

3.1装载mod_ssl

[[email protected] ssl]# yum -y install mod_ssl

 3.2 修改ssl配置文件

配置/etc/httpd/conf.d/ssl.conf

DocumentRoot

[[email protected] ssl]# sed -n "/^DocumentRoot/p" /etc/httpd/conf.d/ssl.conf 
    DocumentRoot "/web/vhosts/www2"

ServerName

[[email protected] ssl]# sed -n "/^ServerName/p" /etc/httpd/conf.d/ssl.conf 
ServerName www2.linux.com:443

<Directory "">

[[email protected] ssl]# sed -n "186,190p" /etc/httpd/conf.d/ssl.conf 
<Directory "/web/vhosts/www2">
Options None
AllowOverride None
Require all granted
</Directory>

SSLCertificateFile

SSLCertificateKeyFile

[[email protected] conf.d]# sed -n ‘101p;109p‘ /etc/httpd/conf.d/ssl.conf 
SSLCertificateFile /etc/httpd/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/ssl/httpd.key

   3.3 重启httpd服务

[[email protected] conf.d]# systemctl restart httpd.service 
[[email protected] conf.d]# ss -tnlp|grep 443
LISTEN     0      128         :::443                     :::*                   users:(("httpd",pid=2475,fd=6),("httpd",pid=2474,fd=6),("httpd",pid=2473,fd=6),("httpd",pid=2472,fd=6),("httpd",pid=2471,fd=6),("httpd",pid=2469,fd=6))

 3.5 浏览器访问

这是证书未收信任,把httpd.crt证书导入浏览器就好了.

时间: 2024-07-28 13:26:35

web服务之Apache实现的https访问的相关文章

linux学习笔记-工程师技术:HTTP服务基础(Web服务基本搭建)、网页内容访问、安全Web服务、动态网站的部署

两台虚拟机,均要检测 1.IP地址     ifconfig 2.是否可以解析,nslookup server0.example.com 3.Yum是否可用   yum repolist 4.防火墙默认区域修改为trusted ------------------------------------------------------------------------------------------- HTTP服务基础 Web通信基本概念 基于 B/S (Browser/Server)架构

linux架构学习第二十六天之web服务搭建(apache)以及https的实现

内容: 1.httpd版本以及安装 2.httpd的配置文件详解 3.httpd的用户身份验证实现 4.httpd的虚拟主机的实现 5.https的通信过程以及实现演示 1.httpd版本以及安装 centos默认的httpd版本是2.2,而centos默认的httpd版本是2.4,这里演示的是centos6的httpd-2.2版本 安装的方式有两种,一个是rpm包安装,一个是编译安装,编译安装相关的配置文件需要手动设置,这里演示的是rpm包安装 httpd2.2和2.4的区别: 2.2的MPM

linux服务安装 -- web服务(Apache)

Apache Web Server是目前linux系统下使用最广泛的处理静态网页的免费web服务器. 在CentOS 6.5,默认安装了Apache web服务器. 1 安装apache服务 查看是否安装了httpd服务 [[email protected] ~]# rpm -qa|grep httpdhttpd-tools-2.2.15-39.el6.centos.i686httpd-devel-2.2.15-39.el6.centos.i686httpd-manual-2.2.15-39.e

Web服务之Apache的安装及配置

"Apache HTTP Server"是开源软件项目的杰出代表,正式名称是"httpd",在Web服务器领域长期保持超过半数的配额,可以在Linux.UNIX.Windows等多种操作系统平台使用 Apache的主要特点 (1)开放源代码 (2)跨平台应用 (3)支持各种Web编程语言 (4)模块化设计 (5)运行稳定 (6)良好的安全性 安装httpd服务器 ? ? httpd服务器的安装方法可以选用RPM安装.源码编译安装两种方式,但是前者在功能上有些局限性,

linux的web服务以及apache初步认识

1.web服务器的主要操作 1.建立连接--接受或拒接客户端连接请求. 2.接收请求--通过网络读取http请求报文. 3.处理请求--解析请求报文并做出相应的动作. 4.访问资源--访问请求报文中的相关资源. 5.构建响应--使用正确的首部生成http相应报文. 6.发送响应--向客户端发送生成的响应报文. 7.记录日志--当已经完成的http事务记录进日志文件. 2.Web服务器处理并发连接请求的架构方式     1.

phpstudy如何安装景安ssl证书 window下apache服务器网站https访问

1. 下载景安免费证书 https://www.zzidc.com/help/helpDetail?id=555 2.文件解压上传至服务器,位置自己决定 3. 调整apache配置 景安原文链接:https://www.zzidc.com/help/helpDetail?id=555 ① 确保你的apache编译了ssl模块,这是支持ssl证书必要的条件(如果没有,请编译,打开phpstudy--设置--PHP模块扩展--php-openssl前面勾选上). ② 进入到apache目录下,htt

HTTP服务基础 、 网页内容访问 、 安全Web服务 、 部署动态网站-day09

############################################################################################检测两台虚拟机:1.ip地址 #ifconfig | head -22.检测dns是否可以解析#vim /etc/resolv.conf                  //dns配置文件#nslookup desktop0.example.com      //dns检测3.检测yum是否可以使用#yum cl

linux web服务基础知识,dns

Web服务基础 用户访问网站的基本流程 我们每天都会用web客户端上网,浏览器就是一个web客户端,例如谷歌浏览器,以及火狐浏览器等. 当我们输入www.oldboyedu.com/时候,很快就能看到老男孩教育的官网了,这一切看起来很平淡无奇,背后又是什么道理呢?普通人可以不知道,但是咱们作为it开发人员,必须得掌握清楚背后的技术. 下面超哥为你揭晓用户访问网站的基本流程 老男孩教育某python总监,讲了一天课感觉很累,下了班躺床上打开他的macbook pro,双击浏览器,输入www.por

简单web服务开发(JWS,CXF,AXIS)

(一)利用JWS开发web服务 开发Web服务器端: 1.定义接口HelloWorld,使用@WebService注解修饰接口名,使用@WebMethod修饰需要对外发布的方法. 2.HelloWorld接口实现类 3.发布类HelloWorldService 4.编译运行HelloWorldService 类之后查看WSDL文档http://localhost:8080/ HelloWorld?wsdl 客户端代码: 1.生成客户端所需要的文件 新建工程WebServerClient,进入工程