Apache 版本: Server version: Apache/2.2.15
总计 1000行左右
英文前带井号的是注释,不起作用。
但很多注释去掉前方的 # 即可生效。
#
# This is the main Apache server configuration file. It contains the 这是Apache服务器主要配置文件
# configuration directives that give the server its instructions. 它包含影响服务器运行的配置指令
# See <URL:http://httpd.apache.org/docs/2.2/> for detailed information. 这里是详细的信息
# In particular, see 尤其是看
# <URL:http://httpd.apache.org/docs/2.2/mod/directives.html>
# for a discussion of each configuration directive. 每项配置的讨论
#
#
# Do NOT simply read the instructions in here without understanding
不要只是简单的阅读这些指令信息而不去理解它
# what they do. They're here only as hints or reminders. If you are unsure
# consult the online docs. You have been warned.
#这里只是做了简单的提示提醒,如果你没有参考在线文件,你就会被警告
# The configuration directives are grouped into three basic sections:这些配置指令被分为下面三个部分
# 1. Directives that control the operation of the Apache server process as a
# whole (the 'global environment').控制整个Apache服务器行为的部分(即全局环境变量)
# 2. Directives that define the parameters of the 'main' or 'default' server,定义主要和默认服务指令的参数
# which responds to requests that aren't handled by a virtual host.对请求作出响应,并非由一个虚拟主机处理
# These directives also provide default values for the settings 也为全部虚拟主机提供默认参数
# of all virtual hosts.
# 3. Settings for virtual hosts, which allow Web requests to be sent to设置虚拟主机,允许web请求发送给不同iIP
# different IP addresses or hostnames and have them handled by the或主机名,这些请求的处理通过同一个
# same Apache server process. Apache服务进程。
#
# Configuration and logfile names: If the filenames you specify for many配置和日志名称,
# of the server's control files begin with "/" (or "drive:/" for Win32), the 文件名开头带/使用绝对路径,不带/使用
# server will use that explicit path. If the filenames do *not* begin 相对路径,相对于服务器根目录。
# with "/", the value of ServerRoot is prepended -- so "logs/foo.log" 举例说明,相对路径文件logs/foo.log 相对
# with ServerRoot set to "/etc/httpd" will be interpreted by the 路径为/etc/httpd,则路径被解释为
# server as "/etc/httpd/logs/foo.log". /etc/httpd/logs/foo.log
#
### Section 1: Global Environment 第一部分,全局环境
#
# The directives in this section affect the overall operation of Apache, 这一部分指令会影响整个apache操作
# such as the number of concurrent requests it can handle or where it 例如Apache能够处理的并发请求的数量
# can find its configuration files. 或者配置文件地址
#
#
# Don't give away too much information about all the subcomponents 不要泄露太多信息关于正在运行的组件信息
# we are running. Comment out this line if you don't mind remote sites 注释掉这一行,如果你不介意远程站点
# finding out what major optional modules you are running 发现你正在运行的其它模块
ServerTokens OS 此指令控制了Server回送给客户端的回应头域是否包含
关于服务器OS类型和编译进的模块描信息
服务器会发送(比如说):Server: Apache/2.0.41
注意:在使用ServerTokens指令时要先启用ServerSignature指令
#
# ServerRoot: The top of the directory tree under which the server's ServerRoot服务根目录这个顶级目录树下
# configuration, error, and log files are kept. 是服务器配置、错误、日志 文件
#
# NOTE! If you intend to place this on an NFS (or otherwise network) 注意 如果你打算放置上述在NFS或其它网络
# mounted filesystem then please read the LockFile documentation 位置文件系统,请一定阅读 LockFile 文档
# (available at <URL:http://httpd.apache.org/docs/2.2/mod/mpm_common.html#lockfile>);
# you will save yourself a lot of trouble. 你自己也会解决很多问题
#
# Do NOT add a slash at the end of the directory path. 不要增加斜线 在目录路径的结尾
#
ServerRoot "/etc/httpd" ServerRoot 服务器根目录……
#
# PidFile: The file in which the server should record its process Pid文件: 服务器记录启动时进程号的文件
# identification number when it starts. Note the PIDFILE variable in 注意 如果文件位置改变 文件变量必须在……
# /etc/sysconfig/httpd must be set appropriately if this location is 设置好
# changed.
# apache进程的进程号文件存放位置
PidFile run/httpd.pid
#
# Timeout: The number of seconds before receives and sends time out. 接收和发生超时信息的时间(秒)
#
Timeout 60
#
# KeepAlive: Whether or not to allow persistent connections (more than 是否允许保持连接(On 则每个连接可以传递多个请求)
# one request per connection). Set to "Off" to deactivate. 设置off为 无效 停用(则一次连接能响应一个请求,而一个请求一般只传输一个文件)
# (若一个页面有多个文件,设置为On可明显提高响应速度)
KeepAlive Off
#
# MaxKeepAliveRequests: The maximum number of requests to allow 最大请求数一次连接期间允许的最大请求数
# during a persistent connection. Set to 0 to allow an unlimited amount. 设置为0 则不限制数量
# We recommend you leave this number high, for maximum performance. 我们推荐你设置较高的数 为了更好性能
#
MaxKeepAliveRequests 100
#
# KeepAliveTimeout: Number of seconds to wait for the next request from the 同一连接 等待下次请求 秒数
# same client on the same connection. 来自同一用户
# (KeepAlive 的超时时间为 15 秒种)
KeepAliveTimeout 15
##
## Server-Pool Size Regulation (MPM specific) 服务器-池 大小规则 (针对MPM)
## MPM:Multi-Processing Module 多路处理模块
这里MPM有两种模式 prefork模式 worker模式
提前说明 选择使用哪种mpm模块,需要在安装apache时指定,例如:
在Apache 2.0中如何指定MPM。
#tar -xvzf httpd-2.0.59.tar.gz
#cd httpd-2.0.59
#./configure --help|grep mpm
--with-mpm=MPM Choose the process model for Apache to use.
MPM={beos|worker|prefork|mpmt_os2|perchild|leader|threadpool}
上述操作用来选择要使用的进程模型,即哪种MPM模块
如果不用“--with-mpm”显式指定某种MPM,prefork就是Unix平台上缺省的MPM。它所采用的预派生子进程方式也是Apache
1.3中采用的模式。prefork本身并没有使用到线程,2.0版使用它是为了与1.3版保持兼容性;另一方面,prefork用单独的子进程来处理不同的请求,进程之间是彼此独立的,这也使其成为最稳定的MPM之一。
我以worker模式进行编译安装
# ./configure --prefix=/opt/apache --with-mpm=worker --enable-module=so
安装完成后,可以使用 httpd -l 来查看使用的哪种mpm
[root@localhost ~]# httpd -l
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c
prefork模式:欲派生,多进程
一个单独的控制进程(父进程)负责产生子进程,这些子进程用于监听请求并作出应答。Apache总是试图保持一些备用的(spare)或者是空闲的子进程用于迎接即将到来的请求。这样客户端就不需要在得到服务前等候子进程的产生。
这个多路处理模块(MPM)实现了一个非线程型的、预派生的web服务器 。
欲派生模式 prefork是Unix平台上的默认(缺省)MPM,使用多个子进程,每个子进程只有一个线程。每个进程在某个确定的时间只能维持一个连接,效率高,但内存占用量比较大。
这个多路处理模块(MPM)实现了一个非线程型的、预派生的web服务器,它的工作方式类似于Apache 1.3。它适合于没有线程安全库,需要避免线程兼容性问题的系统。它是要求将每个请求相互独立的情况下最好的MPM,这样若一个请求出现问题就不会影响到其他请求。
prefork的工作原理是,控制进程在最初建立“StartServers”个子进程后,为了满足MinSpareServers设置的需要创建一个进程,等待一秒钟,继续创建两个,再等待一秒钟,继续创建四个……如此按指数级增加创建的进程数,最多达到每秒32个,直到满足MinSpareServers设置的值为止。这就是预派生(prefork)的由来。这种模式可以不必在请求到来时再产生新的进程,从而减小了系统开销以增加性能。
MaxSpareServers设置了最大的空闲进程数,如果空闲进程数大于这个值,Apache会自动kill掉一些多余进程。这个值不要设得过大,但如果设的值比MinSpareServers小,Apache会自动把其调整为MinSpareServers+1。如果站点负载较大,可考虑同时加大MinSpareServers和MaxSpareServers。
MaxRequestsPerChild设置的是每个子进程可处理的请求数。每个子进程在处理了“MaxRequestsPerChild”个请求后将自动销毁。0意味着无限,即子进程永不销毁。虽然缺省设为0可以使每个子进程处理更多的请求。
但如果设成非零值也有两点重要的好处:
◆ 可防止意外的内存泄漏;
◆ 在服务器负载下降的时侯会自动减少子进程数。
因此,可根据服务器的负载来调整这个值。笔者认为10000左右比较合适。
MaxClients是这些指令中最为重要的一个,设定的是Apache可以同时处理的请求,是对Apache性能影响最大的参数。其缺省值150是远远不够的,如果请求总数已达到这个值(可通过 ps
-ef|grep http|wc -l 来确认),那么后面的请求就要排队,直到某个已处理请求完毕。这就是系统资源还剩下很多而HTTP访问却很慢的主要原因。系统管理员可以根据硬件配置和负载情况来动态调整这个值。虽然理论上这个值越大,可以处理的请求就越多,但Apache默认的限制不能大于256。如果把这个值设为大于256,那么Apache将无法起动。事实上,256对于负载稍重的站点也是不够的。在Apache
1.3中,这是个硬限制。如果要加大这个值,必须在“configure”前手工修改的源代码树下的src/include/httpd.h中查找256,就会发现“#define
HARD_SERVER_LIMIT 256”这行。把256改为要增大的值(如4000),然后重新编译Apache即可。在Apache
2.0中新加入了ServerLimit指令,使得无须重编译Apache就可以加大MaxClients。
# prefork MPM 预创建(欲派生) MPM
# StartServers: number of server processes to start 启动时服务器启动的进程数
# MinSpareServers: minimum number of server processes which are kept spare 保存备用的最小服务进程数(最小空闲子进程数)
所谓空闲子进程是指没有正在处理请求的子进程,
如果你将该指令的值设置为比MinSpareServers
小,Apache将会自动将其修改成"MinSpareServers
+1
# MaxSpareServers: maximum number of server processes which are kept spare 保存备用的最大服务进程数(最大空闲子进程数)
如果当前有超过MaxSpareServers
数量的空闲子进程,那么父进程将杀死多余的子进程
# ServerLimit: maximum value for MaxClients for the lifetime of the server apache子进程最多个数;参见*下方注释
# MaxClients: maximum number of server processes allowed to start 服务器允许启动的最大请求并发数(同时响应客户数)
# MaxRequestsPerChild: maximum number of requests a server process serves * 一个服务子进程允许的最大请求数
<IfModule prefork.c> (到达限制就释放重新建立)
StartServers 8 apache开始运行时,立刻启动8个服务器子进程
MinSpareServers 5
MaxSpareServers 20
ServerLimit 256 *MaxClients最大为256,其若继续增大则要加上ServerLimit这个参数,生效前提:必须放在其 它指令的前面,对于preforkMPM,只有在你需要将MaxClients设置成高于默认值256的时候才需要使用这个指令。要将此指令的值保持和MaxClients一样。
MaxClients 256 最大子进程数量,默认为256。任何超过MaxClients限制的请求都将进入等候队列,一 旦一个链接被释放,队列中的请求将得到服务。要增大这个值,你必须同时增大ServerLimit
MaxRequestsPerChild 4000 *每个子进程在其生存期内允许伺服的最大请求数量到达MaxRequestsPerChild的限制后,子进程将会结束。如果 MaxRequestsPerChild为"0",子进程将永远不会结束。将MaxRequestsPerChild设置成非零值有两个好处:
1.
可以防止(偶然的)内存泄漏无限进行,从而耗尽内存。
2. 给进程一个有限寿命,从而有助于当服务器负载减轻的时候减少活动进程的数量。
</IfModule>
worker模式:欲派生,多线程
此多路处理模块(MPM)使网络服务器支持混合的多线程多进程。由于使用线程来处理请求,所以可以处理海量请求,而系统资源的开销小于基于进程的MPM。但是,它也使用了多进程,每个进程又有多个线程,以获得基于进程的MPM的稳定性。
worker使用多个子进程,每个子进程有多个线程,每个线程在某个确定的时间只能维持一个连接,内存占用量比较小,适合高流量的http服务器。缺点是假如一个线程崩溃,整个进程就会连同其任何线程一起”死掉”,所以要保证一个程式在运行时必须被系统识别为”每个线程都是安全的”。
可以处理海量请求,而系统资源的开销小于基于进程的MPM。但是它也使用了多进程,每个进程又有多个线程,以获得基于进程的MPM的稳定性。
worker是2.0 版中全新的支持多线程和多进程混合模型的MPM。由于使用线程来处理,所以可以处理相对海量的请求,而系统资源的开销要小于基于进程的服务器。但是,worker也使用了多进程,每个进程又生成多个线程,以获得基于进程服务器的稳定性。这种MPM的工作方式将是Apache 2.0的发展趋势。
# worker MPM worker 模式 MPM
# StartServers: initial number of server processes to start 服务器启动时建立的初始子进程数 每个子进程中包含固 定的ThreadsPerChild线程数,各个线程独立地处理请求
# MaxClients: maximum number of simultaneous client connections 同时连接的最大用户数目 MaxClients设置了所有子进程中的线程总数
# MinSpareThreads: minimum number of worker threads which are kept spare 保有的最小空闲工作线程数
这个MPM将基于整个服务器监视空闲线程数。如果服务器中总的空闲线程数太少,子进程将产生新的空闲线程。
# MaxSpareThreads: maximum number of worker threads which are kept spare 保有的最大空闲工作线程数
这个MPM将基于整个服务器监视空闲线程数。如果服务器中总的空闲线程数太多,子进程将杀死多余的空闲线程。
Apache总是试图维持一个备用(spare)或是空闲的服务线程池。这样,客户端无须等待新线程或新进程的建立即可得到处理。初始化时
建立的进程数量由StartServers
指令决定。随后父进程检测所有子进程中空闲线程的总数,并新建或结束子进程使空闲线程的总数维
持在MinSpareThreads
和MaxSpareThreads
所指定的范围内。由于这个过程是自动调整的,几乎没有必要修改这些指令的缺省值。可以
并行处理的客户端的最大数量取决于MaxClients
指令。活动子进程的最大数量取决于MaxClients
除以ThreadsPerChild
的值
# ThreadsPerChild: constant number of worker threads in each server process *每个服务进程中的工作线程常数
# MaxRequestsPerChild: maximum number of requests a server process serves 服务进程中允许的最大请求数目
-
<IfModule worker.c>
StartServers 4
MaxClients 300
MinSpareThreads 25
MaxSpareThreads 75
ThreadsPerChild 25 每个子进程建立的常驻的执行线程数,默认值25子进程在启动时建立这些线程后就不再建立新的线程
MaxRequestsPerChild 0 每个子进程在其生存期内允许伺服的最大请求数量。到达MaxRequestsPerChild的限制后,
</IfModule> 子进程将会结束。如果MaxRequestsPerChild为"0",子进程将永远不会结束
MinSpareThreads和MaxSpareThreads的最大缺省值分别是75和250。这两个参数对Apache的性能影响并不大,可以按照实际情况相应调节。
ThreadsPerChild是worker MPM中与性能相关最密切的指令。ThreadsPerChild的最大缺省值是64,如果负载较大,64也是不够的。这时要显式使用ThreadLimit指令,它的最大缺省值是20000。上述两个值位于源码树server/mpm/worker/worker.c中的以下两行:
#define DEFAULT_THREAD_LIMIT 64
#define MAX_THREAD_LIMIT 20000
这两行对应着ThreadsPerChild和ThreadLimit的限制数。最好在configure之前就把64改成所希望的值。注意,不要把这两个值设得太高,超过系统的处理能力,从而因Apache不起动使系统很不稳定。
Worker模式下所能同时处理的请求总数是由子进程总数乘以ThreadsPerChild值决定的,应该大于等于MaxClients。如果负载很大,现有的子进程数不能满足时,控制进程会派生新的子进程。默认最大的子进程总数是16,加大时也需要显式声明ServerLimit(最大值是20000)。这两个值位于源码树server/mpm/worker/worker.c中的以下两行:
#define DEFAULT_SERVER_LIMIT 16
#define MAX_SERVER_LIMIT 20000
需要注意的是,如果显式声明了ServerLimit,那么它乘以ThreadsPerChild的值必须大于等于MaxClients,而且MaxClients必须是ThreadsPerChild的整数倍,否则Apache将会自动调节到一个相应值(可能是个非期望值)
#
# Listen: Allows you to bind Apache to specific IP addresses and/or 允许你绑定特定的IP地址或者端口 以取代默认值
# ports, in addition to the default. See also the <VirtualHost> 可以参考……指令
# directive.
#
# Change this to Listen on specific IP addresses as shown below to 用特定的IP改变监听端口 如下所示 防止Apache绑定0.0.0.0 IP
# prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
#
#Listen 12.34.56.78:80 例如:绑定监听IP 为12.34.56.78 端口 80
Listen 80 监听端口:80
DSO: 动态共享对象支持 它是现代Unix派生出来的操作系统都存在着的一种动态连接机制。
它提供了一种在运行时将特殊格式的代码,在程序运行需要时,将需要的部分从外存调入内存执行的方法
#
# Dynamic Shared Object (DSO) Support 动态共享对象支持
#
# To be able to use the functionality of a module which was built as a DSO you 为了能够使用那些以DSO模式编译的模块中的函数,
# have to place corresponding `LoadModule' lines at this location so the 你必须有相应的“LoadModule”行 因此,在这里包含了
# directives contained in it are actually available _before_ they are used. 这些指令,以便能在使用它之前激活
# Statically compiled modules (those listed by `httpd -l') do not need 那些静态编译的模块不需要在这里列出
# to be loaded here. (使用httpd -l 命令可以列出这些模块)
#
# Example: 比如
# LoadModule foo_module modules/mod_foo.so
#
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule auth_digest_module modules/mod_auth_digest.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_alias_module modules/mod_authn_alias.so
LoadModule authn_anon_module modules/mod_authn_anon.so
LoadModule authn_dbm_module modules/mod_authn_dbm.so
LoadModule authn_default_module modules/mod_authn_default.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_owner_module modules/mod_authz_owner.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_dbm_module modules/mod_authz_dbm.so
LoadModule authz_default_module modules/mod_authz_default.so
LoadModule ldap_module modules/mod_ldap.so
LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
LoadModule include_module modules/mod_include.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule logio_module modules/mod_logio.so
LoadModule env_module modules/mod_env.so
LoadModule ext_filter_module modules/mod_ext_filter.so
LoadModule mime_magic_module modules/mod_mime_magic.so
LoadModule expires_module modules/mod_expires.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
LoadModule usertrack_module modules/mod_usertrack.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule mime_module modules/mod_mime.so
LoadModule dav_module modules/mod_dav.so
LoadModule status_module modules/mod_status.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule info_module modules/mod_info.so
LoadModule dav_fs_module modules/mod_dav_fs.so
LoadModule vhost_alias_module modules/mod_vhost_alias.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule dir_module modules/mod_dir.so
LoadModule actions_module modules/mod_actions.so
LoadModule speling_module modules/mod_speling.so
LoadModule userdir_module modules/mod_userdir.so
LoadModule alias_module modules/mod_alias.so
LoadModule substitute_module modules/mod_substitute.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule cache_module modules/mod_cache.so
LoadModule suexec_module modules/mod_suexec.so
LoadModule disk_cache_module modules/mod_disk_cache.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule version_module modules/mod_version.so
#
# The following modules are not loaded by default: 以下模块默认不加载
#
#LoadModule asis_module modules/mod_asis.so
#LoadModule authn_dbd_module modules/mod_authn_dbd.so
#LoadModule cern_meta_module modules/mod_cern_meta.so
#LoadModule cgid_module modules/mod_cgid.so
#LoadModule dbd_module modules/mod_dbd.so
#LoadModule dumpio_module modules/mod_dumpio.so
#LoadModule filter_module modules/mod_filter.so
#LoadModule ident_module modules/mod_ident.so
#LoadModule log_forensic_module modules/mod_log_forensic.so
#LoadModule unique_id_module modules/mod_unique_id.so
#
#
# Load config files from the config directory "/etc/httpd/conf.d". 配置文件的加载路径是……
# Incude
/usr/local/apache2/conf/ssl.conf
# Include /usr/local/apache2/conf/vhosts/*.conf
这个指令允许在服务器配置文件中加入其它配置文件
Include conf.d/*.conf 包括这些配置文件(将/etc/httpd/conf.d中的所有.conf结尾文件包含进来)
此处为Apache的一个安全隐患,可能会暴露过多的信息给访问者,开启此状态,访问者可以在特殊网页看到Apache
配置信息,此处单独写附文1
#
# ExtendedStatus controls whether Apache will generate "full" status 扩展状态控制 server-status 处理程序被调用时
# information (ExtendedStatus On) or just basic information (ExtendedStatus ExtendedStatus 为 On时 只生成扩展信息
# Off) when the "server-status" handler is called. The default is Off. ExtendedStatus 为 Off时 生成基本信息
# 默认为off
#ExtendedStatus On (这里的设置要配合后面指令段<Location/server-status>)
#
# If you wish httpd to run as a different user or group, you must run 如果你希望httpd进程运行在不同的用户和组之间
# httpd as root initially and it will switch. 你第一次运行httpd时必须使用root身份,它才能转换
#
# User/Group: The name (or #number) of the user/group to run httpd as. 以用户/组的名字或者编号去运行httpd
# . On SCO (ODT 3) use "User nouser" and "Group nogroup". 在SCO ODT开放式桌面系统中用User nouser 和 Group nogroup
# . On HPUX you may not be able to use shared memory as nobody, and the 在惠普9000系列服务器的OS中,用nobody你可能不能
# suggested workaround is to create a user www and use that user. 使用共享内存。 建议为工作区域建立一个用户 www
# NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET) 注意一些内核拒绝设置组标识
# when the value of (unsigned)Group is above 60000;++ 当组变量超过60000
# don't use Group #-1 on these systems! 在系统中不要使用组-1
#
User apache 运行httpd的用户和组
Group apache