天天看点

Web服务(二)httpd配置参数详细介绍

一、配置文件和基本格式

配置文件路径:/etc/httpd/conf/httpd.conf

配置参数    值

   1、配置指令不区分字符大小写;但是值有可能区分字符大小写

   2、有些指令可以重复出现多次

配置文件格式:

   1、全局配置

   2、主机配置:用于仅提供一个站点

   3、虚拟主机:用于提供多个站点(和主机配置不能同时生效)

配置文件语法测试:{service httpd configtest | httpd -t}

二、详细配置

1、监听套接字

1

2

3

4

5

<code>#配置文件事例</code>

<code>#Listen 12.34.56.78:80</code>

<code>Listen </code><code>80</code>

<code>Listen </code><code>8080</code>

<code>Listen </code><code>192.168</code><code>.</code><code>1.110</code><code>:</code><code>8082</code>

此指令可以出现多次;用于指定监听多个不同的套接字:

6

7

8

9

10

11

12

<code>[Linux]</code><code>#httpd -t</code>

<code>Syntax OK</code>

<code>[Linux]</code><code>#service httpd reload</code>

<code>Reloading httpd:</code>

<code>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    </code> 

<code>[Linux]</code><code>#ss -tnl</code>

<code>State      Recv</code><code>-</code><code>Q Send</code><code>-</code><code>Q                     Local Address:Port                       Peer Address:Port</code>

<code>LISTEN     </code><code>0</code>      <code>128</code>                                   <code>:::</code><code>111</code>                                  <code>:::</code><code>*</code>

<code>LISTEN     </code><code>0</code>      <code>128</code>                                    <code>*</code><code>:</code><code>111</code>                                   <code>*</code><code>:</code><code>*</code>

<code>LISTEN     </code><code>0</code>      <code>128</code>                                   <code>:::</code><code>8080</code>                                 <code>:::</code><code>*</code>

<code>LISTEN     </code><code>0</code>      <code>128</code>                                   <code>:::</code><code>80</code>                                   <code>:::</code><code>*</code>

<code>LISTEN     </code><code>0</code>      <code>128</code>                        <code>192.168</code><code>.</code><code>1.186</code><code>:</code><code>8082</code>                                  <code>*</code><code>:</code><code>*</code>

2、配置使用Keep Alive

<code># KeepAlive: Whether or not to allow persistent connections (more than</code>

<code># one request per connection). Set to "Off" to deactivate.</code>

<code>#</code>

<code>#KeepAlive On</code>

<code>KeepAlive Off</code>

<code>MaxKeepAliveRequests </code><code>100</code> <code>#持久连接最大请求数</code>

<code>KeepAliveTimeout </code><code>15</code> <code>#超时时间</code>

3、多道处理模块MPM

查看系统默认启用的模块

<code>[Linux]</code><code>#httpd -l</code>

<code>Compiled </code><code>in</code> <code>modules:</code>

<code>  </code><code>core.c</code>

<code>  </code><code>prefork.c </code><code>#默认启用prefork模块</code>

<code>  </code><code>http_core.c</code>

<code>  </code><code>mod_so.c</code>

<code>[Linux]</code><code>#</code>

<code>#如需启用worker模块;需要更改配置文件</code>

<code>[Linux]</code><code>#vi /etc/sysconfig/httpd</code>

<code>#HTTPD=/usr/sbin/httpd.worker #启用该项后重启httpd</code>

配置模块信息

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

<code>[Linux]</code><code>#vi /etc/httpd/conf/httpd.conf</code>

<code># prefork MPM</code>

<code># StartServers: number of server processes to start</code>

<code># MinSpareServers: minimum number of server processes which are kept spare</code>

<code># MaxSpareServers: maximum number of server processes which are kept spare</code>

<code># ServerLimit: maximum value for MaxClients for the lifetime of the server</code>

<code># MaxClients: maximum number of server processes allowed to start</code>

