最近需要用脚本来动态修改web服务器的配置并生效,但不想每次都restart服务,发现Lighttpd有reload功能,于是研究了下。

默认reload功能并无作用,需要结合lighttpd-angel实现sighup指令的接收。

本以为这个lighttpd-angel是官方做的支持sighup的启动文件,后来发现实际上它是启动了lighttpd作为子进程..  sighup就是重做一个lighttpd,有点醉

对比得出还是和restart有区别,用lighttpd-angel重载,php-cgi们不需要重来,只有lighttpd的pid更换了(小白浅层理解),所以应该消耗资源较少,而且实测修改config之后可以生效。

下面说关键点

官方没有解释这个lighttpd-angel怎么用,实际上和lighttpd一样,但需要-D参数方能生效(这里我理解为禁用lighttpd自带的后台运行)。

大家可以自行尝试一下: nohup /usr/sbin/lighttpd-angel -D -f /etc/lighttpd/lighttpd.conf

这样子便产生了一个lighttpd-angel和lighttpd,如果不加-D,angel会直接退出,不加nohup的话,无法后台继续执行。

现在,如果你可以试试修改config后,打kill -1 lighttpd-angel的PID,配置文件会立即生效~ 这里-1的意思便是发送sighup

接下来我们把他搞到service里,

我个人的做法是复制了一个/etc/init.d/lighttpd,命名为lighttpd-angel。

主要是修改了execprog用于启动和区分,关键修改原来的启动语句daemon $exec -f $config/usr/bin/nohup $exec -D -f $config >/dev/null 2>&1 &

修改结果如下:

#!/bin/sh

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

exec="/usr/sbin/lighttpd-angel"
prog="lighttpd-angel"
config="/etc/lighttpd/lighttpd.conf"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/$prog

start() {
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    echo -n $"Starting $prog: "
    #daemon $exec -f $config
    /usr/bin/nohup $exec -D -f $config >/dev/null 2>&1 &
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $prog: "
    killproc $prog -HUP
    retval=$?
    echo
    return $retval
}

force_reload() {
    restart
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status &>/dev/null
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

这样之后,你就可以用service lighttpd-angel start/stop来启动或关闭lighttpd,用service lighttpd-angel reload来重载配置了,当然,restart也是可以用的。