zoukankan      html  css  js  c++  java
  • httpd安装和基本配置

    Httpd 安装

    版本:

    CentOS 6: 2.2
    CentOS 7: 2.4

    安装方式:

    rpm:centos发行版,稳定,建议使用
    编译:定制或特殊需求

    CentOS 7程序环境:httpd-2.4(rpm安装)

    配置文件:

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

    检查配置语法:

    httpd –t

    Httpd 程序环境

    服务单元文件:

    /usr/lib/systemd/system/httpd.service

    配置文件:

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

    服务控制和启动:

    systemctl enable|disable httpd.service
    systemctl {start|stop|restart|status|reload} httpd.service

    默认站点网页文档根目录:

    /var/www/html

    模块文件路径:

    /etc/httpd/modules
    /usr/lib64/httpd/modules

    主程序文件:

    /usr/sbin/httpd

    主进程文件:

    /etc/httpd/run/httpd.pid

    日志文件目录:

    /var/log/httpd
    access_log: 访问日志
    error_log:错误日志

    Httpd 常见配置

    httpd主配置文件的组成:

     
    Global Environment
    Main server configuration
    virtual host
    配置格式:directive value
      directive
        注意,不区分字符大小写
       value
        注意,为路径时,是否区分大小写,取决于文件系统

    官方帮助

    http://httpd.apache.org/docs/2.4/
     
     

    1、显示服务器版本信息

    ServerTokens Major|Minor|Min[imal]|Prod[uctOnly]|OS|Full 
    建议使用:ServerTokens Prod。默认为ServerTokens Full
     

    2、修改监听的IP和Port 

    Listen [IP:]PORT
    (1) 省略IP表示为本机所有IP
    (2) Listen指令至少一个,可重复出现多次
    Listen 80
    Listen 8080

    3、持久连接

    Persistent Connection:连接建立,每个资源获取完成后不会断开连接,而是继续等待其它的请求完成,默认关闭持久连接
    断开条件:时间限制:以秒为单位, 默认5s,httpd-2.4 支持毫秒级
    副作用:对并发访问量大的服务器,持久连接会使有些请求得不到响应
    设置:KeepAlive On|Off
       KeepAliveTimeout 15
    测试:telnet WEB_SERVER_IP PORT
    GET URL HTTP/1.1
    Host: WEB_SERVER_IP

    4、DSO: Dynamic Shared Object

    加载动态模块配置,不需重启即生效
    配置指定实现模块加载格式:
    LoadModule <mod_name> <mod_path>
    模块文件路径可使用相对路径:相对于ServerRoot(默认/etc/httpd)
    示例:LoadModule auth_basic_module modules/mod_auth_basic.so
    动态模块路径: /usr/lib64/httpd/modules/
    查看静态编译的模块
    httpd -l
    查看静态编译及动态装载的模块
    httpd –M

    5、MPM( Multi-Processing Module)多路处理模块

    三种:prefork, worker, event
    切换使用的MPM,默认使用的是prefork。
    /etc/httpd/conf.modules.d/00-mpm.conf
    取消注释要启用的MPM相关的LoadModule指令即可。注意需要重启httpd。

    prefork的配置:

    StartServers  8
    MinSpareServers  5
    MaxSpareServers  20
    ServerLimit  256 最多进程数,最大值 20000
    MaxClients  256 最大的并发连接数
    MaxRequestsPerChild  4000 子进程最多能处理的请求数量。在处理MaxRequestsPerChild  个请求之后,子进程将会被父进程终止,这时候子进 程占用的内存就会释放(为0时永远不释放)

    worker的配置:

    ServerLimit 16  启动进程上限
    StartServers 2  httpd启动后开启的进程数,默认为3
    MaxRequestWorkers 150 最大并发数
    MinSpareThreads 25  每个进程的最小空闲线程数
    MaxSpareThreads 75  每个进程的最大空闲线程数
    ThreadsPerChild 25 每个进程开启的线程数

    6、定义'Main' server的文档页面路径

    DocumentRoot “/path” 。path默认路径为/var/www/html
    文档路径映射:
    DocumentRoot指向的路径为URL路径的起始位置
    示例:
    DocumentRoot "/app/data“
    http://HOST:PORT/test/index.html --> /app/data/test/index.html
    /etc/httpd/conf.d/common.conf
    documentroot /app/data directoryindex index1.html <directory /app/data> allowoverride none require all granted </directory>
    mkdir /app/data
    echo "/app/data/index1.html" > /app/data/index1.html

    7、定义站点主页面

    DirectoryIndex index.html
     

    8、站点访问控制常见机制

    可基于两种机制指明对哪些资源进行何种访问控制
    访问控制机制:
      客户端来源地址,用户账号
    (1)文件系统路径:
    <Directory “/path">
    ...
    </Directory>
     
    <File “/path/file”>
    ...
    </File>
    <FileMatch "PATTERN">
    ...
    </FileMatch>
    (2)URL路径:
    <Location "">
    ...
    </Location>
    <LocationMatch "">
    ...
    </LocationMatch>

    9、<Directory>中“基于源地址”实现访问控制

    (1) Options:后跟1个或多个以空白字符分隔的选项列表
    在选项前的+,- 表示增加或删除指定选项
    常见选项:
      Indexes:指明的URL路径下不存在与定义的主页面资源相符的资源文件时,返回索引列表给用户,建议不要使用。
      FollowSymLinks:允许访问符号链接文件所指向的源文件
      None:全部禁用
      All: 全部允许
    (2) AllowOverride
    与访问控制相关的哪些指令可以放在指定目录下的.htaccess(由AccessFileName指定)文件中,覆盖之前的配置指令。只对<directory>语句有效。
    AllowOverride All: #.htaccess中所有指令都有效
    AllowOverride None: #.htaccess 文件无效
    AllowOverride AuthConfig #.htaccess 文件中,除了AuthConfig 其它指令都无法生效
    (3) 基于IP的访问控制:
    无明确授权的目录,默认拒绝
    允许所有主机访问:Require all granted
    拒绝所有主机访问:Require all denied
    控制特定的IP访问:
      Require ip IPADDR:授权指定来源的IP访问
      Require not ip IPADDR:拒绝特定的IP访问
    控制特定的主机访问:
      Require host HOSTNAME:授权特定主机访问
      Require not host HOSTNAME:拒绝
    HOSTNAME:
      FQDN:特定主机
      domin.tld:指定域名下的所有主机
    示例
    <RequireAll> #不能有失败,至少有一个成功匹配才成功,即失败优先 
    Require all granted
    Require not ip 172.16.1.1 拒绝特定IP
    </RequireAll>
     
    <RequireAny> #多个语句有一个成功,则成功,即成功优先
    Require all denied
    require ip 172.16.1.1 允许特定IP
    </RequireAny>

    实验一:实现基于.htacces的访问控制

     /app/data/.htaccess 
    <RequireAll> 
    Require all granted
    Require not ip 192.168.206.20    ##拒绝20访问192.168.206.15主机
    </RequireAll>

    10、日志设定

    日志类型:
      访问日志、错误日志
    错误日志
      ErrorLog logs/error_log
      LogLevel warn
      LogLevel 可选值: debug, info, notice, warn,error, crit, alert, emerg。后边的比前边的就级别高,越高记录的内容越少。
    访问日志:
    定义日志格式:LogFormat format strings
      LogFormat "%h %l %u %{%F %T}t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" testlog
    使用日志格式:
      CustomLog logs/access_log testlog
    参考帮助:
    http://httpd.apache.org/docs/2.4/mod/mod_log_config.html#formats
    %h 客户端IP地址
    %l 远程用户,启用mod_ident才有效,通常为减号“-%u 验证(basic,digest)远程用户,非登录访问时,为一个减号“-%t 服务器收到请求时的时间
    %r First line of request,即表示请求报文的首行;记录了此次请求的“方法”,“URL”以及协议版本
    %>s 响应状态码
    %b 响应报文的大小,单位是字节;不包括响应报文http首部
    %{Referer}i 请求报文中首部“referer”的值;即从哪个页面中的超链接跳转至当前页面的
    %{User-Agent}i 请求报文中首部“User-Agent”的值;即发出请求的应用程序

    11、设定默认字符集

    AddDefaultCharset UTF-8 此为默认值
    中文字符集:GBK, GB2312, GB18030

    12、定义路径别名

    格式:Alias /URL/ "/PATH/"。路径别名和命令别名相类似。alias ll='ls -l --color=auto' ,执行ll就等同于ls -l --color=auto。等于是命令的替换,同理路径别名也是路径的替换。如下
    DocumentRoot "/www/htdocs"
      http://www.magedu.com/download/bash.rpm
        ==>/www/htdocs/download/bash.rpm
    Alias /download/ "/rpms/pub/"
      http://www.magedu.com/download/bash.rpm #直接将download替换为rpms/pub即可。
        ==>/rpms/pub/bash.rpm
      http://www.magedu.com/images/logo.png
        ==>/www/htdocs/images/logo.png

    13、基于用户的访问控制

    认证质询:WWW-Authenticate:响应码为401,拒绝客户端请求,并说明要求客户端提供账号和密码
    认证:Authorization:客户端用户填入账号和密码后再次发送请求报文;认证通过时,则服务器发送响应的资源
    认证方式两种:
      basic:明文
      digest:消息摘要认证,兼容性差
    安全域:需要用户认证后方能访问的路径;应该通过名称对其进行标识,以便于告知用户认证的原因
    用户的账号和密码
      虚拟账号:仅用于访问某服务时用到的认证标识
      存储:文本文件,SQL数据库,ldap目录存储,nis等

    基于用户认证配置示例:

    (1) 定义安全域
    <Directory “/path">
    Options None
    AllowOverride None
    AuthType Basic
    AuthName "String“
    AuthUserFile "/PATH/HTTPD_USER_PASSWD_FILE"
    Require |valid-user|user username1 username2 ...
    </Directory>
    Require valid-user允许账号文件中的所有用户登录访问:
    Require user username1 username2 ... 允许账号文件中的指定用户登录访问:
    (2) 提供账号和密码存储(文本文件)
    使用专用命令完成此类文件的创建及用户管理
    htpasswd [options] /PATH/HTTPD_PASSWD_FILE username
    -c:自动创建文件,仅应该在文件不存在时使用
    -p:明文密码
    -d:CRYPT格式加密,默认
    -m:md5格式加密
    -s:sha格式加密
    -D:删除指定用户

    基于组账号进行认证

    (1) 定义安全域
    <Directory “/path">
    AuthType Basic
    AuthName "String“
    AuthUserFile "/PATH/HTTPD_USER_PASSWD_FILE"
    AuthGroupFile "/PATH/HTTPD_GROUP_FILE"
    Require group grpname1 grpname2 ...
    </Directory>
    (2) 创建用户账号和组账号文件
    组文件:每一行定义一个组
    GRP_NAME: username1 username2 ...

    实验二实现基于文件的用户认证

    /etc/httpd/conf.d/common.conf
    <Directory "/app/data">
    Options None
    AllowOverride None
    AuthType Basic
    AuthName "String“
    AuthUserFile "/etc/httpd/conf.d/user_auth.txt"
    Require valid-user
    </Directory>
    创建密码文件
    htpasswd -c /etc/httpd/conf.d/user_auth.txt test1
    htpasswd  /etc/httpd/conf.d/user_auth.txt test2

    14、实现用户家目录的http共享

    此功能,基于模块mod_userdir.so实现
    相关设置:
    vim /etc/httpd/conf.d/userdir.conf
      <IfModule mod_userdir.c>
      #UserDir disabled
      UserDir public_html #指定共享目录的名称
      </IfModule>
    准备目录
    su – test;mkdir ~/public_html
    setfacl –m u:apache:x ~test
    访问
    http://localhost/~test/index.html

    实验三:实现用户家目录的http共享

    /etc/httpd/confi.d/userdir.conf
    <IfModule mod_userdir.c>
    UserDir public_html
    </IfModule>
    
    <Directory "/home/*/public_html">
    Require method GET POST OPTIONS
    </Directory>
    
    useradd test
    su - test
    mkdir public_html
    setfacl  -m u:apache:x ~test
    echo home > public_html/index.html

    15、ServerSignature On | Off | EMail

    当客户请求的网页并不存在时,服务器将产生错误文档,如果打开了ServerSignature选项,错误文档的最后一行将包含服务器的名字、Apache的版本等信息,如果不对外显示这些信息,就可以将这个参数设置为Off设置为Email,将显示ServerAdmin 的Email提示。
     
     

     16、status页面

    LoadModule status_module modules/mod_status.so
    <Location "/status">
    SetHandler server-status
    </Location>
    ExtendedStatus On 显示扩展信息
    实验四:打开状态页
    /etc/httpd/conf.d/common.conf
    <Location "/status">
     SetHandler server-status
     require all granted
    </Location>

     本章内容的配置文件common.conf内容如下

    servertokens prod
    KeepAlive On
    KeepAliveTimeout 15
    LoadModule auth_basic_module "modules/mod_auth_basic.so"
    documentroot /app/data
    directoryindex  index1.html
    logFormat "%h %l %u %{%F %T}t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" testlog
    CustomLog logs/access_log testlog
    <Directory "/app/data">
    Options None
    AllowOverride None
    require all granted
    #AuthType Basic
    #AuthName "String“
    #AuthUserFile "/etc/httpd/conf.d/user_auth.txt"
    #Require valid-user
    </Directory>
    ServerSignature off
    <Location "/status">
     SetHandler server-status
     require all granted
    </Location>
     
  • 相关阅读:
    从xml中改变checkBox大小和形状
    Android 数字签名学习笔记
    Android 说说钟表控件
    Android中选项卡TabHost的基本使用
    Android 实现图片反转
    Android openfire插件开发
    最大递增子序列——[Usaco2006 Oct]Hungry Cows饥饿的奶牛
    矩阵二分乘法(可做模板)——hdu1575
    树形递归——1621: [Usaco2008 Open]Roads Around The Farm
    规律题——Codeforces Beta Round #11 B
  • 原文地址:https://www.cnblogs.com/wxxjianchi/p/13544401.html
Copyright © 2011-2022 走看看