<code># MaxRequestsPerChild: maximum number of requests a server process serves</code>

<code>prefork 稳定性较好,一个线程崩溃不会影响其他线程</code>

<code>&lt;IfModule prefork.c&gt; 判断prefork模块是否存在</code>

<code>StartServers       </code><code>8</code> <code>默认启动的工作进程数;不包含主进程</code>

<code>MinSpareServers    </code><code>5</code> <code>最少空闲进程数</code>

<code>MaxSpareServers   </code><code>20</code> <code>最大空闲进程数</code>

<code>ServerLimit      </code><code>256</code> <code>最大活动进程数</code>

<code>MaxClients       </code><code>256</code> <code>最多允许发起的请求的个数</code>

<code>MaxRequestsPerChild  </code><code>4000</code> <code>每个子进程在生命周期内所能够服务的最多请求个数</code>

<code>&lt;</code><code>/</code><code>IfModule&gt;</code>

<code># worker MPM</code>

<code># StartServers: initial number of server processes to start</code>

<code># MaxClients: maximum number of simultaneous client connections</code>

<code># MinSpareThreads: minimum number of worker threads which are kept spare</code>

<code># MaxSpareThreads: maximum number of worker threads which are kept spare</code>

<code># ThreadsPerChild: constant number of worker threads in each server process</code>

<code>worker 多个进程;一个进程崩溃会影响其下的其他线程</code>

<code>&lt;IfModule worker.c&gt; 判断worker模块是否存在</code>

<code>StartServers         </code><code>4</code> <code>启动的子进程的个数</code>

<code>MaxClients         </code><code>300</code> <code>并发请求的最大个数</code>

<code>MinSpareThreads     </code><code>25</code> <code>最少空闲线程数</code>

<code>MaxSpareThreads     </code><code>75</code> <code>最大空闲线程数</code>

<code>ThreadsPerChild     </code><code>25</code> <code>每个子进程可生成的线程数</code>

<code>MaxRequestsPerChild  </code><code>0</code> <code>每个子进程在生命周期内所能够服务的最多请求个数;</code><code>0</code><code>表示不做限定</code>

4、DSO模块的加载方式

LoadModule module_name /path/to/module

可以使用相对路径和绝对路径;相对路径则对于ServerRoot所定义的位置而言;

更改完成后service httpd reload可生效

<code># LoadModule foo_module modules/mod_foo.so</code>

<code>LoadModule auth_basic_module modules</code><code>/</code><code>mod_auth_basic.so</code>

<code>LoadModule auth_digest_module modules</code><code>/</code><code>mod_auth_digest.so</code>

<code>LoadModule authn_file_module modules</code><code>/</code><code>mod_authn_file.so</code>

<code>LoadModule authn_alias_module modules</code><code>/</code><code>mod_authn_alias.so</code>

<code>LoadModule authn_anon_module modules</code><code>/</code><code>mod_authn_anon.so</code>

<code>[Linux]</code><code>#httpd -M #可以查看系统所有装载模块</code>

<code>Loaded Modules:</code>

<code> </code><code>core_module (static)</code>

<code> </code><code>mpm_prefork_module (static)</code>

<code> </code><code>http_module (static)</code>

<code> </code><code>so_module (static)</code>

<code> </code><code>auth_basic_module (shared)</code>

<code> </code><code>auth_digest_module (shared)</code>

<code> </code><code>authn_file_module (shared)</code>

<code> </code><code>authn_alias_module (shared)</code>

5、配置站点根目录和页面属性

34

35

36

37

38

39

40

41

42

43

44

45

<code># DocumentRoot: The directory out of which you will serve your</code>

<code># documents. By default, all requests are taken from this directory, but</code>

<code># symbolic links and aliases may be used to point to other locations.</code>

<code>DocumentRoot </code><code>"/var/www/html"</code>

<code>DocumentRoot </code><code>"/path/to/somewhere(站点路径)"</code> <code>#格式</code>

