wfds 发表于 2015-5-4 09:02:27

http服务器在apache上的配置--我的学习记录

HTTP协议:
    超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。
Apache:
    Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。(因为在linux上Apache的主程序为httpd,所以下文就称Apache为httpd。)
注意:本文如没有特别说明版本,则默认为httpd-2.2版本环境。

http服务器工作的三种进程模型:
prefork:多进程模型,每个进程响应一个请求;
    一个主进程:负责生成n个子进程,子进程也称为工作进程,每个子进程处理一个用户请求;即便没有用户请求,也会预先生成多个空闲进程,随时等待请求到达;linux默认最大不会超过1024个;

    worker:多线程模型,每个线程响应一个请求;
    一个主进程:生成多个子进程,每个子进程负责生个多个线程,每个线程响应一个请求;                                    
    event:事件驱动模型,每个线程响应n个请求;
    一个主进程:生成m个子进程,每个进程直接n个请求;

httpd的各个文件存放目录:(统一记录,以便查找)
   
主配置文件:(/etc/httpd/conf/httpd.conf)
    三段组成:
    ### Section 1: Global Environment全局
    ### Section 2: 'Main' serverconfiguration 主要配置
    ### Section 3: Virtual Hosts 虚拟主机配置
    注意:'Main’和Virtual Hosts 配置不能同时启用,使用Virtual Hosts的时候需注释掉'Main'中的’DocumentRoot’

    监听端口:
    Listen 80
    IP省略表示监听本机所有IP地址,Listen可出现多次指明不同的监听端口或套接字

    持久连接:(指的是三次握手建立连接,请求数据完成之后是否立即断开,还是保持连接等待下一个传输)
    KeepAlive off|on         是否开启持久连接
    MaxKeepAliveRequests 100   一个持久连接最大请求数,超过100则断开连接
    KeepAliveTimeout 15      一个持久连接最长时间,超过15秒则断开(httpd-2.4支持毫秒级)

    测试方法:
    # telnet Server 80(在另一个终端键入命令 Server:服务器地址)
    GET /URL HTTP/1.1   (GET:代表请求类型,/URL:请求服务器的内容,HTTP/1.1:HTTP协议版本)
    Host: Serve         (服务器地址)

    多路处理模块:(prefork,worker,event)默认为/usr/sbin/httpd,也就是prefork;
      查看模块列表命令:
            httpd -l:查看静态编译的模块

            httpd -M:查看所有模块

      更换MPM主程序:(编辑/etc/sysconfig/httpd中的HTTPD变量)


      
    prefork模块程序:(会根据你所使用的模块,自动识别对应模块的配置信息)
    <IfModuleprefork.c>                           
    StartServers       8       默认启动8个子服务进程
    MinSpareServers    5       最小空闲服务进程为5个
    MaxSpareServers   20       最大空闲服务进程为20个
    ServerLimit      256       最大服务进程256个
    MaxClients       256       最大客户端数256个
    MaxRequestsPerChild4000每个子进程最大响应请求数4000,达到4000则退出此服务子进程
    </IfModule>

    worker模块程序:
    <IfModule worker.c>
    StartServers         4      默认启动4个子进程
    MaxClients         300      最大客户端数300个
    MinSpareThreads   25      最小空闲线程数25个
    MaxSpareThreads   75      最大空闲线程数75个
    ThreadsPerChild   25      每个子进程可以生成25个线程
    MaxRequestsPerChild0      不限制每个进程响应请求数
    </IfModule>

    文档路径映射:
    DocumentRoot "/var/www/html" 指定/var/www/html/为URL起始位置


    站点路径访问控制:
      基于来源地址控制:
      基于账号控制:
    两种方式:
      文件系统路径:
      <Directory"/PATH/TO/SOMEDIR">
      ...
      </Directory>
      URL路径:
      <Location"/URL">
      ...
      </Location>

      Directory中的访问控制定义:
      (1)Options
            Indexes:当前路径无主页时,将路径下的所有资源以列表呈现给用户。危险!勿用!
            FollowSymLinks:如果某页面文件为DocumentRoot之外路径的其他文件时,将直接显示目标文件内容。
            None:禁用所有
            ALL:启用所有

      (2)基于来源地址访问控制:
            Order:检查次序
            Order Allow Deny:只有明确Allow的来源地址,才允许访问,其它的均为Deny
            Order Deny Allow:只有明确Deny的地址才Deny,其他的均允许访问
            Allow from:允许访问的来源地址
            Deny from:拒绝访问的来源地址
       例如:(在172.16.0.0/16 这个网段中,出了100.2这个IP,其他IP都可以访问)
            <Directory "/var/www/html">
            Options Indexes FollowSymLinks
            AllowOverride None
            Order allow,deny
            Deny from 172.16.100.2
            Allow from 172.16.0.0/16
            </Direcotry>


    DSO动态装卸载模块:



      
      禁用:直接用“#”注释掉;

      启用:取消“#”注释

            建议:装卸载模块后“service httpd reload”

    定义默认的主页:(自左而右依次匹配)
      DirectoryIndexindex.html index.html.var index.php

    配置日志系统:
      错误日志:(级别高于 warn 的错误日志都被保存在 logs/error_log)
      
      访问日志:(保存在logs/access_log,LogFormat是访问日志的输出格式)


      
      
      %h: Remote host,客户端主机
      %l: Remote logname (from identd, ifsupplied). 客户用户通过identd登录时使用名称;一般为-;
      %u: Remote user (from auth; may be bogusif return status (%s) is 401),用户认证登录的名字;无登录机制一般为-;
      %t: Time the request was received (standard englishformat),收到客户端请求时的时间;
      \": 显示引号本身,而不作为引用符号;
      %r:First line ofrequest,请求报文的首行
      <method><url> <version>
      %>s:响应状态状态码
      %b: Size of response in bytes, excluding HTTPheaders,响应报文的大小,单位为字节;不包含首部信息;
      %{Referer}i:记录Http首部Referer对应的值;即访问入口,从哪个页面跳转至此页面;
      %{User-Agent}i:记录http首部User-Agent对应的值;即浏览器类型;
      详情: http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats

    路径别名:
      Alias/URL/ "/path/to/some_directory"
      例如:Alias /bbs/ "/web/bbs/htdocs/"
            URL:http://www.magedu.com/bbs/index.html--> /web/bbs/htdocs/index.html

    扩展:


   basic认证机制的实现
      (1) 定义安全域
      <Directory"/data/web/html/employee">
            Options None
            AllowOverride None
            AuthType Basic
            AuthName "Employee Infomation, onlyfor employee"
            AuthUserFile /etc/httpd/users/.htpasswd
            Require user tom jerry
      </Directory>      
      Require valid-user: 所有位于AuthUserFile文件中定义的用户都允许登录;
      Require user user1 user2 ...: 仅允许user1,user2等出现AuthUserFile文件中定义的特定几个用户登录;

      (2) 提供用户的账号密码文件(虚拟用户,并不是linux系统的用户)
         htpasswd [ -c ] [ -m ] [ -D ] passwdfileusername
               -c:添加第一用户时创建此文件(第二个用户时,无需使用);
               -m:以md5格式加密用户密码存放;
               -s:以sha格式加密用户密码存放;
               -D:删除指定用户
      (3) 组认证

      <Directory"/data/web/html/employee">
            Options None
            AllowOverride None
            AuthType Basic
            AuthName "Employee Infomation, onlyfor employee"
            AuthUserFile /etc/httpd/users/.htpasswd
            AuthGroupFile /etc/httpd/users/.htgroup
            Require group GRP1 GRP2 ...
      </Directory>

       组文件:每行定义一个组
      Grp_Name:User1 User2 …

    虚拟主机:(基于IP,基于port,基于FQDN)
      一个物理器可以服务于多个站点,每个站点可通过一个或多个虚拟主机来实现;
    注意:再次提醒虚拟主机和Mainserver两互相冲突,需关闭DocumentRoot
    定义虚拟主机的方法:(大多数可用于全局或'main' server中的指令,都可以定义有VirtualHost中)
      <VirtualHost"IP:PORT">
      ServerName
      ServerAlias
      DocumentRoot
      </VirtualHost>
    测试:

      示例1:基于IP
      <VirtualHost172.16.100.11:80>
            ServerName www.a.com
            DocumentRoot /vhost/a.com/htdocs/
      </VirtualHost>

      <VirtualHost172.16.100.21:80>
            ServerName www.b.org
            DocumentRoot /vhost/b.org/htdocs/
      </VirtualHost>

      <VirtualHost172.16.100.31:80>
            ServerName www.c.net
            DocumentRoot /vhost/c.net/htdocs/
      </VirtualHost>      

      注意:本机要配置上多个IP地址并能够用于通信;

       示例2:基于Port
      <VirtualHost172.16.100.11:80>
            ServerName www.a.com
            DocumentRoot /vhost/a.com/htdocs/
      </VirtualHost>

      <VirtualHost172.16.100.11:808>
            ServerName www.b.org
            DocumentRoot /vhost/b.org/htdocs/
      </VirtualHost>

      <VirtualHost172.16.100.11:8080>
            ServerName www.c.net
            DocumentRoot /vhost/c.net/htdocs/
      </VirtualHost>      

       注意:配置文件中要监听这里指明的所有端口
            Listen 80

            Listen 808
            Listen 8080

       示例3:混用IP和Port
      <VirtualHost172.16.100.11:80>
            ServerName www.a.com
            DocumentRoot /vhost/a.com/htdocs/
      </VirtualHost>

      <VirtualHost172.16.100.21:80>
            ServerName www.b.org
            DocumentRoot /vhost/b.org/htdocs/
      </VirtualHost>

      <VirtualHost172.16.100.11:8080>
            ServerName www.c.net
            DocumentRoot /vhost/c.net/htdocs/
      </VirtualHost>      

       示例4:基于FQDN
      NameVirtualHost 172.16.100.11:80

      <VirtualHost172.16.100.11:80>
            ServerName www.a.com
            DocumentRoot /vhost/a.com/htdocs/
      </VirtualHost>

      <VirtualHost172.16.100.11:80>
            ServerName www.b.org
            DocumentRoot /vhost/b.org/htdocs/
      </VirtualHost>

      <VirtualHost172.16.100.11:80>
            ServerName www.c.net
            DocumentRoot /vhost/c.net/htdocs/
      </VirtualHost>      


    status页面工具:(查看httpd工作状态,为了安全不能让所有人都查看)

                              <Location /server-status>
                                SetHandler server-status
                                Order deny,allow
                                Deny from all
                                Allow from 192.168.2.0/24(仅允许这个网段的可以访问status页面)
                              </Location>
配置好以上某些必要的选项,那么我们的httpd服务器就可以说简单的搭建好了。那么我们现在放入一个html文件测试一下。
    1.在/var/www/html/目录创建index.html文件,并且键入 “Hello Web !”
    2.重启httpd服务器(service httpd restart)
    3.浏览器输入你的url

   
注意:一般来说,如果没有更改端口和重要配置,使用reload,因为真实的线上环境,不允许随意restart重启服务的。


页: [1]
查看完整版本: http服务器在apache上的配置--我的学习记录