1. 程式人生 > >Apache服務簡介及編譯安裝詳解

Apache服務簡介及編譯安裝詳解

Apache簡介 Apache編譯安裝詳解 Apache設置啟動腳本

Apache服務簡介及編譯安裝詳解

一、Apache簡介

Apache HTTP Server(簡稱Apache)是Apache軟件基金會的一個開放源碼的網頁服務器,是目前世界上使用最廣泛的一種web server,它以跨平臺,高效和穩定而聞名,可以運行在幾乎所有廣泛使用的計算機平臺上。Apache的特點是簡單、速度快、性能穩定,並可做代理服務器來使用。

Apache是用C語言開發的基於模塊化設計的web應用,總體上看起來代碼的可讀性高於php代碼,它的核心代碼並不多,大多數的功能都被分割到各種模塊中,各個模塊在系統啟動時按需載入。

支持SSL技術,支持多個虛擬主機。Apache是以進程的Prefork模式(還有基於線程的Worker模式)為基礎的結構,進程要比線程消耗更多的系統開支,不太適合於多處理器環境,因此,在一個Apache Web站點擴容時,通常是增加服務器或擴充群集節點而不是增加處理器。

二、Apache 的特性

Apacheweb服務器軟件擁有以下特性:

1.支持最新的HTTP/1.1通信協議

2.擁有簡單而強有力的基於文件的配置過程

3.支持通用網關接口

4.支持基於IP和基於域名的虛擬主機

5.支持多種方式的HTTP認證

6.集成Perl處理模塊

7.集成代理服務器模塊

8.支持實時監視服務器狀態和定制服務器日誌

9.支持服務器端包含指令(SSI)

10.支持安全Socket層(SSL)

11.提供用戶會話過程的跟蹤

12.支持FastCGI

13.通過第三方模塊可以支持JavaServlets

三、編譯安裝Apache步驟及參數詳解

源碼的編譯安裝一般由3個步驟組成:

配置(configure),通常依賴gcc編譯器,binutils,glibc。配置軟件特性,檢查編譯環境,生成 Makefile文件

編譯(make)

安裝(make install)

apr和apr-utils下載地址:http://apr.apache.org/download.cgi

PCRE下載地址:http://sourceforge.net/projects/pcre/files/pcre/8.32/

[root@centos4 ~]#yum -y install gcc

[root@centos1 ~] wget http://archive.apache.org/dist/httpd/httpd-2.4.7.tar.gz

[root@centos1 ~]tar xf httpd-2.4. 7.tar.gz

[root@centos1 ~]# cd httpd-2.4.7

[root@centos1 httpd-2.4.7]# ./configure \

> --prefix=/usr/local/apache \ 指定安裝目錄

> --with-apr=/usr/local/apr \ 指定依賴文件的安裝目錄

> --with-apr-util=/usr/local/apr-util \ 指定依賴文件的安裝目錄

> --enable-deflate \ 壓縮文本文件提高速度節約帶寬

> --enable-expires \ 讓瀏覽器緩存,減輕服務器壓力,提高訪問速度

> --enable-headers \ 激活http頭

> --enable-modules=most \ 激活大多數模塊

> --enable-so \ 讓apache核心裝載DSO,但是不實際編譯任何動態模塊;

> --with-mpm=worker \ 讓Apache工作在worker模式下

> --enable-rewrite 激活偽靜態功能

checking for chosen layout... Apache

checking for working mkdir -p... yes

checking for grep that handles long lines and -e... /bin/grep

checking for egrep... /bin/grep -E

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

configure:

configure: Configuring Apache Portable Runtime library...

configure:

checking for APR... no

configure: error: APR not found. Please read the documentation. 這裏出現報錯

[root@centos4 ~]# tar xf apr-1.4.6.tar.gz 解壓並安裝依賴包

[root@centos4 ~]#cd /apr-1.4.6

[root@centos4 apr-1.4.6]# ./configure

[root@centos4 apr-1.4.6]# make && make install

[root@centos4 apr-1.4.6]# cd ..

[root@centos4 ~]# tar xf apr-util-1.4.1 解壓並安裝工具組件依賴包

[root@centos4 ~]# cd apr-util-1.4.1