<code># The Options directive is both complicated and important.  Please see 下述站点有配置详细说明</code>

<code># http://httpd.apache.org/docs/2.2/mod/core.html#options</code>

<code># for more information.</code>

<code>&lt;Directory </code><code>"/var/www/html"</code><code>&gt; </code><code>#页面访问属性</code>

<code>    </code><code>Options Indexes FollowSymLinks</code>

<code>Indexes 缺少默认页面时;允许将目录中的所有文件已列表形式返回给用户</code>

<code>FollowSymLinks 允许跟随符号链接所指向的原始文件;危险</code>

<code>None</code> <code>所有都不启用</code>

<code>All</code> <code>所有都启用</code>

<code>ExecCGI 是否允许使用mod_cgi模块执行CGI脚本</code>

<code>Includes 是否允许使用mod_include模块实现服务器端包含(SSI)</code>

<code>MultiViews 允许使用mod_negotiation实现内容协商</code>

<code>SymLinksIfOwnerMatch 在链接文件属主属组与原始文件的属主属组相同时;允许跟随符号链接所指向的原始文件</code>

<code># AllowOverride controls what directives may be placed in .htaccess files.</code>

<code># It can be "All", "None", or any combination of the keywords:</code>

<code>#   Options FileInfo AuthConfig Limit</code>

<code>基于主机的访问控制</code>

<code>    </code><code>AllowOverride </code><code>None</code> <code>表示下面这些控制机制是否被禁用;</code><code>None</code><code>表示不被禁用</code>

<code># Controls who can get stuff from this server.</code>

<code>    </code><code>#allow允许;deny不允许</code>

<code>    </code><code>Order allow,deny </code><code>#默认deny;没有allow的都deny;可以写多条;自上而下匹配</code>

<code>    </code><code>Allow </code><code>from</code> <code>all</code> <code>格式:</code><code>from</code> <code>IP</code>

<code>    </code><code>Deny</code>

<code>    </code><code>#二者都匹配或二者都无匹配项时,则以后者为准;否则,则以匹配到的为准</code>

<code>&lt;</code><code>/</code><code>Directory&gt;</code>

<code>#最佳匹配:从列表中找出最小的能匹配到访问者的地址的条目为最终是生效的</code>

<code>#详细参考http://httpd.apache.org/docs/2.2/mod/mod_authz_host.html#allow</code>

6、定义默认主页面

<code># The index.html.var file (a type-map) is used to deliver content-</code>

<code># negotiated documents.  The MultiViews Option can be used for the</code>

<code># same purpose, but it is much slower.</code>

<code>DirectoryIndex index.html index.html.var </code><code>#自左而右依次查找</code>

7、用户目录

<code># The path to the end user account 'public_html' directory must be</code>

<code># accessible to the webserver userid.  This usually means that ~userid</code>

<code>权限说明</code>

<code># must have permissions of 711, ~userid/public_html must have permissions</code>

<code># of 755, and documents contained therein must be world-readable.</code>

<code># Otherwise, the client will only receive a "403 Forbidden" message.</code>

<code># See also: http://httpd.apache.org/docs/misc/FAQ.html#forbidden</code>

<code>&lt;IfModule mod_userdir.c&gt;</code>

<code>    </code><code>#</code>

<code>    </code><code># UserDir is disabled by default since it can confirm the presence</code>

<code>    </code><code># of a username on the system (depending on home directory</code>

<code>    </code><code># permissions).</code>

<code>    </code><code>UserDir disabled</code>

<code>    </code><code>disabled 禁止</code>

<code>    </code><code>UserDir public_html 用户家目录下的目录名称,所有位于此目录中的文件均可通过前述的访问路径进行访问;用户的家目录的赋予运行httpd进程的用户拥有执行权限;</code>

<code>    </code><code># To enable requests to /~user/ to serve the user's public_html</code>

<code>    </code><code># directory, remove the "UserDir disabled" line above, and uncomment</code>

