您的位置:首页 > 运维架构 > Apache

Apache服务器的配置指令别分成三个相对集中的部分

2007-09-17 17:35 501 查看
Apache服务器的配置指令别分成三个相对集中的部分

1. 配置Apache服务器在整个运行过程中的环境变量.

2. 配置主服务器或默认服务器运行时的详细接口参数.

3. 设置虚拟服务器,使得在同一个Apache服务器上可完成不同IP地址或不同主机名的web请求

  (即:一台物理上的服务器可被设置为有很多个IP地址或很多个主机名的虚拟服务器)

第一部分: 环境设定

# 设置服务器的的启动方式:独立启动(standalone),还是借由互联网络伺服程序inetd来
启动。
# 缺省值是使用独立启动(standalone)。
ServerType standalone

# 设置服务器目录,用来存放服务器的配置文件、错误文件、记录文件的目录。
# 注意:在目录的最后不能再加斜线"/" 。
ServerRoot "C:/Apache"

# 服务程序启动时,它把父程序 httpd 的程序码(process id)存往这个
# log/httpd.pid 文件。这个文件名称可以配合 PidFile 指令加以改变.
PidFile logs/httpd.pid

# 设置Apache服务器执行程序的记录文件。
ScoreBoardFile logs/apache_status

# 下面这两项看个人的喜好,如果您觉得httpd.conf文件太长,不好找到需要的东西。
# 那么您可以将所需的设置放在srm.conf中(它只有几行),然后在下面打开它:
# 下面的两个设置,在标准配置中是打开的,我们可以不打开它.
# 去除下一行前面的"#" , 启用srm.conf
#ResourceConfig conf/srm.conf
#AccessConfig conf/access.conf

# 服务器与客户端的最大等待时间
# 如果客户端在300秒还没有连上,或者服务器300秒还没有传送数据到客户端,就会自动断
线。Timeout 300

# 设置是否支持续传功能.缺省是支持
KeepAlive On

# 设置支持续传功能的数目。数目越多则浪费的硬盘空间越多, 性能也越好。
# 设置为0则没有限制。
MaxKeepAliveRequests 100

# 设置保持续传的最大等待时间
# 如果连接上的某位使用者在15秒后还没有向服务器发出要求,则他不能使用续传功能。
KeepAliveTimeout 15

# 设置同时间内子进程数目,为了安全,设置为零.
MaxRequestsPerChild 0

# 设置服务器使用进程的数目。
# 这是以服务器的响应速度为准的, 数目太大则会变慢.
ThreadsPerChild 50

# 允许使用另外的Port或IP地址访问服务器. 您可以打开它。
#Listen 3000
#Listen 12.34.56.78:80

# 设置Apache监听的IP地址,这也是您在本机上的虚拟服务器的IP地址.
BindAddress 127.0.0.1

# 打开当前未激活预定义的模块,这里除非您真的需要,否则不要更改它
#LoadModule anon_auth_module modules/ApacheModuleAuthAnon.dll
.

# 设置服务器产生的状态信息。如果设置为"On",则产生详细的信息.
#ExtendedStatus On

--------------------------------------------------------------------------------

第二部分  详细接口参数设定

# 设置服务器独立使用时监听的端口。
Port 80

# 设置服务器管理者的E-Mail地址.
ServerAdmin siron@phpstar.com

# 服务器的主机名。如果你有固定的IP地址,则不需要设置
ServerName localhost

# 设置存放站点html文件的目录
DocumentRoot "C:/Apache/htdocs"

# 设置/目录的指令。具体说明如下:
#   Option:定义在目录内所能执行的操作。
#       None表示只能浏览.
#       FollowSymLinks允许页面连接到别处,
#       ExecCGI允许执行CGI,
#       MultiViews允许看动画或是听音乐之类的操作,
#       Indexes允许服务器返回目录的格式化列表,
#       Includes允许使用SSI。
#   这些设置可以复选。All则可以做任何事,但不包括MultiViews。
# AllowOverride:
#       加None参数表示任何人都可以浏览该目录下的文件,但不会读取该文件。
#       FileInfo 允许控制文件型态之指令的使用,
#       AuthConfig 允许验认指令的使用,
#       Indexes 允许控制目录索引之指令的使用,
#       Limit 允许控制存取主机之指令的使用,
#       Options 允许控制特定目录特色之指令的使用.
#       设为 All 的话服务器将会允许所有的指令.
<Directory />
  options FollowSymLinks
  AllowOverride None
</directory>

# 定义首先显示的文件。
# 您可以这样更改 DirectoryIndex index.html index.htm
DirectoryIndex index.html

# 定义每个目录访问控制文件的名称
AccessFileName .htaccess

# 定义代理服务器不要缓存你的页面. 缺省不使用.
#CacheNegotiatedDocs

# 这个指令设定 mime 型态配置档所在的位置,
# 文件名称是相对于 ServerRoot 的, 不建议变更这个文件.
TypesConfig conf/mime.types

# 服务器会将错误讯息记录到一个记录档去,
# 文件名称可以藉由 ErrorLog 指令设定.
# 可以为不同的虚拟主机设定不同的错误记录
ErrorLog logs/error.log

