天天看點

Apache伺服器的安裝與配置

一、配置檔案

文法

* 主配置檔案httpd.conf,更改隻有重新開機服務才會生效

* 配置中一行一個指令,如果要多行一個指令,則最後以\結束,且與該行最後個字元無其它字元或者空白

* 每行注釋以#開頭,會被忽略,一行指令符後不能再接注釋

* 空行空白字元在配置檔案中被忽略

* 配置檔案錯誤檢測可以通過 Test Configuration來完成(httpd.exe -w -t -f "D:\Software\GreenSoft\Apache\conf\httpd.conf" -d "D:\Software\GreenSoft\Apache\.")

子產品

Related Modules Related Directives

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

mod_so <IfModule>

LoadModule

* httpd基于子產品化,編譯配置時,預設會把基本的功能子產品加載到伺服器核心。

* 對于動态加載的子產品可以通過LoadModule指令在任何時候完成。

* <IfModule>可用于條件加載

* Dos指令 httpd.exe -l 可以檢視已經加載的子產品

* httpd.exe支援的參數指令

-D name : define a name for use in <IfDefine name> directives

-d directory : specify an alternate initial ServerRoot

-f file : specify an alternate ServerConfigFile

-C "directive" : process directive before reading config files

-c "directive" : process directive after reading config files

-n name : set service name and use its ServerConfigFile

-k start : tell Apache to start

-k restart : tell running Apache to do a graceful restart

-k stop|shutdown : tell running Apache to shutdown

-k install : install an Apache service

-k config : change startup Options of an Apache service

-k uninstall : uninstall an Apache service

-w : hold open the console window on error

-e level : show startup errors of level (see LogLevel)

-E file : log startup errors to file

-v : show version number

-V : show compile settings

-h : list available command line options (this page)

-l : list compiled in modules

-L : list available configuration directives

-t -D DUMP_VHOSTS : show parsed settings (currently only vhost settings)

-S : a synonym for -t -D DUMP_VHOSTS

-t -D DUMP_MODULES : show all loaded modules

-M : a synonym for -t -D DUMP_MODULES

-t : run syntax check for config files

-T : start without DocumentRoot(s) check

指令作用範圍

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

<Directory>

<DirectoryMatch>

<Files>

<FilesMatch>

<Location>

<LocationMatch>

<VirtualHost>

* 指令範圍标簽可以指定指令生效的範圍,其可以嵌套

* 可以通過在<VirtualHost>标簽中放置多個指令以同時支援多個虛拟主機站點

.htaccess檔案

* httpd通過在站點目錄裡面放置.htaccess檔案允許配置分散

* .htaccess的文法與主配置檔案一緻,對于該配置檔案的變更隻要請求過來會立即生效

* 如果有權限配置主配置,則應避免使用.htaccess檔案方式來配置,這種方式影響伺服器性能

1)配置.htaccess檔案後,httpd會查找每個目錄的.htaccess檔案,不管你實際有沒有使用

2)文檔被請求的時候,.htaccess檔案每次都會被加載

3)httpd會在多層目錄中查找.htaccess檔案,以應用完整的指令。

4).htaccess中位址重寫指令中的正規表達式在每次請求的時候重新編譯。

5)安全問題,使用者在修改.htaccess使管理者對配置的管理失控。隻有設定AllowOverride指令才可以使使用者使用指定的指令。

* 在.htaccess中配置指令與在主配置中配置Dirctory塊可以達到相同效果,但是後者有更好的性能。

Contents of .htaccess file in /www/htdocs/example

AddType text/example .exm

等價于:

Section from your httpd.conf file

<Directory /www/htdocs/example>

</Directory>

* 禁用.htaccess可以用指令 AllowOverride None

二、Apache目錄結構

根目錄

|-- bin 程式指令目錄

|-- build

|-- cgi-bin

|-- conf 配置檔案目錄

|-- error

|-- htdocs 編譯安裝時站點目錄

|-- icons

|-- include

|-- lib 

|-- logs 預設日志檔案存放包括錯誤日志(error_log)和通路日志(access_log) tail -f access_log可以随時看通路記錄, 裡面httpd.pid還會記錄主程序号

|-- man

|-- manual

|-- modules 子產品目錄 例如PHP MEMCACHE編譯後的子產品在這裡面

Bin目錄

|-- ab apache http伺服器性能壓力測試工具,類似的jmeter loadrunner webbench

|-- apachectl apache的啟動指令

|-- apr-1-config

|-- apu-1-config

|-- apxs apache伺服器編譯和安裝擴充的工具,在進行DSO方式子產品編譯時會用到例如編譯PHP時就用到:--with-apxs2=/usr/local/apache/bin/apxs

|-- checkgid

|-- dbmmanage

|-- envvars

|-- envvars-std

|-- htcacheclean 清理磁盤緩存區的指令,一般少用

|-- htdbm

|-- htdigest

