天天看點

Apache源碼包添加啟動腳本到系統服務

手動編譯apache源碼包安裝的話,apache是沒有啟動腳本的,也就是說使用者不能通過簡單的通過/etc/init.d/httpd(start|stop|restart)來啟動/關閉/重新啟動。

其實源碼裡已經有啟動的腳本,我們要修改下即可,把Apache加入系統SysV服務中來。

在源碼httpd-2.x.x/build/rpm中存在httpd.init    #httpd.ini就是apache啟動腳本

拷貝指令如下:

cp httpd.init /etc/init.d/httpd

編輯該httpd檔案:

#!/bin/bash

#

# Licensed to the ApacheSoftware Foundation (ASF) under one or more

# contributor licenseagreements.  See the NOTICE filedistributed with

# this work for additionalinformation regarding copyright ownership.

# The ASF licenses this fileto You under the Apache License, Version 2.0

# (the "License");you may not use this file except in compliance with

# the License.  You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required byapplicable law or agreed to in writing, software

# distributed under theLicense is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES ORCONDITIONS OF ANY KIND, either express or implied.

# See the License for thespecific language governing permissions and

# limitations under theLicense.

# httpd        Startup script for the Apache WebServer

# chkconfig: - 85 15     #可以改成chkconfig: 35 85 15

# description: The ApacheHTTP Server is an efficient and extensible \

#             server implementing the currentHTTP standards.

# processname: httpd

# pidfile: /var/run/httpd.pid

# config:/etc/sysconfig/httpd

### 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 andstop Apache HTTP Server

# Description: The ApacheHTTP Server is an extensible server

#  implementing the current HTTP standards.

### END INIT INFO

# Source function library.

. /etc/rc.d/init.d/functions

# What were we called?Multiple instances of the same daemon can be

# created by creatingsuitably named symlinks to this startup script

prog=$(basename $0 | sed -e's/^[SK][0-9][0-9]//')

if [ -f/etc/sysconfig/${prog} ]; then

        . /etc/sysconfig/${prog}

fi

# Start httpd in the C localeby default.

HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlogfrom swallowing up a pass-phrase prompt if

# mod_ssl needs a pass-phrasefrom the user.

INITLOG_ARGS=""

# SetHTTPD=/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 athread-based MPM; notably PHP will refuse to start.

httpd=${HTTPD-/usr/local/apache/bin/httpd}

pidfile=${PIDFILE-/usr/local/apache/logs/${prog}.pid}

lockfile=${LOCKFILE-/var/lock/subsys/${prog}}

RETVAL=0

# check for 1.3 configuration

check13 () {

       CONFFILE=/usr/local/apache/conf/httpd.conf

       GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|"

       GONE="${GONE}AgentLog|RefererLog|RefererIgnore|FancyIndexing|"

       GONE="${GONE}AccessConfig|ResourceConfig)"

       if grep -Eiq "^[[:space:]]*($GONE)" $CONFFILE; then

              echo

              echo 1>&2 " Apache 1.3 configurationdirectives found"

              echo 1>&2 " please read@docdir@/migration.html"

              failure "Apache 1.3 config directives test"

              exit 1

       fi

}

# The semantics of these twofunctions differ from the way apachectl does

# things -- attempting tostart while running is a failure, and shutdown

# when not running is also afailure.  So we just do it the way initscripts

# are expected to behavehere.

start() {

        echo -n $"Starting $prog: "

        check13 || exit 1

        LANG=$HTTPD_LANG daemon--pidfile=${pidfile} $httpd $OPTIONS

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && touch${lockfile}

        return $RETVAL

stop() {

       echo -n $"Stopping $prog: "

       killproc -p ${pidfile} -d 10 $httpd

       RETVAL=$?

       echo

       [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

reload() {

       echo -n $"Reloading $prog: "

       check13 || exit 1

       killproc -p ${pidfile} $httpd -HUP

# See how we were called.

case "$1" in

  start)

       start

       ;;

  stop)

       stop

  status)

        if ! test -f ${pidfile}; then

            echo $prog is stopped

            RETVAL=3

        else 

            status -p ${pidfile} $httpd

            RETVAL=$?

        fi

        ;;

  restart)

  condrestart)

       if test -f ${pidfile} && status -p ${pidfile} $httpd>&/dev/null; then

              stop

              start

  reload)

        reload

  configtest)

        LANG=$HTTPD_LANG $httpd $OPTIONS -t

  graceful)

        echo -n $"Gracefully restarting$prog: "

        LANG=$HTTPD_LANG $httpd $OPTIONS -k $@

  *)

       echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|graceful|help|configtest}"

       exit 1

esac

exit $RETVAL

注意檔案中有三處主要的地方需要修改下的:

httpd=${HTTPD-/usr/local/apache2/bin/httpd}

pidfile=${PIDFILE-/usr/local/apache2/logs/${prog}.pid}

CONFFILE=/usr/local/apache2/conf/httpd.conf

上面是我自己的路徑請根據實際情況更改相應的路徑!

然後運作如下指令:

ls–l httpd   #檢視httpd是否有執行權限

chkmod+x httpd  #添加執行權限

chkconfig--add httpd   #添加httpd到系統服務

chkconfig--level 35 httpd on  #設定httpd運作級别為35,開機自動運作

這樣一來,啟動、停止、重新開機Apache就可以用以下方式了:

/etc/init.d/httpd start

/etc/init.d/httpd stop

/etc/init.d/httpd restart

service httpd start

service httpd status

service httpd stop

service httpd restart

本文轉自 Mr_sheng 51CTO部落格,原文連結:http://blog.51cto.com/sf1314/1980623