編譯httpd細節


apache httpd系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html


1.1 下載和解決依賴

以httpd 2.4.27為例。

資源下載:

apache自己的站點提供了基金會下所有的(包括所有版本)資源,包括httpd。

地址:http://archive.apache.org/dist/
httpd下載地址:http://archive.apache.org/dist/httpd

清華大學有一個httpd歸檔的鏡像站點,里面提供最新測試版和最新穩定版的下載,還提供一些依賴包或模塊的下載。

地址:http://mirrors.tuna.tsinghua.edu.cn/apache/httpd/
apache基金會下所有資源地址:http://mirrors.tuna.tsinghua.edu.cn/apache/

httpd同樣使用"./configure"、"make && make install"的編譯流程編譯。但是它有一些依賴包需要提前裝好。官方上指定的依賴環境有:apr、apr-util、pcre、pcre-devle,此外還需要expat-devel包。其中pcre、pcre-devel和expat.devel可以直接使用yum安裝,apr和apr-util需要編譯安裝。下載地址可以從站點 http://mirrors.tuna.tsinghua.edu.cn/apache/ap/ 找到。

yum -y install pcre pcre-devel expat-devel

以下是編譯apr和apr-util的過程。

tar xf apr-1.6.0.tar.gz
cd apr-1.6.0
./configure --prefix=/usr/local/apr 
make
make install
cd ../apr-util-1.6.2
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
make install

然后是編譯httpd,httpd編譯對剛接觸的人來說可能有些麻煩,因為編譯選項太多,其中的一些"潛規則"也不太熟悉。所以下面詳細地說明說明。

1.2 httpd編譯選項

httpd的編譯選項非常多。以下是截取./configure -h中的一部分,使用"......"表示省略了一堆信息。

