天天看點

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,如需轉載請自行聯系原作者

繼續閱讀