httpd-2.2安装,请求状态详解及虚拟主机设定

Centos7系统下实现httpd-2.2的安装,并分别实现prefork、worker、event等几种工作方式

centos7中光盘的本地yum源默认httpd程序包是2.4.6版本的,要安装httpd-2.2就要下载好源码包和对应的依赖包进行编译安装,安装之前先简单讲解一下httpd的这三种处理模型,需要注意的是httpd-2.2的event事件驱动模型还只是测试使用功能并且不支持同时编译多个模块,每次只能选定一个模块.
其对应的配置文件为:/etc/httpd/conf/httpd.conf

prefork:多进程模型,每个进程响应一个请求,一个主进程负责生成n个子进程及回收子进程,创建套接字,接收请求,并将其派发给某子进程进行处理.每个子进程处理一个请求即使没有用户请求也会预先生成几个空闲进程,随时等待用于响应用户请求,最大空闲进程数不能超过1024个.

prefork的配置      
    
    StartServers       8  进程启动后立即启动的空闲进程数量
    MinSpareServers    5  最小空闲进程数量
    MaxSpareServers   20  最大空闲进程数量
    ServerLimit      256  一个周期内允许最大的进程数量
    MaxClients       256  最大允许启动的服务器子进程数量
    MaxRequestsPerChild  4000  允许最大请求的数量(超过数量就会关闭该进程)
     

worker:多进程多线程模型,每线程处理一个用户请求,一个主进程负责生成多个子进程每个子进程负责生成多个线程,主进程负责创建套接字,接收请求,并将其派发给某子进程进行处理.每个线程负责响应用户请求.
并发响应数量:m*n(m:子进程数量,n:每个子进程所能创建的最大线程数量)

worker的配置:
    
    StartServers         4
    MaxClients         300
    MinSpareThreads     25
    MaxSpareThreads     75
    ThreadsPerChild     25   每个子进程所能创建的最大线程数量
    MaxRequestsPerChild  0   0表示不限制
     

event:事件驱动模型,多进程模型,每个进程响应多个请求,一个主进程 负责生成子进程,创建套接字,接收请求,并将其派发给某子进程进行处理,每个子进程基于事件驱动机制直接响应多个请求.
并发响应数量:m*n(m:子进程数量,n:每个子进程响应的请求数量)
在httpd2.2中未对event给出配置模版,并且只是测试功能,不建议使用.

在centos7系统中编译安装httpd2.2要先下载好依赖包(官方网站提示apr/arp-util版本要1.4(含)版本以上)和源码包,下载过程不做表述可以做官网中自行下载,再安装开发环境包组才能编译安装源码包.
1.安装依赖包:zlib-devel

[root@localhost ~]# yum install zlib-devel
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
base                                                                              | 3.6 kB  00:00:00     
epel                                                                              | 4.7 kB  00:00:00     
extras                                                                            | 3.4 kB  00:00:00     
.....

Total download size: 50 k
Installed size: 132 k
Is this ok [y/d/N]: y
Downloading packages:
zlib-devel-1.2.7-17.el7.x86_64.rpm                                                |  50 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : zlib-devel-1.2.7-17.el7.x86_64                                                        1/1 
  Verifying  : zlib-devel-1.2.7-17.el7.x86_64                                                        1/1 

Installed:
  zlib-devel.x86_64 0:1.2.7-17.el7 

2.安装开发环境包组

[root@localhost ~]#  yum groupinstall "Development Tools" "ServerPlatform Development" -y
Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once in the configuration
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.njupt.edu.cn