|-- htpasswd 建立更新基本認證檔案 例如配置nagio監控時候會用到

|-- httpd 為apache的控制指令程式,apachectl執行的時候會調用到httpd

|-- httxt2dbm

|-- logresolve

Conf目錄

|-- extra 輔助apache配置檔案

| |-- httpd-autoindex.conf

| |-- httpd-dav.conf dav支援配置

| |-- httpd-default.conf apache相關服務配置 如逾時時間 保持連接配接時間

| |-- httpd-info.conf

| |-- httpd-languages.conf 語言支援

| |-- httpd-manual.conf

| |-- httpd-mpm.conf 伺服器池管理,如apache模式配置 連接配接等

| |-- httpd-multilang-errordoc.conf

| |-- httpd-ssl.conf 支援SSL加密配置

| |-- httpd-userdir.conf

| |-- httpd-vhosts.conf 虛拟機配置檔案

|-- httpd.conf 主配置檔案

|-- magic

|-- mime.types

|-- original

三、httpd.conf主配置檔案常見配置

ServerRoot "/usr/local/apache2.2.22" apache安裝目錄

Listen 80 apache端口 

<IfModule !mpm_netware_module>

<IfModule !mpm_winnt_module>

User daemon apache運作使用者

Group daemon apache運作組

</IfModule>

ServerAdmin [email protected] 管理者郵箱

DocumentRoot "/usr/local/apache2.2.22/htdocs" 站點目錄,注意最後不要加/線

<Directory /> 根目錄權限

Options FollowSymLinks 可以使用符号連結

AllowOverride None

Order deny,allow

Deny from all

<Directory "/usr/local/apache2.2.22/htdocs"> 目錄權限 注意Indexes的作用是允許浏覽目錄

Options Indexes FollowSymLinks

AllowOverride None 禁止使用者對目錄配置檔案.htaccess修改,一般為none

Order allow,deny

Allow from all 

<IfModule dir_module>

DirectoryIndex index.html 站點目錄首頁檔案多個檔案用空格隔開

<FilesMatch "^\.ht"> 防止WEB使用者檢視以.ht開頭的隐藏檔案

Satisfy All

</FilesMatch>

ErrorLog "logs/error_log" 錯誤日志

LogLevel warn 警告級别

<IfModule log_config_module>

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>

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio 複合日志格式

CustomLog "logs/access_log" common 預設通路日志格式

<IfModule alias_module>

ScriptAlias /cgi-bin/ "/usr/local/apache2.2.22/cgi-bin/" CGI别名配置

<IfModule cgid_module>

<Directory "/usr/local/apache2.2.22/cgi-bin"> CGI權限通路路徑

Options None

Allow from all

DefaultType text/plain 定義不能确定MIME類型時候使用預設MIME類型,如果服務主要包含text或HTML ,text/plain是一個好選擇

<IfModule mime_module>

TypesConfig conf/mime.types

AddType application/x-compress .Z 允許資訊在傳輸中使用mosaic/x 2.1+解壓資訊 但不是所有浏覽器都支援

AddType application/x-gzip .gz .tgz

<IfModule ssl_module>

SSLRandomSeed startup builtin

SSLRandomSeed connect builtin

四、虛拟主機http_vhost.conf配置檔案

NameVirtuaHost *:80 表示基于名稱的虛拟主機 *:80表示監聽本機所有IP的80端口上提供HTTP服務,*可以設定為具體IP

<VirtualHost *:80>

serveradmin 管理者郵箱

servername 域名

serveralias 别名 可以設定多個域名指向同一個站點

errorlog 錯誤日志

customlog 通路日志

</VirtualHost>

虛拟主機的配置是局部優先原則,也就是http_vhost.conf配置了,那麼就不會從主配置檔案http.conf中讀取了

五、修改Apache預設站點目錄

1、文檔路徑更改 

DocumentRoot "D:/Software/GreenSoft/Apache/htdocs" 

改成

DocumentRoot "E:/wwwroot/Apache"

2、目錄更改

<Directory "D:/Software/GreenSoft/Apache/htdocs">

<Directory "E:/wwwroot/Apache">

六、配置多站點方式

方式一 拷貝配置檔案,安裝新服務

1、将httpd.conf複制一個新的檔案httpdNew.conf

2、将httpdNew.conf中的偵聽端口改成未使用的自定義的端口。 Listen:8111

3、将httpdNew.conf中的網站目錄 DocumentRoot "原絕對路徑" 改成 DocumentRoot "新絕對路徑"

4、将httpdNew.conf中的權限設定 <Directory "原絕對路徑" > 改成 <Directory "新絕對路徑" >

5、安裝新服務。Dos下進到Apache安裝目錄下的Bin目錄,輸入指令 httpd.exe -k install -n "服務名" -f "X:\Apache安裝目錄根目錄\conf\httpdNew.conf"

6、啟動新服務。輸入指令 httpd -k start

