Apache 简介及基本配置

类别:Java 点击:0 评论:0 推荐:
一. Apache简介: 
Apache是世界排名第一的Web服务器, 根据Netcraft(www.netsraft.co.uk)所作的调查,世界上百分之五十以上的Web服务器在使用Apache. 
1995年4月, 最早的Apache(0.6.2版)由Apache Group公布发行. Apache Group 是一个完全通过Internet进行运作的非盈利机构, 由它来决定Apache Web服务器的标准发行版中应该包含哪些内容. 准许任何人修改隐错, 提供新的特征和将它移植到新的平台上, 以及其它的工作. 当新的代码被提交给Apache Group时, 该团体审核它的具体内容, 进行测试, 如果认为满意, 该代码就会被集成到Apache的主要发行版中. 
Apache 的特性: 
1) 几乎可以运行在所有的计算机平台上. 
2) 支持最新的HTTP/1.1协议 
3) 简单而且强有力的基于文件的配置(HTTPD.CONF). 
4) 支持通用网关接口(CGI) 
5) 支持虚拟主机. 
6) 支持HTTP认证. 
7) 集成PERL. 
8) 集成的代理服务器 
9) 可以通过WEB浏览器监视服务器的状态, 可以自定义日志. 
10) 支持服务器端包含命令(SSI). 
11) 支持安全SOCKET层(SSL). 
12) 具有用户会话过程的跟踪能力. 
13) 支持FASTCGI 
14) 支持JAVA SERVLETS. 
... 
"缺点"(MAYBE IT IS, MAYBE NOT): 
APACHE没有为管理员提供图形用户接口(GUI), 但最近的APACHE版本已经有了GUI的支持. 

以下是APACHE的许可证 
/* ==================================================================== 
* The Apache Software License, Version 1.1 

* Copyright (c) 2000 The Apache Software Foundation. All rights 
* reserved. 

* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions 
* are met: 

* 1. Redistributions of source code must retain the above copyright 
* notice, this list of conditions and the following disclaimer. 

* 2. Redistributions in binary form must reproduce the above copyright 
* notice, this list of conditions and the following disclaimer in 
* the documentation and/or other materials provided with the 
* distribution. 

* 3. The end-user documentation included with the redistribution, 
* if any, must include the following acknowledgment: 
* "This product includes software developed by the 
* Apache Software Foundation (http://www.apache.org/)." 
* Alternately, this acknowledgment may appear in the software itself, 
* if and wherever such third-party acknowledgments normally appear. 

* 4. The names "Apache" and "Apache Software Foundation" must 
* not be used to endorse or promote products derived from this 
* software without prior written permission. For written 
* permission, please contact [email protected]

* 5. Products derived from this software may not be called "Apache", 
* nor may "Apache" appear in their name, without prior written 
* permission of the Apache Software Foundation. 

* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
* SUCH DAMAGE. 
* ==================================================================== 

* This software consists of voluntary contributions made by many 
* individuals on behalf of the Apache Software Foundation. For more 
* information on the Apache Software Foundation, please see 
* <http://www.apache.org/>. 

* Portions of this software are based upon public domain software 
* originally written at the National Center for Supercomputing Applications, 
* University of Illinois, Urbana-Champaign. 
*/ 


可以看出, APACHE不要求任何人把对APACHE的修改公开(不受GPL条款约束). 

二. Apache的基本配置及说明: 

1.获得Apache: 

获得apache源代码最方便的方法是到http://www.apache.org,选择最新的版本。然后, 
tar xvzf apache_x.x.xx.tar.gz 

2. 编译Apache 
在src目录下 
./configure 可用 ./configure--help|less 得到帮助, 
make, make install, install 

3. apache的启动和关闭 
sbin/apachectl start 
sbin/apachectl stop 
sbin/apachectl restart (重起) 

4. 一些模块介绍: 
下列的组模块控制进入和验证 
Acess 进入控制 
auth 检查Unix-style口令和组文件的基本的验证。 
auth-anon 使用匿名的FTP-style usernames和口令。 
auth-dbm 使用Berkeley db文件存储usernames,口令,和组信息。如果你有很多usernames并关心性能, 这是很有用的。 
digest 使用HTTP文摘认证,这比基本认证更安全。 
expire 模块让你定制Expire过期时间 

5. Apache1.3的缺省目录布局是: 
./bin 
服务器二进制,apachectl原本 
./conf 
Apache配置文件 
./include/apache 
Apache标题文件 
./libexec 
共用的对象文件 
./man 
man 页 
./sbin 
服务器二进制,apachectl原本,log rotator 
./cgi-bin 
缺省CGI原本/二进制的目录 
./share/htdocs 
缺省文件根 
./htdocs/manual 
Apache手册 
./share/icon 
包含各种图标GIFs的目录 
./var/apache/log 
log文件的目录 
./var/apache/proxy 
proxy缓存目录 
./var/apache/run 
httpd.pid(包含主要的httpd过程标识符的文件)在此目录 

6. 主要文件如下: 
httpd.conf, 在/usr/local/apache/conf或/etc/httpd/conf下 
apachectl, 在/usr/local/apache/bin或/etc/httpd/bin下 
access_log, 纪录用户访问信息, 在../logs里 
error_log, 纪录用户访问错误信息, 也在../logs里 

7. httpd.conf 基本设置 
Port, Apache 在哪一个 TCP 端口进行侦听,HTTP 的默认端口为 80。如果你使用其它端口,你不得不在 URLs 中指定端口来连接你的服务器, 

User,Group 由于安全上的原因,在 Linux 上,可以通过把 User 和 Group 都设置成 nobody 

ServerRoot 参数应该是 Apache 安装的基础。除非你移动了周围的资料,否则其值与你所给定的值一样。 