Installed:
  autoconf.noarch 0:2.69-11.el7                 automake.noarch 0:1.13.4-3.el7                          
  bison.x86_64 0:3.0.4-1.el7                    byacc.x86_64 0:1.9.20130304-3.el7                       
  cscope.x86_64 0:15.8-10.el7                   ctags.x86_64 0:5.8-13.el7                               
  diffstat.x86_64 0:1.57-4.el7                  doxygen.x86_64 1:1.8.5-3.el7                            
  flex.x86_64 0:2.5.37-3.el7                    gcc.x86_64 0:4.8.5-16.el7_4.2                           
  gcc-c++.x86_64 0:4.8.5-16.el7_4.2             gcc-gfortran.x86_64 0:4.8.5-16.el7_4.2                  
  git.x86_64 0:1.8.3.1-12.el7_4                 indent.x86_64 0:2.2.11-13.el7                           
  intltool.noarch 0:0.50.2-7.el7                libtool.x86_64 0:2.4.2-22.el7_3                         
  patch.x86_64 0:2.7.1-8.el7                    patchutils.x86_64 0:0.3.3-4.el7                         
  rcs.x86_64 0:5.9.0-5.el7                      redhat-rpm-config.noarch 0:9.1.0-76.el7.centos          
  rpm-build.x86_64 0:4.11.3-25.el7              rpm-sign.x86_64 0:4.11.3-25.el7                         
  subversion.x86_64 0:1.7.14-11.el7_4           swig.x86_64 0:2.0.10-5.el7                              
  systemtap.x86_64 0:3.1-5.el7_4               

Dependency Installed:
  cpp.x86_64 0:4.8.5-16.el7_4.2                     dwz.x86_64 0:0.11-3.el7                              
  efivar-libs.x86_64 0:31-4.el7                     gettext-common-devel.noarch 0:0.19.8.1-2.el7         
  gettext-devel.x86_64 0:0.19.8.1-2.el7             glibc-devel.x86_64 0:2.17-196.el7_4.2                
  glibc-headers.x86_64 0:2.17-196.el7_4.2           kernel-debug-devel.x86_64 0:3.10.0-693.21.1.el7      
  kernel-headers.x86_64 0:3.10.0-693.21.1.el7       libgfortran.x86_64 0:4.8.5-16.el7_4.2                
  libmpc.x86_64 0:1.0.1-3.el7                       libquadmath.x86_64 0:4.8.5-16.el7_4.2                
  libquadmath-devel.x86_64 0:4.8.5-16.el7_4.2       libstdc++-devel.x86_64 0:4.8.5-16.el7_4.2            
  m4.x86_64 0:1.4.16-10.el7                         mokutil.x86_64 0:12-1.el7.centos                     
  perl-Error.noarch 1:0.17020-2.el7                 perl-Git.noarch 0:1.8.3.1-12.el7_4                   
  perl-TermReadKey.x86_64 0:2.30-20.el7             perl-Test-Harness.noarch 0:3.28-3.el7                
  perl-Thread-Queue.noarch 0:3.02-2.el7             perl-XML-Parser.x86_64 0:2.41-10.el7                 
  perl-srpm-macros.noarch 0:1-8.el7                 subversion-libs.x86_64 0:1.7.14-11.el7_4             
  systemtap-client.x86_64 0:3.1-5.el7_4             systemtap-devel.x86_64 0:3.1-5.el7_4                 

Dependency Updated:
  glibc.x86_64 0:2.17-196.el7_4.2                   glibc-common.x86_64 0:2.17-196.el7_4.2              
  libgcc.x86_64 0:4.8.5-16.el7_4.2                  libgomp.x86_64 0:4.8.5-16.el7_4.2                   
  libstdc++.x86_64 0:4.8.5-16.el7_4.2               systemtap-runtime.x86_64 0:3.1-5.el7_4              

Complete!

3.下载源码包及依赖包并解压

[root@localhost Downloads]# ls
apr-1.5.0.tar.bz2  apr-util-1.5.3.tar.bz2  httpd-2.2.32.tar.gz

---------------分割线---------------

[root@localhost Downloads]# tar -xvf apr-1.5.0.tar.bz2 
apr-1.5.0/
apr-1.5.0/libapr.mak
apr-1.5.0/apr.mak
apr-1.5.0/apr.spec
apr-1.5.0/docs/
...
#依次用tar命令解压apr-util-1.5.3.tar.bz2 和httpd-2.2.32.tar.gz 到当前目录即可

4.编译安装依赖包apr/apr-util

编译安装apr-1.5.0
[root@localhost Downloads]# cd apr-1.5.0

[root@localhost apr-1.5.0]# ./configure --prefix=/usr/local/apr-util-httpd/ --with-apr=/usr/local/apr-httpd/

