首頁 > 軟體

Linux原始碼編譯安裝詳解

2020-06-16 16:41:27

1. 編譯原始碼步驟


configure、Makefile.in一般是專案管理器自動生成的,這依賴於兩個開發工具:

  • autoconf:生成configure指令碼檔案;
    • autoconf需要結合一個組態檔來和其它命令生成configure指令碼;這個組態檔告訴autoconf當前程式碼有哪些特性、指定哪些安裝路徑等,而後才能生成configure指令碼;
  • automake:生成Makefile.in模板檔案;
    • automake也要結合一個檔案,這個指明有多少程式檔案、它們的依賴關係是什麼、以及使用哪一種版本的編譯器等,而後才能生成Makefile.in;
Note:在安裝之前檢視INSTALL和README檔案檢視安裝方法,因為不同程式包安裝步驟不一樣,有的程式包不需要configure,直接make後執行make install;而有的程式包不需要執行make install,只有一個二進位制檔案直接複製即可完成;

安裝開發元件
想要進行編譯,提前安裝開發元件

CentOS 6:Development Tools、Server Platform Development
CentOS 7:Development Tools、Development and Creative Workstation

(1) ./configure(也可能是config檔案)
--help:獲取支援使用的選項;

在執行make命令之前,使用者指定程式包的編譯引數、啟用特性、安裝路徑等等;
configure指令碼會收集當前系統上的開發環境中所依賴各元件的版本、特性等,並檢查所依賴的環境是否能滿足,如果檢查到所依賴的程式包不存在立即報錯,無法執行下一步。最後會根據使用者指定資訊和Makefile.in檔案生成一個makefile檔案;

##通用的幾個選項
--prefix=          :指定安裝的路徑
--sysconfdir=      :指定組態檔目錄
--enable-feature   :啟用某個特性
--disable-fecture  :禁用特性
--with-function    :啟用某功能
--without-function :禁用某功能

(2) make

每個原始碼都有專用的makefile,在make時依據這個組態檔,呼叫指定的前處理器做處理、呼叫指定的編譯器做處理、編譯檔案的順序等操作等;

(3) make install

其實是一些指令碼,實現把構建出的應用程式,分別分配到使用者指定的目錄中;如建立出的二進位制檔案放到指定的二進位制目錄、庫檔案放到指定的庫目錄等等,使用install拷貝命令也能實現;

2. 安裝後的設定

(1) 將二進位制程式目錄新增至PATH檔案變數中,否則只能使用絕對路徑才能執行程式

編輯檔案/etc/profile.d/NAME.sh  
export PATH=/PATH/TO/BIN:$PATH

(2) 匯出庫檔案路徑

編輯/etc/ld.so.conf.d/NAME.conf,新增新的庫檔案所在的目錄至此檔案中;    
例如:/usr/local/apache2/lib   

ldconfig -v:重新載入庫檔案路徑,並檢視;
ldconfig -p:檢視已載入所有庫檔案路徑;

(3) 匯出標頭檔案

/usr/include/目錄下,建立軟連結; 
ln -s  target_dir  soft_link        ##可以匯出一個目錄為所有檔案連結,也可以為每一個標頭檔案匯出一個連結,然後把建立的連結輸出到/usr/include/目錄下,或者直接複製標頭檔案至/usr/include/目錄;

(4) 匯出man手冊

Centos7是在/etc/man_db.conf的40到50行之間,新增一個MANPATH引數(Centos6可能是/etc/man.config);    
MANPATH_MAP     /usr/local/apache2/bin  /usr/local/apache2/man  

3. 編譯安裝實驗

3.1. Centos7編譯安裝apache 2.2.27

系統版本與軟體版本

[root@GNOME-C7 ~]# cat /etc/RedHat-release
CentOS Linux release 7.4.1708 (Core)
[root@GNOME-C7 ~]# uname -r
3.10.0-693.el7.x86_64
------------------------------------------------------------------
apr-1.6.3
apr-util-1.6.1
httpd-2.2.27

提前安裝開發環境
[root@GNOME-C7 ~]# yum groupinstall -y "Development tools" "Development and Creative Workstation"