方式二 添加新端口,新虛拟目錄的方式

1、httpd.conf在之前偵聽端口的基礎上,加上新偵聽端口 Listen 8333.這樣Apache有多個偵聽端口

2、httpd.conf中加入虛拟主機支援。加入指令:NameVirtualHost *

3、找到<VirtualHost> definition注釋所在位置。加入指令:

<VirtualHost localhost:8333>

ServerName localhost

DocumentRoot "虛拟站點新絕對路徑"

方式三 建立虛拟主機方式

1、修改虛拟主機配置檔案 X:\Apache安裝目錄根目錄\conf\extra\httpd-vhosts.conf檔案.加入配置:

<VirtualHost 127.0.1.1:8222>

ServerName url.com

ErrorLog "虛拟站點新絕對路徑/error.log"

2、修改主配置檔案X:\Apache安裝目錄根目錄\conf\httpd.conf。去掉#Include conf/extra/httpd-vhosts.conf前面的#号

3、修改主配置檔案X:\Apache安裝目錄根目錄\conf\httpd.conf。加目錄權限:

<Directory "虛拟站點新絕對路徑">

Options Indexes FollowSymLinks MultiViews ExecCGI

AllowOverride All

4、重新開機對應的Apache服務

七、配置Apache支援Url重寫

1、加載重寫子產品。httpd.conf中找到#LoadModule rewrite_module modules/mod_rewrite.so,去掉#号

2、httpd.conf中找到AllowOverride None,改成 AllowOverride All (All表示整台伺服器上都支援URL規則重寫)

3、httpd.conf加入

<Directory "站點絕對路徑">

Options FollowSymLinks

4、重新開機Apache伺服器并啟動服務

5、在URL重寫的網站目錄下放一個.htaccess檔案,檔案名是.htaccess。如無此檔案或此檔案中沒定義任何關于URL重寫的規則,則重寫不會生效。

6、在.htaccess中通過正規表達式映射需要僞靜态的頁面。URL正規表達式如下:

RewriteEngine on

RewriteRule index.html index.php

RewriteRule (\d+).htmlinfo\.php\?infoid=info\.php\?infoid=1

效果為:www.abc.cn/info/info.php?infoid=100 映射為 www.abc.cn/info/100.html

可用于以下用途:

1、http錯誤的自定義頁。格式 ErrorDocument 錯誤代碼 /目錄名/名.擴充名

ErrorDocument 404 /errors/notfound.html

ErrorDocument 401 "你權限通路該頁面!"

2、拒絕某IP的通路

order allow,deny 

deny from 210.10.56.32 

deny from 219.5.45. 

deny from www.baidu.com

allow from all

3、保護.htaccess文檔

4、Url轉向(文檔遷移,目錄更改,顯示友好易記的url)。格式:

Redirect /舊目錄/舊文檔名 新文檔名

Redirect 舊目錄 新目錄

5、更改預設首頁

DirectoryIndex 新的預設名

DirectoryIndex filename.html index.cgi index.pl default.htm

6、反盜鍊

RewriteEngine on 

RewriteCond %{ HTTP_REFERER } !^RewriteCondRewriteCond [NC] 

RewriteRule .(gif&line;jpg)- [F]  RewriteRule .(gif&line;jpg)- [F]  RewriteRule .(gif&line;jpg) http://www.mydomain.com/替代名 [R,L]

八、配置反盜鍊

1、參見 配置Apache支援Url重寫 節,配置Apache支援url重寫

2、在站點的.htaccess檔案中加入

#允許空“HTTP_REFERER”的通路

RewriteCond %{HTTP_REFERER} !^[NC]RewriteCond[NC]RewriteCond http://nobing.cn/no.png [R,NC,L]

說明:

R 就是轉向的意思

NC 指的是不區分大小寫

L 的作用是指明本次轉向到此結束,後續的轉向不受先前判斷語句的影響

多個擴充名用|分隔

九、配置緩存

LoadModule cache_module modules/mod_cache.so 

LoadModule mem_cache_module modules/mod_mem_cache.so 

<IfModule mod_cache.c> 

#預設緩存有效期(秒)

CacheDefaultExpire 3600 

CacheMaxExpire 86400 

CacheLastModifiedFactor 0.1 

#禁止緩存某些特定的URL 

CacheDisable /local_files 

#使用特定的存儲類型緩存某些特定的URL 

CacheEnable mem /manual 

CacheEnable fd /images 

CacheEnable disk / 

<IfModule mod_mem_cache.c> 

CacheEnable mem / 

MCacheSize 8192 

MCacheMaxObjectCount 10000 

MCacheMinObjectSize 1 

MCacheMaxObjectSize 51200 

#MCacheRemovalAlgorithm LRU 

</IfModule> 

     本文轉自yzy121403725 51CTO部落格,原文連結:http://blog.51cto.com/lookingdream/1789170,如需轉載請自行聯系原作者