[root@localhost apr-1.5.0]# make && make install
...
done
/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr-httpd//build-1/apr_rules.mk
/usr/bin/install -c -m 755 apr-config.out /usr/local/apr-httpd//bin/apr-1-config

---------------分割线---------------
编译安装apr-util-1.5.3
[root@localhost apr-1.5.0]# cd ..
[root@localhost Downloads]# cd apr-util-1.5.3/
[root@localhost apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util-httpd/ --with-apr=/usr/local/apr-httpd/
[root@localhost apr-util-1.5.3]# make && make install
...
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
/usr/bin/install -c -m 644 aprutil.exp /usr/local/apr-util-httpd//lib
/usr/bin/install -c -m 755 apu-config.out /usr/local/apr-util-httpd//bin/apu-1-config

5.编译安装httpd-2.2.32

[root@localhost apr-util-1.5.3]# cd ..
[root@localhost Downloads]# cd httpd-2.2.32
[root@localhost httpd-2.2.32]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd/ --enable-so --enable-deflate --enable-headers --enable-rewrite --with-mpm=prefork --with-apr=/usr/local/apr-httpd/ --with-apr-util=/usr/local/apr-util-httpd/
...
Installing build system files
mkdir /usr/local/apache/build
Installing man pages and online manual
mkdir /usr/local/apache/man
mkdir /usr/local/apache/man/man1
mkdir /usr/local/apache/man/man8
mkdir /usr/local/apache/manual
make[1]: Leaving directory `/root/Downloads/httpd-2.2.32'
一些参数的解析:
--prefix:安装路径

--sysconfdir:指定配置文件路径

--enable-so:DSO兼容,DSO=Dynamic Shared Object,动态共享对象,可实现模块动态生效

--enable-deflate:支持压缩功能;需要zlib-devel包支持

--enable-headers:提供允许对HTTP请求头的控制

--enable-rewrite:提供基于URL规则的重写功能

--with-mpm:设置默认启用的MPM模式,{prefork|worker|event}

--with-apr:apr安装路径

--with-apr-util:apr-util安装的路径

6.在安装目录/usr/local/apache下检查主配置文件语法错误

[root@localhost apache]# bin/apachectl -t
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
Syntax OK
#ok表示正常

7.启动httpd服务

[root@localhost apache]# /usr/local/apache/bin/httpd -k start
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName

#这个报错是主机名没有设置好

8.查看80端口是否监听

[root@localhost apache]# ss -tnl
State       Recv-Q Send-Q       Local Address:Port                      Peer Address:Port              
LISTEN      0      128                      *:111                                  *:*                  
LISTEN      0      5            192.168.122.1:53                                   *:*                  
LISTEN      0      128                      *:22                                   *:*                  
LISTEN      0      128              127.0.0.1:631                                  *:*                  
LISTEN      0      100              127.0.0.1:25                                   *:*                  
LISTEN      0      128              127.0.0.1:6010                                 *:*                  
LISTEN      0      128                     :::111                                 :::*                  
LISTEN      0      128                     :::80                                  :::*                  
LISTEN      0      128                     :::22                                  :::*                  
LISTEN      0      128                    ::1:631                                 :::*                  
LISTEN      0      100                    ::1:25                                  :::*                  
LISTEN      0      128                    ::1:6010                                :::*   

9.关闭iptables和selinux之后测试是否安装成功

[root@localhost apache]# iptables -F
[root@localhost apache]# setenforce 0
[root@localhost apache]# getenforce 
Permissive

10.输入主机ip测试

http2.2安装成功.png

出现这个页面就证明安装并启动成功了.

httpd2.2默认是在prefork方式下工作的,要在几个工作方式中切换,则需要修改/etc/sysconfig/httpd下对应的配置.并重启服务即可.

[root@localhost ~]# vim /etc/sysconfig/httpd 

# Configuration file for the httpd service.

#
# The default processing model (MPM) is the process-based
# 'prefork' model.  A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# The service must be stopped before changing this variable.
#
#HTTPD=/usr/sbin/httpd.worker
HTTPD=/usr/sbin/httpd.event     #将该选项切换到对应模式,重启服务即可以

#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
#OPTIONS=

#
# By default, the httpd process is started in the C locale; to 
# change the locale in which the server runs, the HTTPD_LANG
# variable can be set.
#
....
"/etc/sysconfig/httpd" 32L, 975C       

1.默认的prefork方式:
修改/etc/sysconfig/httpd

[root@localhost ~]# vim /etc/sysconfig/httpd 

# Configuration file for the httpd service.

#
# The default processing model (MPM) is the process-based
# 'prefork' model.  A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# The service must be stopped before changing this variable.
#
#HTTPD=/usr/sbin/httpd.worker
HTTPD=/usr/sbin/httpd

#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
#OPTIONS=

#
# By default, the httpd process is started in the C locale; to 
# change the locale in which the server runs, the HTTPD_LANG
# variable can be set.
#
"/etc/sysconfig/httpd" 32L, 969C         

重启服务

[root@localhost ~]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]

查看httpd启动模式

[root@localhost ~]# ps aux | grep httpd
root      2365  0.0  0.2 184324  3864 ?        Ss   23:13   0:00 /usr/sbin/httpd
apache    2368  0.0  0.1 184324  2500 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2369  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2370  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2371  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2372  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2373  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2374  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
apache    2375  0.0  0.1 184324  2484 ?        S    23:13   0:00 /usr/sbin/httpd
root      2377  0.0  0.0 103252   828 pts/1    S+   23:14   0:00 grep httpd

2.worker工作方式
修改etc/sysconfig/httpd文件中的HTTTPD=/usr/sbin/httpd.worker
重启服务
验证worker工作方式

[root@localhost ~]# ps aux | grep httpd
root      2039  0.0  0.2 184532  4052 ?        Ss   22:56   0:00 /usr/sbin/httpd.worker
apache    2042  0.0  0.2 528792  5372 ?        Sl   22:56   0:00 /usr/sbin/httpd.worker
apache    2043  0.0  0.2 528792  5388 ?        Sl   22:56   0:00 /usr/sbin/httpd.worker
apache    2044  0.0  0.2 528792  5376 ?        Sl   22:56   0:00 /usr/sbin/httpd.worker
root      2157  0.0  0.1 143676  3816 pts/0    S+   22:57   0:00 vim /etc/sysconfig/httpd
root      2216  0.0  0.0 103252   828 pts/1    S+   23:07   0:00 grep httpd

3.event工作方式
修改etc/sysconfig/httpd文件中的HTTTPD=/usr/sbin/httpd.event
重启服务
验证event工作方式

[root@localhost ~]# ps aux | grep httpd
root      2234  0.0  0.2 184540  4044 ?        Ss   23:07   0:00 /usr/sbin/httpd.event
apache    2237  0.0  0.2 528800  5356 ?        Sl   23:07   0:00 /usr/sbin/httpd.event
apache    2238  0.0  0.2 528800  5344 ?        Sl   23:07   0:00 /usr/sbin/httpd.event
apache    2239  0.0  0.2 528800  5344 ?        Sl   23:07   0:00 /usr/sbin/httpd.event
root      2322  0.0  0.0 103252   828 pts/1    S+   23:08   0:00 grep httpd

简述request报文请求方法和状态响应码

一个http事务由请求(request) 和响应(response)两种状态,并在响应中返回各种响应码,可以从不同的状态码中了解不同的响应状态.

报文语法格式:
    request报文
          
        

        

    response报文
          
        

        

method: 请求方法
标明客户端希望服务器对资源执行的动作,如下为常用method的解释:
GET:从服务器获取一个资源
HEAD:只从服务器获取文档的响应首部
POST:向服务器发送要处理的数据
PUT:将请求的主体部分存储在服务器上
DELETE:请求删除服务器上指定的文档
TRACE:追踪请求到达服务器中间经过的代理服务器
OPTIONS:请求服务器返回对指定资源支持使用的请求方法

version:HTTPD的版本号
HTTP/.,由主版本和次版本号组成

status:状态码
由三位数字组成,如200,301, 302, 404, 502; 标记请求处理过程中发生的情况,不同的状态码表示的意思不一样.
1xx:100-101, 信息提示
2xx:200-206, 成功
3xx:300-305, 重定向
4xx:400-415, 错误类信息,客户端错误
5xx:500-505, 错误类信息,服务器端错误

常用的状态码:
    200: 成功,请求的所有数据通过响应报文的entity-body部分发送;OK
    301: 请求的URL指向的资源已经被删除;但在响应报文中通过首部Location指明了资源现在所处的新位置;Moved Permanently
    302: 与301相似,但在响应报文中通过Location指明资源现在所处临时新位置; Found
    304: 客户端发出了条件式请求,但服务器上的资源未曾发生改变,则通过响应此响应状态码通知客户端;Not Modified
    401: 需要输入账号和密码认证方能访问资源;Unauthorized
    403: 请求被禁止;Forbidden
    404: 服务器无法找到客户端请求的资源;Not Found
    500: 服务器内部错误;Internal Server Error
    502: 代理服务器从后端服务器收到了一条伪响应;Bad Gateway

reason-phrase:状态码所标记的状态的简要描述

headers:首部
每个请求或响应报文可包含任意个首部;每个首部都有首部名称,后面跟一个冒号,而后跟上一个可选空格,接着是一个值
首部的分类:通用首部,请求首部,响应首部,实体首部,扩展首部等等,
格式为:Name: Value,常见的首部格式如下:

通用首部:
Date: 报文的创建时间
Connection:连接状态,如keep-alive, close
Via:显示报文经过的中间节点
Cache-Control:控制缓存
Pragma: no-cache.相当于Cache-Control: no-cache

请求首部:
Accept:通过服务器自己可接受的媒体类型;
Accept-Charset:浏览器指明自己接收的字符集
Accept-Encoding:接受编码格式,如gzip
Accept-Language:接受的语言
Client-IP: 客户端ip地址
Host: 请求的服务器名称和端口号
Referer:包含当前正在请求的资源的上一级资源;
User-Agent:客户端代理

条件式请求首部:
Expect:服务器指明该实体什么时候过期.
If-Modified-Since:自从指定的时间之后,请求的资源是否发生过修改
If-Unmodified-Since:自从指定的时间之后,请求的资源是否没有修改
If-None-Match:本地缓存中存储的文档的ETag标签是否与服务器文档的
If-Match:Etag没改变,才执行请求

安全请求首部:
Authorization:向服务器发送认证信息,如账号和密码
Cookie: 客户端向服务器发送cookie

代理请求首部:
Proxy-Authorization: 向代理服务器认证

响应首部:
1.信息性:
Age:响应持续时长
Server:服务器程序软件名称和版本
2.协商首部:某资源有多种表示方法时使用
Accept-Ranges:服务器可接受的请求范围类型
Vary:服务器查看的其它首部列表;

安全响应首部:
Set-Cookie:向客户端设置cookie
Set-Cookie2: 同上,版本不一样
WWW-Authenticate:来自服务器的对客户端的质询认证表单

实体首部:
Allow: 列出对此实体可使用的请求方法
Location:告诉客户端真正的实体位于何处
Content-Length: 主体的长度
Content-Location: 实体真正所处位置
Content-Type:主体的对象类型

缓存相关:
ETag:实体的扩展标签
Expires:实体的过期时间
Last-Modified:最后一次修改的时间

entity-body:请求时附加的数据或响应时附加的数据

[root@localhost ~]# curl -I www.baidu.com   #-I:只显示响应报文首部信息
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Sun, 22 Apr 2018 15:25:13 GMT
Content-Type: text/html
Content-Length: 277
Last-Modified: Mon, 13 Jun 2016 02:50:23 GMT
Connection: Keep-Alive
ETag: "575e1f6f-115"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Accept-Ranges: bytes

详细描述httpd虚拟主机、站点访问控制、基于用户的访问控制、持久链接等应用配置实例

在httpd2.2中的相关配置文件路径如下:

    配置文件:
        /etc/httpd/conf/httpd.conf
        /etc/httpd/conf.d/*.conf

    服务脚本:
        /etc/rc.d/init.d/httpd
        脚本配置文件:/etc/sysconfig/httpd

    主程序文件:
        /usr/sbin/httpd
        /usr/sbin/httpd.event
        /usr/sbin/httpd.worker

    日志文件:
        /var/log/httpd:
                access_log:访问日志
                error_log:错误日志
    站点文档:
        /var/www/html

    模块文件路径:
        /usr/lib64/httpd/modules
                
    服务控制和启动:
        chkconfig  httpd  on|off
        service  {start|stop|restart|status|configtest|reload}  httpd

1.虚拟主机:一个物理机可以服务多个站点,没一个站点可以通过一个或多个虚拟主机来实现,httpd有三种模式:
基于IP:通过IP地址访问不同主机,为每个虚拟主机准备至少一个ip地址
基于Port:通过Port访问不同主机,为每个虚拟主机准备至少一个Port
基于FQDN主机名:通过主机名访问不同主机,为每个虚拟主机准备至少一个主机名
在httpd2.2中开启虚拟主机需要先关闭中心主机(Main server),在配置文件中注释掉DocumentRoot指令即可.

虚拟主机的配置方法:
    
        ServerName FQDN
        DocumentRoot  ""
    

这三种模式可以一起混用,可以在/etc/httpd/conf/httpd.conf主配置文件中写入配置信息也可以在/etc/httpd/conf.d/virtualhost.conf模块化的配置文件路径下单独写入配置.

  • 基于ip的虚拟机要保证一个主机中有多个ip地址.
  • 基于port的虚拟机要在配置文件中指明要监听的端口.
  • 基于主机名的虚拟机要在host文件或DNS服务器中,支配好主机域名,保证不同的域名都访问相同的ip地址.
    配置文件格式如下:
[root@localhost ~]# vim /etc/httpd/conf.d/virtualhost.conf

listen 8008   #指明要监听的端口,
   #基于IP的访问控制,保证主机有此ip
        ServerName "www.a.com"
        DocumentRoot "/var/www/a"   #这个目录为服务的根目录要创建好


  #基于IP的访问控制,保证主机有此ip
        ServerName "www.b.com"
        DocumentRoot "/var/www/b"


  #基于Port的访问控制,要先listen port
        ServerName "www.c.com"
        DocumentRoot "/var/www/c"


  ##基于主机名的访问控制
        ServerName "www.d.com"  #这个域名要能解析,可以写入host配置文件或DNS解析文件中
        DocumentRoot "/var/www/d"



        ServerName "www.f.com"  #同上
        DocumentRoot "/var/www/f"


  #基于IP和Port混用的访问控制
        ServerName "www.g.com"  
        DocumentRoot "/var/www/g"

2.站点访问控制:文件系统路径和URL路径两种机制明对哪些资源进行访问控制,可以来源的ip地址来控制哪些地址可以访问资源哪些ip地址不能访问.httpd2.2的配置格式如下:


    AllowOverride None
    Options None
    Order allow,deny  #order:生效次序.指明只有allow的地址可以访问,其他的都deny
    Allow from all   #指明允许的范围,匹配是先匹配小范围在匹配打范围

#在配置文件/etc/httpd/conf/httpd.conf中加入如下配置并保存退出重启服务

    AllowOverride None
    Options None
    Order allow,deny
    Allow from 192.168.1.106


---------------分割线---------------

[root@localhost ~]# service httpd restart   #重启服务
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]

---------------分割线---------------

#在非192.168.1.106的主机上测试
[root@localhost ~]# curl http://192.168.1.107/cgi-bin/


403 Forbidden   #显示禁止了,说明访问受限了

Forbidden

You don't have permission to access /cgi-bin/ on this server.


Apache/2.2.15 (CentOS) Server at 192.168.1.107 Port 80

3.基于用户的访问控制:对于网站中的一些资源,需要只对特定的用户开放,可以通过用户的访问控制来实现.有质询和认证两种方式.
质询:服务器用401响应码拒绝客户端请求,并说明要求客户端提供账号和密码
认证:客户端用户填入账号和密码后再次发送请求报文,认证通过时,则服务器发送响应的资源.有basic基本认证和digest摘要认证两种.这里以basic认证做实验.
basic认证的配置格式如下:

  # 需要访问控制的文件路径
    Options None
    AllowOverride None
    AuthType Basic   # 认证方式
    AuthName "String“   #认证提示信息
    AuthUserFile  "/PATH/TO/HTTPD_USER_PASSWD_FILE"   #认证用户的文件路径
    Require  user  username1  username2 ...   #允许访问的用户

#  Require  valid-user  表示允许所有AuthUserFile 文件中所以的账号
#  AuthGroupFile "/PATH/TO/HTTPD_GROUP_FILE"  基于用户组做访问控制
#  Require  group  grpname1  grpname2 ... 同上

登陆使用的账号为虚拟账号而非系统的账号,而且httpd是明文传输的,所以使用htpasswd命令来创建账号文件格式如下:

htpasswd  [options]   /PATH/TO/HTTPD_PASSWD_FILE  username 
        -c:自动创建此处指定的文件,因此,仅应该在此文件不存在时使用
        -m:md5格式加密
        -s: sha格式加密
        -D:删除指定用户
[root@localhost ~]# mkdir /etc/httpd/users
[root@localhost ~]# htpasswd -c -m /etc/httpd/users/.htpasswd tom
New password: 
Re-type new password: 
Adding password for user tom
[root@localhost ~]# htpasswd -m /etc/httpd/users/.htpasswd jeck
New password: 
Re-type new password: 
Adding password for user jeck
[root@localhost ~]# cat /etc/httpd/users/.htpasswd 
jeck:$apr1$FnsoQ9yU$cjHvdY0VtLKNmpRvLrXG00
tom:$apr1$Z0pZGKXv$HYvkDBq5tW.RkPUQcqj9x1

创建要控制的文件路径及文件并在/etc/httpd/conf.d/admin.conf文件中添加配置

[root@localhost ~]# mkdir /var/www/html/date
[root@localhost ~]# vim /var/www/html/date/index.html

admin page
~            

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


        Options None
        AllowOverride None
        AuthType Basic
        AuthName "please enter the user"
        AuthUserFile "/etc/http/users/.htpasswd"  #htpasswd命令保存的文件路径
        Require user tom  #允许的用户,在htpasswd文件中必须存在,


保存退出之后,检查语法错误并重启服务

[root@localhost ~]# httpd -t
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
Syntax OK
[root@localhost ~]# !serv
service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]

最后测试一下


用户认证登陆.png

出现认证登陆界面说明配置成功了.

4.持久链接:服务器连续建立后,每个资源获取完成后不全断开连接,而是继续等待其它资源请求的进行,通过连接次数或等待时间的限制来断开连接.可以在/etc/httpd/conf.d/创建xxx.conf来配置,相关参数如下:

KeepAlive  On|Off  开启或关闭保持连接
KeepAliveTimeout  15   等待超时时间(超过时间就关闭)
MaxKeepAliveRequests  100  (最大允许请求数量,和Timeout两者谁先满足条件就关闭)

可以使用telnet命令进行测试,格式如下

telnet  WEB_SERVER_IP  PORT   #服务器的ip换端口
GET  /URL  HTTP/1.1 #/url  httpd的版本号
Host: WEB_SERVER_IP
[root@localhost ~]# vim /etc/httpd/conf.d/keepalive.conf

keepalive  On
KeepAliveTimeout  15   
MaxKeepAliveRequests  100  
#报错退出后可以用httpd -t 检查语法,并重启服务
--------------分割线--------------

[root@localhost ~]# telnet 192.168.1.107 80
Trying 192.168.1.107...
Connected to 192.168.1.107.
Escape character is '^]'.
GET /index.html HTTP/1.1
Host: 192.168.1.107

HTTP/1.1 200 OK
Date: Sun, 22 Apr 2018 17:18:21 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Sun, 22 Apr 2018 17:15:44 GMT
ETag: "425a1-10-56a73124ff4ad"
Accept-Ranges: bytes
Content-Length: 16
Content-Type: text/html; charset=UTF-8

KeeepAlive page
#这里连接之后不会立即断开,会等到最大连接数或连接时长到达才会断开.

5.路径别名:将一个长格式的路径名称使用 Alias去定义一个短路径,在访问的上钻就可以不用写那么长的路径了.具体格式如下

Alias /icons/ "/var/www/icons/"  #将/var/www/icons/定义成/icons/别名,注意格式正确

 #定义这个路径允许的操作
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all


你可能感兴趣的:(httpd-2.2安装,请求状态详解及虚拟主机设定)