Getting Apache22 on FreeBSD 8.1 to work with DHCP

Apache likes to have a static IP address to work with and that is not always something you can guarantee to have. An easy way to get around this is to add the following two files to /etc

Firstly add a file called “dhclient-exit-hooks” with the following

#!/bin/sh


APINCDIR=/usr/local/etc/apache22/Includes
APPIDFILE=/var/run/httpd.pid


if [ ! -z “$new_ip_address” ]; then
    # First, configure the /etc/hosts file so Apache can start
    HOSTNAME=`grep ^hostname= /etc/rc.conf | awk -F = ‘{ print $2 }’ | sed -e ‘s/”//g;’`
    if [ ! -z “$HOSTNAME” ]; then
        echo “$new_ip_address $HOSTNAME” > /etc/hosts.dynamic
        cat /etc/hosts.boilerplate /etc/hosts.dynamic > /etc/hosts
    fi


    # Next, we’ll configure the Apache web server, if it has been installed
    if [ -d $APINCDIR ]; then
        echo “ServerName $new_ip_address” > $APINCDIR/server-name.conf


        if [ -f $APPIDFILE ]; then
            kill -HUP `cat $APPIDFILE`
        fi
    fi
fi

Secondly add a file as referenced above called “hosts.boilerplate” with the following content:
# $FreeBSD: src/etc/hosts,v 1.16 2003/01/28 21:29:23 dbaker Exp $
#
# Host Database
#
# This file should contain the addresses and aliases for local hosts that
# share this file.  Replace ‘my.domain’ below with the domainname of your
# machine.
#
# In the presence of the domain name service or NIS, this file may
# not be consulted at all; see /etc/nsswitch.conf for the resolution order.
#
#
::1 localhost localhost.my.domain
127.0.0.1 localhost localhost.my.domain
#
# Imaginary network.
#10.0.0.2 myname.my.domain myname
#10.0.0.3 myfriend.my.domain myfriend
#
# According to RFC 1918, you can use the following IP networks for
# private nets which will never be connected to the Internet:
#
# 10.0.0.0 –   10.255.255.255
# 172.16.0.0 –   172.31.255.255
# 192.168.0.0 –   192.168.255.255
#
# In case you want to be able to connect to the Internet, you need
# real official assigned numbers.  Do not try to invent your own network
# numbers but instead get one from your network provider (if any) or
# from your regional registry (ARIN, APNIC, LACNIC, RIPE NCC, or AfriNIC.)
#
Then reboot, restart service, whatever your preference is, and test it out.