云计算Linux运维——基础服务应用——HTTPD服务(8)更新中......

1http协议

一、http协议

  • 应用层协议
  • 作用
    • 在客户端、web服务器传递数据
  • Hyper Text Transfer Protocol 超文本传输协议
    • 客户端访问web服务时,传递代码,由浏览器解析代码进行展示

二、web服务

1、类型

  • 静态网站
    • 网页上所有资源都是固定的
    • html语言,js, jquery
  • 动态网站
    • 一段程序代码,根据传递的参数不同展示不同的结果
      • PHP语言         *.php文件
      • Java语言         *.jsp文件

2、cookie、session

  • 作用
    • 识别客户端登录身份

 

 三、HTTP协议特性

1、http/0.9

  • 只支持传输纯文本数据

2、http/1.0

  • 引用MIME机制
    • 传输非文本数据(图片、视频、音频、动画)
  • 缓存机制,加速网站的访问
    • 服务器缓存
    • 客户端缓存

3、http/1.1

  • 长连接机制    keepalive
    • 允许在一条连接上发送多次请求
      • 限制长连接的超时时间
      • 限制每个长连接的最大请求数 5000
  • 管道机制、
    • 允许一次性发送多个请求,并行
  • 加强了缓存的管理
    • 缓存静态数据
    • 缓存过期时间

4、http/2

  • 以并行的方式发送请求、响应

四、HTTP状态码、请求方法

1、状态码

  • 200
    • 成功响应
  • 301、302、304
    • 成功响应
    • 重定向
  • 4xx
    • 错误响应
      • 403          权限拒绝
      • 404           文件找不到
  • 5xx 
    • 错误响应
      • 服务端配置

2、请求方法

  • GET
    • 用于获取内容、数据
  • POST
    • 上传数据
  • DELETE

2httpd安装配置

一、httpd软件介绍

  • Linux
    • httpd、nginx、tomcat
  • Windows server
    • IIS

httpd介绍:

1、httpd软件版本

  • 2.2
  • 2.4

2、httpd部署方式

  • RPM
  • 源码编译

3、httpd特性

  • 开源、跨平台的
  • 模块化软件
  • 支持虚拟主机功能
  • 支持https虚拟主机
  • 支持缓存

二、安装启动httpd

1、安装httpd

[root@8 ~]# yum install -y httpd

2、启动httpd

[root@8 ~]# systemctl start httpd
[root@8 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
root@8 ~]# netstat -anlp | grep http
tcp6       0      0 :::80                   :::*                    LISTEN      8048/httpd          
unix  3      [ ]         STREAM     CONNECTED     27734    8048/httpd       
[root@8 ~]# ps -elf | grep http
4 S root       8048      1  0  80   0 - 57610 poll_s 20:14 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
5 S apache     8049   8048  0  80   0 - 57610 inet_c 20:14 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
5 S apache     8050   8048  0  80   0 - 57610 inet_c 20:14 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
5 S apache     8051   8048  0  80   0 - 57610 inet_c 20:14 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
5 S apache     8052   8048  0  80   0 - 57610 inet_c 20:14 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
5 S apache     8053   8048  0  80   0 - 57610 inet_c 20:14 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
0 S root       8075   7980  0  80   0 - 28206 pipe_w 20:16 pts/0    00:00:00 grep --color=auto http
  • 父进程
    • 管理子进程、记录日志、读取配置文件
  • 子进程
    • 用于接收、处理客户端请求

3、测试访问httpd

http://192.168.50.8/

 4、删除测试页面

[root@8 ~]# rm -rf /etc/httpd/conf.d/welcome.conf 
[root@8 ~]# systemctl restart httpd

5、建立测试网页

root@8 ~]# ls /var/www/html/
index.html  music.html
[root@8 ~]# vim /var/www/html/music.html 

 三、httpd相关文件目录

1、主配置文件

/etc/httpd/conf/httpd.conf

2、子配置文件

