前言
前面我們講解了httpd在CentOS6上(httpd-2.2)的相關功能配置,而CentOS7上采用了httpd-2.4的版本,那么httpd-2.4增加了哪些特性呢?接下來讓我們在CentOS6.6上手動編譯安裝一下httpd-2.4.9,看一下和httpd-2.2有什么不同,順便補充說一下httpd的其它功能。
環境及新特性介紹
環境介紹
系統環境:CentOS6.6
所需軟件包:apr-1.5.0.tar.bz2、apr-util-1.5.3.tar.bz2、httpd-2.4.9.tar.bz2
注意:httpd2.4需要依賴apr和arp-util 1.4以上版本
httpd-2.4新特性
①MPM支持在運行時裝載
②支持event MPM
③異步讀寫
④在每模塊及每目錄分別使用不同的日志級別
⑤每請求的配置;<If>,<Elseif>
⑥增強版的表達式分析器
⑦毫秒級的keep alive的timeout
⑧基于FQDN的虛擬主機不再需要NameVirtualHost指令
⑨支持用戶使用自定義變量
新增模塊:
mod_proxy_fcgi
mode_ratelimit
mod_request
修改的配置機制:
不再支持使用order, allow, deny定義基于ip的訪問控制,改為require
編譯安裝
編譯安裝之前請確保開發包組已安裝(Development tools、Server Platform Development)
yum groupinstall Development tools Server Platform Development -y
編譯安裝apr和apr-util
[root@scholar ~]# tar xf apr-1.5.0.tar.bz2 [root@scholar ~]# cd apr-1.5.0 [root@scholar apr-1.5.0]# ./configure --prefix=/usr/local/apr [root@scholar apr-1.5.0]# make && make install [root@scholar apr-1.5.0]# cd .. [root@scholar ~]# tar xf apr-util-1.5.3.tar.bz2 [root@scholar ~]# cd apr-util-1.5.3 [root@scholar apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/ [root@scholar apr-util-1.5.3]# make && make install
編譯安裝httpd
[root@scholar apr-util-1.5.3]# cd .. [root@scholar ~]# tar xf httpd-2.4.9.tar.bz2 [root@scholar ~]# cd httpd-2.4.9 [root@scholar httpd-2.4.9]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-deflate --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-mpms-shared=all --with-mpm=event --enable-modules=most [root@scholar httpd-2.4.9]# make && make install
編譯參數詳解: --prefix:#安裝路徑 --sysconfdir:#指定配置文件路徑 --enable-so:#DSO兼容,DSO=Dynamic Shared Object,動態共享對象,可實現模塊動態生效 --enable-ssl:#支持SSL/TLS,可實現https訪問 需已安裝openssl-devel --enable-cgi:#支持CGI腳本(默認對非線程的MPM模式開啟) --enable-rewrite:#啟用Rewrite功能,URL重寫 --enable-deflate:#支持壓縮功能 --with-zlib:#使用指定的zlib庫,不指定路徑會自動尋找 --with-pcre:#使用指定的PCRE庫,不指定路徑會自動尋找 需已安裝pcre-devel --with-apr:#指定apr安裝路徑 --with-apr-util:#指定apr-util安裝路徑 --enable-mpms-shared:#支持動態加載的MPM模塊,可選參數:all --with-mpm:#設置默認啟用的MPM模式,{prefork|worker|event} --enable-modules:#支持動態啟用的模塊,可選參數:all,most,few,reallyall #編譯之前可使用./configure --help查看各項參數
添加環境變量
為了方便命令使用需要添加環境變量
[root@scholar ~]# vim /etc/profile.d/httpd24.sh #名字自定義 export PATH=/usr/local/apache/bin:$PATH [root@scholar ~]# source /etc/profile.d/httpd24.sh #重讀環境變量
啟動服務,查看80端口是否被監聽
訪問測試,是否工作
編譯安裝默認首頁就是“It works!”,默認文件在安裝目錄的htdocs目錄下
如果二次開發就需要導出頭文件
[root@scholar ~]# ln -sv /usr/local/apache/include/ /usr/include/httpd24
導出man手冊
[root@scholar ~]# vim /etc/man.config MANPATH /usr/local/apache/man #添加編譯安裝的man位置
提供腳本
#!/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/conf/httpd.conf # config: /etc/sysconfig/httpd # pidfile: /var/run/httpd/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 #!/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/conf/httpd.conf # config: /etc/sysconfig/httpd # pidfile: /var/run/httpd/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/apache/bin/apachectl httpd=${HTTPD-/usr/local/apache/bin/httpd} prog=httpd pidfile=${PIDFILE-/usr/local/apache/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 } 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
加入服務列表,測試腳本
OK,腳本可用,至此編譯安裝工作算是完成了
httpd-2.4配置講解
配置文件
#/etc/httpd24為編譯安裝時定義的目錄 /etc/httpd24/httpd.conf # 主配置文件 /etc/httpd24/extra/httpd-default.conf # 默認配置文件(keepalive、AccessFileName等設置) /etc/httpd24/extra/httpd-userdir.conf # 用戶目錄配置文件 /etc/httpd24/extra/httpd-mpm.conf # MPM配置文件 /etc/httpd24/extra/httpd-ssl.conf # SSL配置文件 /etc/httpd24/extra/httpd-vhosts.conf # 虛擬主機配置文件 /etc/httpd24/extra/httpd-info.conf # server-status頁面配置文件
通過以上配置文件可以看出編譯安裝的httpd-2.4與rpm方式安裝的httpd-2.2除了配置文件位置不一樣,其它配置如:虛擬主機,基于用戶的訪問控制,https等配置都和上文一樣的,除了基于IP的訪問控制與上文中的不太一樣,我們在開篇的時候也說了,httpd-2.4不再支持使用order, allow, deny這些機制,而是統一使用require進行訪問控制。
例如: <Directory "/usr/local/apache/htdocs"> AllowOverride None Require ip 172.16.0.0/16 Require not ip 192.168.1.0/24 </Directory>
Require ip IPADDR #控制某特定主機的訪問 Require not ip IPADDR Require all granted #允許所有主機訪問 Require all deny #拒絕所有主機訪問 Require host HOSTNAME Require not host HOSTNAME HOSTNAME: FQDN:具體的主機 DOMAIN: 域內的所有主機
補充
最后補充一點沒講到的功能,通用功能非httpd-2.4特有
CGI功能
CGI(Common Gateway Interface),通用網關接口,被用來解釋處理來自表單的輸入信息,并在服務器產生相應的處理,或將相應的信息反饋給瀏覽器,CGI程序使網頁具有交互功能。
CGI工作原理
配置CGI
我們直接用編譯安裝的httpd-2.4.9配置
啟用cgi模塊
[root@scholar ~]# vim /etc/httpd24/httpd.conf LoadModule cgid_module modules/mod_cgid.so LoadModule cgi_module modules/mod_cgi.so
ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/" #這里我們就用默認位置
創建CGI腳本
[root@scholar ~]# vim /usr/local/apache/cgi-bin/cgi.sh #名字自定義 #!/bin/bash cat << EOF Content-Type: text/html <pre> The requested resource is $(basename $0). The datetime is $(date). </pre> EOF
修改權限,重啟服務
測試CGI
測試成功,CGI配置完成
壓縮功能
壓縮,顧名思義就是將比較大的文件壓縮為較小的文件,節約空間。應用于httpd就可以減小傳輸字節,節省流量,加快網頁加載速度,從而提高用戶體驗。
Web服務器處理HTTP壓縮的過程:
① Web服務器接收到瀏覽器的HTTP請求后,檢查瀏覽器是否支持HTTP壓縮(Accept-Encoding 信息);
② 如果瀏覽器支持HTTP壓縮,Web服務器檢查請求文件的后綴名;
③ 如果請求文件是HTML、CSS等靜態文件,Web服務器到壓縮緩沖目錄中檢查是否已經存在請求文件的最新壓縮文件;
④ 如果請求文件的壓縮文件不存在,Web服務器向瀏覽器返回未壓縮的請求文件,并在壓縮緩沖目錄中存放請求文件的壓縮文件;
⑤ 如果請求文件的最新壓縮文件已經存在,則直接返回請求文件的壓縮文件;
配置壓縮功能
為了演示效果,我們人為制造一個較大的網頁
[root@scholar ~]# cp /var/log/messages /usr/local/apache/htdocs/test.html [root@scholar ~]# chmod +r /usr/local/apache/htdocs/test.html
測試未啟用壓縮功能時的傳輸字節
啟用模塊
[root@scholar ~]# vim /etc/httpd24/httpd.conf LoadModule deflate_module modules/mod_deflate.so
定義壓縮類型,壓縮等級及其它
SetOutputFilter DEFLATE # mod_deflate configuration #指定對某些MIME的格式文件進行壓縮 # Restrict compression to these MIME types AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/css #配置壓縮等級(1-9) # Level of compression (Highest 9 - Lowest 1) DeflateCompressionLevel 9 #對某些不支持壓縮的瀏覽器部分壓縮或不壓縮 # Netscape 4.x has some problems. BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4\.0[678] no-gzip # MSIE masquerades as Netscape, but it is fine BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
檢查語法,重啟服務
測試啟用壓縮功能后的傳輸字節
壓縮成功,壓縮功能配置完成
status頁面
httpd內嵌有handler,其中有一個handler用于輸出當前httpd服務相關狀態信息
handler: server-status
配置status
啟用模塊及配置
[root@scholar ~]# vim /etc/httpd24/httpd.conf LoadModule status_module modules/mod_status.so Include /etc/httpd24/extra/httpd-info.conf
[root@scholar ~]# vim /etc/httpd24/extra/httpd-info.conf <Location /server-status> SetHandler server-status #Require host .example.com Require ip 172.16.10.8 </Location>
檢查語法,重啟服務
測試status頁面
顯示成功,status功能實現
The end
好了,httpd服務就暫時講解到這里了,希望本系列可以對你學習httpd有所幫助,如有更新,請關注后續文章。以上僅為個人學習整理,如有錯漏,大神勿噴~~~
原創文章,作者:書生,如若轉載,請注明出處:http://www.www58058.com/3219