3.1.1. 安裝apr-1.6.3

[root@GNOME-C7 ~]# cd apr-1.6.3/
root@GNOME-C7 apr-1.6.3]# ./configure -prefix=/usr/local/apr-1.6.3
[root@GNOME-C7 apr-1.6.3]# make
[root@GNOME-C7 apr-1.6.3]# make install

3.1.2. 安裝apr-util-1.6.1

[root@GNOME-C7 apr-1.6.3]# cd ../apr-util-1.6.1/
[root@GNOME-C7 apr-util-1.6.1]# ./configure -prefix=/usr/local/apr-unil-1.6.1 --with-apr=/usr/local/apr-1.6.3

[root@GNOME-C7 apr-util-1.6.1]# make
遇到報錯:
xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory
#include <expat.h>
                   ^
compilation terminated.
make[1]: *** [xml/apr_xml.lo] Error 1
make[1]: Leaving directory `/root/apr-util-1.6.1'
make: *** [all-recursive] Error 1
解決方法:yum install -y expat-devel

[root@GNOME-C7 apr-util-1.6.1]# make install

3.1.3. 安裝httpd-2.2.27

編譯安裝

[root@GNOME-C7 apr-util-1.6.1]# cd ../httpd-2.2.27/
[root@GNOME-C7 httpd-2.2.27]# ./configure --sysconfdir=/etc/httpd --prefix=/usr/local/apache2  --with-apr=/usr/local/apr-1.6.3 --with-apr-util=/usr/local/apr-unil-1.6.3 --with-mpm=prefork --with-included-apr --enable-mods-shared=most --enable-ssl --enable-suexec --enable-so
遇到報錯:configure: error: ...No recognized SSL/TLS toolkit detected
解決方法:yum install -y openssl-devel

[root@GNOME-C7 httpd-2.2.27]# make
[root@GNOME-C7 httpd-2.2.27]# make install

編譯第二次,從而得到httpd.worker        ##這種方法不確定是否可以
[root@GNOME-C7 httpd-2.2.27]# cd ../
[root@GNOME-C7 ~]# rm -rf httpd-2.2.27/
[root@GNOME-C7 ~]# tar -xf httpd-2.2.27.tar.bz2
[root@GNOME-C7 ~]# cd ./httpd-2.2.27/
[root@GNOME-C7 httpd-2.2.27]# ./configure --sysconfdir=/etc/httpd --prefix=/usr/local/apache2  --with-apr=/usr/local/apr-1.6.3 --with-apr-util=/usr/local/apr-unil-1.6.3 --with-mpm=worker --with-included-apr --enable-mods-shared=most --enable-ssl --enable-suexec --enable-so
[root@GNOME-C7 httpd-2.2.27]# make       ##不用執行make install否則就會覆蓋當前apache的檔案了
[root@GNOME-C7 httpd-2.2.27]# cp ./httpd /usr/local/apache2/bin/httpd.worker


本次使用的編譯引數
--prefix=/PATH:指明安裝路徑;
--sysconfdir=/PATH:指明組態檔路徑;不指明,預設在安裝路徑下;
--enable-so:表示啟用DSO動態模組載入機制;
--enable-ssl:啟用支援ssl功能;
--enable-mods-shared=most:表示動態的編譯進來大多數的模組;如果使用--enable-mods-shared=all,則指動態的編譯所有的模組。
--with-apr=/PATH:指明apr程式包的路徑;不使用=指明路徑會在預設路徑查詢;
--with-apr-util=/PATH:指明apr-util程式包的路徑;
--with-mpm=prefork:指明預設使用的MPM模組;


說明:
靜態編譯:將module直接在安裝的過程中放到apache中,當apache使用module的時候,就可以直接使用了。
動態編譯:將module引入到apache之中,在使用的時候才會真正的去載入(通過LoadModule命令)
--with.....:表示依賴的包,不指明路徑,編譯安裝時會到預設路徑下找;
--without.....:不依賴的包;
--enable.....:啟用特性;
--disable.....:禁用特性;


可能用到的其他引數
[root@Centos7 httpd-2.2.29]# ./configure --help        ##檢視有哪些支援的選項
部分內容如下:
    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]:庫檔案放在哪個目錄;
      --includedir=DIR        C header files [PREFIX/include]:標頭檔案放在哪個目錄;

安裝後的產生的目錄

]# ll /usr/local/apache2/
顯內容示:
drwxr-xr-x  2 root root  4096 Apr 14 08:51 bin 程式路徑
drwxr-xr-x  2 root root  4096 Apr 14 08:51 build 編譯安裝的相關檔案
drwxr-xr-x  2 root root  4096 Apr 14 08:51 cgi-bin cgi格式的程式檔案
drwxr-xr-x  4 root root  4096 Apr 14 08:51 conf 組態檔
drwxr-xr-x  3 root root  4096 Apr 14 08:51 error 錯誤頁面
drwxr-xr-x  2 root root  4096 Apr 14 08:30 htdocs 網頁檔案存放路徑
drwxr-xr-x  3 root root  4096 Apr 14 08:51 icons 圖示
drwxr-xr-x  2 root root  4096 Apr 14 08:51 include 標頭檔案,任何支援二次開發的程式都提供標頭檔案
drwxr-xr-x  2 root root  4096 Apr 14 08:53 logs 紀錄檔
drwxr-xr-x  4 root root  4096 Apr 14 08:51 man 幫助手冊
drwxr-xr-x 14 root root 12288 Jul 16  2014 manual 官方文件
drwxr-xr-x  2 root root  4096 Apr 14 08:51 modules 模組

3.1.4. 匯出二進位制程式目錄至PATH變數

[root@centos7 ~]# export PATH=$PATH:/usr/local/apache2/bin
[root@centos7 ~]# vim /etc/profile.d/apache2.sh
export PATH=$PATH:/usr/local/apache2/bin

3.1.5. 匯出man手冊

[root@centos7 ~]# vim /etc/man_db.conf
MANPATH_MAP /usr/local/apache2/bin    /usr/local/apache2/man
[root@GNOME-C7 ~]# manpath
/usr/local/share/man:/usr/share/man/overrides:/usr/share/man:/usr/local/apache2/man

3.1.6. 匯出標頭檔案

[root@GNOME-C7 ~]# ln -sv /usr/local/apache2/include /usr/include/httpd
‘/usr/include/httpd’ -> ‘/usr/local/apache2/include’

3.1.7. 匯出庫檔案

[root@GNOME-C7 ~]# vim /etc/ld.so.conf.d/httpd.conf
/usr/local/apache2/lib
[root@GNOME-C7 ~]# ldconfig -v
[root@GNOME-C7 ~]# ldconfig -p | grep apache
    libaprutil-1.so.0 (libc6,x86-64) => /usr/local/apache2/lib/libaprutil-1.so.0
    libaprutil-1.so (libc6,x86-64) => /usr/local/apache2/lib/libaprutil-1.so
    libapr-1.so.0 (libc6,x86-64) => /usr/local/apache2/lib/libapr-1.so.0
    libapr-1.so (libc6,x86-64) => /usr/local/apache2/lib/libapr-1.so

3.1.8. 建立服務管理指令碼

這兩個指令碼是從其他機器上複製的,然後改了一下檔案與目錄的路徑

[root@GNOME-C7 ~]# cat /etc/sysconfig/httpd
# Configuration file for the httpd service.

#
# The default processing model (MPM) is the process-based
# 'prefork' model.  A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# The service must be stopped before changing this variable.
#
HTTPD=/usr/local/apache2/bin/httpd.worker        ##表示啟動的httpd服務mpm為worker

#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
#OPTIONS=

#
# By default, the httpd process is started in the C locale; to
# change the locale in which the server runs, the HTTPD_LANG
# variable can be set.
#
#HTTPD_LANG=C

#
# By default, the httpd process will create the file
# /var/run/httpd/httpd.pid in which it records its process
# identification number when it starts.  If an alternate location is
# specified in httpd.conf (via the PidFile directive), the new
# location needs to be reported in the PIDFILE.
#
#PIDFILE=/var/run/httpd/httpd.pid



[root@GNOME-C7 ~]# cat /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.
# processname: httpd
# config: /etc/httpd/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /usr/local/apache2/logs/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/apache2/bin/apachectl
httpd=${HTTPD-/usr/local/apache2/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/usr/local/apache2/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() {
    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

3.1.9. 使用chkconfig管理

[root@GNOME-C7 ~]# chmod 755 /etc/init.d/httpd 
[root@GNOME-C7 ~]# chkconfig --add httpd
[root@GNOME-C7 ~]# chkconfig --level 3 httpd on
[root@GNOME-C7 ~]# chkconfig --list | grep httpd
httpd              0:off    1:off    2:off    3:on    4:off    5:off    6:off

3.1.10. 啟動服務

[root@GNOME-C7 ~]# service httpd start
Starting httpd (via systemctl):                            [  OK  ]
[root@GNOME-C7 ~]# ss -ant | grep 80
LISTEN     0      128          *:80                       *:*  
[root@GNOME-C7 ~]# ps aux | grep http[d]        ##現在執行httpd的使用者是daemon,可以在組態檔中更改
root     114229  0.0  0.0  97508  2536 ?        Ss   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114231  0.0  0.0 386420  4416 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114232  0.0  0.0 386420  4416 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114233  0.0  0.0 386420  4420 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114234  0.0  0.0 386420  4420 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd

3.1.11. 存取測試

3.2. Centos7編譯安裝apache 2.4.6

系統版本與軟體版本

[root@GNOME-C7 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@GNOME-C7 ~]# uname -r
3.10.0-693.el7.x86_64
------------------------------------------------------------------
apr-1.6.3
apr-util-1.6.1
httpd-2.4.6

提前安裝開發環境
[root@GNOME-C7 ~]# yum groupinstall -y "Development tools" "Development and Creative Workstation"

3.2.1. 安裝apr-1.6.3

[root@GNOME-C7 ~]# cd apr-1.6.3/
root@GNOME-C7 apr-1.6.3]# ./configure -prefix=/usr/local/apr-1.6.3
[root@GNOME-C7 apr-1.6.3]# make
[root@GNOME-C7 apr-1.6.3]# make install

3.2.2. 安裝apr-util-1.6.1

[root@GNOME-C7 apr-1.6.3]# cd ../apr-util-1.6.1/
[root@GNOME-C7 apr-util-1.6.1]# ./configure -prefix=/usr/local/apr-unil-1.6.1 --with-apr=/usr/local/apr-1.6.3

[root@GNOME-C7 apr-util-1.6.1]# make
遇到報錯:
xml/apr_xml.c:35:19: fatal error: expat.h: No such file or directory
#include <expat.h>
                   ^
compilation terminated.
make[1]: *** [xml/apr_xml.lo] Error 1
make[1]: Leaving directory `/root/apr-util-1.6.1'
make: *** [all-recursive] Error 1
解決方法:yum install -y expat-devel

