httpd
CentOS 6: 2.2
CentOS 7: 2.4
http://httpd.apache.org/
安裝方式:
rpm
編譯
CentOS 6:
程序環境
配置文件:
/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
配置文件的組成:
~]# grep "Section" /etc/httpd/conf/httpd.conf
### Section 1: Global Environment
### Section 2: 'Main' server configuration
### Section 3: Virtual Hosts
配置格式:directive value
directive: 不區分字符大小寫;
value: 爲路徑時,取決於文件系統;
常用配置:
1、修改監聽的IP和Port
Listen [IP:]PORT
省略ip表示監聽本機所有IP; Listen可重複出現多次;
2、持久連接
Persistent Connection:連接建立,每個資源獲取完成後不會斷開連接,而是繼續等待其它的請求完成;
如何斷開?
數量限制:100
時間限制:可配置
副作用:對併發訪問量較大的服務器,持久連接功能會使用有些請求得不到響應;
折衷:使用較短的持久連接時間;
httpd-2.4 支持毫秒級持久時間;
非持久連接
KeepAlive On|Off
MaxKeepAliveRequests #
KeepAliveTimeout #
測試:
telnet HOST PORT
GET /URL HTTP/1.1
Host: HOSTNAME or IP
3、MPM
Multipath Process Module:多道處理模塊
prefork, worker, event
httpd-2.2不支持同時編譯多個模塊,所以只能編譯時選定一個;rpm安裝的包提供三個二進制程序文件,分別用於實現對不同MPM機制的支持;確認方法:
# ps aux | grep httpd
默認爲/usr/sbin/httpd, 其使用prefork
查看模塊列表:
查看靜態編譯的模塊
# httpd -l
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c
查看靜態編譯及動態裝載的模塊
# httpd -M
更換使用的httpd程序:
/etc/sysconfig/httpd
HTTPD=
重啓服務生效;
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
獨立IP量;
300*86400=40W+
4、DSO
配置指令實現模塊加載
LoadModule <mod_name> <mod_path>
模塊路徑可使用相對地址
相對於ServerRoot(/etc/httpd)指向的路徑而言;
/etc/httpd/modules/
5、定義'Main' server的文檔頁面路徑
DocumentRoot
文檔路徑映射:
DocumentRoot指向的路徑爲URL路徑的起始位置;
DocumentRoot "/var/www/html"
test/index.html --> http://HOST:PORT/test/index.html
6、站點訪問控制
可基於兩種類型的路徑指明對哪些資源進行訪問控制
文件系統路徑:
<Directory ""> </Direcotry>
<File ""> </File>
<FileMatch ""> </FileMatch>
URL路徑:
<Location ""> </Location>
...
訪問控制機制:
基於來源地址;
基於賬號;
7、Directory中“基於來源地址”實現訪問控制
(1) Options
所有可用特性:Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
None, All
Indexes: 索引;
FollowSymlinks:允許跟蹤符號鏈接文件;
(2) 基於來源地址的訪問控制機制
Order:檢查次序
Order allow,deny
Order deny,allow
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
8、定義默認主頁面
DirecotryIndex index.html index.html.var
9、日誌設定
錯誤日誌:
ErrorLog logs/error_log
LogLevel warn
debug, info, notice, warn, error, crit, alert, emerg
訪問日誌:
CustomLog logs/access_log combined
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
%h:客戶端IP地址;
%l: Remote logname (from identd, if supplied). -表示爲空;
%u: Remote user, (from auth; may be bogus if return status (%s) is 401);
%t:Time the request was received (standard english format),服務器收到請求的時間;
%r:First line of request,請求報文的道行信息(method url version);
%>s: 響應狀態碼;
%b: 響應報文的大小,單位是字節,不包括響應報文首部;
%{Referer}i:請求報文當中"referer"首部的值;當前資源的訪問入口,即從哪個頁面中的超鏈接跳轉而來;
%{User-Agent}i:請求報文當中"User-Agent"首部的值;即發出請求用到的應用程序;
詳情:http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#formats
10、路徑別名
DocumentRoot "/www/htocs"
http://www.magedu.com/download/bash-4.4.2-3.el6.x86_64.rpm
--> /www/htdocs/download/bash-4.4.2-3.el6.x86_64.rpm
Alias /URL/ "/PATH/TO/SOMEDIR/"
Alias /bbs/ "/forum/htdocs"
http://www.magedu.com/bbs/index.html
--> /forum/htdocs/bbs/
11、設定默認字符集
AddDefaultCharset UTF-8
GBK, GB2312, GB18030
12、基於用戶的訪問控制
認證質詢:
WWW-Authenticate:響應碼爲401,拒絕客戶端請求,並說明要求客戶提供賬號和密碼;
認證:
Authorization:客戶端用戶填入賬號和密碼後再次發送請求報文;認證通過,則服務器發送響應的資源;
認證類型:
basic:明文
digest:消息摘要
安全域:需要用戶認證後方能訪問的路徑;
應該通過名稱對其進行標識,並用於告知用戶認證的原因;
用戶的賬號和密碼存儲於何處?
虛擬賬號:僅用於訪問某服務時用到的認證標識;
存儲:
文本文件
SQL數據庫
ldap
nis
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命令進行管理
htpasswd [options] passwordfile username
-c: 自動創建passwordfile,因此,僅應該在添加第一個用戶時使用;
-m: md5加密用戶密碼;
-s: sha1加密用戶密碼;
-D: 刪除指定用戶
(3) 實現基於組進行認證
<Directory "">
Options None
AllowOverride None
AuthType Basic
AuthName "STRING"
AuthUserFile "/PATH/TO/HTTPD_USER_PASSWD_FILE"
AuthGroupFile "/PATH/TO/HTTPD_GROUP_FILE"
Require group GROUP1 GROUP2 ...
</Directory>
要提供:用戶賬號文件和組文件;
組文件:每一行定義一個組
GRP_NAME:user1 user2 user3 ...
示例:
<Directory "/www/htdocs/admin">
Options None
AllowOverride None
AuthType Basic
AuthName "Administator private"
AuthUserFile "/etc/httpd/conf.d/.htpasswd"
AuthGroupFile "/etc/httpd/conf.d/.htgroup"
Require group webadmin
</Directory>
13、虛擬主機
有三種實現方案:
基於ip:
爲每個虛擬主機準備至少一個ip地址;
基於port:
爲每個虛擬主機準備至少一個專用port;實踐中很少使用;
基於hostname:
爲每個虛擬主機準備至少一個專用hostname;
可混合使用上述三種方式中任意方式;
注意:一般虛擬主機莫與中心主機混用,所以,要使用虛擬主機,先禁用中心主機;
禁用中心主機:註釋DocumentRoot
每個虛擬主機都有專用配置:
<VirtualHost "IP:PORT">
SeverName
DocumentRoot ""
</VirtualHost>
ServerAlias: 虛擬主機的別名;
ErrorLog
CustomLog
<Directory "">
</Directory>
示例1:基於ip
<VirtualHost 172.16.100.6:80>
ServerName web1.zhanx.wang
DocumentRoot "/vhosts/web1/htdocs"
</VirtualHost>
<VirtualHost 172.16.100.7:80>
ServerName web2.zhanx.wang
DocumentRoot "/vhosts/web2/htdocs"
</VirtualHost>
示例2:基於port
<VirtualHost 172.16.100.7:80>
ServerName web2.zhanx.wang
DocumentRoot "/vhosts/web2/htdocs"
</VirtualHost>
<VirtualHost 172.16.100.7:8080>
ServerName web3.zhanx.wang
DocumentRoot "/vhosts/web3/htdocs"
</VirtualHost>
示例3:基於hostname
<VirtualHost 172.16.100.6:80>
ServerName web1.zhanx.wang
DocumentRoot "/vhosts/web1/htdocs"
</VirtualHost>
<VirtualHost 172.16.100.6:80>
ServerName web2.zhanx.wang
DocumentRoot "/vhosts/web2/htdocs"
</VirtualHost>
<VirtualHost 172.16.100.6:80>
ServerName web3.zhanx.wang
DocumentRoot "/vhosts/web3/htdocs"
</VirtualHost>
14、內置的status頁面
<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
Allow from 172.16
</Location>
實現:基於賬號實現訪問控制