[root@centos4 apr-util-1.4.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

[root@centos4 apr-util-1.4.1]# make && make install

[root@centos4 ~]# tar xf pcre-8.34.tar.gz 解壓並安裝這個正則表達式函數庫依賴包

[root@centos4 ~]# cd pcre-8.34

[root@centos4 pcre-8.34]# ./configure

checking for a BSD-compatible install... /usr/bin/install -c

checking whether build environment is sane... yes

checking for a thread-safe mkdir -p... /bin/mkdir -p

………………………………………………………省略若幹

checking for dirent.h... yes

checking windows.h usability... no

checking windows.h presence... no

checking for windows.h... no

configure: error: You need a C++ compiler for C++ support. 這裏報錯需要安裝gcc-c++軟件

[root@centos4 pcre-8.34]# yum -y install gcc-c++

[root@centos4 pcre-8.34]# ./configure

[root@centos4 pcre-8.34]# make && make install

……………………………………………………………….省略若幹

ln -sf pcre_version.3 /usr/local/share/man/man3/pcre32_version.3

make[3]: Leaving directory `/root/pcre-8.34'

make[2]: Leaving directory `/root/pcre-8.34'

make[1]: Leaving directory `/root/pcre-8.34' 出現這些證明編譯安裝完成

[root@centos4 ~]# tar xf zlib-1.2.3.tar.gz 安裝依賴的庫文件或者yum安裝zlib zlib-devel

[root@centos4 ~]# cd zlib-1.2.3

[root@centos4 zlib-1.2.3]# ./configure

Checking for gcc...

Building static library libz.a version 1.2.3 with gcc.

Checking for unistd.h... Yes.

Checking whether to use vs[n]printf() or s[n]printf()... using vs[n]printf()

Checking for vsnprintf() in stdio.h... Yes.

Checking for return value of vsnprintf()... Yes.

Checking for errno.h... Yes.

Checking for mmap support... Yes.

[root@centos4 zlib-1.2.3]# vim Makefile

把第21行左右的CFLAGS=-O3 -DUSE_MMAP修改成CFLAGS=-O3 -DUSE_MMAP -fPIC

或使用sed -i ‘s/CFLAGS=-O3 -DUSE_MMAP/CFLAGS=-O3 -DUSE_MMAP -fPIC/g’ Makefile

註意:上面這個如果不修改安裝httpd軟件時就會報以下錯誤:

collect2: ld returned 1 exit status

make[4]: *** [mod_deflate.la] 錯誤 1

make[4]: Leaving directory `/root/httpd/httpd-2.4.7/modules/filters'

make[3]: *** [shared-build-recursive] 錯誤 1

make[3]: Leaving directory `/root/httpd/httpd-2.4.7/modules/filters'

make[2]: *** [shared-build-recursive] 錯誤 1

make[2]: Leaving directory `/root/httpd/httpd-2.4.7/modules'

make[1]: *** [shared-build-recursive] 錯誤 1

make[1]: Leaving directory `/root/httpd/httpd-2.4.7'

make: *** [all-recursive] 錯誤 1

[root@centos4 zlib-1.2.3]# make && make install

[root@centos4 zlib-1.2.3]# cd ..

[root@centos4 ~]# cd httpd-2.4.7

[root@centos4 httpd-2.4.7]# ./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-deflate --enable-expires --enable-headers --enable-modules=most --enable-so --with-mpm=worker --enable-rewrite

[root@centos4 httpd-2.4.7]#make && make install

[root@centos4 httpd-2.4.7]# /usr/local/apache2/bin/apachectl start 啟動Apache

到這裏Apache就已經安裝完成了。

[root@centos4 httpd-2.4.7]# /usr/local/apache2/bin/apachectl -l 可以查看安裝的模塊

[root@centos4 httpd-2.4.7]# /usr/local/apache2/bin/apachectl -t 檢查配置文件語法是否有誤

四、創建httpd啟動腳本並設置開機自啟

1、復制Apache的啟動文件到/etc/init.d下面並改名

[root@centos4 ~]# cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd

2、修改復制的啟動文件

[root@centos4 ~]# vim /etc/init.d/httpd

#!/bin/bash 這行下面增加兩行文字

# chkconfig: 35 70 30

#35指的是在3和5級別開啟該服務 70:指的是開機啟動服務在70位,30指的是關機關閉服務在30位,一般啟動服務的順序和關閉順序的和100。

# description: Apache 描述信息,這裏的#不再試註釋符

3、設置開機自啟動

[root@centos4 ~]# chkconfig --add httpd

[root@centos4 ~]# chmod +x /etc/init.d/httpd

[root@centos4 ~]# chkconfig httpd on

[root@centos4 ~]# systemctl restart httpd


Apache服務簡介及編譯安裝詳解