centos6 httpd2.2详细剖析

栏目: 服务器 · Apache · 发布时间: 5年前

内容简介:以centos6.5为例1,安装httpd,启动服务并设置自动启动

以centos6.5为例

配置文件:
           /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,启动服务并设置自动启动

~]# yum install httpd -y
~]# chkconfig httpd on
~]# service  httpd start
查看主配置段落
~]# grep -i 'section'  /etc/httpd/conf/httpd.conf 
# The configuration directives are grouped into three basic sections:
### Section 1: Global Environment #全局环境配置段
# The directives in this section affect the overall operation of Apache,
### Section 2: 'Main' server configuration 主配置段
# The directives in this section set up the values used by the 'main'
# WebDAV module configuration section.
### Section 3: Virtual Hosts #虚拟主机配置段
# The first VirtualHost section is used for requests without a known
常用配置:
                        1、修改监听的IP和PORT
 Listen  [IP:]PORT

                                        (1) 省略IP表示为0.0.0.0;
                                        (2) Listen指令可重复出现多次;
                                                Listen  80
                                                Listen  8080
                                        (3) 修改监听socket,重启服务进程方可
创建测试网页
~]# /var/www/html/test.html

           <html>
                        <head>
                                <title>百度</title>
                        </head>
                        <body>
                                <h1></h1>
                                        <p> baidu... <a href="http://www.baidu.com"> bla... </a> </p>
                                <h2> </h2>
                        </body>
           </html>

测试机安装telnet
~]# yum install telnet -y

KeepAlive Off测试短连接
 ~]# telnet 172.16.100.65 80
Trying 172.16.100.65...
Connected to 172.16.100.65.
Escape character is '^]'.
GET /test.html HTTP/1.1  
HOST:172.16.100.65

HTTP/1.1 200 OK
Date: Tue, 30 Oct 2018 23:03:09 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Tue, 30 Oct 2018 21:54:05 GMT
ETag: "4c0640-194-579793975ed24"
Accept-Ranges: bytes
Content-Length: 404
Connection: close  #连接建立后立即端开
Content-Type: text/html; charset=UTF-8

           <html>
                        <head>
                                <title>百度</title>
                        </head>
                        <body>
                                <h1></h1>
                                        <p> baidu... <a href="http://www.baidu.com"> bla... </a> </p>
                                <h2> </h2>
                        </body>
           </html>
Connection closed by foreign host.
备注:GET /test.html HTTP/1.0 回车一次 HOST:172.16.100.65回车2次

KeepAlive On测试持久链接
~]# service httpd restart 

测试效果,15s后端口连接
~]# telnet 172.16.100.65 80
Trying 172.16.100.65...
Connected to 172.16.100.65.
Escape character is '^]'.
GET /test.html HTTP/1.1
HOST:172.16.100.65

HTTP/1.1 200 OK
Date: Tue, 30 Oct 2018 23:06:29 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Tue, 30 Oct 2018 21:54:05 GMT
ETag: "4c0640-194-579793975ed24"
Accept-Ranges: bytes
Content-Length: 404
Content-Type: text/html; charset=UTF-8

           <html>
                        <head>
                                <title>百度</title>
                        </head>
                        <body>
                                <h1></h1>
                                        <p> baidu... <a href="http://www.baidu.com"> bla... </a> </p>
                                <h2> </h2>
                        </body>
           </html>
Connection closed by foreign host.

默认MPM模式为prefork,变更为worker模式
~]# vim /etc/sysconfig/httpd 
~]# service httpd restart 
停止 httpd:                                               [确定]
正在启动 httpd:httpd.worker: apr_sockaddr_info_get() failed for Centos6.5
httpd.worker: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

查看worker模式进程数
~]# ps aux | grep httpd
root       2959  0.0  0.1 186608  4168 ?        Ss   08:16   0:00 /usr/sbin/httpd.worker
apache     2962  1.6  0.2 530868  9472 ?        Sl   08:16   0:00 /usr/sbin/httpd.worker
apache     2963  4.0  0.2 530868  9468 ?        Sl   08:16   0:00 /usr/sbin/httpd.worker
apache     2965  1.0  0.2 530868  9484 ?        Sl   08:16   0:00 /usr/sbin/httpd.worker
root       3075  0.0  0.0 103256   840 pts/1    R+   08:16   0:00 grep httpd