[root@GNOME-C7 apr-util-1.6.1]# make install

3.2.3. 安裝httpd-2.4.6

[root@GNOME-C7 apr-util-1.6.1]# cd ../httpd-2.4.6/
[root@GNOME-C7 apr-util-1.6.1]# yum install -y openssl-devel    ##如果想要直接編譯成功ssl模組,需要提前安裝該openssl-devel,否則編譯過程中雖然不報錯(因為不是是動態編譯),但是編譯結束後會發現沒有ssl模組;
[root@GNOME-C7 httpd-2.4.6]# ./configure  --prefix=/usr/local/apache2 --with-apr=/usr/local/apr-1.6.3 --with-apr-util=/usr/local/apr-unil-1.6.1 --with-included-apr --sysconfdir=/etc/httpd --enable-so --enable-mpms-shared=all  --enable-mods-shared=all --with-mpm=prefork
遇到報錯:configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/.
解決方法:cp -r ../apr-1.6.3 ./srclib/apr ; cp -r ../apr-util-1.6.1 ./srclib/apr-util
遇到報錯:configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/
解決方法:yum -y install pcre-devel

[root@GNOME-C7 httpd-2.4.6]# make
[root@GNOME-C7 httpd-2.4.6]# make install

本次使用的編譯引數
--enable-so:表示啟用DSO動態模組載入機制;
--enable-mpms-shared=安裝所有支援的MPM模組
--enable-mods-shared=all:動態的編譯所有的模組
--with-mpm=prefork:指定預設MPM

