Httpd默認提供的安裝包在CentOS 6上是 2.2版本,在CentOS 7上是 2.4版本。
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: 爲路徑時,取決於文件系統;
常用配置
自加:cd /etc/httpd/conf
cp httpd.conf{,.bak} 做備份
vim httpd.conf
1、修改監聽的IP和Port
Listen [IP:]PORT
省略ip表示監聽本機所有IP;Listen可重複出現多次;
自加:在修改服務的配置文件後,reload即可,不用重啓服務;例如,修改/etc/httpd/conf/httpd.conf,增加一條Listen 192.168.1.103:8080使用8080端口監聽該地址;之後輸入命令]# service httpd reload 即可;查看用]# ss -tunl 命令;同時在瀏覽器輸入192.168.1.103:8080會出現歡迎頁面
2、持久連接
Persistent Connection:連接建立,每個資源獲取完成後不會斷開連接,而是繼續等待其它的請求完成;
如何斷開?有兩種控制機制,進行斷開
數量限制:默認100個
時間限制:可配置
副作用:對併發訪問量較大的服務器,持久連接功能會使用有些請求得不到響應;
折衷方案:使用較短的持久連接時間;
httpd-2.4 支持毫秒級持久時間;
非持久連接
自加:打開/etc/httpd/conf/httpd.conf
KeepAlive On|Off 如果是Off,則爲短連接;如果爲On則爲長連接
MaxKeepAliveRequests # 最大持久連接數量,當KeepAlive爲On此項有用
KeepAliveTimeout # 持久連接超時時間,默認15秒
測試:
telnet HOST PORT
GET /URL HTTP/1.1
Host: HOSTNAME or IP
自加:測試過程
~]# telnet 192.168.1.103 80
GET / HTTP/1.1
Host:192.168.1.103 此時按下兩次回車,即可顯示文本格式的歡迎頁面
3、MPM
Multipath Process Module:多道處理模塊
prefork, worker, event
httpd-2.2不支持同時編譯多個模塊,所以只能編譯時選定一個;rpm安裝的包提供三個二進制程序文件,分別用於實現對不同MPM機制的支持;確認方法:
# ps aux | grep httpd
如果使用的是prefork則執行上面一條命令後,結果是/usr/sbin/httpd
如果使用的是worker則執行上面一條命令後,結果是/usr/sbin/worker
如果使用的是event則執行上面一條命令後,結果是/usr/sbin/event
默認爲/usr/sbin/httpd, 其使用prefork
查看模塊列表的方法:
查看靜態編譯的模塊
[root@www1 ~]# httpd -l
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c
查看靜態編譯及動態裝載的模塊
# httpd -M
更換使用的httpd程序:
編輯/etc/sysconfig/httpd
HTTPD=
重啓服務生效;
自加:~]# vim /etc/sysconfig/httpd
啓動HTTPD=/usr/sbin/httpd.worker這一項
~]# service httpd restart
使用]# ps aux | grep httpd會顯示 /usr/sbin/httpd.worker
prefork的配置:vim /etc/httpd/conf/httpd.conf 可以看到下面內容
<IfModule prefork.c> 如果你裝載的模塊是prefork.c模塊,則包含以下內容
StartServers 8 服務啓動時啓動多少個服務器進程
MinSpareServers 5 最小空閒進程數
MaxSpareServers 20 最大空閒進程數
ServerLimit 256
MaxClients 256 定義最多256個客戶端請求該服務器資源
MaxRequestsPerChild 4000
</IfModule>
worker的配置:
<IfModule worker.c>
StartServers 4 服務器啓動時啓動的進程數
MaxClients 300 服務器端啓動的最大線程數
MinSpareThreads 25 最小空閒線程數
MaxSpareThreads 75 最大空閒線程數
ThreadsPerChild 25 每個進程所能啓動的線程數
MaxRequestsPerChild 0 每個線程所能響應的最大請求數量,0表示不作限制
</IfModule>
PV, UV
PV: Page View
UV: User View
獨立IP量;
300*86400=40W+
4、DSO
在 ]# vim /etc/httpd/conf/httpd.conf 啓用或停用模塊,之後重載一下,service httpd reload
配置指令實現模塊加載
LoadModule <mod_name> <mod_path>
模塊路徑可使用相對地址
相對於ServerRoot(/etc/httpd)指向的路徑而言;
/etc/httpd/modules/
自加:/etc/httpd/modules是一個連接文件
~]# ls -l /etc/httpd/modules
lrwxrwxrwx 1 root root 29 Sep 14 17:28 /etc/httpd/modules -> ../../usr/lib64/httpd/modules
5、定義'Main' server的文檔頁面路徑
DocumentRoot 文檔根路徑
文檔路徑映射:
DocumentRoot指向的路徑爲URL路徑的起始位置;
DocumentRoot "/var/www/html"
test/index.html --> http://HOST:PORT/test/index.html 其中,/test/index.html中標紅的部分,相當於/var/www/html
注意:通過getenforce 查看SELinux是否關閉
小實驗:
1.mkdir /www/htdocs -pv
在htdocs下建立index.html文件
2.~]# vim /etc/httpd/conf/httpd.conf
修改DocumentRoot "/var/www/html"爲DocumentRoot "/www/htdocs"
3.重載 ~]# service httpd reload
4.在瀏覽器中輸入http://192.168.1.103:8080/ 則會顯示index.html中的內容
6、站點訪問控制
可基於兩種類型的路徑指明對哪些資源進行訪問控制
文件系統路徑:
<Directory ""> </Direcotry>
<File ""> </File>
<FileMatch ""> </FileMatch> 正則表達式匹配,儘量不要用
URL路徑:
<Location ""> </Location>
...
訪問控制機制:基於來源地址IP(風險較大);基於賬號(賬號密碼)
7、Directory中“基於來源地址”實現訪問控制
自加:打開/etc/httpd/conf/httpd.conf
(1) Options
所有可用特性:Indexes Includes FollowSymLinksSymLinksifOwnerMatch ExecCGI MultiViews
None, All
Indexes: 索引;
FollowSymlinks:允許跟蹤符號鏈接文件;
(2) 基於來源地址的訪問控制機制
Order:檢查次序
Order allow,deny 這是做白名單,默認是deny,只有allow的纔可以訪問
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、日誌設定
自加:打開/etc/httpd/conf/httpd.conf
錯誤日誌:
ErrorLog logs/error_log
LogLevel warn
debug, info, notice, warn, error, crit, alert, emerg
訪問日誌:
CustomLog logs/access_log combined 自加:(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
自加:我有開了一臺MageCentos3 地址爲192.168.1.105
vim /etc/httpd/conf/httpd.conf之後加入如下內容:
<Directory "/www/htdocs/admin">
Options None
AllowOverride None
AuthType Basic
AuthName "Adimistator private"
AuthUserFile "/etc/httpd/conf.d/.htpasswd"
Require valid-user
</Directory>
重啓服務 ]# service httpd restart
之後再瀏覽器上輸入192.168.1.105/admin/會顯示如圖的內容
(2) 提供賬號和密碼存儲(文本文件)
使用htpasswd命令進行管理
htpasswd [options] passwordfile username
-c: 自動創建passwordfile,因此,僅應該在添加第一個用戶時使用;
-m: md5加密用戶密碼;
-s: sha1加密用戶密碼;
-D: 刪除指定用戶
自加:添加用戶tom 和密碼123456
]# htpasswd -c -m /etc/httpd/conf.d/.htpasswd tom
]# htpasswd -m /etc/httpd/conf.d/.htpasswd jerry
重載一下 ]# service httpd reload
在瀏覽器裏輸入192.168.1.105/admin/然後在上圖中輸入用戶名和密碼即可登錄
(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>
自加:在增加一個用戶,密碼123456
]# htpasswd -m /etc/httpd/conf.d/.htpasswd obama
定義一個組文件]# vim /etc/httpd/conf.d/.htgroup 內容 webadmin: jerry obamba
vim /etc/httpd/conf/httpd.conf之後加入如下內容:
<Directory "/www/htdocs/admin">
Options None
AllowOverride None
AuthType Basic
AuthName "Adimistator private"
AuthUserFile "/etc/httpd/conf.d/.htpasswd"
AuthGroupFile "/etc/httpd/conf.d/.htgroup"
Require group webadmin
</Directory>
問題:爲什麼obama登錄不了
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.magedu.com
DocumentRoot "/vhosts/web1/htdocs"
</VirtualHost>
<VirtualHost 172.16.100.7:80>
ServerName web2.magedu.com
DocumentRoot "/vhosts/web2/htdocs"
</VirtualHost>
自加:在MageCentos3上做的實驗,地址192.168.1.105
1.註釋中心主機 vim /etc/httpd/conf/httpd.conf
#DocumentRoot "/www/htdocs"
然後 在文件最後加上如下內容
<VirtualHost 192.168.1.105:80>
Servername web1.magedu.com
DocumentRoot "/vhosts/web1/htdocs"
</VirtualHost>
<VirtualHost 192.168.1.106:80>
Servername web2.magedu.com
DocumentRoot "/vhosts/web2/htdocs"
</VirtualHost>
2. 使用]# service httpd configtest來測試配置文件是否有語法錯誤,用http -t也可以
3.創建虛擬主機文件 ]# mkdir -p /vhosts/{web1,web2,web3,web4}/htdocs
]# vim /vhosts/web1/htdocs/index.html 裏面的內容隨便寫
]# vim /vhosts/web2/htdocs/index.html
4.添加一個IP地址
]# ip addr add 192.168.1.106/24 dev eth0
5.在瀏覽器中輸入192.168.1.105顯示如圖
在瀏覽器中輸入192.168.1.106
示例2:基於port
<VirtualHost 172.16.100.7:80>
ServerName web2.magedu.com
DocumentRoot "/vhosts/web2/htdocs"
</VirtualHost>
<VirtualHost 172.16.100.7:8080>
ServerName web3.magedu.com
DocumentRoot "/vhosts/web3/htdocs"
</VirtualHost>
自加:在MageCentos3上做的實驗,地址192.168.1.105 接着示例1的實驗
1.vim /etc/httpd/conf/httpd.conf 在後面加入如下內容
<VirtualHost 192.168.1.106:8080>
Servername web3.magedu.com
DocumentRoot "/vhosts/web3/htdocs"
</VirtualHost>
同時增加一條 Listen 8080
2.爲web3提供頁面 ]# vim /vhosts/web3/htdocs/index.html
3.]# service httpd restart
4.在瀏覽器輸入192.168.1.106:8080 則出現如圖
示例3:基於hostname
<VirtualHost 172.16.100.6:80>
ServerName web1.magedu.com
DocumentRoot "/vhosts/web1/htdocs"
</VirtualHost>
<VirtualHost 172.16.100.6:80>
ServerName web2.magedu.com
DocumentRoot "/vhosts/web2/htdocs"
</VirtualHost>
<VirtualHost 172.16.100.6:80>
ServerName web3.magedu.com
DocumentRoot "/vhosts/web3/htdocs"
</VirtualHost>
自加:1.要想使用基於hostname的認證方式,則要啓用/etc/httpd/conf/httpd.conf中的 NameVirtualHost *:80 (在http2.2中要這樣改)
並改爲NameVirtualHost 192.168.1.105:80
2. 將前兩個示例中的配置修改爲
<VirtualHost 192.168.1.105:80>
Servername web1.magedu.com
DocumentRoot "/vhosts/web1/htdocs"
</VirtualHost>
<VirtualHost 192.168.1.105:80>
Servername web2.magedu.com
DocumentRoot "/vhosts/web2/htdocs"
</VirtualHost>
<VirtualHost 192.168.1.105:80>
Servername web3.magedu.com
DocumentRoot "/vhosts/web3/htdocs"
</VirtualHost>
檢查語法 ]# httpd -t
重載服務 ]# service httpd reload
3.在Centos7上 地址 192.168.1.104上]# vim /etc/hosts
寫入如下內容192.168.1.105 web1.magedu.com web2.magedu.com web3.magedu.com
加入專用日誌文件:
]# vim /etc/httpd/conf/httpd.conf 紅框中爲加入內容
之後檢查語法]# httpd -t 然後重載 ]# service httpd reload
在Centos7上執行如下命令
查看產生的日誌文件
14、內置的status頁面
<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
Allow from 172.16
</Location>