zoukankan      html  css  js  c++  java
  • apache服务器的常用功能及设置

    安装httpd

        yum -y install  httpd
        服务脚本:/etc/rc.d/init.d/httpd
           脚本配置文件:/etc/sysconfig/httpd
        运行目录:/etc/httpd
        配置文件:
         主配置文件:/etc/httpd/conf/httpd.conf
         扩展配置:/etc/httpd/conf.d/*.conf
         Socket:80/tcp,443/tcp
         文档根目录:/var/www/html
             CGI:/var/www/cgi-bin/


    修改apache的工作模式

    vim /etc/sysconfig/httpd
    #HTTPD=/usr/sbin/httpd.worker

    ps -elfH #查看进程,主进程和辅助进程之间的关系


    MPM
       多道处理模块
       httpd -l 查看编译进内核的模块
       想使用不同的机制,修改配置文件即可 /etc/syconfig/httpd文件
     

    <IfModule preforck.c>判断模块是否存在
    	StartServers       8 默认启动的工作进程数
    	MinSpareServers    5 最少空闲进程数
    	MaxSpareServers   20 最大空闲进程数
    	ServerLimit      256 最大活动进程数
    	MaxClients       256 最大并发连接数,最多允许发起的连接请求的个数
    	MaxRequestsPerChild  4000 每个子进程在生命周期内最大允许服务的最多请求个数
      </IfModule>
    
    
    	<IfModule worker.c>
    	StartServers         4 启动的子进程的个数
    	MaxClients         300 最大并发连接数,最多允许发起的连接请求的个数
    	MinSpareThreads     25 最少空闲线程数
    	MaxSpareThreads     75 最大空闲线程数
    	ThreadsPerChild     25 每个子进程生成的线程数
    	MaxRequestsPerChild  0 每个子进程在声明周期内最大允许服务的最多请求个数
    	</IfModule>


    配置日志功能

       /var/log/http/
           access.log:访问日志,其需要记录的内容需要自定义
           error.log
          
          访问日志:
            CustomLog "/path/to/log_file" LogFormat
            LogFormat定义日志格式
            "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i""
            %h:客户端地址
            %l:远程的登录名,通常为-
            %u:认证时的远程用户名,通常为-
            %t:接收到的请求时的时间,为标准英文格式时间+时区
            " :转义,显示""
            %r:请求报文的起始行
            %>s:响应状态码,
            %b:以字节响应报文的长度,不包含http报文
            %{Header_Name}i:记录指定请求报文首部的内容(value)
            %u:请求的URL
            
         详情请参考:http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats
            错误日志:
             ErrorLog

    如设置日志文件为时间日期格式显示,并且超过1天则自动切割

    在apache的配置文件中找到
    ErrorLog logs/error_log
    CustomLog logs/access_log common
    将其改为 
    ErrorLog "| /usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/error_%Y%m%d.log 86400 480"
    CustomLog "| /usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/access_%Y%m%d.log 86400 480" common



    rotatelogs这样日志回滚的工具

    用法

    rotatelogs  [ -l ] logfile  [ rotationtime [ offset ]] | [ filesizeM ]


    -l 使用本地时间代替GMT时间作为时间基准。注意:在一个改变GMT偏移量(比如夏令时)的环境中使用-l会导致不可预料的结果。


    logfile 它加上基准名就是日志文件名。如果logfile中包含"%",则它会被视为用于strftime()的格式字符串;否则它会被自动加上以秒为单位的".nnnnnnnnnn"后缀。这两种格式都表示新的日志开始使用的时间。


    rotationtime 日志文件滚动的以秒为单位的间隔时间。


    offset 相对于UTC的时差的分钟数。如果省略,则假定为"0"并使用UTC时间。比如,要指定UTC时差为"-5小时"的地区的当地时间,则此参数应为"-300"。


    filesizeM 指定以filesizeM文件大小滚动,而不是按照时间或时差滚动。


    解决方法:


    ErrorLog "|/usr/local/apache2/bin/rotatelogs /usr/local/apache2/logs/www.test.com-error.log.%Y-%m-%d-%H_%M_%S 10M"
    CustomLog "|/usr/local/apache/bin/rotatelogs /usr/local/apache2/logs/www.test.com-access.log 100M" commom


    当日志大小达到10M时,日志就会滚动,产生新的日志。当然你可以根据时间来进行滚动,把100M换成36000,表时日志10个小时滚动一次。


    路径别名

       Alias /alias/ "/path/to/somewhere"
       Alias /images/ "/www/img/"
        意味着访问http://Server_IP/alias时,其页面文件来自于/path/to/somewhere中


    指定默认的字符集

         AddDefaultCharset  UTF-8


    配置通过用户目录访问

    首先编辑httpd服务器文件

    vim /etc/httpd/conf/httpd.conf 

    #UserDir disabled
        UserDir public_html


    重新加载httpd服务 service httpd reload


    创建hadoop用户并设置相关访问测试网页文件

    useradd haddop

    su - hadoop

    mkdir public_html

    vim /home/hadoop/public_html/index.html

    <h1>Welcome to Hadoop's home</h1>

    su -

    设置apache用户对hadoop目录的访问权限(setfacl -m u:apache:x ~Username)

    setfacl -m u:apache:x /home/hadoop/

    通过如下方式访问(http://Server_IP/~Username/):

    http://192.168.8.39/~hadoop/


    脚本路径别名(CGI接口)

    vim /etc/httpd/conf/httpd.conf


    vim /var/www/cgi-bin/test2

    添加如下内容:

    #!/bin/bash
    #
    cat << EOF
    Content-Type:text/html
    
    
    <pre>
    The hostname is : `hostname`
    This time is `date`
    <pre>
    
    
    EOF

    通过访问测试

    http://192.168.8.39/cgi-bin/test2 


    基于用户的访问控制

    虚拟用户:不是系统用户,只是为了获取某种资源类型的一种虚拟的用户
      文件/etc/httpd/conf/.htpasswd
      SQL数据库
      dbm:
      ldap:轻量级目录访问协议
        认证类型(auth):
            basic:基本认证,账号和密码明文发送
            digest:摘要认证,hash编码之后发送
        认证提供者(authentication provider):账号和密码的存放位置
          authn
        授权机制(authorization):根据什么进行授权
        案例:基于文件,做基本认证根据用户和组进行授权
        1、编辑配置文件,为需要认证的目录配置认证机制
        <Directory "/www/htdocs/fin">A
            options None
            AllowOverride AuthConfig 使用认证配置
            AuthType Basic 使用基本认证
            AuthName "Private Area" 质询时标题
            AuthUserFile /etc/httpd/conf/.htpasswd  密码的存放位置
            Require valid-user 可访问的用户

        </Directory>

        2、使用htpsswdm命令使用生成认证库
          htpasswd 
          -c 创建密码,创建第一个用户时使用
          htpasswd -c -m /etc/httpd/conf/.htpasswd tom
          -m MD5格式存放
          -b 批量模式
          -D 删除用户

    具体操作:

    vim /etc/httpd/conf/httpd.conf

    <Directory "/www/htdocs/fin">
            options None
            AllowOverride AuthConfig
            AuthType Basic
            AuthName "Private Area"
            AuthUserFile /etc/httpd/conf/.htpasswd
            Require valid-user
    </Directory>

    htpasswd -c -m /etc/httpd/conf/.htpasswd tom

    htpasswd -m /etc/httpd/conf/.htpasswd tom


    mkdir /www/htdocs/fin

    vim index.html


    <h1>authrization site</h1>


    通过访问以下地址验证

    http://192.168.8.39/fin/


    虚拟主机的配置


    基于不同IP的虚拟主机配置

    1.添加多个IP

    ifconfig eth0:1 192.168.8.100

    ifconfig eth0:2 192.168.8.101


    2.创建存放网站的目录文件,并创建测试文件

    mkdir /web/a.com/htdocs -pv

    vim /web/a.com/htdocs/index.html

    <h1>www.a.com</h1>

    mkdir /web/b.net/htdocs -pv

    mkdir /web/c.org/htdocs -pv

    本地hosts文件中添加相应的域名解析(路径C:WindowsSystem32driversetc)

    192.168.8.39 www.a.com
    192.168.8.100 www.b.net
    192.168.8.101 www.c.org


    3.关闭全局的虚拟目录设置

    #DocumentRoot "/www/htdocs"



    4.设置虚拟主机

    <VirtualHost 192.168.8.39:80>
            ServerName www.a.com
            DocumentRoot "/web/a.com/htdocs"
    </VirtualHost>
    <VirtualHost 192.168.8.100:80>
            ServerName www.b.net
            DocumentRoot "/web/b.net/htdocs"
    </VirtualHost>
    <VirtualHost 192.168.8.101:80>
            ServerName www.c.org
            DocumentRoot "/web/c.org/htdocs"
    </VirtualHost>

    测试 httpd -t

    重启service httpd restart

    访问测试


    基于不同端口的虚拟主机配置

    1.添加配置

    Listen 8080


    2.配置虚拟主机

    <VirtualHost 192.168.8.39:8080>
            ServerName www.d.com
            DocumentRoot "/web/d.com/htdocs"
    </VirtualHost>

    3.创建主机目录

    mkdir /web/d.com/htdocs -pv

     vim /web/d.com/htdocs/index.html


    访问 www.d.com:8080测试


    基于不同主机名实现不同的虚拟主机

            变化ServerName的值
        通过请求报文中的HOST来实现不同的虚拟主机访问


    1.配置httpd.conf

    NameVirtualHost *:80


    <VirtualHost *:80>
            ServerName www.a.com
            DocumentRoot "/web/a.com/htdocs"
            CustomLog "/var/log/httpd/www.a.com.acclog_log" combined
    </VirtualHost>
    <VirtualHost *:80>
            ServerName www.b.net
            DocumentRoot "/web/b.net/htdocs"
            CustomLog "/var/log/httpd/www.b.net.acclog_log" combined
    </VirtualHost>
    <VirtualHost *:80>
            ServerName www.c.org
            DocumentRoot "/web/c.org/htdocs"
            CustomLog "/var/log/httpd/www.c.org.acclog_log" combined
    </VirtualHost>



  • 相关阅读:
    mysql-数据库增删改查
    判断,循环
    数组
    html 三种垂直居中
    箭头函数
    Array类型
    object
    JAVA WEB 行业技术
    一个好的程序员
    经典语录
  • 原文地址:https://www.cnblogs.com/reblue520/p/6239890.html
Copyright © 2011-2022 走看看