httpd-2.4的安裝
實驗環境爲centOS6.5,由於centOS6上的庫文件及開發工具等不適合安裝httpd-2.4,所以需要手動安裝這兩個包apr和apr-util,httpd-2.4依賴1.4版本以上的apr和apr-util。下面簡單介紹一下apr。
apr簡介
apr(apache portable runtime),即apache的可移植運行環境,由於底層環境的不同,apache應用程序需要根據不同的平臺來開發,而apr消除了底層平臺的差異,apr能夠爲大多數的平臺提供所有的apr特性支持,包括BeOS,UNIX,Linux等等,apr爲這些大部分的平臺提供了一個公共的統一操作函數接口,使得不同平臺上的apache應用程序執行的接口基本都是統一一致的。
編譯安裝
下載源碼包
-rw-r--r-- 1 root root 813976 Jul 1 09:09 apr-1.5.0.tar.bz2 -rw-r--r-- 1 root root 695303 Jul 1 09:09 apr-util-1.5.3.tar.bz2 -rw-r--r-- 1 root root 4994460 Jul 1 09:09 httpd-2.4.9.tar.bz2
1)編譯安裝apr-1.5.0
[root@www ~]# tar xf apr-1.5.0.tar.bz2 [root@www ~]# cd apr-1.5.0 [root@www apr-1.5.0]# ./configure --prefix=/usr/local/apr-1.5.0 checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking target system type... x86_64-unknown-linux-gnu Configuring APR library ............. [root@www apr-1.5.0]# make && make install
2)編譯安裝apr-util-1.5.3
[root@www ~]# tar xf apr-util-1.5.3.tar.bz2 [root@www ~]# cd apr-util-1.5.3 [root@www apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util-1.5.3/ --with-apr=/usr/local/apr-1.5.0/ checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking target system type... x86_64-unknown-linux-gnu checking for a BSD-compatible install... /usr/bin/install -c checking for working mkdir -p... yes .............. [root@www apr-util-1.5.3]# make && make install
由於apr-util依賴apr所以在安裝apr-util時需要通過“--with-apr”指定已安裝的apr。
httpd還依賴pcre的開發庫pcre-devel,PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。這個也需要安裝一下。
[root@CentOS6 yum.repos.d]# yum install pcre-devel -y
2)編譯安裝httpd-2.4.9
[root@www ~]# tar xf httpd-2.4.9.tar.bz2 [root@www ~]# cd httpd-2.4.9 [root@www httpd-2.4.9]# ./configure --prefix=/usr/local/apache2.4.9 --sysconfdir=/etc/http d --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/ usr/local/apr-1.5.0 --with-apr-util=/usr/local/apr-util-1.5.3 --enable-mpms-shared=all --with -mpm=event --enable-modules=most ........... [root@www httpd-2.4.9]# make && make install
簡單介紹一下涉及到的各參數:
--prefix=/usr/local/apache2.4.9 #應用程序安裝路徑
--sysconfdir=/etc/httpd #配置文件的安裝路徑
--enable-so #支持DSO動態裝載模塊
--enable-ssl #要編譯啓用ssl模塊(前提是需要安裝openssl-devel)
--enable-cgi #啓用CGI模塊(默認就啓用)
--enable-rewrite #URL重寫(把用戶訪問的URL由服務器自動的改成另外一個URL,這是一個非常有用的機
#制)
--with-zlib #這是一個壓縮庫(專用於網絡傳輸)
--with-pcre #使用增強的perl正則表達式分析工具(使用這項需要安裝pcre-devel,pcre:正則表達式分
#析器)
--with-apr=/usr/local/apr #指明apr的目錄(若apr在特殊路徑下)
--with-apr-util=/usr/local/apr-util/ #指明apr-util路徑(若apr-util在特殊路徑下)
--enable-mpms-shared=all #把所有的mpm模塊都編譯進來而且是共享模塊
--with-mpm=event #默認使用的mpm模塊
--enable-modules=most|all #還有很多其他模塊,其他的動態可裝載模塊需要編譯哪些
#(all:所有都編譯,most:編譯一些常用的模塊)
[root@www profile.d]# vim /etc/profile.d/apache.sh export PATH=/usr/local/apache2.4.9/bin:$PATH [root@www profile.d]# . /etc/profile.d/apache.sh
導出頭文件和man幫助文檔
[root@CentOS6 extra]# ln -sv /usr/local/apache-2.4.9/include/ /usr/include/httpd-2.4 `/usr/include/httpd-2.4' -> `/usr/local/apache-2.4.9/include/' [root@CentOS6 extra]# vim /etc/man.config MANPATH /usr/local/apache-2.4.9/man
將apache應用程序bin目錄添加到path環境變量中。由於是編譯安裝,所以在/etc/init.d目錄下沒有對應的腳本文件,需要自己編寫,就不編寫了,直接將rpm包安裝後自動生成的腳本文件httpd複製過來吧,改一下其中的幾個參數。
#!/bin/bash # # httpd Startup script for the Apache HTTP Server # # chkconfig: - 85 15 # description: The Apache HTTP Server is an efficient and extensible \ # server implementing the current HTTP standards. # processname: httpd # config: /etc/httpd/httpd.conf # pidfile: /usr/local/apache2.4.9/logs/httpd.pid # ### BEGIN INIT INFO # Provides: httpd # Required-Start: $local_fs $remote_fs $network $named # Required-Stop: $local_fs $remote_fs $network # Should-Start: distcache # Short-Description: start and stop Apache HTTP Server # Description: The Apache HTTP Server is an extensible server # implementing the current HTTP standards. ### END INIT INFO # Source function library. . /etc/rc.d/init.d/functions #if [ -f /etc/sysconfig/httpd ]; then # . /etc/sysconfig/httpd #fi # Start httpd in the C locale by default. HTTPD_LANG=${HTTPD_LANG-"C"} # This will prevent initlog from swallowing up a pass-phrase prompt if # mod_ssl needs a pass-phrase from the user. INITLOG_ARGS="" # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server # with the thread-based "worker" MPM; BE WARNED that some modules may not # work correctly with a thread-based MPM; notably PHP will refuse to start. # Path to the apachectl script, server binary, and short-form for messages. apachectl=/usr/local/apache2.4.9/bin/apachectl httpd=${HTTPD-/usr/local/apache2.4.9/bin/httpd} prog=httpd pidfile=${PIDFILE-/usr/local/apache2.4.9/logs/httpd.pid} lockfile=${LOCKFILE-/var/lock/subsys/httpd} RETVAL=0 STOP_TIMEOUT=${STOP_TIMEOUT-10} # The semantics of these two functions differ from the way apachectl does # things -- attempting to start while running is a failure, and shutdown # when not running is also a failure. So we just do it the way init scripts # are expected to behave here. start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL } # When stopping httpd, a delay (of default 10 second) is required # before SIGKILLing the httpd parent; this gives enough time for the # httpd parent to SIGKILL any errant children. stop() { echo -n $"Stopping $prog: " killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} } reload() { echo -n $"Reloading $prog: " if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then RETVAL=6 echo $"not reloading due to configuration syntax error" failure $"not reloading $httpd due to configuration syntax error" else # Force LSB behaviour from killproc LSB=1 killproc -p ${pidfile} $httpd -HUP RETVAL=$? if [ $RETVAL -eq 7 ]; then failure $"httpd shutdown" fi fi echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p ${pidfile} $httpd RETVAL=$? ;; restart) stop start ;; condrestart|try-restart) if status -p ${pidfile} $httpd >&/dev/null; then stop start fi ;; force-reload|reload) reload ;; graceful|help|configtest|fullstatus) $apachectl $@ RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}" RETVAL=2 esac exit $RETVAL
apachectl=/usr/local/apache2.4.9/bin/apachectl
httpd=${HTTPD-/usr/local/apache2.4.9/bin/httpd}
pidfile=${PIDFILE-/usr/local/apache2.4.9/logs/httpd.pid}
修改了上面幾個參數,這裏編譯安裝的應用程序將pidfile放置在/usr/local/apache2.4.9/logs/,可以通過在主配置文件中通過PidFile參數修改pid文件的放置位置。
PidFile "/var/run/httpd/httpd.pid"
[root@www bin]# chkconfig --add /etc/init.d/httpd [root@www bin]# chkconfig --list httpd httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
啓動服務
[root@www logs]# service httpd start Starting httpd: [ OK ] [root@www logs]# ss -tuln | grep 80 tcp LISTEN 0 128 :::80 :::*
ok......
目錄介紹
[root@www httpd]# tree /etc/httpd /etc/httpd #配置文件目錄 ├── extra │ ├── httpd-autoindex.conf │ ├── httpd-dav.conf │ ├── httpd-default.conf #默認配置文件(包括keep-alive的配置) │ ├── httpd-info.conf #server-status頁面配置 │ ├── httpd-languages.conf #爲主機設置不同的語言 │ ├── httpd-manual.conf #訪問控制配置文件 │ ├── httpd-mpm.conf #MPM配置文件 │ ├── httpd-multilang-errordoc.conf │ ├── httpd-ssl.conf #ssl配置文件 │ ├── httpd-userdir.conf #用戶目錄配置文件 │ ├── httpd-vhosts.conf #虛擬主機配置文件 │ └── proxy-html.conf ├── httpd.conf #主配置文件,包含了extra目錄下的所有文件 ├── magic ├── mime.types └── original .......
區別於httpd-2.2,這裏對配置文件做了分離。
mpm模塊介紹
MPM(Multipath Processing Module),即多道模塊處理,通過增加服務進程數量是服務器能夠同時處理多個用戶請求。mpm的實現方式有3種:
prefork:每個進程響應一個用戶請求,預先生成多個空閒進程;
worker:啓動多個進程,每個進程生成多個線程,每個線程響應一個用戶請求;
event:基於事件驅動,啓動多個線程,每個線程響應多個請求;(event在httpd2.2中是實驗性版本,httpd2.4中正式引入)
構建MPM模塊
(1)構建MPM爲靜態模塊
在全部平臺中,MPM都可以構建爲靜態模塊。在構建時選擇一種MPM,鏈接到服務器中。如果要改變MPM,必須重新構建。爲了使用指定的MPM,請在執行configure腳本 時,使用參數 --with-mpm=NAME。NAME是指定的MPM名稱。編譯完成後,可以使用 ./httpd -l 來確定選擇的MPM。 此命令會列出編譯到服務器程序中的所有模塊,包括 MPM。
(2)構建 MPM 爲動態模塊
在Unix或類似平臺中,MPM可以構建爲動態模塊,與其它動態模塊一樣在運行時加載。 構建 MPM 爲動態模塊允許通過修改LoadModule指令內容來改變MPM,而不用重新構建服務器程序。在執行configure腳本時,使用--enable-mpms-shared選項即可啓用此特性。當給出的參數爲all時,所有此平臺支持的MPM模塊都會被安裝。還可以在參數中給出模塊列表。默認MPM,可以自動選擇或者在執行configure腳本時通過--with-mpm選項來指定,然後出現在生成的服務器配置文件中。編輯LoadModule指令內容可以選擇不同的MPM。
配置這3種MPM
在編譯安裝時,--enable-mpms-shared=all即可將3種模塊都編譯到應用程序目錄下的modules目錄中,通過 --with-mpm=mpm指定使用哪個mpm。通過httpd -l或httpd -D DUMP_MODULES查看當前已加載的模塊。
[root@www ~]# httpd -D DUMP_MODULES | grep mpm mpm_event_module (shared)
當前加載的mpm模塊是event,若要動態的切換,需要修改主配置文件/etc/httpd/httpd.conf,用如下的格式加載modules目錄下對應的mpm模塊即可。
LoadModule mpm_event_module modules/mod_mpm_event.so
關於mpm模塊的具體參數在/etc/httpd/extra/httpd-mpm.conf文件中,需要在/etc/httpd/httpd.conf中包含該文件方可生效。
Include /etc/httpd/extra/httpd-mpm.conf
簡單介紹下3種MPM的參數
# prefork MPM
<IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxRequestWorkers 250 MaxConnectionsPerChild 0 </IfModule>
# IfModule表示判斷模塊是否存在,若存在啓動後面的參數。
# StartServers:在服務啓動時就創建的用於響應用戶的進程(空閒進程)
# MinSpareServers:最少空閒進程數
# MaxSpareServers:最大空閒進程數(這個數字不應該小於StartServers)
# MaxRequestWorkers:允許同時開啓的最大服務進程數量
# MaxConnectionsPerChild:一個進程最多允許的響應數量(然後銷燬進程)
# worker MPM
<IfModule mpm_worker_module> StartServers 3 MinSpareThreads 75 MaxSpareThreads 250 ThreadsPerChild 25 MaxRequestWorkers 400 MaxConnectionsPerChild 0 </IfModule>
# StartServers: 服務啓動時就創建的用於響應用戶的進程
# MinSpareThreads: 最少空閒線程數
# MaxSpareThreads: 最大空閒線程數
# ThreadsPerChild: 每個子進程可以啓動多少個線程
# MaxRequestWorkers: 允許最大數量的工作線程(最大併發用戶請求數)
# MaxConnectionsPerChild: 每個線程最多允許處理多少個請求(0表示不限定)
# event MPM
<IfModule mpm_event_module> StartServers 3 MinSpareThreads 75 MaxSpareThreads 250 ThreadsPerChild 25 MaxRequestWorkers 400 MaxConnectionsPerChild 0 </IfModule>
# 各參數的意義與worker相同!
httpd-2.4的新特性
1)MPM支持在運行時裝載;
--enable-mpms-shared=all #把所有的mpm模塊都編譯進來而且是共享模塊
--with-mpm={prefork|worker|event} #默認使用的mpm模塊
2)支持event mpm
3)異步讀寫
4)在每模塊及每目錄分別使用不同的日誌級別
5)每請求的配置;<If>,<Elseif>
6)增強版的表達式分析器
7)毫秒級的keep alive的timeout
8)基於FQDN的虛擬主機不再需要NameVirtualHost指令;
9)支持用戶使用自定義變量
新增了一些模塊:mod_proxy_fcgi(支持使用fast CGI協議的方式連接後端的PHP服務),
mode_ratelimit(速率限定,限定用戶訪問時傳輸的限定),
mod_request(對用戶的請求做更強大的過濾功能)
mod_remoteip(對客戶端請求的ip地址作更強大的控制能力)
修改的一些配置機制:不再支持使用order, allow, deny定義基於ip的訪問控制;改爲require