3.2.4. 匯出二進位制程式目錄至PATH變數

[root@centos7 ~]# export PATH=$PATH:/usr/local/apache2/bin
[root@centos7 ~]# vim /etc/profile.d/apache2.sh
export PATH=$PATH:/usr/local/apache2/bin

3.2.5. 匯出man手冊

[root@centos7 ~]# vim /etc/man_db.conf
MANPATH_MAP /usr/local/apache2/bin    /usr/local/apache2/man
[root@GNOME-C7 ~]# manpath
/usr/local/share/man:/usr/share/man/overrides:/usr/share/man:/usr/local/apache2/man

3.2.6. 匯出標頭檔案

[root@GNOME-C7 ~]# ln -sv /usr/local/apache2/include /usr/include/httpd
‘/usr/include/httpd’ -> ‘/usr/local/apache2/include’

3.2.7. 匯出庫檔案

[root@GNOME-C7 ~]# vim /etc/ld.so.conf.d/httpd.conf
/usr/local/apache2/lib
[root@GNOME-C7 ~]# ldconfig -v
[root@GNOME-C7 ~]# ldconfig -p | grep apache
    libaprutil-1.so.0 (libc6,x86-64) => /usr/local/apache2/lib/libaprutil-1.so.0
    libaprutil-1.so (libc6,x86-64) => /usr/local/apache2/lib/libaprutil-1.so
    libapr-1.so.0 (libc6,x86-64) => /usr/local/apache2/lib/libapr-1.so.0
    libapr-1.so (libc6,x86-64) => /usr/local/apache2/lib/libapr-1.so

