Mauserrifle.nl Tech and Life

    Home     Archive     Projects     Contact

Better Apache2 Config Test for Missing Log Folders

By default the command apachectl configtest does not check whether the defined log folders exist. This causes the config test to give positive feedback about valid config files, but when restarting apache2 it might fail due missing folders.

A failing restart is troubling because it might happen during a scheduled Linux update at night.

Consider using the script below at all times when checking apache2 after vhost changes.

#!/bin/bash

# Check config
/usr/sbin/apachectl configtest

# Check log dirs
HTTPD_CONFS="/etc/apache2/sites-available/*.conf"
HTTPD_DIR="/etc/apache2"
RET=3

cd $HTTPD_DIR

function test_if_exists ()
{
    if [ -f $1 ]; then
        RET=0
    else
        RET=1
    fi
}

function gimmie_the_dirs ()
{
    LFILES=$(egrep '^ErrorLog|^CustomLog' $1 | awk {'print $2'} | tr '\n' ' ')
}

for i in `ls $HTTPD_CONFS`; do
    gimmie_the_dirs $i
    for j in $LFILES; do
        test_if_exists $j
        if [ $RET -eq 1 ]; then
            echo -en "ERROR: $j does not exist\n"
        fi
    done
done

echo "Don't forget to restart httpd! (just in case)"

Change the dir paths when using an other operating system than Debian.

Example to run:

/root/apache.check.sh


Hope this helps!

If you liked this post, you can share it with your followers!