Apache是世界使用排名第一的Web服務(wù)器軟件。它可以運(yùn)行在幾乎所有廣泛使用的計(jì)算機(jī)平臺(tái)上,由于其跨平臺(tái)和安全性被廣泛使用,是最流行的Web服務(wù)器端軟件之一。當(dāng)前Apache版本為2.4,本文主要描述基于CentOS 6.5以源碼方式安裝Apache httpd。
一、編譯安裝的優(yōu)勢(shì)
源碼的編譯安裝一般由3個(gè)步驟組成:
配置(configure),通常依賴gcc編譯器,binutils,glibc。配置軟件特性,檢查編譯環(huán)境,生成 Makefile文件
編譯(make)
安裝(make install)
優(yōu)勢(shì)
自定義軟件功能
優(yōu)化編譯參數(shù),提高性能
解決不必要的軟件間依賴
方便清理與卸載
configure是一個(gè)可執(zhí)行腳本,它有很多選項(xiàng),在待安裝的源碼路徑下使用命令./configure –-help輸出詳細(xì)的選項(xiàng)列表。
常用的選項(xiàng)
--prefix
該選項(xiàng)是配置安裝的路徑,如果不配置該選項(xiàng),安裝后可執(zhí)行文件默認(rèn)放在/usr /local/bin,
庫(kù)文件默認(rèn)放在/usr/local/lib,配置?件默認(rèn)放在/usr/local/etc,其它的資源文?放在/usr /local/share
如果配置--prefix,如: ./configure --prefix=/usr/local/test
則可以把所有資源文件放在/usr/local/test的路徑中,不會(huì)雜亂。
用了—prefix選項(xiàng)的另一個(gè)好處是卸載軟件或移植軟件。
當(dāng)某個(gè)安裝的軟件不再需要時(shí),只須簡(jiǎn)單的刪除該安裝目錄,就可以把軟件卸載得干干凈凈;
移植軟件只需拷貝整個(gè)目錄到另外一個(gè)機(jī)器即可(相同的?作系統(tǒng))。
當(dāng)然?卸載程序,也可以在原來(lái)的make目錄下用一次make uninstall,但前提是make文件指定過(guò)uninstall。
二、httpd的版本
版本:
httpd-1.3
httpd-2.0
httpd-2.2
httpd-2.4
三、httpd 2.4的新特性
1) MPM支持運(yùn)行時(shí)裝載
--enable-mpms-shared=all --with-mpm=prefork|worker|event
2) 支持event MPM
3) 異步讀寫(xiě)支持
4) 支持每模塊及每目錄分別使用不同的日志級(jí)別
5) 支持per-request(即支持<If>, <ElseIf>, and <Else>條件判斷)
6) 增強(qiáng)版的表達(dá)式分析器;
7) 支持毫秒級(jí)keepalive timeout;
8) 基于FQDN(域名)的虛擬主機(jī)不再需要NameVirtualHost;
9) 支持用戶使用自定義變量;
新增一些模塊:mod_proxy_fcgi, mod_ratelimit, mod_request, mod_remoteip
修改了一些配置機(jī)制?不再支持使用order, allow, deny來(lái)實(shí)現(xiàn)基于IP的訪問(wèn)控制;
四、編譯安裝httpd 2.4
1、依賴關(guān)系
httpd依賴于apr, apr-util
apr全稱為apache portable runtime,能實(shí)現(xiàn)httpd跨平臺(tái)運(yùn)行
httpd-2.4 依賴于1.4+及以上版本的apr
apr-1.5.0.tar.bz2
apr-util-1.5.3.tar.bz2
httpd-2.4.9.tar.bz2
pcre-devel包
openssl-devel
2、編譯安裝
# yum install gcc
# yum install pcre-devel
# tar xf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure --prefix=/usr/local/apr (--prefix指定apr安裝的目錄)
# make
# make install
# tar xf apr-util-1.5.3.tar.bz2
# cd apr-util-1.5.3
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install ###該項(xiàng)被漏掉,補(bǔ)充@20160714
# tar xf httpd-2.4.9.tar.bz2
以下為幾個(gè)主要的配置項(xiàng)
--sysconfdir=/etc/httpd24 指定配置文件路徑
--enable-so 啟動(dòng)模塊動(dòng)態(tài)裝卸載
--enable-ssl 編譯ssl模塊
--enable-cgi 支持cgi機(jī)制(能夠讓靜態(tài)web服務(wù)器能夠解析動(dòng)態(tài)請(qǐng)求的一個(gè)協(xié)議)
--enable-rewrite 支持url重寫(xiě) --Author : Leshami
--with-zlib 支持?jǐn)?shù)據(jù)包壓縮 --Blog : http://www.tjdsmy.cn/leshami
--with-pcre 支持正則表達(dá)式
--with-apr=/usr/local/apr 指明依賴的apr所在目錄
--with-apr-util=/usr/local/apr-util/ 指明依賴的apr-util所在的目錄
--enable-modules=most 啟用的模塊
--enable-mpms-shared=all 以共享方式編譯的模塊
--with-mpm=prefork 指明httpd的工作方式為prefork
# cd httpd-2.4.9
# ./configure --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-mpm=prefork --enable-modules=most --enable-mpms-shared=all
# make
# make install
五、配置http2.4啟動(dòng)及停止
1、修改端口號(hào)
修改端口號(hào)使得與2.2版本使用不同的端口,可以同時(shí)運(yùn)行,修改后如下
# cat /etc/httpd24/httpd.conf |grep Listen |grep -v ^#
Listen 8080
2、啟動(dòng)與停止
# /usr/local/apache/bin/apachectl start
# netstat -nltp|grep 80
tcp 0 0 :::8080 :::* LISTEN 17365/httpd
# /usr/local/apache/bin/apachectl status
Not Found
The requested URL /server-status was not found on this server.
通過(guò)修改httpd.conf,增加如下配置
# grep server-stat /etc/httpd24/httpd.conf -A5
<Location /server-status>
SetHandler server-status
# Order deny,allow
# Deny from all
Allow from 192.168.21.157 192.168.21.10
</Location>
# /usr/local/apache/bin/apachectl restart
# /usr/local/apache/bin/apachectl status
Apache Server Status for localhost (via 127.0.0.1)
Server Version: Apache/2.4.9 (Unix)
Server MPM: prefork
..........
# /usr/local/apache/bin/apachectl stop
3、配置自啟?文件
可以通過(guò)復(fù)制2.2版本的啟動(dòng)文件,修改相關(guān)路徑后將2.4版作為單獨(dú)服務(wù)運(yùn)行,如下
注啟動(dòng)文件pid文件位置要配置成與/usr/local/apache/bin/apachectl -V看到的pid位置一致
查看pid位置
# /usr/local/apache/bin/apachectl -V|grep pid
-D DEFAULT_PIDLOG="logs/httpd.pid"
# cp /etc/init.d/httpd /etc/init.d/httpd24
# vi /etc/init.d/httpd24
# diff /etc/init.d/httpd /etc/init.d/httpd24
26,27c26,27
< if [ -f /etc/sysconfig/httpd ]; then
< . /etc/sysconfig/httpd
---
> if [ -f /etc/httpd24 ]; then
> . /etc/httpd24
42,46c42,46
< apachectl=/usr/sbin/apachectl
< httpd=${HTTPD-/usr/sbin/httpd}
< prog=httpd
< pidfile=${PIDFILE-/var/run/httpd/httpd.pid}
< lockfile=${LOCKFILE-/var/lock/subsys/httpd}
---
> apachectl=/usr/local/apache/bin/apachectl
> httpd=${HTTPD-/usr/local/apache/bin/httpd}
> prog=httpd24
> pidfile=${PIDFILE-/usr/local/apache/logs/httpd.pid}
> lockfile=${LOCKFILE-/var/lock/subsys/httpd24}
# service httpd24 start
Starting httpd24: [ OK ]
# service httpd24 status
httpd (pid 15641) is running...
# netstat -nltp|grep 80
tcp 0 0 :::80 :::* LISTEN 15677/httpd ###2.2版httpd
tcp 0 0 :::8080 :::* LISTEN 15641/httpd ###2.4版httpd
可以通過(guò)復(fù)制apachectl文件生成服務(wù)腳本
# cp /usr/local/apache/bin/apachectl /etc/init.d/httpd249
# service httpd249 start
# service httpd249 status
ELinks: Connection refused ###該方式無(wú)法查看到狀態(tài)
[root@orasrv1 bin]# netstat -nltp|grep 80
tcp 0 0 :::8080 :::* LISTEN 15999/httpd
最后將配置文件添加到服務(wù),以下為http24為例
# chkconfig --add httpd24
# chkconfig httpd24 on
六、配置man手冊(cè)
vi /etc/man.config
MANPATH /usr/local/apache/man
七、驗(yàn)證
# echo "This is a apached 2.4.9 version">>/usr/local/apache/htdocs/index.html
# curl http://www.tjdsmy.cn:8080
<html><body><h1>It works!</h1></body></html>
This is a apached 2.4.9 version