Apache的主配置文件:/etc/httpd/conf/httpd.conf
yum安装默认站点主目录:/var/www/html/
源码安装默认站点目录: /usr/local/httpd/htdocs
用wc命令统计一共有475行
[root@localhost ~]# wc -l /etc/httpd/httpd.conf
475 /etc/httpd/httpd.conf
重点关注的配置行275行
[root@localhost ~]# grep -v ^#/etc/httpd/httpd.conf | wc -l
如何查看apache工作模式
[root@localhost ~]#/usr/local/httpd/bin/apachectl –l
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c
如果你看到perfork.c 则表示当前为perfork MPM模式。worker.c 则表示为 worker MPM模式。
1.1 ServerRoot 配置
【ServerRoot "" 主要用于指定Apache的安装路径,此选项参数值在安装Apache时系统会自动把Apache的路径写入。Windows安装时,该选项的值为Windows安装的路径,Linux安装时该选项值为编译时选择的路径】
ServerRoot "/usr/local/httpd"
1.2 Listen 配置
【Listen主要侦听web服务端口状态,默认为:80,即侦听所有的地址的80端口,注意这里也可以写成IP地址的侦听形式,不写即默认的地址:0.0.0.0】
Listen 80
1.3 Dynamic Shared Object (DSO) Support(动态共享对象支持)
【主要用于添加Apache一些动态模块,比如php支持模块。重定向模块,认证模块支持,注意如果需要添加某些模块支持,只需把相关模块前面注释符号取消掉。如图所示,要对Apache添加某个功能模块,把前面的注释符号去掉就行】
LoadModule authn_file_modulemodules/mod_authn_file.so
LoadModule authn_dbm_modulemodules/mod_authn_dbm.so
LoadModule authn_anon_modulemodules/mod_authn_anon.so
LoadModule authn_dbd_modulemodules/mod_authn_dbd.so
LoadModule authn_default_modulemodules/mod_authn_default.so
LoadModule authz_host_modulemodules/mod_authz_host.so
LoadModule authz_groupfile_modulemodules/mod_authz_groupfile.so
LoadModule authz_user_modulemodules/mod_authz_user.so
LoadModule authz_dbm_modulemodules/mod_authz_dbm.so
LoadModule authz_owner_modulemodules/mod_authz_owner.so
LoadModule authz_default_modulemodules/mod_authz_default.so
LoadModule auth_basic_modulemodules/mod_auth_basic.so
LoadModule auth_digest_modulemodules/mod_auth_digest.so
LoadModule file_cache_modulemodules/mod_file_cache.so
LoadModule cache_modulemodules/mod_cache.so
LoadModule disk_cache_modulemodules/mod_disk_cache.so
LoadModule mem_cache_modulemodules/mod_mem_cache.so
LoadModule dbd_module modules/mod_dbd.so
LoadModule dumpio_modulemodules/mod_dumpio.so
LoadModule reqtimeout_modulemodules/mod_reqtimeout.so
LoadModule ext_filter_modulemodules/mod_ext_filter.so
LoadModule include_modulemodules/mod_include.so
LoadModule filter_modulemodules/mod_filter.so
LoadModule substitute_modulemodules/mod_substitute.so
LoadModule deflate_modulemodules/mod_deflate.so
LoadModule log_config_modulemodules/mod_log_config.so
LoadModule log_forensic_modulemodules/mod_log_forensic.so
LoadModule logio_modulemodules/mod_logio.so
LoadModule env_module modules/mod_env.so
LoadModule mime_magic_modulemodules/mod_mime_magic.so
LoadModule cern_meta_modulemodules/mod_cern_meta.so
LoadModule expires_modulemodules/mod_expires.so
LoadModule headers_modulemodules/mod_headers.so
LoadModule ident_modulemodules/mod_ident.so
LoadModule usertrack_modulemodules/mod_usertrack.so
LoadModule unique_id_modulemodules/mod_unique_id.so
LoadModule setenvif_modulemodules/mod_setenvif.so
LoadModule version_modulemodules/mod_version.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule mime_module modules/mod_mime.so
LoadModule dav_module modules/mod_dav.so
LoadModule status_modulemodules/mod_status.so
LoadModule autoindex_modulemodules/mod_autoindex.so
LoadModule asis_module modules/mod_asis.so
LoadModule info_module modules/mod_info.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule dav_fs_modulemodules/mod_dav_fs.so
LoadModule vhost_alias_modulemodules/mod_vhost_alias.so
LoadModule negotiation_modulemodules/mod_negotiation.so
LoadModule dir_module modules/mod_dir.so
LoadModule imagemap_modulemodules/mod_imagemap.so
LoadModule actions_modulemodules/mod_actions.so
LoadModule speling_modulemodules/mod_speling.so
LoadModule userdir_modulemodules/mod_userdir.so
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_modulemodules/mod_rewrite.so
LoadModule php5_module modules/libphp5.so
1.4 Apache运行用户配置
【此选项主要用指定Apache服务的运行用户和用户组,默认为:daemon,如图所示下,启动服务后转换的身份,在启动服务时通常以root身份,然后转换身份,这样增加系统安全】
User apache
Group apache
1.5 Apache服务默认管理员地址设置
【此选项主要用指定Apache服务管理员通知邮箱地址,选择默认值即可,如果有真实的邮箱地址也可以设置此值】
ServerAdmin you@example.com
1.6 Apache的默认服务名及端口设置
【此选项主要用指定Apache默认的服务器名以及端口,默认参数值设置为:ServerName localhost:80即可】
ServerName localhost:80
1.7 Apache的根目录访问控制设置
【此选项主要是针对用户对根目录下所有的访问权限控制,默认Apache对根目录访问都是拒绝访问。后面会继续讲到】
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
1.8 Apache的默认网站根目录设置及访问控制
【此区域的配置文件,主要是针对Apache默认网站根目录的设置以及相关的权限访问设置,默认对网站的根目录具有访问权限,此选项默认值即可】
DocumentRoot "/usr/local/httpd/htdocs "
<Directory"/usr/local/httpd/htdocs">
#
#Possible values for the Options directive are "None","All",
#or any combination of:
# Indexes IncludesFollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
#Note that "MultiViews" must be named *explicitly* --- "OptionsAll"
#doesn't give it to you.
#
#The Options directive is both complicated and important. Please see
#http://httpd.apache.org/docs/2.2/mod/core.html#options
#for more information.
#
Options Indexes FollowSymLinks
#
#AllowOverride controls what directives may be placed in .htaccess files.
#It can be "All", "None", or any combination of thekeywords:
# Options FileInfo AuthConfigLimit
#
AllowOverride None
#
#Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
</Directory>
1.9 Apache的默认首页设置
【此区域文件主要设置Apache默认支持的首页,默认只支持:index.html首页,如要支持其他类型的首页,需要在此区域添加:如index.php表示支持index.php类型首页】
<IfModule dir_module>
DirectoryIndexindex.html index.php
</IfModule>
1.10 Apache关于日志文件配置
【此区域文件主要是针对Apache默认的日志级别,默认的访问日志路径,默认的错误日志路径等相关设置,此选项内容默认即可】
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
#
#The following directives define some format nicknames for use with
#a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b\"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b\"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
#
#The location and format of the access logfile (Common Logfile Format).
#If you do not define any access logfiles within a <VirtualHost>
#container, they will be logged here. Contrariwise, if you *do*
#define per-<VirtualHost> access logfiles, transactions will be
#logged therein and *not* in this file.
#
CustomLog "logs/access_log" common
#
#If you prefer a logfile with access, agent, and referer information
#(Combined Logfile Format) you can use the following directive.
#
#CustomLog "logs/access_log" combined
</IfModule>
1.11 URL重定向,cgi模块配置说明
【此区域文件主要包含一些URL重定向,别名,脚本别名等相关设置,以及一些特定的处理程序,比如cgi设置说明。后期会继续说道】
<IfModule alias_module>
#
#Redirect: Allows you to tell clients about documents that used to
#exist in your server's namespace, but do not anymore. The client
#will make a new request for the document at its new location.
#Example:
#Redirect permanent /foo http://www.example.com/bar
#
#Alias: Maps web paths into filesystem paths and is used to
#access content that does not live under the DocumentRoot.
#Example:
#Alias /webpath /full/filesystem/path
#
#If you include a trailing / on /webpath then the server will
#require it to be present in the URL. Youwill also likely
#need to provide a <Directory> section to allow access to
#the filesystem path.
#
#ScriptAlias: This controls which directories contain server scripts.
#ScriptAliases are essentially the same as Aliases, except that
#documents in the target directory are treated as applications and
#run by the server when requested rather than as documents sent to the
#client. The same rules about trailing"/" apply to ScriptAlias
#directives as to Alias.
#
ScriptAlias /cgi-bin/ "/usr/local/httpd/cgi-bin/"
</IfModule>
1.12 MIME媒体文件,以及相关http文件解析配置说明
【此区域文件主要包含一些mime文件支持,以及添加一些指令在给定的文件扩展名与特定的内容类型之间建立映射关系,比如添加对php文件扩展名映射关系。】
<IfModule mime_module>
#
#TypesConfig points to the file containing the list of mappings from
#filename extension to MIME-type.
#
TypesConfig /etc/httpd/mime.types
#
#AddType allows you to add to or override the MIME configuration
#file specified in TypesConfig for specific file types.
#
#AddType application/x-gzip .tgz
#
#AddEncoding allows you to have certain browsers uncompress
#information on the fly. Note: Not all browsers support this.
#
#AddEncoding x-compress .Z
#AddEncoding x-gzip .gz .tgz
#
#If the AddEncoding directives above are commented-out, then you
#probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
AddType application/x-httpd-php-sourece .phps
#
#AddHandler allows you to map certain file extensions to "handlers":
#actions unrelated to filetype. These can be either built into the server
#or added with the Action directive (see below)
#
#To use CGI scripts outside of ScriptAliased directories:
#(You will also need to add "ExecCGI" to the "Options"directive.)
#
#AddHandler cgi-script .cgi
#For type maps (negotiated resources):
#AddHandler type-map var
#
#Filters allow you to process content before it is sent to the client.
#
#To parse .shtml files for server-side includes (SSI):
#(You will also need to add "Includes" to the "Options"directive.)
#
#AddType text/html .shtml
#AddOutputFilter INCLUDES .shtml
</IfModule>
<Directory"/usr/local/httpd/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
1.13 服务器页面提示设置
【此区域可定制的访问错误响应提示,支持三种方式:1明文,2本地重定向 3,外部重定向;另外还包括内存映射或“发送文件系统调用”可被用于分发文件等配置】
# The mod_mime_magic module allows theserver to use various hints from the
# contents of the file itself to determineits type. The MIMEMagicFile
# directive tells the module where the hintdefinitions are located.
#
#MIMEMagicFile /etc/httpd/magic
#
# Customizable error responses come inthree flavors:
# 1) plain text 2) local redirects 3)external redirects
#
# Some examples:
#ErrorDocument 500 "The server made aboo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404"/cgi-bin/missing_handler.pl"
#ErrorDocument 402http://www.example.com/subscription_info.html
#
#
# MaxRanges: Maximum number of Ranges in arequest before
# returning the entire resource, or one ofthe special
# values 'default', 'none' or 'unlimited'.
# Default setting is to accept 200 Ranges.
#MaxRanges unlimited
#
# EnableMMAP and EnableSendfile: On systemsthat support it,
# memory-mapping or the sendfile syscall isused to deliver
# files. This usually improves server performance, but must
# be turned off when serving fromnetworked-mounted
# filesystems or if support for thesefunctions is otherwise
# broken on your system.
#
#EnableMMAP off
#EnableSendfile off
1.14 Apache服务器补充设置
【此区域主要包括:服务器池管理,多语言错误消息,动态目录列表形式配置,语言设置,用户家庭目录,请求和配置上的实时信息,虚拟主机,Apache Http Server手册,分布式创作和版本控制,多种类默认设置,mod_proxy_html,使其支持HTML4/XHTML1等等补充配置的补充】
# Server-pool management (MPM specific)
#Include /etc/httpd/extra/httpd-mpm.conf
# Multi-language error messages
#Include/etc/httpd/extra/httpd-multilang-errordoc.conf
# Fancy directory listings
#Include/etc/httpd/extra/httpd-autoindex.conf
# Language settings
#Include/etc/httpd/extra/httpd-languages.conf
# User home directories
#Include /etc/httpd/extra/httpd-userdir.conf
# Real-time info on requests andconfiguration
#Include /etc/httpd/extra/httpd-info.conf
# Virtual hosts
#Include /etc/httpd/extra/httpd-vhosts.conf
# Local access to the Apache HTTP ServerManual
#Include /etc/httpd/extra/httpd-manual.conf
# Distributed authoring and versioning(WebDAV)
#Include /etc/httpd/extra/httpd-dav.conf
# Various default settings
#Include/etc/httpd/extra/httpd-default.conf
# Secure (SSL/TLS) connections
#Include /etc/httpd/extra/httpd-ssl.conf
1.15 Apache服务器安全连接设置
【此区域主要是关于服务器安全连接设置,用于使用https连接服务器等设置的地方】
# Secure (SSL/TLS) connections
#Include /etc/httpd/extra/httpd-ssl.conf
#
# Note: The following must must be presentto support
# starting without SSL on platforms with no /dev/random equivalent
# but a statically compiled-in mod_ssl.
#
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
1.16服务器与客户端断开的时间设置
【此区域定义客户程序和服务器连接的超时间隔,超过这个时间间隔(秒)后服务器将断开与客户机的连接。】
Timeout 60
1.17定义是否启用保持连接
【此区域是否持续连接(因为每次连接都得三次握手,如果是访问量不大,建议打开此项,如果网站访问量比较大关闭此项比较好),修改为:KeepAlive On 表示允许程序性联机。由于http是无状态协议,一次请求(建立一次TCP连接)只能获取一个资源,当页面中包含多个资源的时候,就需要多次请求(建立多次TCP连接),这样导致服务器性能下降。为此定义了KeepAlive机制,可以定义在一定时间内或请求数在一定数量之内只需要建立一次TCP连接。】
KeepAlive On
1.18表示一个连接的最大请求数
MaxKeepAliveRequests 100
1.19定义httpd的pid文件
【此区域记录httpd守护进程的pid号码,这是系统识别一个进程的方法,系统中httpd进程可以有多个,但这个PID对应的进程是其他的父进程】
PidFile run/httpd.pid
1.20 apahce三种MPM模式配置
1.20.1prefork 模式
<IfModule prefork.c> #当httpd服务使用的profork模型的时候:
StartServers 10 #默认启动10个作业进程
MinSpareServers 10 #空闲进程数不低0于10个
MaxSpareServers 20 #空闲进程数最大20个
ServerLimit 256 #最多可以启动256个进程
MaxClients 256 #最大并发客户端数为256个
MaxRequestsPerChild 4000 #每个进程可以处理4000个请求,超过此数目进程被杀死并重新创建
</IfModule>
需要注意的是:ServerLimit最大值为20000个,并且:由于profork是单一线程的进程,所以每个进程在同一时间里仅能处理一个请求(也就是一个请求一个进程),所以MaxClients的值要和ServerLimit一致。而且,profork的开销比较大,不过稳定性比较强。
1.20.2worker 模式
<IfModule worker.c> #当httpd服务使用的是worker模型的时候
StartServers 6 #默认启动6个作业进程
MaxClients 300 #最大并发客户端数为256个
MinSpareThreads 25 #空闲线程的数量不低于25个
MaxSpareThreads 75 #空闲线程数最大75个
ThreadsPerChild 25 #每个进程默认启动25个线程
MaxRequestsPerChild 0 #每个进程可以处理的请求数不限制
</IfModule>
worker是一种基于线程的模型,一个进程内部可以启动N个线程(最大20000个),每个线程处理一个客户请求。理论上线程的开销要小于进程,但是由于Linux本身对多线程特性的支持并不是太好,所以可能会导致多线程程序运行出问题的几率比单线程程序要大的多,而且一旦一个线程出现问题,可能会导致整个进程出现问题。这个模型用的比较少。
1.20.3 event模式
event模型是一个基于线程的、更高级的事件驱动模型,可以实现一个线程处理N个请求。
在apache的早期版本2.0默认prefork,2.2版本是worker,2.4版本是event.
在configure配置编译参数的时候,可以使用 --with-mpm=prefork|worker|event 来指定编译为那一种MPM,当然也可以用编译为三种都支持:--enable-mpms-shared=all,这样在编译的时候会在modules目录下自动编译出三个MPM文件的so,然后通过修改httpd.conf配置文件更改MPM