HTTP基础及基本配置

1、配置监听的地址和端口;

    Listen [IP:]PORT

            例如:Listen 80 监听所有80端口

2、配置所选用的MPM的属性

配置使用编译进不同MPMhttpd,编辑/etc/sysconfig/httpd配置文件,定义如下行:

    HTTPD=/usr/sbin/httpd.worker


MPM:多道处理模块

prefork: 一个进程响应一个请求;

  主进程功能

     (1) 绑定特权端口;

     (2) 派发或回收子进程;

     (3) 读取分析主配置文件;

worker:一个进程生成多个线程,一个线程响应一个请求;

        默认使用的是prefork模型、如果要使用worker模型,要在配置文件里启用

eventhttpd-2.2 版本上只是测试的

       httpd命令选项:

             httpd -h:列出所有选项

             httpd -l:显示核心模块

httpd -D:显示所有已装载的模块  

httpd -t:测试配置文件

     # vim /etc/httpd/conf/httpd.conf打开配置文件:

     <IfModule prefork.c>条件性指令,判断这个模块是否存在,存在下面的配置就生效

     StartServers       8    刚启动web服务的时候,启动几个空闲进程

     MinSpareServers    5   最小空闲进程数

     MaxSpareServers   20   最大空闲进程数

     ServerLimit      256    最多允许并发的进程活动个数

     MaxClients       256   服务器允许连进来的客户端数目

     MaxRequestsPerChild  4000 一个进程最多可以处理多少个请求

     </IfModule>

3、配置服务器支持keep-alived是否支持常连接

      KeepAlive {On|Off}

         对于比较空闲的服务器,建议KeepAlive on;对于非常忙的服务器,建议KeepAlive off

      KeepAliveTimeout 2  最多允许使用2

      MaxKeepAliveRequests 50  一次连接之内,最多允许50个资源请求

4、配置加载的模块

      LoadModule foo_module modules/mod_foo.so

5、配置站点根目录

      DocumentRoot ""     定义网页存放目录

     <Directory "FS_PATH">

     </Directory>

     <Location "URL">

     </Location>

例如/var/www/html/p_w_picpaths/logo.jpg根目录/var/www/html,那么我们的访问路径就是:http://www.magedu.com/p_w_picpaths/logo.jpg

6、配置页面文件访问属性

     <Directory "FS_PATH">

     Options

     Indexes: 是否允许索引页面文件,建议关闭;除非提供专门下载可以打开

     FollowSynLinks: 是否跟随软链接文件;不安全。

     SymLinksifOwnerMatch:是属主的话允许链接

     ExecCGI:是否允许执行CGI脚本;

     All

     None

     </Directory>

7、访问控制