/etc/httpd/conf.d/*.conf

3、模块路径、配置文件

/etc/httpd/modules

/etc/httpd/conf.modules.d/*.conf

4、存放PID文件

/etc/httpd/run 

5、日志目录、

/var/log/httpd

access_log error_log

  • access_log:访问日志
  • error_log:错误日志

6、默认网页数据目录

/var/www/html

四、配置文件解析——httpd.conf

1、指定httpd的工作目录

ServerRoot "/etc/httpd"

2、指定监听的端口

Listen 192.168.59.8:80

3、加载子配置文件

Include conf.modules.d/*.conf

4、指定启动进程的用户身份

User www

Group www

5、指定管理员邮箱

ServerAdmin root@localhost

6、指定网站的主机名

ServerName test1.linux.com

7、指定网页目录

DocumentRoot "/var/www/html"

8、指定默认的网页名称

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

9、错误日志文件、级别

ErrorLog "logs/error_log"
# Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
LogLevel error

10、访问日志名称、记录格式

CustomLog "logs/access_log" combined

web访问量:

1、PV Page View 页面访问量

2、UV User View 用户访问量

11、定义访问日志格式

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

变量说明:

  1. %h 客户端地址
  2. %l web认证用户
  3. %u windows用户
  4. %t 访问时间
  5. %r 请求方法 文件名称 HTTP协议版本
  6. %s 状态码
  7. %b 响应数据的大小
  8. %{Referer}i 超链接地址
  9. %{User-Agent}i 浏览器类型

12、长连接配置

KeepAliveTimeout 5
MaxKeepAliveRequests 100

13、针对目录授权

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

配置说明:

  • options
    1. 客户端访问对应目录下的文件时受什么影响
      1. Indexes
        1. 无index.html首页时,列出所有文件
      2. FollowSymLinks
        1. 允许网页目录下的软链接文件被正常访问
  • require 客户端访问认

14、客户端访问认证

  • 基于客户端地址进行认证
  • 基于用户名、密码进行认证

1) 基于客户端地址进行认证

Require all granted			//允许所有客户端访问
Require ip 10.1.1.1			//仅允许10.1.1.1访问
<RequireAll>
    Require all granted
    Require not ip 10.252.46.165
</RequireAll>
Require all denied

2) 基于用户名、密码认证

  • a) 创建认证用户
[root@localhost ~]# htpasswd -c /etc/httpd/.webuser martin
New password: 
Re-type new password: 
Adding password for user martin
[root@localhost ~]# 
[root@localhost ~]# cat /etc/httpd/.webuser 
martin:$apr1$6KV5I5w.$6iQ5ip.1bF3la2pHq9lX4/

b) 编辑httpd.conf

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf 

<Directory "/var/www/html">
    ........................
    AuthType Basic
    AuthName "Need to login: "
    AuthUserFile "/etc/httpd/.webuser"
    Require valid-user
</Directory>

c) 重启服务

[root@localhost ~]# systemctl restart httpd


3虚拟主机

虚拟主机 Virtual Host

一、虚拟主机介绍

1、作用

  • 在一台服务器可以部署多套网站
  • 注意:
    • 虚拟主机配置后,主配置文件中部署的网站会失效!!!!!

2、类型

  • 基于名称的虚拟主机 【常用】
    • 不同的网站指定不同的ServerName、DocumentRoot
  • 基于IP地址的虚拟主机
    • 不同的网站监听在不同的IP地址上
  • 基于端口的虚拟主机

3、配置虚拟主机

<VirtualHost IP:port>
    DocumentRoot xxxxx
    ServerName xxxxx
    ErrorLog xxxxxxxxxxxx
    CustomLog xxxxxxxxxxxxxxxx
</VirtualHost>

二、基于名称的虚拟主机

网站名称: vedio.linux.com 网页目录: /var/www/html/vedio

网站名称: mp3.linux.com 网页目录: /mp3

1、配置vedio网站

1) 创建网页目录、测试首页

[root@localhost ~]# mkdir /var/www/html/vedio
[root@localhost ~]# vim /var/www/html/vedio/index.html
<h1> vedio </h1>

2) 编辑虚拟主机配置文件

[root@localhost ~]# cat /etc/httpd/conf.d/vedio.conf
<VirtualHost 192.168.140.10:80>
    ServerName vedio.linux.com
    DocumentRoot /var/www/html/vedio
    ErrorLog /var/log/httpd/vedio_error.log
    CustomLog /var/log/httpd/vedio_access.log combined
</VirtualHost>
[root@localhost ~]# httpd -t 
Syntax OK
[root@localhost ~]# systemctl restart httpd

3) 测试访问

2、配置mp3网站

1) 创建网页目录、测试首页

[root@localhost ~]# mkdir /mp3
[root@localhost ~]# vim /mp3/index.html
<h1> mp3 </h1>

2) 编辑配置文件

[root@localhost ~]# cat /etc/httpd/conf.d/mp3.conf 
<VirtualHost 192.168.140.10:80>
     ServerName mp3.linux.com
     DocumentRoot /mp3
     ErrorLog /var/log/httpd/mp3_error.log
     CustomLog /var/log/httpd/mp3_access.log combined
</VirtualHost>

<Directory "/mp3">
    Require all granted
</Directory>
[root@localhost ~]# httpd -t
Syntax OK
[root@localhost ~]# systemctl restart httpd

3) 测试访问

三、基于IP地址的虚拟主机

网站: python.linux.com 网页目录: /python 192.168.140.100

网站: shell.linux.com 网页目录: /shell 192.168.140.200

1、主机添加网卡、配置多个地址

[root@localhost network-scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:8f:c3:68 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.10/24 brd 192.168.140.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe8f:c368/64 scope link 
       valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:8f:c3:72 brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.100/24 brd 192.168.140.255 scope global noprefixroute ens37
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe8f:c372/64 scope link 
       valid_lft forever preferred_lft forever
4: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:8f:c3:7c brd ff:ff:ff:ff:ff:ff
    inet 192.168.140.200/24 brd 192.168.140.255 scope global noprefixroute ens38
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe8f:c37c/64 scope link tentative 
       valid_lft forever preferred_lft forever

2、配置python网站

[root@localhost ~]# cat /etc/httpd/conf.d/python.conf
<VirtualHost 192.168.140.100:80>
    ServerName python.linux.com
    DocumentRoot /python
    ErrorLog /var/log/httpd/python_error.log
    CustomLog /var/log/httpd/python_access.log combined
</VirtualHost>

<Directory "/python">
   Require all granted
</Directory>

3、配置shell网站

[root@localhost ~]# cat /etc/httpd/conf.d/shell.conf 
<VirtualHost 192.168.140.200:80>
    ServerName shell.linux.com
    DocumentRoot /shell
    ErrorLog /var/log/httpd/shell_error.log
    CustomLog /var/log/httpd/shell_access.log combined
</VirtualHost>

<Directory "/shell">
   Require all granted
</Directory>

4基于https协议部署虚拟主机

一、网站传输协议

  • http
    • 80/tcp
    • 数据以明文的方式传输
  • https
    • 443/tcp
    • 数据以密文的方式传输

二、安全性保障

  • 数据安全性
    • 加密、解密
  • 数据完整性
    • 校验算法 MD5/SHA hash哈希算法
  • 身份的真实性保障

三、数据安全性的保障

通过加密算法确保数据的安全

发送方发送数据前加密数据、接收方解密数据

1、对称加密算法

加密数据时使用的密钥、解密的密钥是一样的

典型对称加密算法

  • DES
  • 3DES
  • AES

加密数据

#  openssl enc -e -des -in /tmp/file01 -out /tmp/file01_s 
enter des-cbc encryption password:
Verifying - enter des-cbc encryption password:

解密数据

#  openssl enc -d -des -in /opt/file01_s -out /opt/file01 
enter des-cbc decryption password:

2、非对称加密算法

加密、解密数据时用的密钥是不一样的

密钥对 【公钥、私钥】

数据加密:

公钥加密、私钥解密

典型非对称加密算法:

  • RSA
  • DSA

实际使用:

使用对称算法加密真实的数据, 使用非对称加密算法加密对称算法的密钥

四、数据完整性保障

1、校验算法

  • MD5
  • SHA

发送数据时,使用校验算法对数据进行校验,生成校验码,同时将数据+校验码发送出去

接收方接收数据,使用相同的校验算法再次进行校验,对比校验码

五、身份的真实性

1、密钥对

  • 数据加密
    • 公钥加密、私钥解密
  • 验证签名
    • 私钥签名、公钥验证签名

流程:

1、电商服务器生成证书申请【.csr文件】, 同时将电商服务器的公钥放入证书申请;将证书申请发送给CA

2、CA审批信息, 通过后CA会使用自己的私钥进行签名;相当于签署证书【.crt】

3、客户端访问电商服务器时,服务器会将证书信息发送给客户端

4、客户端通过CA的公钥验证证书,验证通过后可获取电商服务器的公钥

5、客户端选取对称算法、密钥,使用公钥加密,发送给服务器、服务器解密

6、进行真实数据交互

六、配置基于https协议的虚拟主机

1、配置私有CA

  • 生成密钥对
  • 生成自签证书

1) 创建CA服务器需要的数据库文件

[root@ca ~]# touch /etc/pki/CA/index.txt
[root@ca ~]# echo 01 > /etc/pki/CA/serial

2) 创建CA服务器需要的密钥

[root@ca ~]# openssl genrsa -out /etc/pki/CA/private/cakey.pem 1024
Generating RSA private key, 1024 bit long modulus
..++++++
.....++++++
e is 65537 (0x10001)

3) 颁发自签证书

[root@ca ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3650
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]:china
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:bj
Locality Name (eg, city) [Default City]:bj
Organization Name (eg, company) [Default Company Ltd]:bj
Organizational Unit Name (eg, section) []:bj
Common Name (eg, your name or your server's hostname) []:ca.linux.com
Email Address []:bj@qq.com
[root@ca ~]# 
[root@ca ~]# 
[root@ca ~]# ls /etc/pki/CA/
cacert.pem  certs  crl  index.txt  newcerts  private  serial

2、在web server上申请证书

1) 在web server创建密钥对

 [root@web_server ~]# mkdir /etc/httpd/ssl
[root@web_server ~]# openssl genrsa -out /etc/httpd/ssl/www.linux.com.key 2048
Generating RSA private key, 2048 bit long modulus
......................+++
.....................................................................+++
e is 65537 (0x10001)
[root@web_server ~]#

2) 创建证书申请

[root@localhost ~]# openssl req -new -key /etc/httpd/ssl/www.linux.com.key -out /etc/httpd/ssl/www.linux.com.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) []:bj
Locality Name (eg, city) [Default City]:bj
Organization Name (eg, company) [Default Company Ltd]:bj
Organizational Unit Name (eg, section) []:bj
Common Name (eg, your name or your server's hostname) []:www.linux.com
Email Address []:bj@qq.com

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

3) 将证书申请发送给CA

[root@web_server ~]# rsync -av /usr/local/nginx/ssl/www.linux.com.csr root@192.168.140.11:/tmp/

4) CA签署证书

[root@ca ~]# openssl ca -in /tmp/www.linux.com.csr -out /etc/pki/tls/certs/www.linux.com.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: Jun 21 06:06:54 2021 GMT
            Not After : Jun 19 06:06:54 2031 GMT
        Subject:
            countryName               = cn
            stateOrProvinceName       = bj
            organizationName          = bj
            organizationalUnitName    = bj
            commonName                = www.linux.com
            emailAddress              = bj@qq.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                67:6D:B8:84:3A:5C:BE:99:81:4A:49:F4:CD:61:5E:8B:99:3B:EF:13
            X509v3 Authority Key Identifier: 
                keyid:C8:EF:D2:FB:E8:D5:2B:50:D8:7C:07:7A:27:72:B5:D6:33:09:D6:56

Certificate is to be certified until Jun 19 06:06:54 2031 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
[root@ca ~]# ls /etc/pki/tls/certs/
ca-bundle.crt  ca-bundle.trust.crt  make-dummy-cert  Makefile  renew-dummy-cert  www.linux.com.crt
[root@ca ~]# 
[root@ca ~]# cat /etc/pki/CA/serial
02
[root@ca ~]# cat /etc/pki/CA/index.txt
V	310619060654Z		01	unknown	/C=cn/ST=bj/O=bj/OU=bj/CN=www.linux.com/emailAddress=bj@qq.com

5) 将证书发送给web server

[root@ca ~]# rsync -av /etc/pki/tls/certs/www.linux.com.crt root@192.168.140.10:/etc/httpd/ssl

3、安装mod_ssl模块

[root@localhost ~]# yum install -y mod_ssl 

4、创建网页目录、测试文件

[root@localhost ~]# mkdir /linux
[root@localhost ~]# vim /linux/index.html
<h1> www.linux.com </h1>

5、配置ssl虚拟主机

[root@localhost ~]# vim /etc/httpd/conf.d/ssl.conf 
....................
<VirtualHost _default_:443>

# General setup for the virtual host, inherited from global configuration
DocumentRoot "/linux"
ServerName www.linux.com:443
SSLCertificateFile /etc/httpd/ssl/www.linux.com.crt
SSLCertificateKeyFile /etc/httpd/ssl/www.linux.com.key

<Directory "/linux">
    Require all granted
</Directory>
[root@localhost ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# netstat -antp | grep http
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7144/httpd          
tcp6       0      0 :::443                  :::*                    LISTEN      7144/httpd          

6、测试访问

Linux.com - News For Open Source Professionals

七、配置https自动跳转

     RewriteEngine On
     RewriteCond %{HTTP_HOST} www.linux.com [NC]
     RewriteRule ^/   https://www.linux.com [L]
  • %{HTTP_HOST}
    • 调用客户端所访问的网站主机名
  • [NC]
    • 忽略大小写
  • [L]
    • 立即响应

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

¥阿拉伯王子¥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值