<code>    </code><code># the following line instead:</code>

<code>    </code><code>#UserDir public_html</code>

8、配置日志功能

/var/log/httpd/access.log &amp;&amp; error.log

access.log:其需要记录的内容需要自定义

访问日志:

   CustomLog "/path/to/access_log_file" Format_Name

   LogFormat Format_String Format_Nam

<code># The following directives define some format nicknames for use with</code>

<code># a CustomLog directive (see below).</code>

<code>LogFormat </code><code>"%h %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\""</code> <code>combined</code>

<code>LogFormat </code><code>"%h %l %u %t \"%r\" %&gt;s %b"</code> <code>common</code>

<code>LogFormat </code><code>"%{Referer}i -&gt; %U"</code> <code>referer</code>

<code>LogFormat </code><code>"%{User-agent}i"</code> <code>agent</code>

<code>%</code><code>h:客户端地址</code>

<code>%</code><code>l:远程登录名;通常为</code><code>-</code>

<code>%</code><code>u:认证时的远程用户名;通常为</code><code>-</code>

<code>%</code><code>t:接受到请求时的时间;</code>

<code>%</code><code>r:请求报文的起始行;</code>

<code>%</code><code>&gt;s:响应状态码;</code>

<code>%</code><code>b:响应报文的长度;单位字节;不包含HTTP首部</code>

<code>%</code><code>{Header_Name}i:记录指定请求报文首部的内容(value)</code>

<code>%</code><code>U:请求的URL;不包含其他任何请求串</code>

<code>#具体请参照http://httpd.apache.org/docs/2.2/mod/mod_log_config.html</code>

<code># ErrorLog: The location of the error log file.</code>

<code># If you do not specify an ErrorLog directive within a &lt;VirtualHost&gt;</code>

<code># container, error messages relating to that virtual host will be</code>

<code># logged here.  If you *do* define an error logfile for a &lt;VirtualHost&gt;</code>

<code># container, that host's errors will be logged there and not here.</code>

<code>ErrorLog logs</code><code>/</code><code>error_log</code>

9、路径别名和默认字符集

Alias /alias/ "/path/to/somewhere/" :前面别名结尾有/后面结尾就一定得有/

<code># We include the /icons/ alias for FancyIndexed directory listings.  If you</code>

<code># do not use FancyIndexing, you may comment this out.</code>

<code>Alias </code><code>/</code><code>icons</code><code>/</code> <code>"/var/www/icons/"</code>

<code>#字符集</code>

<code># Specify a default charset for all content served; this enables</code>

<code># interpretation of all content as UTF-8 by default.  To use the</code>

<code># default browser choice (ISO-8859-1), or to allow the META tags</code>

<code># in HTML content to override this choice, comment out this</code>

<code># directive:</code>

<code>AddDefaultCharset UTF</code><code>-</code><code>8</code>

10、CGI脚本路径别名

URL --&gt; FileSystem Directory

CGI:Common Gateway Interface

有很多机制需要SUID或SGID权限;

httpd无法直接执行脚本;基于CGI协议调用脚本解释器;等待脚本解释器返回结果到web服务器

<code># ScriptAlias: This controls which directories contain server scripts.</code>

<code># ScriptAliases are essentially the same as Aliases, except that</code>

<code># documents in the realname directory are treated as applications and</code>

<code># run by the server when requested rather than as documents sent to the client.</code>

<code># The same rules about trailing "/" apply to ScriptAlias directives as to</code>

<code># Alias.</code>

<code>ScriptAlias </code><code>/</code><code>cgi</code><code>-</code><code>bin</code><code>/</code> <code>"/var/www/cgi-bin/"</code>

<code>ScriptAlias </code><code>/</code><code>URL</code><code>/</code> <code>"/path/to/somewhere/"</code> <code>#格式;路径需要执行权限</code>

<code>#测试</code>

