Venturing Forth

apache 2.4 logo

Bash Script to merge Apache configuration files

Are you looking for a shell script to merge all configuration files of Apache 2.4 in one single text file? This comes handy when you have a lot of separated files scattered around the whole filesystem. Almost always such files need to be accessed on a ssh tunnel as the server is normally headless.

I created this script because sometimes you need to see the entire sequence of configuration statements in the correct order to understand the reason of weird behavior of your web server.

This bash script works on a RHEL server with the configuration almost totally contained into /etc/httpd. Anyhow it should be clever enough to fetch the path of all major include statements available in Apache.

The only requirement (that is generally verified in Apache 2.4) is that all inclusions are relative to the ServerRoot declaration.

Copy the snippet here below into a .sh file and remember to chmod executable rights to your user.

The file can reside wherever you like, the important thing is that you have write permissions on the directory where you want to store the file deriving from the concatenation.

#!/bin/bash
# AUTHOR VenturingForth 

# display usage message
usage(){
	echo "Usage: $0 [base_conf_file] [output_text_file]"
    echo "NOTE: output_text_file will be created if not existing otherwise replaced. Path shall be writable."
	exit 1
}


fetch_lines(){

BASECONF=$1
TARGETOUTPUT=$2
SERVROOT=""
if [[ $3 ]]; then
	SERVROOT=$3
echo $SERVROOT
fi
SWAP="$TARGETOUTPUT"
SWAP+="swap"
INCLUSIONSET=""
INCLUSIONSETOPTIONAL=""

touch $SWAP

echo "# File generated by $0 " > $TARGETOUTPUT

while read LINE; do
           LINE=`echo "${LINE}" | sed -e "s/#.*$//" -e "/^$/d"  -e "s/^[ \t]*//"`
           if [[ $LINE =~ ^ServerRoot ]]; then
		echo "$LINE" >> "$TARGETOUTPUT"
		LINE=`echo "${LINE}" | sed -e "s|^[^/]*||"`
		LINE=`echo "${LINE}" | sed -e "s|\"||"`
	   	SERVROOT+=$LINE
		SERVROOT+="/"
		continue
	   fi
           if [[ $LINE =~ ^IncludeOptional ]]; then
					  LINE=`echo "${LINE}" | sed -e 's|IncludeOptional||'`
					  #clean line again
					  LINE=`echo "${LINE}" | sed -e 's/^[ \t]*//'`
					  INCLUSIONSETOPTIONAL="$SERVROOT$LINE"
					  echo "DEBUG... SERVROOT again:: $SERVROOT"
					  echo "DEBUG- INCLUSIONSETOPTIONAL:::: $INCLUSIONSETOPTIONAL"
					  cat $INCLUSIONSETOPTIONAL > $SWAP
					  fetch_lines $SWAP $TARGETOUTPUT $SERVROOT
    						rm -f $SWAP
		   fi
           if [[ $LINE =~ ^Include ]]; then
					  #remove IncludeOptional
					  LINE=`echo "${LINE}" | sed -e 's|Include||'`
					  #clean line again
					  LINE=`echo "${LINE}" | sed -e 's/^[ \t]*//'`
					  INCLUSIONSET="$SERVROOT$LINE"
					  cat $INCLUSIONSET > $SWAP
					  fetch_lines $SWAP $TARGETOUTPUT $SERVROOT
						rm -f $SWAP
		   fi

	   # this is really not elegant
       if [[ ${#LINE} -lt 2 ]]; then
 		 continue
 	   fi

	echo "$LINE" >> "$TARGETOUTPUT"

done < $BASECONF
}




# check input and output are given
[ $# -lt 2 ] && usage

fetch_lines $1 $2

#rm  $SWAP
exit 0 

The scripts provides also a basic synopsis in the following form

./apacheconfmerge.sh path/to/main/config/file path/to/store/resulting/textfile

Notes:

  • I did not test it on the Apache structure commonly installed on QNAP NAS as there may be issues in following the probable symlinks of that filesystem.  I did not check it in details but I will find the time do it shortly. In the meantime, if you found this snippet useful and improved it, don’t hesitate to share your improements with me. I would be glad to have part in it again.

Auf wiederluoege,

Alex

3 thoughts on “Bash Script to merge Apache configuration files”

  1. The script idea is great, but have you tried to test it?
    The following line will delete everything but the last file
    echo “# File generated by $0 ” > $TARGETOUTPUT

    There are other things as well, but I’m not a bash expert to point them out exactly.

    1. Hi Natalia,
      sorry for the late reply.
      I did not catch your comment. The output file should anyhow be rewritten at every run.
      I was actually using it while setting up my apache and the merged file was fine to me.
      Let me know if yoou rewrite it or improve so I can post it to help others in future
      cheers
      Alex

Leave a Comment

Your email address will not be published. Required fields are marked *