3.2.8. 建立服務管理指令碼

這兩個指令碼是從其他機器上複製的,然後改了一下檔案與目錄的路徑

[root@GNOME-C7 ~]#  cat /etc/sysconfig/httpd
# Configuration file for the httpd service.

#
# The default processing model (MPM) is the process-based
# 'prefork' model.  A thread-based model, 'worker', is also
# available, but does not work with some modules (such as PHP).
# The service must be stopped before changing this variable.
#
HTTPD=/usr/local/apache2/bin/httpd

#
# To pass additional options (for instance, -D definitions) to the
# httpd binary at startup, set OPTIONS here.
#
#OPTIONS=

#
# By default, the httpd process is started in the C locale; to
# change the locale in which the server runs, the HTTPD_LANG
# variable can be set.
#
#HTTPD_LANG=C



[root@GNOME-C7 ~]# cat /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.
# processname: httpd
# config: /etc/httpd/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /usr/local/apache2/logs/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/apache2/bin/apachectl
httpd=${HTTPD-/usr/local/apache2/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/usr/local/apache2/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() {
    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

3.2.9. 使用chkconfig管理

[root@GNOME-C7 ~]# chmod 755 /etc/init.d/httpd 
[root@GNOME-C7 ~]# chkconfig --add httpd
[root@GNOME-C7 ~]# chkconfig --level 3 httpd on
[root@GNOME-C7 ~]# chkconfig --list | grep httpd
httpd              0:off    1:off    2:off    3:on    4:off    5:off    6:off

3.2.10. 啟動服務

[root@GNOME-C7 ~]# service httpd start
Starting httpd (via systemctl):                            [  OK  ]
[root@GNOME-C7 ~]# ss -ant | grep 80
LISTEN     0      128          *:80                       *:*  
[root@GNOME-C7 ~]# ps aux | grep http[d]        ##現在執行httpd的使用者是daemon,可以在組態檔中更改
root     114229  0.0  0.0  97508  2536 ?        Ss   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114231  0.0  0.0 386420  4416 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114232  0.0  0.0 386420  4416 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114233  0.0  0.0 386420  4420 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd
daemon   114234  0.0  0.0 386420  4420 ?        Sl   18:34   0:00 /usr/local/apache2/bin/httpd

3.2.11. 存取測試

 


IT145.com E-mail:sddin#qq.com