With a little side of applesauce...

Thursday, February 1, 2007

Replacing mod_macro for Apache webfarm configuration

Finally, I have come up with a way to configure my apache servers across a web farm, (using a single configuration). All we need to do is to add four lines to our /etc/init.d/apache2 shell script (on Debian):

#!/bin/sh -e
#
# apache2 This init.d script is used to start apache2.
# It basically just calls apache2ctl.

ENV=”env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin”

### let’s create our temp httpd.conf file (we’ll call it apache2.conf for fun ;) )

# grab the local machine’s ip address
LOCALIP=$(/sbin/ifconfig eth0 | awk ‘/inet/ { print $2 }’ | awk -F “:” ‘{ print $2 }’)

# copy the main apache2.conf file from the NAS to a tmp location on the local machine
cat /etc/apache2/apache2.conf > /etc/apache2.conf

# cat (append) all of the vhost files from /etc/apache2/sites-enabled/* into /etc/apache2.conf
cat /etc/apache2/sites-enabled/* >> /etc/apache2.conf

# replace our placeholder (CWSIPADDRESS) with the local ip address
sed -i “s/CWSIPADDRESS/${LOCALIP}/g” /etc/apache2.conf

### Now, we run the rest of the apache startup script.

#[ `ls -1 /etc/apache2/sites-enabled/ | wc -l | sed -e ’s/ *//;’` -eq 0 ] && \
#echo “You haven’t enabled any sites yet, so I’m not starting apache2.” && \
#echo “To add and enable a host, use addhost and enhost.” && exit 0
….

NOTES:

1. It might be better to place these lines in the start sections… (I haven’t really thought about it).
2. My only issue with mod_macro was that we couldn’t take advantage of the cool tools given to us by the Debian Apache2 team. (ie a2ensite/a2dissite), and the ease of maintenance of the conf file as it pushes over 1,000 lines.

2 comments:

speeves said...

Ok… It appears that the best place to put our new shell commands is in /usr/sbin/apache2ctl:

case $ARGV in
start|stop|restart|graceful)
$HTTPD -k $ARGV
ERROR=$?
;;
startssl|sslstart|start-SSL)

### let’s create our temp httpd.conf file (we’ll call it apache2.conf for fun ;) )
LOCALIP=$(/sbin/ifconfig eth0 | awk ‘/inet/ { print $2 }’ | awk -F “:” ‘{ print $2 }’)
cat /etc/apache2/apache2.conf > /etc/apache2.conf
cat /etc/apache2/sites-enabled/* >> /etc/apache2.conf
sed -i “s/IPADDRESS/${LOCALIP}/g” /etc/apache2.conf

$HTTPD -f /etc/apache2.conf -k start -DSSL
ERROR=$?
;;
configtest)
$HTTPD -t
ERROR=$?
;;
status)

speeves said...

Updated to add the new /etc/apache2.conf to the start|stop|restart|graceful case in /usr/sbin/apache2ctl:

case $ARGV in
start|stop|restart|graceful)
$HTTPD -f /etc/apache2.conf -k $ARGV
ERROR=$?
;;
startssl|sslstart|start-SSL)

### let’s create our temp httpd.conf file (we’ll call it apache2.conf for fun ;) )
LOCALIP=$(/sbin/ifconfig eth0 | awk ‘/inet/ { print $2 }’ | awk -F “:” ‘{ print $2 }’)
cat /etc/apache2/apache2.conf > /etc/apache2.conf
cat /etc/apache2/sites-enabled/* >> /etc/apache2.conf
sed -i “s/CWSIPADDRESS/${LOCALIP}/g” /etc/apache2.conf

$HTTPD -f /etc/apache2.conf -k start -DSSL
ERROR=$?
;;