標籤:phrase 錯誤記錄檔 改變 參數配置 pps out keepalive erer 頭部
(1) 基本配置:
ServerRoot "/mnt/software/apache2" #你的apache軟體安裝的位置。其它指定的目錄如果沒有指定絕對路徑,則目錄是相對於該目錄。
PidFile logs/httpd.pid #第一個httpd進程(所有其他進程的父進程)的進程號檔案位置。
Listen 80 #伺服器監聽的連接埠號碼。
ServerName www.clusting.com:80 #主要站台名稱(網站的主機名稱)。
ServerAdmin [email protected] #管理員的郵件地址。
DocumentRoot "/mnt/web/clusting" #主要站台的網頁儲存位置。
以下是對主要站台的目錄進行存取控制:
<Directory "/mnt/web/clusting">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
在上面這段目錄屬性配置中,主要有下面的選項:
Options:配置在特定目錄使用哪些特性,常用的值和基本含義如下:
ExecCGI: 在該目錄下允許執行CGI指令碼。
FollowSymLinks: 在該目錄下允許檔案系統使用符號串連。
Indexes: 當使用者訪問該目錄時,如果使用者找不到DirectoryIndex指定的首頁檔案(例如index.html),則返回該目錄下的檔案清單給使用者。
SymLinksIfOwnerMatch: 當使用符號串連時,只有當符號串連的檔案擁有者與實際檔案的擁有者相同時才可以訪問。
其它可用值和含義請參閱:http://www.clusting.com/Apache/ApacheManual/mod/core.html#options
AllowOverride:允許存在於.htaccess檔案中的指令類型(.htaccess檔案名稱是可以改變的,其檔案名稱由AccessFileName指令決定):
None: 當AllowOverride被設定為None時。不搜尋該目錄下的.htaccess檔案(可以減小伺服器開銷)。
All: 在.htaccess檔案中可以使用所有的指令。
其他的可用值及含義(如:Options FileInfo AuthConfig Limit等),請參看: http://www.clusting.com/Apache/ApacheManual/mod/core.html#AllowOverride
Order:控制在訪問時Allow和Deny兩個訪問規則哪個優先:
Allow:允許訪問的主機列表(可用網域名稱或子網,例如:Allow from 192.168.0.0/16)。
Deny:拒絕訪問的主機列表。
更詳細的用法可參看:http://www.clusting.com/Apache/ApacheManual/mod/mod_access.html#order
DirectoryIndex index.html index.htm index.php #首頁檔案的設定(本例將首頁檔案設定為:index.html,index.htm和index.php)
(2) 伺服器的最佳化 (MPM: Multi-Processing Modules)
apache2主要的優勢就是對多處理器的支援更好,在編譯時間同過使用--with-mpm選項來決定apache2的工作模式。如果知道當前的apache2使用什麼工作機制,可以通過httpd -l命令列出apache的所有模組,就可以知道其工作方式:
prefork:如果httpd -l列出prefork.c,則需要對下面的段進行配置:
<IfModule prefork.c>
StartServers 5 #啟動apache時啟動的httpd進程個數。
MinSpareServers 5 #伺服器保持的最小空閑進程數。
MaxSpareServers 10 #伺服器保持的最大空閑進程數。
MaxClients 150 #最大並發串連數。
MaxRequestsPerChild 1000 #每個子進程被請求服務多少次後被kill掉。0表示不限制,推薦設定為1000。
</IfModule>
在該工作模式下,伺服器啟動後起動5個httpd進程(加父進程共6個,通過ps -ax|grep httpd命令可以看到)。當有使用者串連時,apache會使用一個空閑進程為該串連服務,同時父進程會fork一個子進程。直到記憶體中的空閑進程達到MaxSpareServers。該模式是為了相容一些舊版本的程式。我預設編譯時間的選項。
worker:如果httpd -l列出worker.c,則需要對下面的段進行配置:
<IfModule worker.c>
StartServers 2 #啟動apache時啟動的httpd進程個數。
MaxClients 150 #最大並發串連數。
MinSpareThreads 25 #伺服器保持的最小空閑線程數。
MaxSpareThreads 75 #伺服器保持的最大空閑線程數。
ThreadsPerChild 25 #每個子進程的產生的線程數。
MaxRequestsPerChild 0 #每個子進程被請求服務多少次後被kill掉。0表示不限制,推薦設定為1000。
</IfModule>
該模式是由線程來監聽客戶的串連。當有新客戶串連時,由其中的一個空閑線程接受串連。伺服器在啟動時啟動兩個進程,每個進程產生的線程數是固定的(ThreadsPerChild決定),因此啟動時有50個線程。當50個線程不夠用時,伺服器自動fork一個進程,再產生25個線程。
perchild:如果httpd -l列出perchild.c,則需要對下面的段進行配置:
<IfModule perchild.c>
NumServers 5 #伺服器啟動時啟動的子進程數
StartThreads 5 #每個子進程啟動時啟動的線程數
MinSpareThreads 5 #記憶體中的最小空閑線程數
MaxSpareThreads 10 #最大空閑線程數
MaxThreadsPerChild 2000 #每個線程最多被請求多少次後退出。0不受限制。
MaxRequestsPerChild 10000 #每個子進程服務多少次後被重新fork。0表示不受限制。
</IfModule>
該模式下,子進程的數量是固定的,線程數不受限制。當用戶端串連到伺服器時,又閒置線程提供服務。 如果空閑線程數不夠,子進程自動產生線程來為新的串連服務。該模式用於多站台伺服器。
(3) HTTP返頭回資訊配置:
ServerTokens Prod #該參數設定http頭部返回的apache版本資訊,可用的值和含義如下:
Prod:僅軟體名稱,例如:apache
Major:包括主要版本號,例如:apache/2
Minor:包括次版本號碼,例如:apache/2.0
Min:僅apache的完整版本號碼,例如:apache/ 2.0.54
OS:包括作業系統類型,例如:apache/2.0.54(Unix)
Full:包括apache支援的模組及模組版本號碼,例如:Apache/2.0.54 (Unix) mod_ssl/2.0.54 OpenSSL/0.9.7g
ServerSignature Off #在頁面產生錯誤時是否出現伺服器版本資訊。推薦設定為Off
(4) 持久性串連設定
KeepAlive On #開啟持久性串連功能。即當用戶端串連到伺服器,下載完資料後仍然保持串連狀態。
MaxKeepAliveRequests 100 #一個串連服務的最多請求次數。
KeepAliveTimeout 30 #持續串連多長時間,該串連沒有再請求資料,則斷開該串連。預設為15秒。
別名設定
對於不在DocumentRoot指定的目錄內的頁面,既可以使用符號串連,也可以使用別名。別名的設定如下:
Alias /download/ "/var/www/download/" #訪問時可以輸入:http://www.custing.com/download/
<Directory "/var/www/download"> #對該目錄進行存取控制設定
Options Indexes MultiViews
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
CGI設定
ScriptAlias /cgi-bin/ "/mnt/software/apache2/cgi-bin/" # 訪問時可以:http://www.clusting.com/cgi-bin/ 。但是該目錄下的CGI指令檔要加可執行許可權!
<Directory "/usr/local/apache2/cgi-bin"> #設定目錄屬性
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
個人首頁的設定 (public_html)
UserDir public_html (間使用者的首頁儲存在使用者主目錄下的public_html目錄下 URL http://www.clusting.com/~bearzhang/file.html 將讀取 /home/bearzhang/public_html/file.html 檔案)
chmod 755 /home/bearzhang #使其它使用者能夠讀取該檔案。
UserDir /var/html (the URL http://www.clusting.com/~bearzhang/file.html 將讀取 /var/html/bearzhang/file.html)
UserDir /var/www/*/docs (the URL http://www.clusting.com/~bearzhang/file.html 將讀取 /var/www/bearzhang/docs/file.html)
日誌的設定
(1)錯誤記錄檔的設定
ErrorLog logs/error_log #日誌的儲存位置
LogLevel warn #日誌的層級
顯示的格式日下:
[Mon Oct 10 15:54:29 2005] [error] [client 192.168.10.22] access to /download/ failed, reason: user admin not allowed access
(2)訪問日誌設定
日誌的預設格式有如下幾種:
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %b" common #common為日誌格式名稱
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
CustomLog logs/access_log common
格式中的各個參數如下:
%h --用戶端的ip地址或主機名稱
%l --The 這是由用戶端 identd 判斷的RFC 1413身份,輸出中的符號 "-" 表示此處資訊無效。
%u --由HTTP認證系統得到的訪問該網頁的客戶名。有認證時才有效,輸出中的符號 "-" 表示此處資訊無效。
%t --伺服器完成對請求的處理時的時間。
"%r" --引號中是客戶發出的包含了許多有用資訊的請求內容。
%>s --這個是伺服器返回給用戶端的狀態代碼。
%b --最後這項是返回給用戶端的不包括回應標頭的位元組數。
"%{Referer}i" --此項指明了該請求是從被哪個網頁提交過來的。
"%{User-Agent}i" --此項是客戶瀏覽器提供的瀏覽器識別資訊。
下面是一段訪問日誌的執行個體:
192.168.10.22 - bearzhang [10/Oct/2005:16:53:06 +0800] "GET /download/ HTTP/1.1" 200 1228
192.168.10.22 - - [10/Oct/2005:16:53:06 +0800] "GET /icons/blank.gif HTTP/1.1" 304 -
192.168.10.22 - - [10/Oct/2005:16:53:06 +0800] "GET /icons/back.gif HTTP/1.1" 304 -
各參數的詳細解釋,請參閱:http://www.clusting.com/Apache/ApacheManual/logs.html
使用者認證的配置
(1)in the httpd.conf:
AccessFileName .htaccess
.........
Alias /download/ "/var/www/download/"
<Directory "/var/www/download">
Options Indexes
AllowOverride AuthConfig
</Directory>
(2) create a password file:
/usr/local/apache2/bin/htpasswd -c /var/httpuser/passwords bearzhang
(3)onfigure the server to request a password and tell the server which users are allowed access.
vi /var/www/download/.htaccess:
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /var/httpuser/passwords
Require user bearzhang
#Require valid-user #all valid user
虛擬機器主機的配置
(1)基於IP地址的虛擬機器主機配置
Listen 80
<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost>
(2) 基於IP和多連接埠的虛擬機器主機配置
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080
<VirtualHost 172.20.30.40:80>
DocumentRoot /www/example1-80
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
DocumentRoot /www/example1-8080
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.50:80>
DocumentRoot /www/example2-80
ServerName www.example1.org
</VirtualHost>
<VirtualHost 172.20.30.50:8080>
DocumentRoot /www/example2-8080
ServerName www.example2.org
</VirtualHost>
(3)單個IP地址的伺服器上基於網域名稱的虛擬機器主機配置:
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
ServerAlias example1.com. *.example1.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
</VirtualHost>
(4)在多個IP地址的伺服器上配置基於網域名稱的虛擬機器主機:
Listen 80
# This is the "main" server running on 172.20.30.40
ServerName server.domain.com
DocumentRoot /www/mainserver
# This is the other address
NameVirtualHost 172.20.30.50
<VirtualHost 172.20.30.50>
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here ...
</VirtualHost>
<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here ...
</VirtualHost>
(5)在不同的連接埠上運行不同的網站(基於多連接埠的伺服器上配置基於網域名稱的虛擬機器主機):
Listen 80
Listen 8080
NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080
<VirtualHost 172.20.30.40:80>
ServerName www.example1.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
ServerName www.example1.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost 172.20.30.40:80>
ServerName www.example2.org
DocumentRoot /www/otherdomain-80
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
ServerName www.example2.org
DocumentRoot /www/otherdomain-8080
</VirtualHost>
(6)基於網域名稱和基於IP的混合虛擬機器主機的配置:
Listen 80
NameVirtualHost 172.20.30.40
<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>
<VirtualHost 172.20.30.40>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost>
<VirtualHost 172.20.30.40>
DocumentRoot /www/example3
ServerName www.example3.net
</VirtualHost>
SSL加密的配置
首先在配置之前先來瞭解一些基本概念:
認證的概念:首先要有一個根憑證,然後用根憑證來簽發伺服器憑證和客戶認證,一般理解:伺服器憑證和客戶認證是平級關係。SSL必須安裝伺服器憑證來認證。 因此:在此環境中,至少必須有三個認證:根憑證,伺服器憑證,用戶端認證。 在產生認證之前,一般會有一個私密金鑰,同時用私密金鑰產生認證請求,再利用認證伺服器的根證來簽發認證。
SSL所使用的認證可以自己產生,也可以通過一個商業性CA(如Verisign 或 Thawte)簽署認證。
簽發認證的問題:如果使用的是商業認證,具體的簽署方法請查看相關銷售商的說明;如果是知己簽發的認證,可以使用openssl內建的CA.sh指令碼工具。
如果不為單獨的用戶端簽發認證,用戶端認證可以不用產生,用戶端與伺服器端使用相同的認證。
(1) conf/ssl.conf 設定檔中的主要參數配置如下:
Listen 443
SSLPassPhraseDialog buildin
#SSLPassPhraseDialog exec:/path/to/program
SSLSessionCache dbm:/usr/local/apache2/logs/ssl_scache
SSLSessionCacheTimeout 300
SSLMutex file:/usr/local/apache2/logs/ssl_mutex
<VirtualHost _default_:443>
# General setup for the virtual host
DocumentRoot "/usr/local/apache2/htdocs"
ServerName www.example.com:443
ServerAdmin [email protected]
ErrorLog /usr/local/apache2/logs/error_log
TransferLog /usr/local/apache2/logs/access_log
SSLEngine on
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
SSLCertificateFile /usr/local/apache2/conf/ssl.crt/server.crt
SSLCertificateKeyFile /usr/local/apache2/conf/ssl.key/server.key
CustomLog /usr/local/apache2/logs/ssl_request_log "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x "%r" %b"
</VirtualHost>
(2) 建立和使用自簽署的認證:
a.Create a RSA private key for your Apache server
/usr/local/openssl/bin/openssl genrsa -des3 -out /usr/local/apache2/conf/ssl.key/server.key 1024
b. Create a Certificate Signing Request (CSR)
/usr/local/openssl/bin/openssl req -new -key /usr/local/apache2/conf/ssl.key/server.key -out /usr/local/apache2/conf/ssl.key/server.csr
c. Create a self-signed CA Certificate (X509 structure) with the RSA key of the CA
/usr/local/openssl/bin/openssl req -x509 -days 365 -key /usr/local/apache2/conf/ssl.key/server.key -in /usr/local/apache2/conf/ssl.key/server.csr -out /usr/local/apache2/conf/ssl.crt/server.crt
/usr/local/openssl/bin/openssl genrsa 1024 -out server.key
/usr/local/openssl/bin/openssl req -new -key server.key -out server.csr
/usr/local/openssl/bin/openssl req -x509 -days 365 -key server.key -in server.csr -out server.crt
(3) 建立自己的CA(認證認證),並使用該CA來簽署伺服器的認證。
mkdir /CA
cd /CA
cp openssl-0.9.7g/apps/CA.sh /CA
./CA.sh -newca
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
cp server.csr newreq.pem
./CA.sh -sign
cp newcert.pem /usr/local/apache2/conf/ssl.crt/server.crt
cp server.key /usr/local/apache2/conf/ssl.key/
Apache的配置httpd.conf檔案配置