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

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

centos6中光盘的本地yum源默认httpd程序包是2.2版本的,而centos7中默认的httpd程序包2.4版本的,centos7要安装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:每个子进程响应的请求数量)
在httpd-2.2中未对event给出配置模版,并且只是测试功能,不建议使用。

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

[root@zcy520ooooo ~]# yum install zlib-devel
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.cn99.com
http://centos.ustc.edu.cn/centos/7.5.1804/os/x86_64/repodata/repomd.xml: [Errno 12] Timeout on http://centos.ustc.edu.cn/centos/7.5.1804/os/x86_64/repodata/repomd.xml: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
正在尝试其它镜像。
base                                                                                   | 3.6 kB  00:00:00     
extras                                                                                 | 3.4 kB  00:00:00     
updates                                                                                | 3.4 kB  00:00:00     
updates/7/x86_64/primary_db                                                            | 6.0 MB  00:00:01     
正在解决依赖关系
--> 正在检查事务
---> 软件包 zlib-devel.x86_64.0.1.2.7-17.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==============================================================================================================
 Package                    架构                   版本                            源                    大小
==============================================================================================================
正在安装:
 zlib-devel                 x86_64                 1.2.7-17.el7                    base                  50 k

事务概要
==============================================================================================================
安装  1 软件包

总下载量:50 k
安装大小: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
  正在安装    : zlib-devel-1.2.7-17.el7.x86_64                                                            1/1 
  验证中      : zlib-devel-1.2.7-17.el7.x86_64                                                            1/1 

已安装:
  zlib-devel.x86_64 0:1.2.7-17.el7                                                                            

完毕!

2.安装开发环境包组

[root@zcy520ooooo ~]# yum groupinstall "Development Tools" "ServerPlatform Development" -y
已加载插件:fastestmirror
没有安装组信息文件
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.cn99.com
......
已安装:
  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                   
  elfutils.x86_64 0:0.170-4.el7                                flex.x86_64 0:2.5.37-3.el7                     
  gcc.x86_64 0:4.8.5-28.el7_5.1                                gcc-c++.x86_64 0:4.8.5-28.el7_5.1              
  gcc-gfortran.x86_64 0:4.8.5-28.el7_5.1                       git.x86_64 0:1.8.3.1-14.el7_5                  
  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-10.el7_5                  
  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-80.el7.centos               rpm-build.x86_64 0:4.11.3-32.el7               
  rpm-sign.x86_64 0:4.11.3-32.el7                              subversion.x86_64 0:1.7.14-14.el7              
  swig.x86_64 0:2.0.10-5.el7                                   systemtap.x86_64 0:3.2-8.el7_5                 

作为依赖被安装:
  apr.x86_64 0:1.4.8-3.el7_4.1                            apr-util.x86_64 0:1.5.2-6.el7                       
  avahi-libs.x86_64 0:0.6.31-19.el7                       boost-date-time.x86_64 0:1.53.0-27.el7              
  boost-system.x86_64 0:1.53.0-27.el7                     boost-thread.x86_64 0:1.53.0-27.el7                 
  bzip2.x86_64 0:1.0.6-13.el7                             cpp.x86_64 0:4.8.5-28.el7_5.1                       
  dwz.x86_64 0:0.11-3.el7                                 dyninst.x86_64 0:9.3.1-1.el7                        
  efivar-libs.x86_64 0:31-4.el7                           emacs-filesystem.noarch 1:24.3-20.el7_4             
  gdb.x86_64 0:7.6.1-110.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-222.el7                   
  glibc-headers.x86_64 0:2.17-222.el7                     gnutls.x86_64 0:3.3.26-9.el7                        
  kernel-debug-devel.x86_64 0:3.10.0-862.14.4.el7         kernel-headers.x86_64 0:3.10.0-862.14.4.el7         
  libdwarf.x86_64 0:20130207-4.el7                        libgfortran.x86_64 0:4.8.5-28.el7_5.1               
  libgnome-keyring.x86_64 0:3.12.0-1.el7                  libmodman.x86_64 0:2.0.1-8.el7                      
  libmpc.x86_64 0:1.0.1-3.el7                             libproxy.x86_64 0:0.4.11-11.el7                     
  libquadmath.x86_64 0:4.8.5-28.el7_5.1                   libquadmath-devel.x86_64 0:4.8.5-28.el7_5.1         
  libstdc++-devel.x86_64 0:4.8.5-28.el7_5.1               m4.x86_64 0:1.4.16-10.el7                           
  mokutil.x86_64 0:12-2.el7                               mpfr.x86_64 0:3.1.1-4.el7                           
  neon.x86_64 0:0.30.0-3.el7                              nettle.x86_64 0:2.7.1-8.el7                         
  pakchois.x86_64 0:0.4-10.el7                            perl-Data-Dumper.x86_64 0:2.145-3.el7               
  perl-Error.noarch 1:0.17020-2.el7                       perl-Git.noarch 0:1.8.3.1-14.el7_5                  
  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                       rsync.x86_64 0:3.1.2-4.el7                          
  subversion-libs.x86_64 0:1.7.14-14.el7                  systemtap-client.x86_64 0:3.2-8.el7_5               
  systemtap-devel.x86_64 0:3.2-8.el7_5                    systemtap-runtime.x86_64 0:3.2-8.el7_5              
  trousers.x86_64 0:0.3.14-2.el7                          unzip.x86_64 0:6.0-19.el7                           
  zip.x86_64 0:3.0-11.el7                                

