Page 1 of 1

kleines apache-reload-script

Posted: Tue Feb 28, 2006 9:39 am
by equinox0r
folgendes szenario:

auf meinem rootserver läuft ein apache, mysql und ein paar php und bash-voodoo-scripte.
die apache-vhosts sind in der mysql-datenbank abgebildet und ein php-script erstellt via cronjob alle vhost-dateien neu (bei bedarf).
anschliessend muss noch der apache neu geladen werden, damit die config auch übernommen wird.

dafür habe ich mir folgendes kleines script geschrieben:

Code: Select all

#!/bin/bash
# reload_apache_config.sh version 0.4
version=0.4
# this script checks if apache vhost-files have changed
# from md5 hashes stored in a md5sum-output file
#
# mailto: Tristan Cebulla <equinox@lichtspiele.org>

# path to md5file which should be used
md5_file=/data/www/system/md5/apache_vhost_files.md5

# remove # to enable debug messages in cronjobs
# or call  export DDEBUG="yes" && reload_apache_config.sh --option
#DDEBUG="yes"

# do not change code from here...
function debug {
    # TERM environment is set to dumb in cronjobs
    if [ "$TERM" != "dumb" ]; then
        echo "$@"
    else
        if [ ${DDEBUG} ]; then
            echo "$@"
        fi
    fi
}

function gen_new_md5 {
    for file in $@; do
        if [ -f $file ]; then
            md5sum $file >> $md5_file
            debug " * indexing: adding $file"
        else
            debug " * indexing: not adding $file"
        fi
    done
}

function print_md5 {
    awk '{ print $2 " -> " $1; }' $md5_file
}

function check_update {
    md5sum -c $md5_file >/dev/null 2>&1
    if [ $? != "0" ]; then
        files=`awk {'print $2'} $md5_file`
        rm $md5_file 2>&1 /dev/null && debug " * deleting original md5 file"
        gen_new_md5 $files
        reload_apache
    else
        debug " * apache needs not to be reloaded"
    fi
}

function reload_apache {
    /etc/init.d/apache2 configtest >/dev/null 2>&1 
    if [ $? == "0" ]; then
        debug " * syntax ok"
        /etc/init.d/apache2 reload >/dev/null 2>&1
        debug " * apache has been reloaded"
        debug " * new config is active"
    else
        debug "error while reloading new vhost-config"
        debug "new config NOT active!"
        debug "see /var/log/apache2/startuperror.log for details"

        if [ "$TERM" == "dumb" ]; then
            echo " * error loading apache vhosts on $HOSTNAME !"
        fi
    fi
}

debug "apache config reloader $version (cronjob edition)"

case "$1" in
    --index)
        if [ "$2" == "" ]; then
            debug "no file(s) given for indexing"
            exit 1
        fi

        if [ -f $md5_file ]; then
            debug " * existing md5 file found, backuping to ${md5_file}.old"
            mv $md5_file ${md5_file}.old
        fi 

        gen_new_md5 $@
        ;;
    --print)
        print_md5
        ;;
    --reload)
        reload_apache
        ;;
    --force-reload)
        /etc/init.d/apache2 reload >/dev/null 2>&1
        debug " * apache forcereloaded" 
        ;;
    --check)
        if [ ! -f $md5_file ]; then
            debug "md5 list $md5_file does not exist!"
            debug "please run  $0 --index file [file]  as root"
            exit 1
        fi

        check_update
        ;;
    --help|*)
        echo "usage: $0 [option] [file] [..]"
        echo ""
        echo "  --check         : reload apache only if necessary"
        echo "  --index  file   : create new checksum of the given files"
        echo "  --print         : print out md5/file list"
        echo "  --reload        : reload apache with configtest"
        echo "  --force-reload  : simply reload apache"
        ;;
esac

## die gracefully
exit 0

#:set tabstop=4
in meinen cronjobs wird es alle 5 minuten via reload_apache_config.sh --check aufgerufen.

als erstes sollte man jedoch den index erstellen, das macht man mit reload_apache_config.sh --index datei1 datei2 datei3 ...
der rest ist oder sollte zumindest selbsterklärend sein, bei fragen fragen :)

Re: kleines apache-reload-script

Posted: Tue Feb 28, 2006 5:36 pm
by xces
equinox0r wrote:die apache-vhosts sind in der mysql-datenbank abgebildet und ein php-script erstellt via cronjob alle vhost-dateien neu (bei bedarf).
net-www/mod_vdbh bzw. mod_vhost ;)
Funktioniert auch ohne Cronjob und Apache Reload bei jeder Änderung.

Posted: Tue Feb 28, 2006 10:08 pm
by equinox0r
mod_vhost kenne ich schon und nutze es deshalb nicht, weil ich die vhosts viel spezieller konfigurieren möchte als es dieses modul kann :/ das ist dann auch der grund weshalb ich eine eigene lösung verwende ;)

deinen ersten vorschlag kenne ich nicht und leider ist deren seite down sodass man sich das gar nicht anschauen kann :( ich werds aber schnell testen, danke für den hinweis ;)

Posted: Tue Feb 28, 2006 10:24 pm
by equinox0r
also, ich habs mir angeschaut. pro domain/subdomain muss immernoch ein extra vhost eintrag erstellt werden (wenn ich das richtig verstanden habe):

Code: Select all

<IfModule mod_vdbh.c>

  #NameVirtualHost 206.9.161.29
  #<VirtualHost 206.9.161.29>
  #    vdbh On
  #    vdbh_CLIENT_COMPRESS On
  #    vdbh_MySQL_Database virtual_hosts
  #    vdbh_MySQL_Table virtual_hosts
  #    vdbh_MySQL_Host_Field server
  #    vdbh_MySQL_Path_Field path
  #    vdbh_MySQL_Environment_Field environment_variable
  #    vdbh_Default_Host julia.fractal.net
  #    vdbh_Declines .htpasswd *.txt
  #</VirtualHost>

</IfModule>
da die dokumentation auf der seite praktischerweise nicht existent ist hilft mir das leider auch nicht so recht weiter. interessant finde ich lediglich vdbh_MySQL_Environment_Field environment_variable, mit dem man wohl andere vhost-einstellungen angeben kann?

Posted: Mon Mar 13, 2006 10:06 pm
by equinox0r
ich pusche mal gaanz flauschig :)