监控进程
~]# watch -n.5 'ps aux | grep httpd'
重启看监控效果
~]# service httpd restart

创建web站点目录(创建URL根路径)
~]# mkdir -pv /web/host1
mkdir: 已创建目录 "/web"
mkdir: 已创建目录 "/web/host1"
[root@Centos6 ~]# vim /web/host1/index.html
           <html>
                        <head>
                                <title>host1</title>
                        </head>
                        <body>
                                <h1></h1>
                                        <p> host1 test.index... <a href="http://www.baidu.com"> bla... </a> </p>
                                <h2> </h2>
                        </body>
           </html>

 ~]# vim /etc/httpd/conf/httpd.conf
DocumentRoot "/web/host1"

http://172.16.100.65

                        2、持久连接
                                Persistent Connection:tcp连续建立后,每个资源获取完成后不全
断开连接,而是继续等待其它资源请求的进行; 
                                如何断开?
                                        数量限制
                                        时间限制

                        副作用:对并发访问量较大的服务器,长连接机制会使得后续某些请求无法得到正常 响应;
                        折衷:使用较短的持久连接时长,以及较少的请求数量;

                                        KeepAlive  On|Off
                                        KeepAliveTimeout  15
                                        MaxKeepAliveRequests  100

                                测试:
                                        telnet  WEB_SERVER_IP  PORT
                                        GET  /URL  HTTP/1.1
                                        Host: WEB_SERVER_IP

                        3、MPM 

                        httpd-2.2不支持同时编译多个MPM模块,所以只能编译选定要使用的那个;CentOS 6的rpm包为此专门提供了三个应用程序文件,httpd(prefork), httpd.worker, httpd.event,分别用于实现对不同的MPM机制的支持;确认现在使用的是哪下程序文件的方法:
                                        ps  aux  | grep httpd

                                默认使用的为/usr/sbin/httpd,其为prefork的MPM模块 ;
                                        查看httpd程序的模块列表:
                                                查看静态编译的模块:
                                                        # httpd  -l
                                                查看静态编译及动态编译的模块:
                                                        # httpd  -M

                                更换使用httpd程序,以支持其它MPM机制;
                                        /etc/sysconfig/httpd
                                                HTTPD=/usr/sbin/httpd.{worker,event}

                                注意:重启服务进程方可生效

                                MPM配置:
                                        prefork的配置           
                                                <IfModule prefork.c>
                                                StartServers       8
                                                MinSpareServers    5
                                                MaxSpareServers   20
                                                ServerLimit      256
                                                MaxClients       256
                                                MaxRequestsPerChild  4000
                                                </IfModule>             

                                        worker的配置:
                                                <IfModule worker.c>
                                                StartServers         4
                                                MaxClients         300
    MinSpareThreads     25
                                                MaxSpareThreads     75
                                                ThreadsPerChild     25
                                                MaxRequestsPerChild  0
                                                </IfModule>                             

                                PV,UV
                                        PV:Page View
                                        UV: User View

                        4、DSO
                                配置指定实现模块加载
                                        LoadModule  <mod_name>  <mod_path>

                                        模块文件路径可使用相对路径:
                                                相对于ServerRoot(默认/etc/httpd)

                        5、定义'Main' server的文档页面路径
                                DocumentRoot  ""

                                文档路径映射:
                                        DoucmentRoot指向的路径为URL路径的起始位置
                                                其相当于站点URL的根路径;

                       (FileSystem) /web/host1/index.html  -->  (URL)  /index.html

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

                                可基于两种机制指明对哪些资源进行何种访问控制

        <Directory  "">
                                                ...
                                                </Directory>

                                                <File  "">
                                                ...
                                                </File>

                                                <FileMatch  "PATTERN">
                                                ...
                                                </FileMatch>
                                        URL路径:
                                                <Location  "">
                                                ...
                                                </Location>

                                                <LocationMatch "">
                                                ...
                                                </LocationMatch>

                                <Directory>中“基于源地址”实现访问控制:
                                        (1) Options
                                                后跟1个或多个以空白字符分隔的“选项”列表;
                                            Indexes:指明的URL路径下不存在与定义的主页面资源相符的资源文件时,返回索引列表给用户;
                                            FollowSymLinks:允许跟踪符号链接文件所指向的源文件;
                                            None:
                                            All:

                                        (2)  AllowOverride与访问控制相关的哪些指令可以放在.htaccess文件(每个目录下都可以有一个)中;
                                             All: 
                                             None:

                                        (3) order和allow、deny
                                            order:定义生效次序;写在后面的表示默认法则;

                                            Allow from, Deny from
                                                        来源地址:
                                                                IP
                                                                NetAddr:
                                                                        172.16
                                                                        172.16.0.0
                                                                        172.16.0.0/16
                                                                        172.16.0.0/255.255.0.0

                        7、定义站点主页面:
                                DirectoryIndex  index.html  index.html.var

                        8、定义路径别名
                                格式:
                                        Alias  /URL/  "/PATH/TO/SOMEDIR/"

                                DocumentRoot "/www/htdocs"
                                        http://www.iecentury.com/download/bash-4.4.2-3.el6.x86_64.rpm 
                                                /www/htdocs/download/bash-4.4.2-3.el6.x86_64.rpm 

                                Alias  /download/  "/rpms/pub/"
  http://www.iecentury.com/download/bash-4.4.2-3.el6.x86_64.rpm 
                                                /rpms/pub/bash-4.4.2-3.el6.x86_64.rpm

                                        http://www.iecentury.com/images/logo.png
                                                /www/htdocs/images/logo.png

                        9、设定默认字符集
                                AddDefaultCharset  UTF-8

                                中文字符集:GBK, GB2312, GB18030

                        10、日志设定
                                日志类型:访问日志 和 错误日志

                                错误日志:
                                        ErrorLog  logs/error_log
                                        LogLevel  warn
        Possible values include: debug, info, notice, warn, error, crit, alert, emerg.

                                访问日志:
                                        LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
                                        CustomLog  logs/access_log  combined

                                        LogFormat format strings:
                                                http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats

                              %h:客户端IP地址;
                              %l:Remote User, 通常为一个减号(“-”);
                              %u:Remote user (from auth; may be bogus if return status (%s) is 401);非为登录访问时,其为一个减号;
                              %t:服务器收到请求时的时间;
                              %r:First line of request,即表示请求报文的首行;记录了此次请求的“方法”,“URL”以及协议版本;
                              %>s:响应状态码;
                              %b:响应报文的大小,单位是字节;不包括响应报文的http首部;
                              %{Referer}i:请求报文中首部“referer”的值;即从哪个页面中的超链接跳转至当前页面的;
                              %{User-Agent}i:请求报文中首部“User-Agent”的值;即发出请求的应用程序;

                        11、基于用户的访问控制

                                认证质询:
                                        WWW-Authenticate:响应码为401,拒绝客户端请求,并说明要求客户端提供账号和密码;

                                认证:
                                        Authorization:客户端用户填入账号和密码后再次发送请求报文;认证通过时,则服务器发送响应的资源;

                                        认证方式有两种:
                                                basic:明文 
                                                digest:消息摘要认证

                                安全域:需要用户认证后方能访问的路径;应该通过名称对其进行标