作为依赖被升级:
  libgcc.x86_64 0:4.8.5-28.el7_5.1   libgomp.x86_64 0:4.8.5-28.el7_5.1   libstdc++.x86_64 0:4.8.5-28.el7_5.1 

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

[root@zcy520ooooo ~]# ls
anaconda-ks.cfg  apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.2.32.tar.gz
-------------------------分割线--------------------------

[root@zcy520ooooo ~]# tar -xvf apr-1.6.5.tar.gz 
[root@zcy520ooooo ~]# tar -xf apr-util-1.6.1.tar.gz
[root@zcy520ooooo ~]# tar xf httpd-2.2.32.tar.gz 
[root@zcy520ooooo ~]# ls
anaconda-ks.cfg  apr-1.6.5.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.2.32.tar.gz
apr-1.6.5        apr-util-1.6.1    httpd-2.2.32
#依次用tar命令解压到当前目录即可

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

编译安装apr-1.6.5
[root@zcy520ooooo ~]# cd apr-1.6.5

[root@zcy520ooooo apr-1.6.5]# ./configure --prefix=/usr/local/apr-httpd/ --with-apr=/usr/local/apr-httpd/
...
[root@zcy520ooooo apr-1.6.5]# make && make install
...
done
/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr-util-httpd//build-1/apr_rules.mk
/usr/bin/install -c -m 644 /root/apr-1.6.5/build/apr_common.m4 /usr/local/apr-util-httpd//build-1
/usr/bin/install -c -m 644 /root/apr-1.6.5/build/find_apr.m4 /usr/local/apr-util-httpd//build-1
/usr/bin/install -c -m 755 apr-config.out /usr/local/apr-util-httpd//bin/apr-1-config

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

