231weq 发表于 2016-1-4 10:31:13

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/^//')
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"
            echo
            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
       RETVAL=$?
       echo
}

# 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)
       stop
       start
       ;;
condrestart)
       if test -f ${pidfile} && status -p ${pidfile} $httpd>&/dev/null; then
            stop
            start
       fi
       ;;
reload)
      reload
       ;;
configtest)
      LANG=$HTTPD_LANG $httpd $OPTIONS -t
      RETVAL=$?
      ;;
graceful)
      echo -n $"Gracefully restarting$prog: "
      LANG=$HTTPD_LANG $httpd $OPTIONS -k $@
      RETVAL=$?
      echo
      ;;
*)
       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
servicehttpd startservicehttpd statusservicehttpd stopservicehttpd restart
页: [1]
查看完整版本: Apache源码包添加启动脚本到系统服务