httpd配置(2) 

    12、设定默认字符集  ASCII     

        字符集:GB2312, GB18030, GBK, UTF       

        AddDefaultCharset 

    13、CGI脚本

        CGI脚本路径别名

        

        /var/www/cgi-bin/

        http://server/cgi-bin/bash写CGI脚本:所有文本都使用命令输出:echo, printf, cat 执行程序:命令引用 

        

        Content-Type: text/html

        <pre>

        </pre>

        FastCGI: 协议


    14、基于用户访问控制 :基本认证: Basic 、摘要认证:digest


        虚拟用户:仅用于访问某服务或获取某资源的凭证;

        密码验证:文本文件:.htpasswd 、 SQL数据库、 dbm: 数据库引擎,提供API、 ldap: 

        authentication provider: 账号和密码的存储机制;

        authorization provider: 授权


    案例:基于文件做访问控制


    (1) 基于用户进行认证 

        <Directory "/var/www/html/admin">

            Options none

            AllowOverride AuthConfig

            AuthType Basic

            AuthName "Admin Area."

            #AuthBasicProvider file

            AuthUserFile /etc/httpd/conf/.htpasswd

            Require valid-user

        </Directory>

        Require valid-user: 文件中所有用户均可访问

        Require user USERNAME, ...


    (2) 提供认证文件

        htpasswd

        -c: 如果此文件事先不存在,则创建;注意,只能在创建第一个用户时使用;

        -m:以md5的格式编码存储用户的密码信息

        -D:删除指定用户


    (3) 组认证

        <Directory "/var/www/html/admin">

            Options none

            AllowOverride AuthConfig

            AuthType Basic

            AuthName "Admin Area."

            #AuthBasicProvider file

            AuthUserFile /etc/httpd/conf/.htpasswd

            AuthGroupFile /etc/httpd/conf/.htgroup

            Require group GROUP_NAME

        </Directory>


        组文件: 如:

        组名:user1 user2 user3


    15、虚拟主机(使用不同访问路径):基于端口、基于IP、基于主机名

        (1) 使用虚拟的前提:取消主服务器、注释主服务器的站点根路径指定:DocumentRoot

        (2) 如何定义虚拟主机

            注意:apache 2.2时需要启用  NameVirtualHost IP:PORT

                  apache 2.4时不需要

     一个站点独有的资源信息应该有哪些

        1:不同的目录,DocumentRoot

        2:两个网站如何实现一个认证,一个不认证呢,需要验证的用<Directory>封装起来。

    例如:1基于不同IP地址创建虚拟主机

            #DocumentRoot   首先注释DocumentRoot

            <VirtualHost IP:PORT>

            </VirtualHost>

            或者在/conf.d/virtual.conf  编辑virtual.conf文件       

                <VirtualHost 192.168.100.1:80>

                    ServerName hello.mageedu.com

                    DocumentRoot “/www/mageedu.com”                 

                </VirtualHost>

                

                <VirtualHost 192.168.100.2:80>

                    ServerName www.a.org

                    DocumentRoot “/www/a.org”                 

                </VirtualHost>

        #httpd -t 检测,提示目录不存在,创建目录#mkdir /www/{mageedu.com,a.org},分别在mageedu.com,a.org目录下创建测试网页。#serveice httpd restart #ifconfig eth0:1 192.168.100.1,测试OK。

             2、基于同一地址不同端口创建虚拟主机

            编辑/etc/httpd/conf.d/virtual.conf,添加

                <VirtualHost 192.168.100.1:8080>

                    ServerName www.b.net

                    DocumentRoot “/www/b.net”                 

                </VirtualHost>

         #创建b.net目录添加测试网页,在主配置文件/etc/httpd/conf/httpd.conf,添加listen 8080测试,访问OK。

             3、基于主机名创建虚拟机               

            编辑/etc/httpd/conf.d/virtual.conf,启用NameVirtualHost 并添加如下:

                NameVirtualHost 192.168.100.2:80

                <VirtualHost 192.168.100.2:80>

                    ServerName www.d.gov

                    DocumentRoot “/www/d.gov”                 

                </VirtualHost>

        #创建d.gov目录添加测试网页,修改hosts ##192.168.100.2    www.a.org,

                                               ##192.168.100.2    www.d.gov用域名访问OK,如果此时用IP地址访问,返回的是第一个解析结果。

             4、自定义日志路劲

         日志文件默认在/etc/httpd/log,如果要自定义日志文件路径:如下    

           <VirtualHost 192.168.100.1:80>

              ServerName hello.mageedu.com

              DocumentRoot “/www/mageedu.com”

              customlog  /var/log/httpd/mageedu.com/access_log combined #combined定义日志格式          

            </VirtualHost>

        #创建定义访问日志路劲,重启服务器测试,创建错误日志也是一样。

            5、访问权限限制,如果禁止192.168.100.10访问d.gov           

            <VirtualHost 192.168.100.2:80>

                    ServerName www.d.gov

                    DocumentRoot “/www/d.gov” 

                    <Directory "/www/d.gov">

                        Options none

                        Allowoverride none

                        Order deny,allow

                        Denyfrom 192.168.100.10

                    </Directory>                

             </VirtualHost>   ##重启服务器,测试ok .

                6.身份验证,需要密码访问

            

            <VirtualHost 192.168.100.2:80>

                    ServerName www.a.org

                    DocumentRoot “/www/a.org”

                    CustomLog /var/log/httpd/a.org/access_log combined 

                    <Directory "/www/a.org">

                        Options none

                        Allowoverride authconfig

                        AuthType basic

                        AuthName "Restrict area."

                        AuthUserFile "/etc/httpd/.htpasswd"

                        Required valid-user

                    </Directory>                

             </VirtualHost>

        

            wKiom1dX3pzjhW8BAAB9b2qRFgc968.png-wh_50

            创建验证文件

 #htpasswd -c -m /etc/httpd/.htpasswd tom  回车输入密码,-c:第一次创建用户使用,-m:md5加密



        虚拟主机格式:

        <VirtualHost IP:PORT>

            ServerName 

            DocumentRoot 

            ServerAlias

            ErrorLog

            CustomLog

        </VirtualHost>


        配置文件语法检查: httpd -t , service httpd configtest

        

        配置示例:

        <VirtualHost 172.16.100.7:80>

            ServerName www.mageedu.com

            DocumentRoot "/web/hosta"

        </VirtualHost>


        <VirtualHost 172.16.100.8:80>

            ServerName www.mageedu.com

            DocumentRoot "/web/hostb"

        </VirtualHost>

        

        <VirtualHost 172.16.100.8:8080>

            ServerName www.mageedu.com

            DocumentRoot "/web/hostc"

        </VirtualHost>

              

        测试:elinks   -dump: 获取到页面数据后直接退出进程;

        

    16、https   ssl(安全的套接字层), tls(传输层安全)  http协议:文本编码

        

      验正:使用telnet发请求     

        # telnet 172.16.100.7 80

        Trying 172.16.100.7...

        Connected to 172.16.100.7.

        Escape character is '^]'.

        GET /index.html HTTP/1.0

        Host: www.b.org 

        HTTP/1.1 200 OK

        Date: Fri, 08 Aug 2014 03:03:51 GMT

        Server: Apache/2.2.15 (CentOS)

        Last-Modified: Fri, 08 Aug 2014 02:14:52 GMT

        ETag: "e0009-12-50014c53e753f"

        Accept-Ranges: bytes

        Content-Length: 18

        Connection: close

        Content-Type: text/html; charset=UTF-8  

        <h1> Host B </h1>

        Connection closed by foreign host.

        

        

        httpd的ssl方式有2中:1、 ssl模块,2、单独成包     

        ssl会话基于IP地址创建,所以,每一个IP仅创建一个SSL会话;

        

        SSL握手要完成的工作:

            交换协议版本号、

            选择双方都支持的加密方式、

            客户端对服务器端实、

            身份验正、密钥交换

        

        客户端验正服务器端证书:

            有效性检测:证书是否仍然在有效期内

            CA的可信度检测:

            证书的完整性检测:

            持有者的身份检测

        

        配置httpd工作于https:

        (1) 安装mod_ssl模块

        # yum install mod_ssl

        

        (2) 为服务端生成私钥,并为其提供证书;

        # mkdir /etc/httpd/ssl && cd /etc/httpd/ssl

        # (umask 077; openssl genrsa -out httpd.key 1024)

        # openssl req -new -key httpd.key -out httpd.csr

        

        签署后的证书为:/etc/httpd/ssl/httpd.crt

        

        (3) 配置使用https的虚拟主机;

        

        SSLCertificateFile

        SSLCertificateKeyFile


        <VirtualHost IP:443>

        DocumentRoot

        ServerName

        </VirtualHost>

        

        (4) 重新装载配置

        

        (5) 测试

        # openssl s_client -connect IP:PORT -CAfile /path/to/ca_certificate

        

        17、status页面

        httpd内嵌有handler,其中有一个handler用于输出当前httpd服务相关状态信息      

        handler: server-status       

        启用handler要使用SetHandler指令      

        handler: 当文件被调用时,apache内部表示形式;一般每种文件类型都有其隐式处理器

        

    18、访问属性配置总结

        配置文件系统访问路径:

        <Directory [~] "">

        </Directory>

        

        <File [~] "">

        </File>

        

        配置URL访问路径:

        <Location [~] "">

        </Location>

        

        <LocationMatch "">

        </LocationMatch>

              

        /var/www/html/

        p_w_picpaths/

 


    19、curl命令


    curl是基于URL语法在命令行方式下工作的文件传输工具,它支持FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE及LDAP等协议。curl支持HTTPS认证,并且支持HTTP的POST、PUT等方法, FTP上传, kerberos认证,HTTP上传,代理服务器, cookies, 用户名/密码认证, 下载文件断点续传,上载文件断点续传,,http代理服务器管道( proxy tunneling), 甚至它还支持IPv6, socks5代理服务器,,通过http代理服务器上传文件到FTP服务器等等,功能十分强大。

    

    curl的常用选项:

        -A/--user-agent <string> 设置用户代理发送给服务器

        -basic 使用HTTP基本认证

        --tcp-nodelay 使用TCP_NODELAY选项

        -e/--referer <URL> 来源网址

        --cacert <file> CA证书 (SSL)

        --compressed 要求返回是压缩的格式

        -H/--header <line>自定义头信息传递给服务器

        -I/--head 只显示响应报文首部信息

        --limit-rate <rate> 设置传输速度

        -u/--user <user[:password]>设置服务器的用户和密码

        -0/--http1.0 使用HTTP 1.0


    20、使用mod_deflate模块压缩页面优化传输速度


        SetOutputFilter DEFLATE

        # mod_deflate configuration

        # Restrict compression to these MIME types

        AddOutputFilterByType DEFLATE text/plain 

        AddOutputFilterByType DEFLATE text/html

        AddOutputFilterByType DEFLATE application/xhtml+xml

        AddOutputFilterByType DEFLATE text/xml

        AddOutputFilterByType DEFLATE application/xml

        AddOutputFilterByType DEFLATE application/x-javascript

        AddOutputFilterByType DEFLATE text/javascript

        AddOutputFilterByType DEFLATE text/css

 

        # Level of compression (Highest 9 - Lowest 1)

        DeflateCompressionLevel 9

         

        # Netscape 4.x has some problems.

        BrowserMatch ^Mozilla/4 gzip-only-text/html

         

        # Netscape 4.06-4.08 have some more problems

        BrowserMatch ^Mozilla/4\.0[678] no-gzip

         

        # MSIE masquerades as Netscape, but it is fine

        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

    

    练习:


    1、建立httpd服务器(基于编译的方式进行),要求:

       提供两个基于名称的虚拟主机:

    (a)www1.stuX.com,页面文件目录为/web/vhosts/www1;错误日志为/var/log/httpd/www1.err,访问日志为/var/log/httpd/www1.access;

    (b)www2.stuX.com,页面文件目录为/web/vhosts/www2;错误日志为/var/log/httpd/www2.err,访问日志为/var/log/httpd/www2.access;

    (c)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名;

    (d)通过www1.stuX.com/server-status输出httpd工作状态相关信息,且只允许提供帐号密码才能访问(status:status);

    

    2、为上面的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点;

    (1)要求使用证书认证,证书中要求使用的国家(CN)、州(Henan)、城市(Zhengzhou)和组织(MageEdu);

    (2)设置部门为tech,主机名为www2.stuX.com,邮件为admin@stuX.com;