编译安装apr-util-1.6.5
[root@zcy520ooooo apr-1.6.5]# cd
[root@zcy520ooooo ~]# cd apr-util-1.6.1
[root@zcy520ooooo apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util-httpd/ --with-apr=/usr/local/apr-httpd/
[root@zcy520ooooo apr-util-1.6.1]# 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
--------------------------------分割线-------------------------
#如果在安装apr-util时出现以下情况:

xml/apr_xml.c:35:19: 致命错误:expat.h:没有那个文件或目录
 #include 
                   ^
编译中断。
make[1]: *** [xml/apr_xml.lo] 错误 1
make[1]: 离开目录“/root/apr-util-1.6.1”
make: *** [all-recursive] 错误 1

#此时需要使用yum安装expat-devel,安装完成后,重新make && make install 即可

5.编译安装httpd-2.2

[root@zcy520ooooo apr-util-1.6.5]# cd ..
[root@zcy520ooooo ~]# cd httpd-2.2.32
[root@zcy520ooooo 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/ --with-included-apr
...
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/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安装的路径

--with-included-apr:由于我的CENTOS 是 64bit的,在编译配置中并没有指定 --with-included-apr的具体引用路径,导致找不到相应的库文件,而 --with-included-apr 默认是查找/usr/lib下的库文件,而,正确的引用应该是/usr/lib64下的

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

[root@zcy520ooooo ~]# cd /usr/local/apache/
[root@zcy520ooooo 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@zcy520ooooo 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

#这个报错是主机名没有设置好
#要更改主机名可以这样做:
[root@zcy520ooooo apache]# vim /etc/httpd/httpd.conf

...
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
#ServerName www.example.com:80
ServerName 192.168.80.27    #在此处添加主机IP地址或域名,可不加端口号
...

8.查看80端口是否监听

State      Recv-Q Send-Q          Local Address:Port                         Peer Address:Port              
LISTEN     0      128                         *:22                                      *:*                  
LISTEN     0      100                 127.0.0.1:25                                      *:*                  
ESTAB      0      0               192.168.80.27:22                          192.168.80.47:53813              
LISTEN     0      128                        :::80                                     :::*                  
LISTEN     0      128                        :::22                                     :::*                  
LISTEN     0      100                       ::1:25                                     :::* 

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

[root@zcy520ooooo ~]# iptables -F
[root@zcy520ooooo ~]# setenforce 0
[root@zcy520ooooo ~]# getenforce
Permissive

10.输入主机ip测试

http2.2安装成功.png

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

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

[root@zcy520ooooo ~]# 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).
# 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.
#
#HTTPD_LANG=C

#
# By default, the httpd process will create the file

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

[root@zcy520ooooo ~]# 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   #将该选项切换到对应模式,默认为prefork方式,重启服务即可
#
# 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.
#
#HTTPD_LANG=C

#
# By default, the httpd process will create the file

重启服务

[root@zcy520ooooo ~]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [确定]

查看httpd启动模式

[root@zcy520ooooo ~]# ps aux | grep httpd
root      1598  0.2  0.3  11188  3340 ?        Ss   23:19   0:00 /usr/sbin/httpd
apache    1600  0.0  0.2  11188  2136 ?        S    23:19   0:00 /usr/sbin/httpd
apache    1601  0.0  0.2  11188  2164 ?        S    23:19   0:00 /usr/sbin/httpd
apache    1602  0.0  0.2  11188  2136 ?        S    23:19   0:00 /usr/sbin/httpd
apache    1603  0.0  0.2  11188  2136 ?        S    23:19   0:00 /usr/sbin/httpd
apache    1604  0.0  0.2  11188  2136 ?        S    23:19   0:00 /usr/sbin/httpd
apache    1605  0.0  0.2  11188  2136 ?        S    23:19   0:00 /usr/sbin/httpd
apache    1606  0.0  0.2  11188  2136 ?        S    23:19   0:00 /usr/sbin/httpd
apache    1607  0.0  0.2  11188  2136 ?        S    23:19   0:00 /usr/sbin/httpd
root      1609  0.0  0.0   6044   808 pts/0    S+   23:19   0:00 grep httpd

2.worker工作方式
修改etc/sysconfig/httpd文件中HTTPD=/usr/sbin/httpd.worker
验证worker工作方式

[root@zcy520ooooo ~]# ps aux | grep httpd
root      1637  0.0  0.3  11364  3508 ?        Ss   23:22   0:00 /usr/sbin/httpd.worker
apache    1639  0.5  0.2 289096  2916 ?        Sl   23:22   0:00 /usr/sbin/httpd.worker
apache    1640  0.0  0.2 289096  2932 ?        Sl   23:22   0:00 /usr/sbin/httpd.worker
apache    1641  0.0  0.0      0     0 ?        Z    23:22   0:00 [httpd.worker] 
apache    1642  0.0  0.2 289096  2932 ?        Sl   23:22   0:00 /usr/sbin/httpd.worker

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

[root@zcy520ooooo ~]# ps aux | grep httpd
root      1782  0.0  0.3  11352  3512 ?        Ss   23:24   0:00 /usr/sbin/httpd.event
apache    1784  0.0  0.2 289084  2812 ?        Sl   23:24   0:00 /usr/sbin/httpd.event
apache    1785  0.0  0.2 289084  2816 ?        Sl   23:24   0:00 /usr/sbin/httpd.event
apache    1786  0.0  0.2 289084  2828 ?        Sl   23:24   0:00 /usr/sbin/httpd.event
root      1869  1.0  0.0   6044   808 pts/0    S+   23:24   0:00 grep httpd

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

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

报文语法格式:
    requrest报文
          
        

        

    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:通过服务器自己可接受的媒体类型(MIME,text/html,appliction/javascript,images/jpeg);
Accept-Charset:浏览器指明自己接收的字符集
Accept-Encoding:接受编码格式,一般指压缩机制,如gzip、deflate、sdch
Accept-Language:接受的语言
Client-IP:客户端ip地址
Host:请求的服务器名称和端口号
Referer:包含当前正在请求的资源的上一级资源
User-Agent:客户端代理

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

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

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

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

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

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

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

curl的常用选项:

    -A/--user-agent  设置用户代理发送给服务器
    --basic 使用HTTP基本认证
    --tcp-nodelay 使用TCP_NODELAY选项
    -e/--referer  来源网址
    --cacert  CA证书 (SSL)
    --compressed 要求返回是压缩的格式
    -H/--header 自定义首部信息传递给服务器
    -I/--head 只显示响应报文首部信息
    --limit-rate  设置传输速度
    -u/--user 设置服务器的用户和密码
    -0/--http1.0 使用HTTP 1.0 

用法:curl [options] [URL...]

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

[root@localhost ~]# curl -I www.baidu.com    
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Tue, 06 Nov 2018 04:24:39 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虚拟主机、站点访问控制、基于用户的访问控制、持久链接等应用配置实例

在httpd-2.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 10086    指明要监听的端口
    #基于IP的访问控制,保证主机有此IP
        ServerName "www.one.com"
        DocumentRoot "/var/www/one"    #这个目录为服务的根目录要创建好


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


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


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


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


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

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.199.130


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

[root@localhost www]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:httpd.event: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[Mon Nov 05 21:38:18 2018] [warn] VirtualHost 192.168.199.130:80 overlaps with VirtualHost 192.168.199.130:80, the first has precedence, perhaps you need a NameVirtualHost directive
[Mon Nov 05 21:38:18 2018] [warn] VirtualHost 192.168.199.130:80 overlaps with VirtualHost 192.168.199.130:80, the first has precedence, perhaps you need a NameVirtualHost directive
                                                           [确定]

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

[root@localhost ~]# curl http://192.168.199.130/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.199.130 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 jerry
New password: 
Re-type new password: 
Adding password for user jerry
[root@localhost ~]# cat /etc/httpd/users/.htpasswd 
tom:$apr1$sNR5ph.L$do0VUPGF54k4TISodgGqz.
jerry:$apr1$Mq/ekV8h$urpmRqZzmt191DwjSeSoN1

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

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

hello admin

[root@localhost ~]# vim /etc/httpd/conf.d/admin.conf NameVirtualHost 192.168.199.130:80 #http-2.2使用基于FQDN的虚拟主机时,需要事先使用此指令 ServerName www.magedu.com DocumentRoot "/var/www/html/date" Options None AllowOverride None AuthType basic AuthName "please enter the user" AuthUserFile "/etc/httpd/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 ~]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:httpd.event: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [确定]

最后测试一下


用户认证登陆.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  HTTPD/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.199.130 80
Trying 192.168.199.130...
Connected to 192.168.199.130.
Escape character is '^]'.
GET /index.html HTTP/1.1
Host: 192.168.199.130

HTTP/1.1 200 OK
Date: Mon, 05 Nov 2018 14:40:46 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Mon, 05 Nov 2018 14:30:46 GMT
ETag: "425a1-10-56a73124ff4ad"
Accept-Ranges: bytes
Content-Length: 16
Content-Type: text/html; charset=UTF-8

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

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