识,以便于告知用户认证的原因;

                                用户的账号和密码存放于何处?
 虚拟账号:仅用于访问某服务时用到的认证标识

                                        存储:
                                                文本文件;
                                                SQL数据库;
                                                ldap目录存储;

                                basic认证配置示例:
                                        (1) 定义安全域
                                                <Directory "">
                                                        Options None
                                                        AllowOverride None
                                                        AuthType Basic
                                                        AuthName "String“
                                                        AuthUserFile  "/PATH/TO/HTTPD_USER_PASSWD_FILE"
                                                        Require  user  username1  username2 ...
                                                </Directory>

                                                允许账号文件中的所有用户登录访问:
                                                        Require  valid-user

                                        (2) 提供账号和密码存储(文本文件)
                                                使用专用命令完成此类文件的创建及用户管理
                                                        htpasswd  [options]   /PATH/TO/HTTPD_PASSWD_FILE  username 
                                                                -c:自动创建此处指定的文件,
因此,仅应该在此文件不存在时使用;
                                                                -m:md5格式加密
                                                                -s: sha格式加密
                                                                -D:删除指定用户

                                        另外:基于组账号进行认证;
                                                (1) 定义安全域
                                                        <Directory "">
                                                                Options None
                                                                AllowOverride None
                                                                AuthType Basic
                                                                AuthName "String“
                                                                AuthUserFile  "/PATH/TO/HTTPD_USER_PASSWD_FILE"
                                                                AuthGroupFile "/PATH/TO/HTTPD_GROUP_FILE"
                                                                Require  group  grpname1  grpname2 ...
                                                        </Directory>

                                                (2) 创建用户账号和组账号文件;

                                                        组文件:每一行定义一个组
                                                                GRP_NAME: username1  username2  ...

                        12、虚拟主机

                                站点标识: socket
                                        IP相同,但端口不同;
                                        IP不同,但端口均为默认端口;
                                        FQDN不同;
                                                请求报文中首部
                                                Host: www.iecentury.com 

                                有三种实现方案:
基于ip:
                                                为每个虚拟主机准备至少一个ip地址;
                                        基于port:
                                                为每个虚拟主机使用至少一个独立的port;
                                        基于FQDN:
                                                为每个虚拟主机使用至少一个FQDN;

                                注意:一般虚拟机不要与中心主机混用;因此,要使用虚拟主机,得
先禁用'main'主机;
                                        禁用方法:注释中心主机的DocumentRoot指令即可;

                                虚拟主机的配置方法:
                                        <VirtualHost  IP:PORT>
                                                ServerName FQDN
                                                DocumentRoot  ""
                                        </VirtualHost>

                                        其它可用指令:
                                                ServerAlias:虚拟主机的别名;可多次使用;
                                                ErrorLog:
                                                CustomLog:
                                                <Directory "">
                                                ...
                                                </Directory>
                                                Alias
                                                ...

                                        基于IP的虚拟主机示例:
                                        <VirtualHost 172.16.100.6:80>
                                                ServerName www.a.com
                                                DocumentRoot "/www/a.com/htdocs"
                                        </VirtualHost>

                                        <VirtualHost 172.16.100.7:80>
                                                ServerName www.b.net
                                                DocumentRoot "/www/b.net/htdocs"
                                        </VirtualHost>

                                        <VirtualHost 172.16.100.8:80>
                                                ServerName www.c.org
                                                DocumentRoot "/www/c.org/htdocs"
                                        </VirtualHost>

                                        基于端口的虚拟主机:
                                        <VirtualHost 172.16.100.6:80>
                                                ServerName www.a.com
                                                DocumentRoot "/www/a.com/htdocs"
                                        </VirtualHost>

                                        <VirtualHost 172.16.100.6:808>
                                                ServerName www.b.net
                                                DocumentRoot "/www/b.net/htdocs"
                                        </VirtualHost>

                                        <VirtualHost 172.16.100.6:8080>
                                                ServerName www.c.org
                                                DocumentRoot "/www/c.org/htdocs"
                                        </VirtualHost>

                                        基于FQDN的虚拟主机:
                                        NameVirtualHost 172.16.100.6:80

                                        <VirtualHost 172.16.100.6:80>
                                                ServerName www.a.com
                                                DocumentRoot "/www/a.com/htdocs"
                                        </VirtualHost>

                                        <VirtualHost 172.16.100.6:80>
                                                ServerName www.b.net
                                                DocumentRoot "/www/b.net/htdocs"
                                        </VirtualHost>

                                        <VirtualHost 172.16.100.6:80>
                                                ServerName www.c.org
                                                DocumentRoot "/www/c.org/htdocs"
                                        </VirtualHost>                                  

                        13、status页面
                                LoadModule  status_module  modules/mod_status.so

                                <Location /server-status>
                                        SetHandler server-status
                                        Order allow,deny
                                        Allow from 172.16
                                </Location>

以上所述就是小编给大家介绍的《centos6 httpd2.2详细剖析》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art and Science of Java

The Art and Science of Java

Eric Roberts / Addison-Wesley / 2007-3-1 / USD 121.60

In The Art and Science of Java, Stanford professor and well-known leader in CS Education Eric Roberts emphasizes the student-friendly exposition that led to the success of The Art and Science of C. By......一起来看看 《The Art and Science of Java》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具