基于客户端访问控制:(基于ip地址访问控制

Order:定义allowdeny哪个为默认法则;写在后面的为默认法则:写在前面的指令没有显式定义的即受后面的指令控制;

    (1) Order allow,deny

          Allow from 172.16.0.0/16   仅允许172.16.0.0/16 网段内客户访问

    (2) Order allow,deny

          Deny from 172.16.4.12 拒绝所有用户访问

    (3) Order allow,deny

          Deny from 172.16.4.12

          Allow from 172.16.0.0/16

         地址范围匹配越小的是最佳匹配,172.16.4.12 最小,所以为拒绝172.16.4.12 网段内客户访问

基于用户访问控制

8、userdir

   让每个用户都拥有个人站点:http://HOST/~username/

   需要启用UserDir public_html

        需要设定用户对家目录文件的访问控制权限

9、定义默认主页面:

         DirectoryIndexindex.php index.jsp index.html

         默认先找最左侧的,也就是index.php

10、配置日志功能       默认路径在/var/log/httpd/

日志有两类:访问日志(格式需自定义)、错误日志

   错误日志:

       ErrorLog "/path/to/error_log_file"

   访问日志:

       CustomLog /PATH/TO/CUSTEOM_LOG_FILE LOGFORMAT, 例如:

       CustomLog logs/access_log combinedcombined

   LogFormat 定义格式

      安装httpd-manual包,service httpd reload重新载入服务,在浏览器中键入172.16.4.1/manual/就可以在本地访问了:

     Log Files ==> format strings 就可以查看定义的日志格式了

11、设定默认字符集

      AddDefaultCharset UTF-8

12、路径别名

      DocumentRoot "/web/htdocs"

      http://www.magedu.com/p_w_picpaths/logo.gif  ==> /web/htdocs/p_w_picpaths/logo.gif

      定义别名为/www/static/

      访问路径就是/www/static/logo.gif

   首先:定义Alias /p_w_picpaths/ "/www/static/"

   然后:创建目录/www/static/  在目录下创建文件index.html    在文件里面编辑内容    

   最后:重新载入service httpd reload

   我们重新访问172.16.0.1/p_w_picpaths时,看到的内容会变成所定义别名的路径下的内容。

13、脚本路径别名:

CGI:协议

    ScriptAlias /PATH/ /PATH/TO/SOMFEDIR/

    例如:ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"或者ScriptAlias /cgi-bin"/var/www/cgi-bin"

    注意/cgi-bin/var/www/cgi-bin后面要保持一致

    操作:

        #cd /etc/httpd/conf

        #mkdir -pv /website/cgi-bin/

        #vim /website/cgi-bin/test.sh

CGI测试脚本

#!/bin/bash

#

cat << EOF

Content-Type: text/html

<pre>

The hostname is: `/bin/hostname`.

The time is: `date`.

</pre>

EOF

给脚本执行权限,脚本执行结果为:

Content-Type: text/html

<pre>

The hostname is: station93.magelinux.com.

The time is: Sat Aug 17 01:11:13 CST 2013.

</pre>

# end of cgi script file

用浏览器访问172.16.4.1/cgi-bin/test.sh 可以访问到

14、基于用户访问控制

      DocumentRoot "/var/www/html"

      <Directory "/PATH/TO/DocumentRoot_SUBDIR">

      Options None

      AllowOverride AuthConfig   是否允许覆盖

      AuthName "Realm"          认证名称

      AuthType Basic              认证类型

      AuthUserFile /path/to/passwords 用户账户文件叫什么,

      Require jerry tom             哪些用户可以登陆

      </Directory>

(1) 建立用户帐号文件

     htpasswd -c -m /path/to/password_file USERNAME

     第一次创建文件添加用户的时候用-c选项,以后就不能用-c选项了,否则会将原来的文件内容覆盖的。

(2) 一个配置示例

<Directory "/website/htdocs/downloads">

   Options Indexes

   AllowOverride AuthConfig

   AuthName "Only for employees."

   AuthType Basic

   AuthUserFile /etc/httpd/conf/.htpass

   Require valid-user        允许所有用户登陆

</Directory>

重新载入后,当用户访问172.16.4.1/website/htdocs/downloads时,会提示用户输入密码。

(3)组

   组文件:

   组名:用户1 用户2 用户3

   AuthGroupFile

   Require GRP_NAME

15、虚拟主机 :一个web程序服务于多个站点

虚拟主机类型:

(1)基于端口的虚拟主机:

(2)基于IP的虚拟主机:

(3)基于主机名的虚拟主机:

请求报文首部有一个Host:,保留了主机名称,所有web服务器收到请求后,能够看到Host所对应的是哪个主机,这样基于主机名的虚拟主机才能实现。

虚拟主机和主服务器不能同时使用:关闭主服务器,注释主服务器的DocumentRoot即可;

每个虚拟主机的定义:

   <VirtualHost IP:PORT>

        ServerName

        DocumentRoot ""

   </VirutalHost>

(1) 基于端口的虚拟主机

首先确保监听在所要求的端口,然后关闭主服务器的DocumentRoot,在配置文件 /etc/httpd/conf/httpd.conf中定义:

   <VirtualHost *:80>  

         ServerName www.a.com  可以随便定义,因为它不区别主机名称

         DocumentRoot "/web/host1/"

   </VirutalHost>

   <VirtualHost *:8080>

         ServerName www.b.org  

         DocumentRoot "/web/host2/"

   </VirutalHost>

   配置文件编辑完成。

目录不存在,所以先要创建mkdir -pv /web/host{1,2}

    然后编辑网页内容进行测试#vim /web/host1/index.html

                             #vim /web/host2/index.html

重启服务。这样基于端口就可以访问网页了。

(2)基于IP的虚拟主机

例如:

   <VirtualHost 172.16.4.180>

          ServerName www.a.com  

          DocumentRoot "/web/host1/"

   </VirutalHost>

   <VirtualHost 172.16.4.180>

          ServerName www.b.org  

          DocumentRoot "/web/host2/"

   </VirutalHost>

编辑网页内容,重启服务。这样基于ip虚拟主机就可以访问网页了。

(3)基于主机名的虚拟主机

httpd-2.2: NameVirtualHost

虚拟主机单独配置:

     CustomLog

     ErrorLog

     <Directory "">

     </Directory>

     ScriptAlias /cgi-bin/

     Alias

#NameVirtualHost *:80修改为NameVirtualHost 172.16.4.1:80

     <VirtualHost 172.16.4.180>

            ServerName www.a.com  

            DocumentRoot "/web/host1/"

     </VirutalHost>

     <VirtualHost 172.16.4.180>

            ServerName www.b.org  

            DocumentRoot "/web/host2/"

     </VirutalHost>

然后service httpd restart

用本机测试先要编辑/etc/hosts

172.16.4.1www.a.com  a.com

172.16.4.1 www.b.org   b.org

安装elinks包,测试

#elinks http://www.a.com进入交互式模式测试

#elinks http://www.b.org

#elinks -dump http://www.a.com可以不用进入交互式模式测试

#elinks -dump http://www.b.org