Configuration:
  -h, --help              display this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...' messages --cache-file=FILE cache test results in FILE [disabled] -C, --config-cache alias for `--cache-file=config.cache' -n, --no-create do not create output files --srcdir=DIR find the sources in DIR [configure dir or `..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local/apache2]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]

By default, `make install' will install all the files in `/usr/local/apache2/bin', `/usr/local/apache2/lib' etc.  You can specify
an installation prefix other than `/usr/local/apache2' using `--prefix', for instance `--prefix=$HOME'.

For better control, use the options below.

Fine tuning of the installation directories:
  --bindir=DIR            user executables [EPREFIX/bin]
  --sbindir=DIR           system admin executables [EPREFIX/sbin]
  --libexecdir=DIR        program executables [EPREFIX/libexec]
  --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
  --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]
  --localstatedir=DIR     modifiable single-machine data [PREFIX/var]
  --libdir=DIR            object code libraries [EPREFIX/lib]
........................................

System types:
  --build=BUILD     configure for building on BUILD [guessed]
  --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  --target=TARGET   configure for building compilers for TARGET [HOST]

Optional Features:
  --disable-option-checking  ignore unrecognized --enable/--with options
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
  --enable-layout=LAYOUT
  --enable-dtrace         Enable DTrace probes
  --enable-hook-probes    Enable APR hook probes
  --enable-exception-hook Enable fatal exception hook
  --enable-load-all-modules
                          Load all modules
  --enable-maintainer-mode
                          Turn on debugging and compile time warnings and load
                          all compiled modules
  --enable-debugger-mode  Turn on debugging and compile time warnings and turn
                          off optimization
  --enable-pie            Build httpd as a Position Independent Executable
  --enable-modules=MODULE-LIST
                          Space-separated list of modules to enable | "all" |
                          "most" | "few" | "none" | "reallyall"
  --enable-mods-shared=MODULE-LIST
                          Space-separated list of shared modules to enable |
                          "all" | "most" | "few" | "reallyall"
  --enable-mods-static=MODULE-LIST
                          Space-separated list of static modules to enable |
                          "all" | "most" | "few" | "reallyall"
.........................................

Optional Packages:
  --with-PACKAGE[=ARG]    use PACKAGE [ARG=yes]
  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=no)
  --with-included-apr     Use bundled copies of APR/APR-Util
  --with-apr=PATH         prefix for installed APR or the full path to
                             apr-config
  --with-apr-util=PATH    prefix for installed APU or the full path to
                             apu-config
  --with-pcre=PATH        Use external PCRE library
....................

以下是一個編譯配置:

./configure --prefix=/usr/local/apache --sysconfdir=/etc/apache --enable-so --enable-ssl --enable-cgi --enable-rewrite --enable-modules=most --enable-mpms-shared=all --with-z --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-mpm=event

但這個配置中有些項是多余的,以下是等價編譯配置:

./configure --prefix=/usr/local/apache --sysconfdir=/etc/apache --enable-mpms-shared=all --with-z --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-mpm=event

具體哪些項多余,看完下面的1.3-1.6就知道了。

1.3 模塊動靜態編譯

httpd是高度模塊化的程序,各個功能通過加載各個模塊來實現。但前提是將功能對應的模塊先編譯好,以供httpd加載。

httpd對模塊有兩種編譯方式:靜態編譯和動態編譯。

  • 靜態編譯:將模塊直接編譯進httpd的核心中。靜態編譯的所有模塊都會隨着httpd的啟動和啟動。
  • 動態編譯:將模塊編譯好,但不編譯到httpd的核心中。要啟動動態編譯的模塊,需要在httpd的配置文件中使用LoadModule指令加載。

httpd的一個優點是可以實現動態模塊的熱插拔。因為httpd是獨立於終端的守護進程,可以通過發送HUP信號給httpd讓其重讀配置文件。而是否加載動態編譯模塊正是由httpd配置文件中的LoadModule指令決定的。當想要加載某個模塊A時(即模塊熱插),使用LoadModule指定A模塊的鏈接地址,再發送HUP信號重讀配置文件即可。而想要停止某個模塊A時(即模塊熱拔),只需將對應模塊的LoadModule指令行給注釋,再重讀配置文件即可。

甚至,可以隨時動態編譯某個外部模塊到httpd中,然后再熱插。因為何時編譯需要動態加載的模塊對httpd來說是無關緊要的,它只需LoadModule和重讀配置文件兩個過程對模塊進行控制。

在編譯選項中,有幾種類型的選項:

--disable-FEATURE:禁用某特性,等價於--enable-FEATURE=no
--enable-FEATURE[=ARG]:啟用某特性,默認參數值為YES
--enable-Module_Name=shared:指定的模塊Module_Name以動態編譯方式安裝
--enable-Module_Name=static:指定的模塊Module_Name以靜態編譯方式安裝

對於./configure --help中給定的選項,如果該選項是--disable的,那么表示該選項默認是啟用的,需要顯式使用--disable選項禁用;如果該選項是--enable的,那么表示該選項默認是禁用的,需要使用--enable選項來啟用。例如:

--disable-authz-user  :表示authz-user特性默認啟用,編譯時無需指定該項。如果要禁用,編譯時需顯式指定--disable-authz-user
--enable-echo    :表示echo特性默認是禁用的,如果要啟用,則編譯時需顯式指定--enable-echo

模塊名的書寫是有規則的,一般模塊的全稱類似於"mod_BASENAME.so"格式,例如"mod_charset_lite.so",但在編譯選項中指定模塊時,只需指定BASENAME,且如果basename中包含下划線時,需要轉換為短橫線。例如"--enable-echo"表示編譯的模塊是"mod_echo.so"。

此外,還支持3種列表方式的動靜態編譯選項:列表項之間使用空格分隔,但要使用單引號包圍。

--enable-modules='Module_Name1 Moduel_Name2'
--enable-mods-shared='Module_Name1 Module_Name2'
--enable-mods-statics='Module_Name1 Module_Name2'

列表部分還可以使用關鍵字"all/few/most/reallyall"。分別表示編譯所有、少量、大多數、真正的所有模塊。

"--enable-modules"基本等價於"--enable-mods-shared",都是動態編譯給定列表中的模塊,但"--enable-modules"可以額外使用一個關鍵字"none",表示不編譯所有模塊。

1.4 動靜態編譯的優先級規則

httpd動靜態模塊編譯有一套規則,各種動靜態便宜選項之間有優先級的存在。例如,某個非核心模塊既指定了動態編譯,同時又指定了靜態編譯,那到底是靜態還是動態編譯?

以下是我總結的一些優先級規則。

  1. 不指定任何模塊編譯選項時,默認的選項為"--enable-mods-shared",而該選項的默認值又是most,所以等價於"--enable-mods-shared=most"。
  2. 顯式指定要動態或靜態編譯的優先級最高。有以下幾種方式顯式指定:
    --enable-Module_Name=shared 
    --enable-Module_Name=static 
    --enable-mods-shared='Module_Name1 Module_Name2'
    --enable-mods-statics='Module_Name1 Module_Name2'
    --enable-modules='Module_Name1 Moduel_Name2'
    
    如果某個模塊既顯式指定了動態,又顯式指定了靜態編譯,則靜態編譯優先級更高。例如:
    --enable-echo=shared
    --enable-echo=static
    
    那么,mod_echo模塊將被靜態編譯。
  3. 指定了關鍵字(all/most/few/reallyall)的"--enable-mods-static"選項,優先級高於指定或未指定關鍵字的"--enable-mods-shared"和"--enable-modules"選項,即靜態關鍵字規則強於動態關鍵字規則。
    例如,下面兩個編譯配置中,都是"--enable-mods-static=few"生效。第二個編譯配置語句中將忽略"--enable-mods-shared=all"。
    ./configure --prefix=/tmp/apache --enable-mods-static=few
    ./configure --prefix=/tmp/apache --enable-mods-static=few --enable-mods-shared=all
    
    對於下面的例子,authn-file和echo這兩個模塊既指定了動態編譯又指定了靜態編譯,靜態優先級高於動態,所以這兩個模塊靜態被靜態編譯。由於沒有使用關鍵字,所以會使用默認的"--enable-mods-shared=most"配置。即動態編譯大部分,但指定的這兩個模塊被靜態編譯。
    ./configure --prefix=/tmp/apache \
    --enable-mods-static='authn-file echo' --enable-mods-shared='authn-file echo'
    
    而下面這個例子由於額外指定了使用"--enable-mods-static=few"選項,其優先級高於默認的"--enable-mods-shared=most",所以結果是靜態編譯few,且顯式指定的兩個模塊也被靜態編譯。
    ./configure --prefix=/tmp/apache \
    --enable-mods-static='authn-file echo' \
    --enable-mods-shared='authn-file echo' \
    --enable-mods-static=few
    
  4. 使用了關鍵字的"--enable-mods-static"、"--enable-mods-shared "和"--enable-modules"的選項,隱含了"沒有指定何種編譯方式的模塊"的默認編譯方式。
    例如下面的編譯配置,"--enable-mods-static"指定了關鍵字few,它將優先於默認的配置規則"--enable-mods-shared=most",所以沒有指定編譯方式的模塊"data"將以靜態的方式編譯。
    ./configure --prefix=/tmp/apache --enable-mods-static=few --enable-data
    
    下面的配置如何編譯的?由於默認的是"--enable-mods-shared=most"編譯方式,所以模塊"data"將以動態的方式編譯。
    ./configure --prefix=/tmp/apache --enable-data
    
    再看下面的例子,配置中出現了"--enable-mods-static=few"和"--enable-mods-shared"(未給定值時也是默認為most),static的優先級高於shared,所以沒有指定編譯方式的模塊"data"使用靜態編譯方式編譯,而顯式指定了編譯方式的模塊"echo"其優先級最強,所以動態編譯"echo"。
    ./configure --prefix=/tmp/apache \
    --enable-mods-static=few --enable-mods-shared --enable-data --enable-echo=shared
    

1.5 MPM的安裝

編譯mpm模塊(prefork/worker/event)和其他模塊差不多,唯一的區別是必須至少編譯一個mpm模塊,且必須有且僅有一個加載被httpd加載。

編譯安裝時默認的mpm是event模式(和發行版有關)。但可以通過"--with-mpm=MPM_NAME"來指定被加載的mpm模塊。以下是幾個相關編譯選項:

--with-mpm=MPM_Name:用於指定默認的mpm模塊,它所指定的模塊會被靜態編譯,並在httpd啟動時加載。
--enable-mpms-shared=MPM-LIST:指定動態編譯安裝的MPM列表,動態編譯的MPM必須使用LoadModule指令加載才能使用。

如果定"--with-mpm"選項指定了某個mpm,則默認該模塊被靜態編譯,但如果同時使用"--enable-mpms-shared"指定了該mpm,則該mpm模塊被動態編譯。

如果某個mpm模塊被靜態編譯,在httpd啟動時會加載它,如果想要切換到其他mpm模塊,只有一種方法:重新編譯httpd。

而動態編譯mpm模塊時,則可以通過LoadModule來切換到其他mpm模塊。由於編譯時自帶默認mpm模塊,還可以使用"--with-mpm"指定默認mpm模塊,所以動態編譯mpm模塊無疑比靜態編譯要好。

"--enable-mpms-shared"可以指定動態編譯的mpm列表,使用空格分隔,但需要使用單引號包圍。還可以使用關鍵字"all"表示動態編譯所有mpm模塊。 例如:

--enable-mpms-shared='prefork worker'
--enable-mpms-shared=all

1.6 關於"--enable-so"

一個模塊被動態編譯,在需要加載的時候使用LoadModule指令指定該模塊,並重讀配置文件即可。但httpd為什么能加載該動態模塊?這就是mod_so的能力。實際上,LoadModule和LoadFile指令就是該模塊提供的。

該選項使得httpd有加載某動態模塊的能力(DSO,Dynamic Shared Object),也因此它只能使用靜態編譯方式隨httpd啟動被加載。只要不顯式指定"--enable-so=shared"或者將其加入顯式編譯列表,它都會默認以靜態方式編譯。實際上,只要顯式指定了動態方式編譯該選項,編譯時會報錯。

1.7 開始編譯httpd

至此,就可以開始編譯httpd了。過程如下:

cd
tar xf httpd-2.4.27.tar.gz
cd httpd-2.4.27
./configure --prefix=/usr/local/apache --sysconfdir=/etc/apache --with-z --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-mpm=event --enable-mpms-shared=all

其中安裝路徑為/usr/local/apache,配置文件路徑為/etc/apache。

[root@xuexi ~]# ls /usr/local/apache/
bin  build  cgi-bin  error  htdocs  icons  include  logs  man  manual  modules

bin目錄為二進制程序存放位置,如啟動腳本apachectl、httpd、htpasswd、ab(壓力測試工具)等;htdocs目錄存放網頁文件,默認里面有index.html;logs目錄存放了日志文件,除了日志文件,默認還有httpd運行的pid文件httpd.pid,這個建議修改到/var/run目錄下(方便判斷);modules存放了編譯后的模塊;man目錄為幫助文檔路徑。

使用httpd的啟動腳本bin/apahcectl啟動httpd,然后測試其是否正常。

[root@xuexi apache]# bin/apachectl start
[root@xuexi apache]# netstat -tnlp | grep httpd
tcp        0      0 :::80          :::*         LISTEN      38798/httpd

在瀏覽器中輸入IP地址即可訪問。

1.8 編譯后的規范化操作

  1. 設置man路徑。
    echo "MANPATH /usr/local/apache/man" >>/etc/man.config
    
  2. 設置PATH環境變量。
    echo 'PATH=/usr/local/apache/bin:$PATH' >/etc/profile.d/apache.sh
    source /etc/profile.d/apache.sh
    
  3. 輸出頭文件。
    ln -s /usr/include /usr/local/apache/include
    
  4. 提供服務啟動腳本。

提供不提供沒多大所謂,因為apachectl或httpd命令自身可以管理進程的啟停,但自身管理啟停時不提供lock文件。

如果要提供的話,從yum安裝的httpd提供的/usr/lib/systemd/system/httpd.service(systemd)或/etc/init.d/httpd(sysV)拷貝后稍作修改就可以了。以下是按照我上面編譯的環境做了修改后的systemd和sysV服務管理腳本。

以下是httpd的systemd服務管理腳本/usr/lib/systemd/system/httpd.service。

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/local/apache/bin/httpd $OPTIONS -DFOREGROUND
ExecReload=/usr/local/apache/bin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

說明:上面的腳本中使用了"kill -WINCH"信號,它是graceful stop的信號。如不明白,見我的另一篇文章:進程和信號

以下是httpd的sysV服務管理腳本/etc/rc.d/init.d/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.
#
######################################################################
# 若httpd配置文件中指定了PidFile,則修改此腳本中的pidfile變量 #
######################################################################

. /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=/usr/local/apache/bin/apachectl
prog=httpd
pidfile=/usr/local/apache/logs/httpd.pid
lockfile=/var/lock/subsys/httpd
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
config=/etc/apache/httpd.conf

# 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 -f $config $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() {
    status -p ${pidfile} $httpd > /dev/null
    if [[ $? = 0 ]]; then
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
    else
        echo -n $"Stopping $prog: "
        success
    fi
    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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM