centos6 httpd2.2詳細剖析
阿新 • • 發佈:2018-11-08
以centos6.5為例
配置檔案: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.conf 服務指令碼: /etc/rc.d/init.d/httpd 指令碼配置檔案:/etc/sysconfig/httpd 主程式檔案: /usr/sbin/httpd /usr/sbin/httpd.event /usr/sbin/httpd.worker 日誌檔案: /var/log/httpd: access_log:訪問日誌 error_log:錯誤日誌 站點文件(預設存放網站目錄): /var/www/html 模組檔案路徑: /usr/lib64/httpd/modules 服務控制和啟動: chkconfig httpd on|off service {start|stop|restart|status|configtest|reload} httpd
1,安裝httpd,啟動服務並設定自動啟動
~]# yum install httpd -y
~]# chkconfig httpd on
~]# service httpd start
檢視主配置段落 ~]# grep -i 'section' /etc/httpd/conf/httpd.conf # The configuration directives are grouped into three basic sections: ### Section 1: Global Environment #全域性環境配置段 # The directives in this section affect the overall operation of Apache, ### Section 2: 'Main' server configuration 主配置段 # The directives in this section set up the values used by the 'main' # WebDAV module configuration section. ### Section 3: Virtual Hosts #虛擬主機配置段 # The first VirtualHost section is used for requests without a known
常用配置: 1、修改監聽的IP和PORT Listen [IP:]PORT (1) 省略IP表示為0.0.0.0; (2) Listen指令可重複出現多次; Listen 80 Listen 8080 (3) 修改監聽socket,重啟服務程序方可生效; 2、持久連線 Persistent Connection:tcp連續建立後,每個資源獲取完成後不全 斷開連線,而是繼續等待其它資源請求的進行; 如何斷開? 數量限制 時間限制 副作用:對併發訪問量較大的伺服器,長連線機制會使得後續某些請求無法得到正常 響應; 折衷:使用較短的持久連線時長,以及較少的請求數量; KeepAlive On|Off KeepAliveTimeout 15 MaxKeepAliveRequests 100 測試: telnet WEB_SERVER_IP PORT GET /URL HTTP/1.1 Host: WEB_SERVER_IP 3、MPM httpd-2.2不支援同時編譯多個MPM模組,所以只能編譯選定要使用的那個;CentOS 6的rpm包為此專門提供了三個應用程式檔案,httpd(prefork), httpd.worker, httpd.event,分別用於實現對不同的MPM機制的支援;確認現在使用的是哪下程式檔案的方法: ps aux | grep httpd 預設使用的為/usr/sbin/httpd,其為prefork的MPM模組 ; 檢視httpd程式的模組列表: 檢視靜態編譯的模組: # httpd -l 檢視靜態編譯及動態編譯的模組: # httpd -M 更換使用httpd程式,以支援其它MPM機制; /etc/sysconfig/httpd HTTPD=/usr/sbin/httpd.{worker,event} 注意:重啟服務程序方可生效 MPM配置: prefork的配置 <IfModule prefork.c> StartServers 8 MinSpareServers 5 MaxSpareServers 20 ServerLimit 256 MaxClients 256 MaxRequestsPerChild 4000 </IfModule> worker的配置: <IfModule worker.c> StartServers 4 MaxClients 300 MinSpareThreads 25 MaxSpareThreads 75 ThreadsPerChild 25 MaxRequestsPerChild 0 </IfModule> PV,UV PV:Page View UV: User View 4、DSO 配置指定實現模組載入 LoadModule <mod_name> <mod_path> 模組檔案路徑可使用相對路徑: 相對於ServerRoot(預設/etc/httpd) 5、定義'Main' server的文件頁面路徑 DocumentRoot "" 文件路徑對映: DoucmentRoot指向的路徑為URL路徑的起始位置 其相當於站點URL的根路徑; (FileSystem) /web/host1/index.html --> (URL) /index.html 6、站點訪問控制常見機制 可基於兩種機制指明對哪些資源進行何種訪問控制 <Directory ""> ... </Directory> <File ""> ... </File> <FileMatch "PATTERN"> ... </FileMatch> URL路徑: <Location ""> ... </Location> <LocationMatch ""> ... </LocationMatch> <Directory>中“基於源地址”實現訪問控制: (1) Options 後跟1個或多個以空白字元分隔的“選項”列表; Indexes:指明的URL路徑下不存在與定義的主頁面資源相符的資原始檔時,返回索引列表給使用者; FollowSymLinks:允許跟蹤符號連結檔案所指向的原始檔; None: All: (2) AllowOverride與訪問控制相關的哪些指令可以放在.htaccess檔案(每個目錄下都可以有一個)中; All: None: (3) order和allow、deny order:定義生效次序;寫在後面的表示預設法則; Allow from, Deny from 來源地址: IP NetAddr: 172.16 172.16.0.0 172.16.0.0/16 172.16.0.0/255.255.0.0 7、定義站點主頁面: DirectoryIndex index.html index.html.var 8、定義路徑別名 格式: Alias /URL/ "/PATH/TO/SOMEDIR/" DocumentRoot "/www/htdocs" http://www.iecentury.com/download/bash-4.4.2-3.el6.x86_64.rpm /www/htdocs/download/bash-4.4.2-3.el6.x86_64.rpm Alias /download/ "/rpms/pub/" http://www.iecentury.com/download/bash-4.4.2-3.el6.x86_64.rpm /rpms/pub/bash-4.4.2-3.el6.x86_64.rpm http://www.iecentury.com/images/logo.png /www/htdocs/images/logo.png 9、設定預設字符集 AddDefaultCharset UTF-8 中文字符集:GBK, GB2312, GB18030 10、日誌設定 日誌型別:訪問日誌 和 錯誤日誌 錯誤日誌: ErrorLog logs/error_log LogLevel warn Possible values include: debug, info, notice, warn, error, crit, alert, emerg. 訪問日誌: LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined CustomLog logs/access_log combined LogFormat format strings: http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats %h:客戶端IP地址; %l:Remote User, 通常為一個減號(“-”); %u:Remote user (from auth; may be bogus if return status (%s) is 401);非為登入訪問時,其為一個減號; %t:伺服器收到請求時的時間; %r:First line of request,即表示請求報文的首行;記錄了此次請求的“方法”,“URL”以及協議版本; %>s:響應狀態碼; %b:響應報文的大小,單位是位元組;不包括響應報文的http首部; %{Referer}i:請求報文中首部“referer”的值;即從哪個頁面中的超連結跳轉至當前頁面的; %{User-Agent}i:請求報文中首部“User-Agent”的值;即發出請求的應用程式; 11、基於使用者的訪問控制 認證質詢: WWW-Authenticate:響應碼為401,拒絕客戶端請求,並說明要求客戶端提供賬號和密碼; 認證: Authorization:客戶端使用者填入賬號和密碼後再次傳送請求報文;認證通過時,則伺服器傳送響應的資源; 認證方式有兩種: basic:明文 digest:訊息摘要認證 安全域:需要使用者認證後方能訪問的路徑;應該通過名稱對其進行標 識,以便於告知使用者認證的原因; 使用者的賬號和密碼存放於何處? 虛擬賬號:僅用於訪問某服務時用到的認證標識 儲存: 文字檔案; SQL資料庫; ldap目錄儲存; basic認證配置示例: (1) 定義安全域 <Directory ""> Options None AllowOverride None AuthType Basic AuthName "String“ AuthUserFile "/PATH/TO/HTTPD_USER_PASSWD_FILE" Require user username1 username2 ... </Directory> 允許賬號檔案中的所有使用者登入訪問: Require valid-user (2) 提供賬號和密碼儲存(文字檔案) 使用專用命令完成此類檔案的建立及使用者管理 htpasswd [options] /PATH/TO/HTTPD_PASSWD_FILE username -c:自動建立此處指定的檔案, 因此,僅應該在此檔案不存在時使用; -m:md5格式加密 -s: sha格式加密 -D:刪除指定使用者 另外:基於組賬號進行認證; (1) 定義安全域 <Directory ""> Options None AllowOverride None AuthType Basic AuthName "String“ AuthUserFile "/PATH/TO/HTTPD_USER_PASSWD_FILE" AuthGroupFile "/PATH/TO/HTTPD_GROUP_FILE" Require group grpname1 grpname2 ... </Directory> (2) 建立使用者賬號和組賬號檔案; 組檔案:每一行定義一個組 GRP_NAME: username1 username2 ... 12、虛擬主機 站點標識: socket IP相同,但埠不同; IP不同,但埠均為預設埠; FQDN不同; 請求報文中首部 Host: www.iecentury.com 有三種實現方案: 基於ip: 為每個虛擬主機準備至少一個ip地址; 基於port: 為每個虛擬主機使用至少一個獨立的port; 基於FQDN: 為每個虛擬主機使用至少一個FQDN; 注意:一般虛擬機器不要與中心主機混用;因此,要使用虛擬主機,得 先禁用'main'主機; 禁用方法:註釋中心主機的DocumentRoot指令即可; 虛擬主機的配置方法: <VirtualHost IP:PORT> ServerName FQDN DocumentRoot "" </VirtualHost> 其它可用指令: ServerAlias:虛擬主機的別名;可多次使用; ErrorLog: CustomLog: <Directory ""> ... </Directory> Alias ... 基於IP的虛擬主機示例: <VirtualHost 172.16.100.6:80> ServerName www.a.com DocumentRoot "/www/a.com/htdocs" </VirtualHost> <VirtualHost 172.16.100.7:80> ServerName www.b.net DocumentRoot "/www/b.net/htdocs" </VirtualHost> <VirtualHost 172.16.100.8:80> ServerName www.c.org DocumentRoot "/www/c.org/htdocs" </VirtualHost> 基於埠的虛擬主機: <VirtualHost 172.16.100.6:80> ServerName www.a.com DocumentRoot "/www/a.com/htdocs" </VirtualHost> <VirtualHost 172.16.100.6:808> ServerName www.b.net DocumentRoot "/www/b.net/htdocs" </VirtualHost> <VirtualHost 172.16.100.6:8080> ServerName www.c.org DocumentRoot "/www/c.org/htdocs" </VirtualHost> 基於FQDN的虛擬主機: NameVirtualHost 172.16.100.6:80 <VirtualHost 172.16.100.6:80> ServerName www.a.com DocumentRoot "/www/a.com/htdocs" </VirtualHost> <VirtualHost 172.16.100.6:80> ServerName www.b.net DocumentRoot "/www/b.net/htdocs" </VirtualHost> <VirtualHost 172.16.100.6:80> ServerName www.c.org DocumentRoot "/www/c.org/htdocs" </VirtualHost> 13、status頁面 LoadModule status_module modules/mod_status.so <Location /server-status> SetHandler server-status Order allow,deny Allow from 172.16 </Location>