<code>cat &lt;&lt; EOF</code>

<code>Content</code><code>-</code><code>Type</code><code>: text</code><code>/</code><code>html</code>

<code>&lt;pre&gt;</code>

<code>The hostname </code><code>is</code><code>:`hostname`.</code>

<code>The time </code><code>is</code><code>:`date`.</code>

<code>&lt;</code><code>/</code><code>pre&gt;</code>

<code>EOF</code>

11、基于用户的访问控制

虚拟用户:不是系统的账号密码;

在配置文件LoadModule下(auth)开头的认证类型:

   basic:基本认证;账号和密钥明文发送;

   digest:摘要认证;hash编程之后发送

认证提供者(authentication provider):账号和密钥的存放位置(authn)

授权机制(authentication):根据什么进行授权(authz)

1、编辑配置文件使用:

<code>#在&lt;Directory&gt;网站附近下找一个位置新建一个</code>

<code>&lt;Directory </code><code>"/var/www/html/fin"</code><code>&gt; </code><code>#指定目录文件</code>

<code>        </code><code>Options </code><code>None</code> <code>#没有任何选项</code>

<code>        </code><code>AllowOverride AuthConfig </code><code>#使用认证配置</code>

<code>        </code><code>AuthType Basic </code><code>#认证类型</code>

<code>        </code><code>AuthName </code><code>"Private Area"</code> <code>#质询时窗口标题</code>

<code>#       AuthBasicProvider file #认证提供者;默认为文件</code>

<code>        </code><code>AuthUserFile </code><code>/</code><code>etc</code><code>/</code><code>httpd</code><code>/</code><code>conf</code><code>/</code><code>.htpasswd </code><code>#指定文件存放用户账号</code>

<code>#       AuthGroupFile /etc/httpd/conf/.htgroup #指定文件存放组</code>

<code>#       Require group GroupName #指定组名</code>

<code>        </code><code>Require valid</code><code>-</code><code>user </code><code>#所有的合法账户</code>

2、使用htpasswd命令生成认证库

<code>[Linux]</code><code>#htpasswd -b /etc/httpd/conf/.htpasswd pipi pipi</code>

<code>Adding password </code><code>for</code> <code>user pipi</code>

<code>详细参数可以man htpasswd</code>

<code>SYNOPSIS</code>

<code>       </code><code>htpasswd [ </code><code>-</code><code>c ] [ </code><code>-</code><code>m ] [ </code><code>-</code><code>D ] passwdfile username</code>

<code>       </code><code>htpasswd </code><code>-</code><code>b [ </code><code>-</code><code>c ] [ </code><code>-</code><code>m | </code><code>-</code><code>d | </code><code>-</code><code>p | </code><code>-</code><code>s ] [ </code><code>-</code><code>D ] passwdfile username password</code>

<code>       </code><code>htpasswd </code><code>-</code><code>n [ </code><code>-</code><code>m | </code><code>-</code><code>d | </code><code>-</code><code>s | </code><code>-</code><code>p ] username</code>

<code>       </code><code>htpasswd </code><code>-</code><code>nb [ </code><code>-</code><code>m | </code><code>-</code><code>d | </code><code>-</code><code>s | </code><code>-</code><code>p ] username password</code>

<a href="http://s3.51cto.com/wyfs02/M00/22/DC/wKioL1MpmeOizcX-AADiKRb9vfw638.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M02/22/DB/wKiom1Mpmh2yO9K8AACL6ZXKNPo593.jpg" target="_blank"></a>

12、虚拟主机

一个物理服务器提供多个站点;使用虚拟主机得先取消中心主机

1、基于不同的IP实现不同的虚拟

  使用不同IP;

2、基于不同的port实现不同的虚拟主机

  使用不同端口

3、基于不同的FQDN实现不同的虚拟主机

  使用不同的ServerName的值:FQDN

46

47

<code>#DocumentRoot "/var/www/html" #这项需要先注释;中心主机</code>

