環境:Ubuntu 12.04
【簡單安裝】
一般第一次安裝Apache都較為順利。
1. 下載並解壓
root@ubuntu:/home/qy/share# tar zxvf httpd-2.2.22.tar.gz root@ubuntu:/home/qy/share# cd httpd-2.2.22
在http-2.2.22里有文件README和INSTALL,用more命令可以閱讀。
2. 配置
root@ubuntu:/home/qy/share/httpd-2.2.22# ./configure --prefix=/usr/local/apache
--prefix參數指定了將要安裝到的目錄。此時/usr/local下還沒有該目錄,make install后才會出現。
注意:Apache在安裝時不會檢查參數是否正確,錯誤的參數會直接被丟棄,不會報告給用戶。所以使用echo $?命令檢查是否有錯誤,當輸出結果為0時表示沒有錯誤。
3. 編譯
root@ubuntu:/home/qy/share/httpd-2.2.22# make
4. 安裝
root@ubuntu:/home/qy/share/httpd-2.2.22# make install
5. 啟動服務器
root@ubuntu:/home/qy/share/httpd-2.2.22# cd /usr/local/apache/bin root@ubuntu:/usr/local/apache/bin# ./apachectl start
為了以后使用方便,可以把啟動文件apachectl復制到/sbin下,以后直接apachectl start啟動。
#vi /etc/rc.local
增加一行 /sbin/apachectl start
6. 驗證Apache是否工作
此時,服務器端窗口應該顯示:
#ps -e|grep httpd
在客戶端瀏覽器輸入服務器的IP地址(或者http://localhost),IE應該顯示:It works!畫面。
Ubuntu重啟后,需要重啟Apache: apachectl start
停止服務器: ./apachectl stop
重啟服務器: ./apachectl graceful 或 ./apachectl restarted
通常,第一次安裝Apache沒有什么問題,但以后安裝,如果沒有apr,apr-util,pcre,在執行./configure的配置過程中可能遇到的錯誤:
make[2]: *** [install] Error 1 make[2]: Leaving directory `/tmp/httpd-2.2.22/srclib/apr-util' make[1]: *** [install-recursive] Error 1 make[1]: Leaving directory `/tmp/httpd-2.2.22/srclib' make: *** [install-recursive] Error 1 |
Apache2.0.x與Apache2.2.x在apr上有本質的區別,前者為依賴公用apr,后者依賴於自身的apr。2.0.x的編譯基本上沒有apr方面的問題,除非,在編譯前,安裝了非2.0.x所需的apr,如果是這樣,則需要將已經安裝的apr去除,然后再編譯。HTTP Sever2.2.22修復了不少重要安全問題,包含APR(Apache Portable Runtime)1.4.5和APR-util(Apache Utility Library)1.4.2。因此不需要像網上其它教程那樣從外部找源碼安裝apr和apr-util。將安裝前已存在於系統中的apr去除后,再編譯apache2.2.x自身srclib里的apr和apr-util。其它操作系統這樣解決就沒問題了,但是Ubuntu不太一樣。安裝完apr和apr-util后,./configure配置Apache時可能會出現下面錯誤:
configure: error: Cannot use an external APR with the bundled APR-util |
或
configure: error: APR version 1.2.0 or later is required |
srclib目錄中有apr,apr-util,pcre三個源碼包,其中pcre是不可用的,編譯不出來,有如下錯誤:
libtool: compile: unrecognized option `-DHAVE_CONFIG_H' libtool: compile: Try `libtool --help' for more information. make: *** [pcrecpp.lo] Error 1 |
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/上下載的pcre-8.30也不可用。不妨用make check命令查看下錯誤出在什么地方。一般懷疑是g++版本低,更新即可。我沒有這樣做,而是直接用apt-get install libpcre3-dev安裝了pcre后,再安裝apache就沒問題了。
【推薦完整步驟】
安裝包解壓:httpd-2.2.22.tar.gz和httpd-2.4.2.tar.gz,當我糾結在上述錯誤的時候,最新版2.4.2發布了。因為后者scrlib不提供apr,apr-util,pcre,因此借用了較低版本的。注意:用2.2.22做下面的第4步會出錯。
1. 安裝apr
root@ubuntu:/home/qy/share# cd httpd-2.2.22 root@ubuntu:/home/qy/share/httpd-2.2.22# cd apr root@ubuntu:/home/qy/share/httpd-2.2.22/apr# ./configure --prefix=/usr/local/apr root@ubuntu:/home/qy/share/httpd-2.2.22/apr# make root@ubuntu:/home/qy/share/httpd-2.2.22/apr# make install
2. 安裝apr-util
root@ubuntu:/home/qy/share/httpd-2.2.22/apr# cd .. root@ubuntu:/home/qy/share/httpd-2.2.22# cd apr-util root@ubuntu:/home/qy/share/httpd-2.2.22/apr-util# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr root@ubuntu:/home/qy/share/httpd-2.2.22/apr-util# make root@ubuntu:/home/qy/share/httpd-2.2.22/apr-util# make install
3. 安裝pcre
root@ubuntu:/home/qy/share/httpd-2.2.22/apr-util# apt-get install libpcre3-dev
4. 安裝apache
root@ubuntu:/home/qy/share/httpd-2.2.22/apr-util# cd /home/qy/share/httpd-2.4.2 root@ubuntu:/home/qy/share/httpd-2.4.2# ./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-so --enable-dav --enable-mainer-mode --enable-rewrite root@ubuntu:/home/qy/share/httpd-2.4.2# make root@ubuntu:/home/qy/share/httpd-2.4.2# make install
Apache作為開源服務器,在編譯前需要了解系統的庫安裝情況,某些模塊需要依賴於特定的庫,如果這些庫不存在,配置腳本將自動忽略這些庫的編譯。經過檢測時候會生成合適的MakeFile文件。這里特別提醒一句,如果直接執行配置腳本,是不會編譯額外的模塊的,我們希望使用額外模塊時,需要在運行配置腳本命令后加入參數,讓其盡最大可能編譯可用的庫。
5. 啟動apache
root@ubuntu:/home/qy/share/httpd-2.4.2# /usr/local/apache/bin/apachectl start
附:
–prefix=/usr/local/apache //體系無關文件的頂級安裝目錄PREFIX ,也就Apache的安裝目錄。
–enable-module=so //打開 so 模塊,so 模塊是用來提 DSO 支持的 apache 核心模塊
–enable-mods-shared=all //編譯全部的模板,對於不需要我們可以在httpd.conf去掉。
–enable-cache //支持緩存
–enable-file-cache //支持文件緩存
–enable-mem-cache //支持記憶緩存
–enable-disk-cache //支持磁盤緩存
–enable-static-support //支持靜態連接(默認為動態連接)
–enable-static-htpasswd //使用靜態連接編譯 htpasswd – 管理用於基本認證的用戶文件
–enable-static-htdigest //使用靜態連接編譯 htdigest – 管理用於摘要認證的用戶文件
–enable-static-rotatelogs //使用靜態連接編譯 rotatelogs – 滾動 Apache 日志的管道日志程序
–enable-static-logresolve //使用靜態連接編譯 logresolve – 解析 Apache 日志中的IP地址為主機名
–enable-static-htdbm //使用靜態連接編譯 htdbm – 操作 DBM 密碼數據庫
–enable-static-ab //使用靜態連接編譯 ab – Apache HTTP 服務器性能測試工具
–enable-static-checkgid //使用靜態連接編譯 checkgid
–disable-cgid //禁止用一個外部 CGI 守護進程執行CGI腳本
–disable-cgi //禁止編譯 CGI 版本的 PHP我們不再使用worker模式編譯apache,worker模式和php貌似有一些不協調不穩定之處。所以使用了默認的perfork模式。
【參考文獻】
http://hi.baidu.com/rel_conquer/blog/item/477d7e1fb2610166f624e4cd.html