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.

How to install MemCached with Apache, PHP, and MySQL on FreeBSD

memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Danga Interactive developed memcached to enhance the speed of LiveJournal.com, a site which was already doing 20 million+ dynamic page views per day for 1 million users with a bunch of webservers and a bunch of database servers. memcached dropped the database load to almost nothing, yielding faster page load times for users, better resource utilization, and faster access to the databases on a memcache miss.

Notes

  • There are definitely steps in this that can be changed, extended, other, but these will get you what you want in a good learning fashion. And learning is key!
  • You can just as easily install memcached on an IIS server.
  • Yes you can download binaries with pkg_add etc. but building will help you literally see what is going where. One just has to make sure that some housekeeping is performed during the build process. This is done with the clean parameter in the make command.
  • This appears to be quite a popular post. Please keep in mind that version numbers change so update the strings below.
To Start

First off build a FreeBSD box. It’s easy, fast, and best of all free once you have the hardware – real or virtualized. Don’t forget to install the ports. You’re going to need them going forward.

For dependency issues we’re going to install all the items in this order:

  1. MySQL Server 5.x
  2. Apache 2.x
  3. PHP5
  4. PDF-Lite
  5. PHP Extensions
  6. MemCached

1: MySQL

Move to the ports folder, specify some build parameters that will helps speed things up a tad.
cd /usr/ports/databases/mysql51-server
make BUILD_OPTIMIZED=yes BUILD_STATIC=yes
make install clean
Edit /etc/rc.conf and add mysql_enable=”YES”
Apply the configuration file: cp /usr/local/share/mysql/my-medium.cnf /etc/my.cnf
Assign the master password: /usr/local/bin/mysqladmin -u root password ‘new-password’
Start MySQL: /usr/local/etc/rc.d/mysql-server start

2: Apache

cd /usr/ports/www/apache22
make install clean
Edit /etc/rc.conf and add apache22_enable=”YES”
Edit /boot/loader.conf and add accf_http_load=YES

3: PHP

cd /usr/ports/lang/php5
make install clean  !!In the popup at the least check off “Build Apache Module”
4: PDF Lite
fetch ftp://ftp.swin.edu.au/gentoo/distfiles/PDFlib-Lite-7.0.2.tar.gz
sudo mv PDFlib-Lite-7.0.2.tar.gz /usr/ports/distfiles/

5: PHP Extensions
cd /usr/ports/lang/php5-extension 
make install clean !!Confirm that sockets is selected.
Apply the configuration file: cp /usr/local/etc/php.ini-dist /usr/local/etc/php.ini
Enabling Apache to run PHP
Edit the Apache configuration file /usr/local/etc/apache22/httpd.conf and add
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
Change DirectoryIndex index.html to DirectoryIndex index.php index.html
Start Apache /usr/local/etc/rc.d/apache22 start
6: MemCached
cd /usr/ports/databases/pecl-memcache
make install clean
cd /usr/ports/databases/memcached
make install clean
Edit /etc/rc.conf and add memcached_enable=”YES”
Start MemCached with /usr/local/bin/memcached -d -u nobody
Confirming MemCached is running
Run netstat -an to confirm that a process is listening on TCP 11211
It may also be useful to run /usr/local/bin/php -i | grep -i ‘memcache’