<code>#基于主机名不同进行测试;下面这项需要开启;IP和port是不需要开启的</code>

<code>NameVirtualHost </code><code>*</code><code>:</code><code>80</code>

<code># NOTE: NameVirtualHost cannot be used without a port specifier</code>

<code># (e.g. :80) if mod_ssl is being used, due to the nature of the</code>

<code># SSL protocol.</code>

<code># VirtualHost example:</code>

<code># Almost any Apache directive may go into a VirtualHost container.</code>

<code># The first VirtualHost section is used for requests without a known</code>

<code># server name.</code>

<code>&lt;VirtualHost </code><code>*</code><code>:</code><code>80</code><code>&gt;</code>

<code>    </code><code>ServerAdmin [email protected]</code>

<code>    </code><code>DocumentRoot </code><code>/</code><code>var</code><code>/</code><code>www</code><code>/</code><code>docs</code><code>/</code><code>pipi </code><code>#指定站点路径</code>

<code>    </code><code>ServerName www.pipi.com </code><code>#指定FQDN</code>

<code>    </code><code>ErrorLog logs</code><code>/</code><code>pipi.com</code><code>-</code><code>error_log </code><code>#指定错误日志路径及名称</code>

<code>    </code><code>CustomLog logs</code><code>/</code><code>pipi.com</code><code>-</code><code>access_log common </code><code>#指定访问日志路径及名称</code>

<code>&lt;</code><code>/</code><code>VirtualHost&gt;</code>

<code>    </code><code>ServerAdmin [email protected]</code>

<code>    </code><code>DocumentRoot </code><code>/</code><code>var</code><code>/</code><code>www</code><code>/</code><code>docs</code><code>/</code><code>soul</code>

<code>    </code><code>ServerName www.soul.org</code>

<code>    </code><code>ErrorLog logs</code><code>/</code><code>soul.org</code><code>-</code><code>error_log</code>

<code>    </code><code>CustomLog logs</code><code>/</code><code>soul.org</code><code>-</code><code>access_log common</code>

<code>    </code><code>ServerAdmin [email protected]</code>

<code>    </code><code>DocumentRoot </code><code>/</code><code>www</code><code>/</code><code>docs</code><code>/</code><code>dark</code>

<code>    </code><code>ServerName www.dark.net</code>

<code>    </code><code>ErrorLog logs</code><code>/</code><code>dark.net</code><code>-</code><code>error_log</code>

<code>    </code><code>CustomLog logs</code><code>/</code><code>dark.net</code><code>-</code><code>access_log common</code>

<code>#配置完成后需要在对应的路径下建立相应的文件</code>

<code>[Linux]</code><code>#service httpd restart</code>

<code>Stopping httpd:                                            [  OK  ]</code>

<code>Starting httpd:                                            [  OK  ]</code>

配置完成后如在linux下测试则修改/etc/hosts文件;windows下修改C:\Windows\System32\drivers\etc\hosts文件

<code>X.X.X.129 www.pipi.com</code>

<code>X.X.X.129 www.soul.org</code>

<code>X.X.X.129 www.dark.net</code>

修改完成后直接访问即可。

<code>#查看日志文件</code>

<code>[Linux]</code><code>#cd /var/log/httpd/</code>

<code>[Linux]</code><code>#ls</code>

<code>access_log           dark.net</code><code>-</code><code>access_log  error_log           pipi.com</code><code>-</code><code>access_log  soul.org</code><code>-</code><code>access_log</code>

<code>access_log</code><code>-</code><code>20140309</code>  <code>dark.net</code><code>-</code><code>error_log   error_log</code><code>-</code><code>20140309</code>  <code>pipi.com</code><code>-</code><code>error_log   soul.org</code><code>-</code><code>error_log</code>

本文转自Mr_陈 51CTO博客,原文链接:http://blog.51cto.com/chenpipi/1379923,如需转载请自行联系原作者

继续阅读