ServerName, 如果你的机器有多个名称或者 IP 地址,设置 Apache 表征服务器的名称。 

DocumentRoot 是 Web 树的根目录, 

UserDir 在启用 ~username 处理请求时, 此指令告诉 Apache 哪个目录追加到用户的主目录(home)。UserDir 被设置为 public_html。用户将只有基本的网页服务功能, 

<Directory>是一个容器指令。</Directive>格式的结束指令。 

<Directive>及相关的<Files>和<FilesMatch>指令的目的,是为了让你在每个目录或者每个文件的基础上,控制Apache行为的方式。指令放在<Directory>中,也适用于子目录。 

FollowSymLinks选项,告诉 Apache 它可以从文档根目录内部到文档根目录外部跟踪符号链接。在文档根目录内部,FollowSymLinks 和 Indexes 都处于开启状态(on)。万一缺省的索引文件(通常为 index.html)不存在时,最后允许目录列表显示。 

Directory 中,用 Options 指令你可以控制什么选项是被允许的。 

Options 指令: 
1) ExecCGI CGI 脚本程序能够运行于该目录树。 
2) FollowSymLinks 在该目录中,服务器将跟踪符号链接。注意:即使服务器跟踪符号链接,它也不会改变用来匹配不同区域的路径名。注意:如果在区域内设置,该选项会被忽略。 
3) Includes 服务器端包容被允许。 
4) IncludesNOEXEC 服务器端引用被允许,但 #exec 命令和 CGI 脚本程序的 #include 被禁止。 
5) Indexes 如果要求映射到目录的 URL 和在该目录里没有 DirectoryIndex(例如,index.html),那么服务器将返回格式化的目录列表。 
6) MultiViews 处理多重浏览内容被允许。 
7) SymLinksIfOwnerMatch 通过与用户 id 一样的链接,服务器只跟踪目标文件或者目录所拥有的符号链接。 
8) All 除 MultiViews 外的一切东西。要是没有 Options 指令,默认为all. 

AccessFileName 命令来设置(缺省设置为 .htaccess)。在任何目录中,Apache 配置指令可被放入一个叫做 .htaccess 的文件。指令也可应用于目录里的子目录。在访问文件中, 

AllowOverride 控制哪个指令将被使用。如果 AllowOverride 被设置为 none。Apache 甚至不会检查访问文件。 

AllowOverride 参数: 
1) AuthConfig, 授权指令哪一个允许使用; 
2) FileInfo,用来控制文档类型和处理方法的指令; 
3)Indexes,使用其中的一个指令来控制目录列表、图标等的显示; 
4) Limit,允许使用指令来控制主机访问; 
5) Options,使用 Options 指令中的一个。 

“allow”和“deny”指令就是用来控制访问的。第三个指令“order”,控制这些指令的使用顺序。如果你把顺序设置成 allow,deny,而没有指定哪个主机是被拒绝的话,则所有主机都被假定为禁止访问。在本例子中,allow 指令被选中,deny 指令被普遍应用。在 .htaccess 文件或者 <Directory> 指令内,更多典型的配置可能是: 
order allow,deny 
allow from all 
deny aaa.com 
所有主机被允许,除了那些来自 aaa.com 域的主机。 

如果相反, 
order deny,allow 
deny from all 
allow from 11.23.44 
deny 首先被检查。只有来自 11.23.44 IP 网络的主机才被允许访问。它也可能指定网络掩码,参数 11.23.44.0/255.255.255.0 相当于上面的例子。 

三. 特殊配置: 
1. 支持用户个人主页: 
在../conf/httpd.conf中加入 
UserDir /home/*/public_html 
设置用户可以使用CGI等: 
<Directory /home/*/public_html/cgi-bin> 
options ExecCGI 
SetHandler cgi-bin #如果在全局设置了AddHandler cgi-bin .cgi ..., 
#这儿就不用设了 
</Directory> 

2. 虚拟主机设置: 

1)基于IP的虚拟主机 
若该机器的IP地址为192.168.0.2,虚拟的IP为192.168.0.3 
首先, 在一台机器的网卡上绑定多个IP地址去服务多个虚拟主机。 
#/sbin/ifconfig eth0:0 192.168.0.2 netmask 255.255.255.0 broadcast 192.168.11.255 up 
#/sbin/route add -host 192.168.0.2 eth0:0 

设置../conf/httpd.conf文件: 
看DocumentRoot是什么, 如果为/home/httpd 
<VirtualHost 192.168.0.3> 
DocumentRoot /home/httpd/aaa 
</VirtualHost> 

创建目录 
/home/httpd/aaa, 

2)基于名字的虚拟主机: 
编辑../conf/httpd.conf 

NameVirtualHost 192.168.0.2 
<VirtualHost 192.168.11.2> 
DocumentRoot /home/httpd/aaa 
ServerName aaa.yourdomain.com 
</VirtualHost> 

要有NameVirtualHost在最前面,下面依次为各个虚拟主机, 
还需要把aaa.yourdomain.com的DNS都指向IP地址192.168.0.2 

3. Apache用户认证 
基本的Apache用户认证 
假定, 对目录/home/aaa下的文件需要做到用户认证,在httpd.conf 
中加入下面的行 
<Directory "/home/aaa"> 
allowoverride authconfig 
order allow,deny 
allow from all 
</Directory> 
用在目录/home/aaa下放文件.htaccess,内容如下: 
authname "Please give me your ID number and Password" 
authtype basic 
authuserfile /etc/.passwd 
require valid-user 

用htpasswd 生成文件/etc/.passwd, 
htpasswd -c /etc/.passwd username 

本文地址:http://com.8s8s.com/it/it11864.htm