Centos7系統(tǒng)下實現(xiàn)httpd-2.2的安裝撕阎,并分別實現(xiàn)prefork、worker碌补、event等幾種工作方式
centos6中光盤的本地yum源默認httpd程序包是2.2版本的虏束,而centos7中默認的httpd程序包2.4版本的,centos7要安裝httpd-2.2就要下載好源碼包和對應的依賴包進行編譯安裝厦章,安裝之前先簡單講解一下httpd的這三種處理模型镇匀,需要注意的是httpd-2.2的event事件驅(qū)動模型還只是測試使用功能并且不支持同時編譯多個模塊,每次只能選定一個模塊袜啃。
其對應的配置文件 為:/etc/httpd/conf/httpd.conf
prefork:多進程模型汗侵,每個進程響應一個請求,一個主進程負責生成N個子進程及回收子進程群发,創(chuàng)建套接字晰韵,接收請求,并將其派發(fā)給某子進程進行處理熟妓,每個子進程處理一個請求即使沒有用戶請求也會預先生成幾個空閑進程雪猪,隨時等待用于響應用戶請求,最大空閑進程數(shù)不能超過1024個起愈。
prefork的配置
<IfModule prefork.c>
StartServers 8 進程啟動時創(chuàng)建多少個空閑子進程
MinSpareServers 5 最少空閑進程數(shù)
MaxSpareServers 20 最大空間進程數(shù)
ServerLimit 256 生命周期內(nèi)只恨,最多允許多少個子進程存在
MaxClients 256 最多所允許創(chuàng)建的子進程數(shù)量
MaxRequestsPerChild 4000 每個子進程最多允許處理個多少個請求后就被強制退出
worker:多進程多線程模型,每線程處理一個用戶請求抬虽,一個主進程負責生成多個子進程官觅,每個子進程生成多個線程,主進程負責創(chuàng)建套接字阐污,接收請求休涤,并將其派發(fā)給某子進程進行處理,每個線程負責響應用戶請求疤剑。
并發(fā)響應數(shù)量:m*n(m:子進程數(shù)量滑绒,n:每個子進程所能創(chuàng)建的最大線程數(shù)量)
worker的配置:
<IfModule worker.c>
StartServers 4 進程啟動時創(chuàng)建多少個空閑子進程
MaxClients 300 最多所允許創(chuàng)建的子進程數(shù)量
MinSpareThreads 25 最少空閑進程數(shù)
MaxSpareThreads 75 最大空間進程數(shù)
ThreadsPerChild 25 每個子進程生成多少個線程
MaxRequestsPerChild 0 每個子進程最多允許處理個多少個請求后就被強制退出,0表示不限制
event:事件驅(qū)動模型闷堡,多進程模型,每個進程響應多個請求疑故,一個主進程負責生成子進程杠览,創(chuàng)建套接字,接收請求纵势,并將其派發(fā)給某子進程進行處理踱阿,每個子進程基于事件驅(qū)動機制直接響應多個請求。
并發(fā)響應數(shù)量:m*n(m:子進程數(shù)量钦铁,n:每個子進程響應的請求數(shù)量)
在httpd-2.2中未對event給出配置模版软舌,并且只是測試功能,不建議使用牛曹。
在centos7系統(tǒng)中編譯安裝httpd-2.2要先下載好依賴包(官方網(wǎng)站提示apr/apr-util版本要1.4(含)版本以上)和源碼包佛点,下載過程不做表述可以從官網(wǎng)中自行下載,再安裝開發(fā)環(huán)境包組才能編譯安裝源碼包黎比。
1.安裝依賴包:zlib-devel
[root@zcy520ooooo ~]# yum install zlib-devel
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.cn99.com
http://centos.ustc.edu.cn/centos/7.5.1804/os/x86_64/repodata/repomd.xml: [Errno 12] Timeout on http://centos.ustc.edu.cn/centos/7.5.1804/os/x86_64/repodata/repomd.xml: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
正在嘗試其它鏡像超营。
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
updates/7/x86_64/primary_db | 6.0 MB 00:00:01
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 zlib-devel.x86_64.0.1.2.7-17.el7 將被 安裝
--> 解決依賴關系完成
依賴關系解決
==============================================================================================================
Package 架構(gòu) 版本 源 大小
==============================================================================================================
正在安裝:
zlib-devel x86_64 1.2.7-17.el7 base 50 k
事務概要
==============================================================================================================
安裝 1 軟件包
總下載量:50 k
安裝大小:132 k
Is this ok [y/d/N]: y
Downloading packages:
zlib-devel-1.2.7-17.el7.x86_64.rpm | 50 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安裝 : zlib-devel-1.2.7-17.el7.x86_64 1/1
驗證中 : zlib-devel-1.2.7-17.el7.x86_64 1/1
已安裝:
zlib-devel.x86_64 0:1.2.7-17.el7
完畢阅虫!
2.安裝開發(fā)環(huán)境包組
[root@zcy520ooooo ~]# yum groupinstall "Development Tools" "ServerPlatform Development" -y
已加載插件:fastestmirror
沒有安裝組信息文件
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.cn99.com
......
已安裝:
autoconf.noarch 0:2.69-11.el7 automake.noarch 0:1.13.4-3.el7
bison.x86_64 0:3.0.4-1.el7 byacc.x86_64 0:1.9.20130304-3.el7
cscope.x86_64 0:15.8-10.el7 ctags.x86_64 0:5.8-13.el7
diffstat.x86_64 0:1.57-4.el7 doxygen.x86_64 1:1.8.5-3.el7
elfutils.x86_64 0:0.170-4.el7 flex.x86_64 0:2.5.37-3.el7
gcc.x86_64 0:4.8.5-28.el7_5.1 gcc-c++.x86_64 0:4.8.5-28.el7_5.1
gcc-gfortran.x86_64 0:4.8.5-28.el7_5.1 git.x86_64 0:1.8.3.1-14.el7_5
indent.x86_64 0:2.2.11-13.el7 intltool.noarch 0:0.50.2-7.el7
libtool.x86_64 0:2.4.2-22.el7_3 patch.x86_64 0:2.7.1-10.el7_5
patchutils.x86_64 0:0.3.3-4.el7 rcs.x86_64 0:5.9.0-5.el7
redhat-rpm-config.noarch 0:9.1.0-80.el7.centos rpm-build.x86_64 0:4.11.3-32.el7
rpm-sign.x86_64 0:4.11.3-32.el7 subversion.x86_64 0:1.7.14-14.el7
swig.x86_64 0:2.0.10-5.el7 systemtap.x86_64 0:3.2-8.el7_5
作為依賴被安裝:
apr.x86_64 0:1.4.8-3.el7_4.1 apr-util.x86_64 0:1.5.2-6.el7
avahi-libs.x86_64 0:0.6.31-19.el7 boost-date-time.x86_64 0:1.53.0-27.el7
boost-system.x86_64 0:1.53.0-27.el7 boost-thread.x86_64 0:1.53.0-27.el7
bzip2.x86_64 0:1.0.6-13.el7 cpp.x86_64 0:4.8.5-28.el7_5.1
dwz.x86_64 0:0.11-3.el7 dyninst.x86_64 0:9.3.1-1.el7
efivar-libs.x86_64 0:31-4.el7 emacs-filesystem.noarch 1:24.3-20.el7_4
gdb.x86_64 0:7.6.1-110.el7 gettext-common-devel.noarch 0:0.19.8.1-2.el7
gettext-devel.x86_64 0:0.19.8.1-2.el7 glibc-devel.x86_64 0:2.17-222.el7
glibc-headers.x86_64 0:2.17-222.el7 gnutls.x86_64 0:3.3.26-9.el7
kernel-debug-devel.x86_64 0:3.10.0-862.14.4.el7 kernel-headers.x86_64 0:3.10.0-862.14.4.el7
libdwarf.x86_64 0:20130207-4.el7 libgfortran.x86_64 0:4.8.5-28.el7_5.1
libgnome-keyring.x86_64 0:3.12.0-1.el7 libmodman.x86_64 0:2.0.1-8.el7
libmpc.x86_64 0:1.0.1-3.el7 libproxy.x86_64 0:0.4.11-11.el7
libquadmath.x86_64 0:4.8.5-28.el7_5.1 libquadmath-devel.x86_64 0:4.8.5-28.el7_5.1
libstdc++-devel.x86_64 0:4.8.5-28.el7_5.1 m4.x86_64 0:1.4.16-10.el7
mokutil.x86_64 0:12-2.el7 mpfr.x86_64 0:3.1.1-4.el7
neon.x86_64 0:0.30.0-3.el7 nettle.x86_64 0:2.7.1-8.el7
pakchois.x86_64 0:0.4-10.el7 perl-Data-Dumper.x86_64 0:2.145-3.el7
perl-Error.noarch 1:0.17020-2.el7 perl-Git.noarch 0:1.8.3.1-14.el7_5
perl-TermReadKey.x86_64 0:2.30-20.el7 perl-Test-Harness.noarch 0:3.28-3.el7
perl-Thread-Queue.noarch 0:3.02-2.el7 perl-XML-Parser.x86_64 0:2.41-10.el7
perl-srpm-macros.noarch 0:1-8.el7 rsync.x86_64 0:3.1.2-4.el7
subversion-libs.x86_64 0:1.7.14-14.el7 systemtap-client.x86_64 0:3.2-8.el7_5
systemtap-devel.x86_64 0:3.2-8.el7_5 systemtap-runtime.x86_64 0:3.2-8.el7_5
trousers.x86_64 0:0.3.14-2.el7 unzip.x86_64 0:6.0-19.el7
zip.x86_64 0:3.0-11.el7
作為依賴被升級:
libgcc.x86_64 0:4.8.5-28.el7_5.1 libgomp.x86_64 0:4.8.5-28.el7_5.1 libstdc++.x86_64 0:4.8.5-28.el7_5.1
3.下載源碼包及依賴包并解壓
[root@zcy520ooooo ~]# ls
anaconda-ks.cfg apr-1.6.5.tar.gz apr-util-1.6.1.tar.gz httpd-2.2.32.tar.gz
-------------------------分割線--------------------------
[root@zcy520ooooo ~]# tar -xvf apr-1.6.5.tar.gz
[root@zcy520ooooo ~]# tar -xf apr-util-1.6.1.tar.gz
[root@zcy520ooooo ~]# tar xf httpd-2.2.32.tar.gz
[root@zcy520ooooo ~]# ls
anaconda-ks.cfg apr-1.6.5.tar.gz apr-util-1.6.1.tar.gz httpd-2.2.32.tar.gz
apr-1.6.5 apr-util-1.6.1 httpd-2.2.32
#依次用tar命令解壓到當前目錄即可
4.編譯安裝依賴包apr/apr-util
編譯安裝apr-1.6.5
[root@zcy520ooooo ~]# cd apr-1.6.5
[root@zcy520ooooo apr-1.6.5]# ./configure --prefix=/usr/local/apr-httpd/ --with-apr=/usr/local/apr-httpd/
...
[root@zcy520ooooo apr-1.6.5]# make && make install
...
done
/usr/bin/install -c -m 644 build/apr_rules.out /usr/local/apr-util-httpd//build-1/apr_rules.mk
/usr/bin/install -c -m 644 /root/apr-1.6.5/build/apr_common.m4 /usr/local/apr-util-httpd//build-1
/usr/bin/install -c -m 644 /root/apr-1.6.5/build/find_apr.m4 /usr/local/apr-util-httpd//build-1
/usr/bin/install -c -m 755 apr-config.out /usr/local/apr-util-httpd//bin/apr-1-config
-----------------------分割線----------------------
編譯安裝apr-util-1.6.5
[root@zcy520ooooo apr-1.6.5]# cd
[root@zcy520ooooo ~]# cd apr-util-1.6.1
[root@zcy520ooooo apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util-httpd/ --with-apr=/usr/local/apr-httpd/
[root@zcy520ooooo apr-util-1.6.1]# make && make install
......
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
/usr/bin/install -c -m 644 aprutil.exp /usr/local/apr-util-httpd//lib
/usr/bin/install -c -m 755 apu-config.out /usr/local/apr-util-httpd//bin/apu-1-config
--------------------------------分割線-------------------------
#如果在安裝apr-util時出現(xiàn)以下情況:
xml/apr_xml.c:35:19: 致命錯誤:expat.h:沒有那個文件或目錄
#include <expat.h>
^
編譯中斷演闭。
make[1]: *** [xml/apr_xml.lo] 錯誤 1
make[1]: 離開目錄“/root/apr-util-1.6.1”
make: *** [all-recursive] 錯誤 1
#此時需要使用yum安裝expat-devel,安裝完成后颓帝,重新make && make install 即可
5.編譯安裝httpd-2.2
[root@zcy520ooooo apr-util-1.6.5]# cd ..
[root@zcy520ooooo ~]# cd httpd-2.2.32
[root@zcy520ooooo httpd-2.2.32]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd/ --enable-so --enable-deflate --enable-headers --enable-rewrite --with-mpm=prefork --with-apr=/usr/local/apr-httpd/ --with-apr-util=/usr/local/apr-util-httpd/ --with-included-apr
...
Installing build system files
mkdir /usr/local/apache/build
Installing man pages and online manual
mkdir /usr/local/apache/man
mkdir /usr/local/apache/man/man1
mkdir /usr/local/apache/man/man8
mkdir /usr/local/apache/manual
make[1]: Leaving directory `/root/httpd-2.2.32'
一些參數(shù)的解析:
--prefix:安裝路徑
--sysconfdir:指定配置文件路徑
--enable-so:DSO兼容米碰,DSO=Dynamic Shared Object,動態(tài)共享對象购城,可實現(xiàn)模塊動態(tài)生效
--enable-deflate:支持壓縮功能吕座;需要zlib-devel包支持
--enable-headers:提供允許對HTTP請求頭的控制
--enable-rewrite:提供基于URL規(guī)則的重寫功能
--with-mpm:設置默認啟用的MPM模式,{prefork|worker|event}
--with-apr:apr安裝路徑
--with-apr-util:apr-util安裝的路徑
--with-included-apr:由于我的CENTOS 是 64bit的工猜,在編譯配置中并沒有指定 --with-included-apr的具體引用路徑米诉,導致找不到相應的庫文件,而 --with-included-apr 默認是查找/usr/lib下的庫文件篷帅,而史侣,正確的引用應該是/usr/lib64下的
6.在安裝目錄/usr/local/apache下檢查主配置文件語法錯誤
[root@zcy520ooooo ~]# cd /usr/local/apache/
[root@zcy520ooooo apache]# bin/apachectl -t
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
Syntax OK
#ok表示正常
7.啟動httpd服務
[root@zcy520ooooo apache]# /usr/local/apache/bin/httpd -k start
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
#這個報錯是主機名沒有設置好
#要更改主機名可以這樣做:
[root@zcy520ooooo apache]# vim /etc/httpd/httpd.conf
...
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
#ServerName www.example.com:80
ServerName 192.168.80.27 #在此處添加主機IP地址或域名,可不加端口號
...
8.查看80端口是否監(jiān)聽
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
ESTAB 0 0 192.168.80.27:22 192.168.80.47:53813
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
9.關閉iptables和selinux之后測試是否安裝成功
[root@zcy520ooooo ~]# iptables -F
[root@zcy520ooooo ~]# setenforce 0
[root@zcy520ooooo ~]# getenforce
Permissive
10.輸入主機ip測試
出現(xiàn)這個頁面就證明安裝并啟動成功了.
httpd2.2默認是prefork方式下工作的魏身,要在幾個工作方式中切換惊橱,則需要修改etc/sysconfig/httpd下對應的配置,并重啟服務即可箭昵。
[root@zcy520ooooo ~]# vim /etc/sysconfig/httpd
# Configuration file for the httpd service.
#
# The default processing model (MPM) is the process-based
# 'prefork' model. A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# Configuration file for the httpd service.
#
# The default processing model (MPM) is the process-based
# 'prefork' model. A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# The service must be stopped before changing this variable.
#
#HTTPD=/usr/sbin/httpd.worker
HTTPD=/usr/sbin/httpd.event #將該選項切換到對應模式税朴,重啟服務即可
#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
#OPTIONS=
#
# By default, the httpd process is started in the C locale; to
# change the locale in which the server runs, the HTTPD_LANG
# variable can be set.
#
#HTTPD_LANG=C
#
# By default, the httpd process will create the file
1.默認的prefork方式:
修改/etc/sysconfig/httpd
[root@zcy520ooooo ~]# vim /etc/sysconfig/httpd
# Configuration file for the httpd service.
#
# The default processing model (MPM) is the process-based
# 'prefork' model. A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# The service must be stopped before changing this variable.
#
#HTTPD=/usr/sbin/httpd.worker
HTTPD=/usr/sbin/httpd #將該選項切換到對應模式,默認為prefork方式,重啟服務即可
#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
#OPTIONS=
#
# By default, the httpd process is started in the C locale; to
# change the locale in which the server runs, the HTTPD_LANG
# variable can be set.
#
#HTTPD_LANG=C
#
# By default, the httpd process will create the file
重啟服務
[root@zcy520ooooo ~]# service httpd restart
停止 httpd: [確定]
正在啟動 httpd:httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[確定]
查看httpd啟動模式
[root@zcy520ooooo ~]# ps aux | grep httpd
root 1598 0.2 0.3 11188 3340 ? Ss 23:19 0:00 /usr/sbin/httpd
apache 1600 0.0 0.2 11188 2136 ? S 23:19 0:00 /usr/sbin/httpd
apache 1601 0.0 0.2 11188 2164 ? S 23:19 0:00 /usr/sbin/httpd
apache 1602 0.0 0.2 11188 2136 ? S 23:19 0:00 /usr/sbin/httpd
apache 1603 0.0 0.2 11188 2136 ? S 23:19 0:00 /usr/sbin/httpd
apache 1604 0.0 0.2 11188 2136 ? S 23:19 0:00 /usr/sbin/httpd
apache 1605 0.0 0.2 11188 2136 ? S 23:19 0:00 /usr/sbin/httpd
apache 1606 0.0 0.2 11188 2136 ? S 23:19 0:00 /usr/sbin/httpd
apache 1607 0.0 0.2 11188 2136 ? S 23:19 0:00 /usr/sbin/httpd
root 1609 0.0 0.0 6044 808 pts/0 S+ 23:19 0:00 grep httpd
2.worker工作方式
修改etc/sysconfig/httpd文件中HTTPD=/usr/sbin/httpd.worker
驗證worker工作方式
[root@zcy520ooooo ~]# ps aux | grep httpd
root 1637 0.0 0.3 11364 3508 ? Ss 23:22 0:00 /usr/sbin/httpd.worker
apache 1639 0.5 0.2 289096 2916 ? Sl 23:22 0:00 /usr/sbin/httpd.worker
apache 1640 0.0 0.2 289096 2932 ? Sl 23:22 0:00 /usr/sbin/httpd.worker
apache 1641 0.0 0.0 0 0 ? Z 23:22 0:00 [httpd.worker] <defunct>
apache 1642 0.0 0.2 289096 2932 ? Sl 23:22 0:00 /usr/sbin/httpd.worker
3.event工作方式
修改/etc/sysconfig/httpd文件中的HTTPD=/user/sbin/httpd.event
重啟服務
驗證event工作方式
[root@zcy520ooooo ~]# ps aux | grep httpd
root 1782 0.0 0.3 11352 3512 ? Ss 23:24 0:00 /usr/sbin/httpd.event
apache 1784 0.0 0.2 289084 2812 ? Sl 23:24 0:00 /usr/sbin/httpd.event
apache 1785 0.0 0.2 289084 2816 ? Sl 23:24 0:00 /usr/sbin/httpd.event
apache 1786 0.0 0.2 289084 2828 ? Sl 23:24 0:00 /usr/sbin/httpd.event
root 1869 1.0 0.0 6044 808 pts/0 S+ 23:24 0:00 grep httpd
簡述request報文請求方法和狀態(tài)響應碼
一個http事務由請求(request)和響應(response)兩種狀態(tài)正林,并在響應中返回各種響應碼泡一,可以從不同的狀態(tài)碼中了解不同的響應狀態(tài)。
報文語法格式:
requrest報文
<method> <request-URL> <version>
<HEADERS>
<entity-body>
response報文
<version> <status> <reason_phrase>
<HEADERS>
<entity-body>
method:請求方法
標明客戶端希望服務器對資源執(zhí)行的動作觅廓,如下為常用method的解釋:
GET:從服務器獲取一個資源
HEAD:只從服務器獲取文檔的響應首部
POST:向服務器發(fā)送要處理的數(shù)據(jù)
PUT:將請求的主體部分存儲在服務器上
DELETE:請求刪除服務器上指定的文檔
TRACE:追蹤請求到達服務器中間經(jīng)過的代理服務器
OPTIONS:請求服務器返回對指定資源支持使用的請求方法
version:HTTPD的版本號
HTTP/<major>.<minor>,由主版本和次版本號組成
status:狀態(tài)碼
由三位數(shù)字組成鼻忠,如200,301杈绸,302帖蔓,404,502;標記請求處理過程中發(fā)生的情況瞳脓,不同的狀態(tài)碼表示的意思不一樣塑娇。
1xx:100-101,信息提示
2xx:200-206劫侧,成功
3xx:300-305埋酬,重定向
4xx:400-415,錯誤類信息板辽,客戶端錯誤
5xx:500-505奇瘦,錯誤類信息,服務器端錯誤
常用的狀態(tài)碼:
200:成功劲弦,請求的所有數(shù)據(jù)通過響應報文的entity-body部分發(fā)送;OK
301:請求的URL指向的資源已經(jīng)被刪除;但在響應報文中通過首部Location指明了資源現(xiàn)在所處的新位置;Moved Permanently
302:與301相似,但在響應報文中通過Location指明資源現(xiàn)在所處臨時新位置;Found
304:客戶端發(fā)出了條件式請求醇坝,但服務器上的資源未曾發(fā)生改變邑跪,則通過響應此響應狀態(tài)碼通知客戶端; Not Modified
401:需要輸入帳號和密碼認證方能訪問資源;Unauthorized
403:請求被禁止;Forbidden
404:服務器無法找到客戶端請求的資源; Not Found
500:服務器內(nèi)部錯誤; Internal Server Error
502:代理服務器從后端服務器收到了一條偽響應; Bad Gateway
reason-phrase:狀態(tài)碼所標記的狀態(tài)的簡要描述
HEADERS:首部
每個請求或響應報文可包含任意個首部;每個首部都有首部名稱,后面跟一個冒號呼猪,而后跟上一個可選空格画畅,接著是一個值
首部的分類,通用首部宋距,請求首部轴踱,響應首部,實體首部谚赎,擴展首部等等淫僻,
格式為:Name:Value,常見的首部格式如下:通用首部:
Date:報文的創(chuàng)建時間
Connection:連接狀態(tài),如keep-alive,close
Via:顯示報文經(jīng)過的中間節(jié)點
Cache-Control:控制緩存
Pragma:no-cache,相當于Cache-Control:no-cache
請求首部:
Accept:通過服務器自己可接受的媒體類型(MIME,text/html,appliction/javascript,images/jpeg);
Accept-Charset:瀏覽器指明自己接收的字符集
Accept-Encoding:接受編碼格式壶唤,一般指壓縮機制雳灵,如gzip、deflate闸盔、sdch
Accept-Language:接受的語言
Client-IP:客戶端ip地址
Host:請求的服務器名稱和端口號
Referer:包含當前正在請求的資源的上一級資源
User-Agent:客戶端代理
條件式請求首部:
Expect:服務器指明該實體什么時候過期
If-Modified-Since:自從指定的時間之后悯辙,請求的資源是否發(fā)生過修改;
If-Unmodified-Since:自從指定的時間之后,請求的資源是否沒有修改
If-None-Match:本地緩存中存儲的文檔的ETag標簽是否與服務器文檔的Etag不匹配躲撰;
If-Match:沒改變针贬,才執(zhí)行請求
安全請求首部:
Authorization:向服務器發(fā)送認證信息,如帳號和密碼
Cookie:客戶端向服務器發(fā)送cookie
代理請求首部:
Proxy-Authorization:向代理服務器認證
安全響應首部:
Set-Cookie:向客戶端設置cookie
Set-Cookie2:同上拢蛋,版本不一樣
WWW-Authenticate:來自服務器對客戶端的質(zhì)詢認證表單
實體首部:
Allow:列出對此實體可使用的請求方法
Location:告訴客戶端真正的實體位于何處
Content-Length:主體的長度
Content-Location:實體真正所處位置
Content-Type:主體的對象類型
緩存相關:
ETag:實體的擴展標簽
Expires:實體的過期時間
Last-Modified:最后一次
entity-body:請求時附加的數(shù)據(jù)或響應時附加的數(shù)據(jù)
curl的常用選項:
-A/--user-agent <string> 設置用戶代理發(fā)送給服務器
--basic 使用HTTP基本認證
--tcp-nodelay 使用TCP_NODELAY選項
-e/--referer <URL> 來源網(wǎng)址
--cacert <file> CA證書 (SSL)
--compressed 要求返回是壓縮的格式
-H/--header <line>自定義首部信息傳遞給服務器
-I/--head 只顯示響應報文首部信息
--limit-rate <rate> 設置傳輸速度
-u/--user <user[:password]>設置服務器的用戶和密碼
-0/--http1.0 使用HTTP 1.0
用法:curl [options] [URL...]
-----------------------分割線--------------------------
[root@localhost ~]# curl -I www.baidu.com
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Tue, 06 Nov 2018 04:24:39 GMT
Content-Type: text/html
Content-Length: 277
Last-Modified: Mon, 13 Jun 2016 02:50:23 GMT
Connection: Keep-Alive
ETag: "575e1f6f-115"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Accept-Ranges: bytes
詳細描述httpd虛擬主機桦他、站點訪問控制、基于用戶的訪問控制瓤狐、持久鏈接等應用配置實例
在httpd-2.2中的相關配置文件路徑如下:
配置文件:
/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.虛擬主機:一個物理機可以服務多個站點瞬铸,每一個站點可以通過一個或多個虛擬主機來實現(xiàn),httpd有三種模式:
基于IP:通過IP地址訪問不同主機础锐,為每個虛擬主機準備至少一個IP地址
基于Port:通過Port訪問不同主機嗓节,為每個虛擬主機準備至少一個Port
基于FQDN主機名:通過主機名訪問不同主機,為每個虛擬主機準備至少一個主機名在httpd2.2中開啟虛擬主機需要先關閉中心主機(Main server)皆警,在配置文件中注釋掉DocumentRoot指令即可
虛擬主機的配置方法:
<VirtualHost IP:PORT>
ServerName FQDN
DocumentRoot ""
</VirtualHost>
這三種模式可以一起混用拦宣,可以在/etc/httpd/conf/httpd.conf主配置文件中寫入配置信息,也可以在/etc/httpd/conf.d/virtualhost.conf模塊化的配置文件路徑下單獨寫入配置信姓。
- 基于ip的虛擬機要保證一個主機中有多個ip地址鸵隧。
- 基于port的虛擬機要在配置文件中指明要監(jiān)聽的端口。
- 基于主機名的虛擬機在host文件或DNS服務器中意推,支配好主機域名豆瘫,保證不同的域名都訪問相同的ip地址。
配置文件格式如下:
[root@localhost ~]# vim /etc/httpd/conf.d/virtualhost.conf
Listen 10086 指明要監(jiān)聽的端口
<VirtualHost 192.168.199.130:80> #基于IP的訪問控制菊值,保證主機有此IP
ServerName "www.one.com"
DocumentRoot "/var/www/one" #這個目錄為服務的根目錄要創(chuàng)建好
</virtualHost>
<VirtualHost 192.168.199.140:80> #基于IP的訪問控制外驱,保證主機有此IP
ServerName "www.two.com"
DocumentRoot "/var/www/two"
</VirtualHost>
<VirtualHost 192.168.199.130:10086> #基于Port的訪問控制,要先listen port
ServerName "www.three.com"
DocumentRoot "/var/www/three"
</VirtualHost>
<VirtualHost 192.168.199.130:80> #基于主機名的訪問控制
ServerName "www.four.com" #這個域要能解析腻窒,可以寫入/etc/hosts配置文件或DNS解析文件中
DocumentRoot "/var/www/four"
</VirtualHost>
<VirtualHost 192.168.199.130:80> #同上
ServerName "www.five.com"
DocumentRoot "/var/www/five"
</VirtualHost>
<VirtualHost 192.168.199.150:10086> #基于IP和Port混用的訪問控制
ServerName "www.six.com"
DocumentRoot "/var/www/six"
</VirtualHost>
2.站點訪問控制:文件系統(tǒng)路徑和URL路徑兩種機制對哪些資源進行訪問控制昵宇,可以來源的IP地址來控制哪些地址,可以訪問資源哪些IP地址不能訪問儿子,httpd2.2的配置格式如下:
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Order allow,deny #order:生效次序瓦哎,指明只有allow的地址可以訪問,其他的都deny
Allow from all #指明允許的范圍柔逼,匹配是先匹配小范圍再匹配大范圍
</Directory>
#在配置文件/etc/httpd/conf/httpd.conf中加入如下配置并保存退出重啟服務
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from 192.168.199.130
</Directory>
--------------------------分割線--------------------------
[root@localhost www]# service httpd restart
停止 httpd: [確定]
正在啟動 httpd:httpd.event: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[Mon Nov 05 21:38:18 2018] [warn] VirtualHost 192.168.199.130:80 overlaps with VirtualHost 192.168.199.130:80, the first has precedence, perhaps you need a NameVirtualHost directive
[Mon Nov 05 21:38:18 2018] [warn] VirtualHost 192.168.199.130:80 overlaps with VirtualHost 192.168.199.130:80, the first has precedence, perhaps you need a NameVirtualHost directive
[確定]
-------------------------------分割線-----------------------------
[root@localhost ~]# curl http://192.168.199.130/cgi-bin/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title> #顯示禁止了蒋譬,說明訪問受限了
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /cgi-bin/
on this server.</p>
<hr>
<address>Apache/2.2.15 (CentOS) Server at 192.168.199.130 Port 80</address>
</body></html>
3.基于用戶的訪問控制:對于網(wǎng)站中的一些資源,需要只對特定的用戶開放卒落,可以通過用戶的訪問控制來實現(xiàn) 羡铲。有質(zhì)詢和認證兩種方式:
質(zhì)詢:服務器用401響應碼拒絕客戶端請求,并說明要求客戶端提供帳號和密碼
認證:客戶端用戶填入帳號和密碼后再次發(fā)送請求報文儡毕,認證通過時也切,則服務器發(fā)送響應的資源扑媚,有basic基本認證和digest摘要認證兩種。這里以basic認證做實驗雷恃。
basic認證的配置格式如下:
<Directory "/somepath/to/file"> #需要訪問控制的文件路徑
Options None
AllowOverride None
AuthType Basic #認證方式
AuthName "String" #認證提示信息
AuthUserFile "/PATH/TO/HTTPD_USER_PASSWD_FILE" #認證用戶的文件路徑
Require user username1 username2 ... #允許訪問的用戶
</Directory>
# Require valid-user 表示允許所有AuthUserFile 文件中所有的帳號
# AuthGroupFile "/PATH/TO/HTTPD_GROUP_FILE" 基于用戶做訪問控制
# Require group grpname1 grpname2 ...同上
登陸使用的帳號為虛擬帳號而非系統(tǒng)的帳號疆股,而且httpd是明文傳輸?shù)模允褂胔tpasswd命令來創(chuàng)建帳號文件格式如下:
htpasswd [options] /PATH/TO/HTTPD_PASSWD_FILE username
-c:自動創(chuàng)建此處指定的文件倒槐,因此旬痹,僅應該在此文件不存在時使用
-m:md5格式加密
-s:sha格式加密
-D:刪除指定用戶
[root@localhost ~]# mkdir /etc/httpd/users
[root@localhost ~]# htpasswd -c -m /etc/httpd/users/.htpasswd tom
New password:
Re-type new password:
Adding password for user tom
[root@localhost ~]# htpasswd -m /etc/httpd/users/.htpasswd jerry
New password:
Re-type new password:
Adding password for user jerry
[root@localhost ~]# cat /etc/httpd/users/.htpasswd
tom:$apr1$sNR5ph.L$do0VUPGF54k4TISodgGqz.
jerry:$apr1$Mq/ekV8h$urpmRqZzmt191DwjSeSoN1
創(chuàng)建要控制的文件路徑及文件并在/etc/httpd/conf.d/admin.conf文件中添加配置
[root@localhost ~]# mkdir /var/www/html/date
[root@localhost ~]# vim /var/www/html/date/index.html
<h1>hello admin</h1>
[root@localhost ~]# vim /etc/httpd/conf.d/admin.conf
NameVirtualHost 192.168.199.130:80 #http-2.2使用基于FQDN的虛擬主機時,需要事先使用此指令
<VirtualHost 192.168.199.130:80>
ServerName www.magedu.com
DocumentRoot "/var/www/html/date"
<Directory "/var/www/html/date">
Options None
AllowOverride None
AuthType basic
AuthName "please enter the user"
AuthUserFile "/etc/httpd/users/.htpasswd" #htpasswd命令保存的文件路徑
Require user tom #允許的用戶讨越,在htpasswd文件中必須存在
</Directory>
</VirtualHost>
保存退出后两残,檢查語法錯誤并重啟服務
[root@localhost ~]# httpd -t
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
Syntax OK
[root@localhost ~]# service httpd restart
停止 httpd: [確定]
正在啟動 httpd:httpd.event: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[確定]
最后測試一下
4.持久鏈接:服務器連續(xù)建立后,每個資源獲取完成后不全斷開連接把跨,而是繼續(xù)等待其他資源請求的進行人弓,通過連接次數(shù)或等待時間的限制來斷開連接,可以在/etc/httpd/conf.d/創(chuàng)建xxx.conf來配置着逐,相關參數(shù)如下:
KeepAlive On|Off 開啟或關閉保持連接
KeepAliveTimeout 15 等等超時時間(超過時間就關閉)
MaxKeepAliveRequests 100 (最大允許請求數(shù)量崔赌,和Timeout兩者認證先滿足條件就關閉)
可以使用telnet命令進行測試,格式如下:
telnet WEB_SERVER_IP PORT #服務器的ip和端口
GET /URL HTTPD/1.1 #/url httpd的版本號
Host:WEB_SERVER_IP
[root@localhost ~]# vim /etc/httpd/conf.d/keepalive.conf
keepalive On
KeepAliveTimeout 15
MaxKeepAliveRequests 100
#報錯退出后可以用httpd -t檢查語法耸别,并重啟服務
------------------------------分割線-------------------------
[root@localhost ~]# telnet 192.168.199.130 80
Trying 192.168.199.130...
Connected to 192.168.199.130.
Escape character is '^]'.
GET /index.html HTTP/1.1
Host: 192.168.199.130
HTTP/1.1 200 OK
Date: Mon, 05 Nov 2018 14:40:46 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Mon, 05 Nov 2018 14:30:46 GMT
ETag: "425a1-10-56a73124ff4ad"
Accept-Ranges: bytes
Content-Length: 16
Content-Type: text/html; charset=UTF-8
KeeepAlive page
#這里連接之后不會立即斷開,會等到最大連接數(shù)或連接時長到達才會斷開.