# 设定记录档的格式
LogFormat "%h %l %u %t /"%r/" %>s %b" common
.
.
.

# Alias 指令使文件可以存放在 DocumentRoot 之外的本地文件系统里
# ScriptAlias 指令与 Alias 指令相同, 只不过它还标记改目录包含CGI或PHP指令文件
# AddType 指令以指定的内容型态作为该文件名称结尾的文件的副文件名
Alias /icons/ "C:/Apache/icons/"
Alias /test/ "c:/php/test/"
Alias /admin/ "c:/admin/phpMyAdmin/"
ScriptAlias /php/ "c:/php/"
AddType application/x-httpd-php .php
AddType application/x-httpd-php3 .php3
Action application/x-httpd-php "/php/php.exe"

# AddIcon, AddIconByEncoding and AddIconByType 都
# 是用来设定显示不同文件型态所使用的图示列表;对於每种列
# 出的文件型态,显示列出的第一个符合的图示。
AddIconByType (VID,/icons/movie.gif) video/*
AddIcon /icons/binary.gif .bin .exe

# 缺省的文件图示
DefaultIcon /icons/unknown.gif

# 有问题或错误发生的状况下,可以配置Apache做四种反应
# 1. 输出自定的(customized)讯息(文本)
#ErrorDocument 500 "The server made a boo boo.
# 2. 重导到某个本地的 URL 以处理该问题/错误
#ErrorDocument 404 /missing.html
#ErrorDocument 404 /cgi-bin/missing_handler.pl
# 3. 重导到某个外部的 URL 以处理该问题/错误
#ErrorDocument 402 http://some.other_server.com/subscription_info.html
三 设置虚拟 WEB

IP型虚拟主机

IP型虚拟主机指每一虚拟主机对应唯一的IP。可通过多个物理网卡或虚拟网口实现多IP,So
laris2.5和Windows NT都支持这种方式。
两种配置多虚拟主机的方法:

1、为每一虚拟主机启动一个httpd进程。
下列情况下使用此方法:
1) 需考虑安全隔离问题,如两个httpd运行于不同的User、Group、Listen、ServerRoot,两
者用户除通过Web相互浏览数据,无法访问其他数据。
2) 能提供足够内存和文件描述器。
设置方法:
为每一虚拟主机建立一个独立的httpd安装,在每一个安装路径的配置文件httpd.conf里,用
Listen指令指定进程服务的IP,如:Listen 10.68.37.10:80

2、为所有虚拟主机启动一个httpd进程。
下列情况下使用此方法:
1) 允许在虚拟主机间共享httpd配置。
2) 计算机服务于大量的请求,运行多个进程使服务器性能降低成为重要考虑因素。
设置方法:
在配置文件httpd.conf里,用VirtualHost指令为每一虚拟主机设置ServerAdmin、ServerNa
me、DocumentRoot、 ErrorLog、TransferLog或CustomLog,如:
〈VirtualHost www.smallco.com〉 #此处建议用IP
ServerAdmin webmaster@mail.smallco.com
DocumentRoot /usr/local/etc/httpd/htdocs/smallco
ServerName www.smallco.com #建议此处用域名
ErrorLog /usr/local/etc/httpd/logs/smallco/error_log
TransferLog /usr/local/etc/httpd/logs/smallco/access_log
〈/VirtualHost〉
〈VirtualHost www.baygroup.org〉 #此处建议用IP
ServerAdmin webmaster@mail.baygroup.org
DocumentRoot /groups/baygroup/www
ServerName www.baygroup.org #建议此处用域名
ErrorLog /groups/baygroup/logs/error_log
TransferLog /groups/baygroup/logs/access_log
〈/VirtualHost〉
同时要做虚拟网口或网卡的配置,在DNS也要做相应设置。

名字型虚拟主机(Apache1.3以上版本支持)
  IP型虚拟主机虽好,但不是最佳方案。它要求每一虚拟主机有一专用 IP,在某些机器上
难于实现。名字型虚拟主机是指每一虚拟主机的名字不相同,但IP一样。它的好处是不限制
虚拟主机数量,配置、使用简单,不需另外的软硬件。缺点是客户端必须支持该部分协议,
最近版本的浏览器都支持,某些老版本浏览器不支持。但Apache为此提供了解决方法。
设置方法:
在配置文件httpd.conf里,用NameVirtualHost指令设置虚拟主机,如:
NameVirtualHost 111.22.33.44
〈VirtualHost 111.22.33.44〉 #建议此处用IP
ServerName www.domain.tld #建议此处用域名
DocumentRoot /web/domain
〈/VirtualHost〉
同时,在DNS定义www.domain.tld指向111.22.33.44。
注意:当在NameVirtualHost指令后使用IP时,任何使用IP的URL请求都是针对虚拟主机的,
主服务器从不会响应一个使用IP的URL 请求。另外,有些服务器希望以多个名字被访问。例
如,假设有某一IP的服务器,希望以名domain.tld、www2.domain.tld都能被访问,做法是在
VirtualHost指令部分使用ServerAlias指令。如:ServerAlias domain.tld *.domain.tld
另附一些虚拟主机的设置实例。
 
 
附:虚拟主机设置实例
IP型的虚拟主机配置
Setup 1: 服务器有两个IP,
111.22.33.44 server.domain.tld
111.22.33.55 www.otherdomain.tld
www.domain.tld 是server.domain.tld 的别名(CNAME),代表主服务器。
服务器配置:
...
Port 80
DocumentRoot /www/domain
ServerName www.domain.tld
〈VirtualHost 111.22.33.55〉
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
〈/VirtualHost〉
Setup 2: 基本同Setup1,但不设置专门的主服务器。
服务器配置:
...
Port 80
ServerName server.domain.tld
〈VirtualHost 111.22.33.44〉
DocumentRoot /www/domain
ServerName www.domain.tld
...
〈/VirtualHost〉
〈VirtualHost 111.22.33.55〉
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
〈/VirtualHost〉
这种设置只有当URL为http://server.domain.tld时才击中主服务器
Setup 3: 服务器有两个IP,
111.22.33.44 server.domain.tld
111.22.33.55 www-cache.domain.tld
www.domain.tld 是server.domain.tld 的别名(CNAME),代表主服务器。
www-cache.domain.tld是proxy-cache,端口是8080,Web服务器使用默认的80。
服务器配置:
...
Port 80
Listen 111.22.33.44:80
Listen 111.22.33.55:8080
ServerName server.domain.tld
〈VirtualHost 111.22.33.44:80〉
DocumentRoot /www/domain
ServerName www.domain.tld
...
〈/VirtualHost〉
〈VirtualHost 111.22.33.55:8080〉
ServerName www-cache.domain.tld
...
〈Directory proxy:〉
order deny,allow
deny from all
allow from 111.22.33
〈/Directory〉
〈/VirtualHost〉
 
名字型虚拟主机配置
Setup 1: 服务器有一个IP,
111.22.33.44 server.domain.tld.
www.domain.tld和www.sub.domain.tld是别名(CNAMEs) 。
服务器配置:
...
Port 80
ServerName server.domain.tld
NameVirtualHost 111.22.33.44
〈VirtualHost 111.22.33.44〉
DocumentRoot /www/domain
ServerName www.domain.tld
...
〈/VirtualHost〉
〈VirtualHost 111.22.33.44〉
DocumentRoot /www/subdomain
ServerName www.sub.domain.tld
...
〈/VirtualHost〉
若使用IP访问服务器,由于 www.domain.tld 有最高优先级,被认为是默认服务器或
第一服务器。
Setup 2:服务器有两个IP,
111.22.33.44 server1.domain.tld 用于主服务器
111.22.33.55 server2.domain.tld 用于虚拟主机
别名www.domain.tld用于主服务器,
别名www.otherdomain.tld用于一个虚拟主机,
别名www.sub.domain.tld,*.sub.domain.tld 用于另一虚拟主机,
服务器配置:
...
Port 80
ServerName www.domain.tld
DocumentRoot /www/domain
NameVirtualHost 111.22.33.55
〈VirtualHost 111.22.33.55〉
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
...
〈/VirtualHost〉
〈VirtualHost 111.22.33.55〉
DocumentRoot /www/subdomain
ServerName www.sub.domain.tld
ServerAlias *.sub.domain.tld
...
〈/VirtualHost〉
混合型(IP/名字)虚拟主机配置
Setup:服务器有三个IP,
111.22.33.44 server.domain.tld 用于名字型虚拟主机
111.22.33.55 www.otherdomain1.tld 用于IP型虚拟主机
111.22.33.66 www.otherdomain2.tld 用于IP型虚拟主机
服务器配置:
...
Port 80
ServerName server.domain.tld
NameVirtualHost 111.22.33.44
〈VirtualHost 111.22.33.44〉
DocumentRoot /www/domain
ServerName www.domain.tld
...
〈/VirtualHost〉
〈VirtualHost 111.22.33.44〉
DocumentRoot /www/subdomain1
ServerName www.sub1.domain.tld
...
〈/VirtualHost〉
〈VirtualHost 111.22.33.44〉
DocumentRoot /www/subdomain2
ServerName www.sub2.domain.tld
...
〈/VirtualHost〉
〈VirtualHost 111.22.33.55〉
DocumentRoot /www/otherdomain1
ServerName www.otherdomain1.tld
...
〈/VirtualHost〉
〈VirtualHost 111.22.33.66〉
DocumentRoot /www/otherdomain2
ServerName www.otherdomain2.tld
...
〈/VirtualHost〉
端口型虚拟主机配置
Setup: 服务器有一个IP,
111.22.33.44 www.domain.tld
不需要另外的别名或IP,采用端口型虚拟主机即可设置一个配置有别于主服务器的虚
拟主机。
服务器配置:
...
Listen 80
Listen 8080
ServerName www.domain.tld
DocumentRoot /www/domain
〈VirtualHost 111.22.33.44:8080〉
DocumentRoot /www/domain2
...
〈/VirtualHost〉
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息