Pete Freitag Pete Freitag

Building Apache2 From Source on Linux (Redhat 8)

Published on September 21, 2004
By Pete Freitag
linux

This blog entry shows the steps used to compile and build apache2 on linux. The example uses Apache 2.0.46 on Redhat 8 using a bash shell, you will need gcc installed. You can find Apache's install instructions here

Downlaod the latest tar.gz file for apache2

You can download it from httpd.apache.org.

Login as root

You can type su to switch to root. Then run source /etc/profile to ensure that your path environment variable is setup properly.

Extract the source code

In this example we extract the source code to a directory under /usr/local/src/

 
 cp httpd-2.0.46.tar.gz /usr/local/src

 cd /usr/local/src

 gunzip httpd-2.0.46.tar.gz

 tar -xvf httpd-2.0.46.tar

 rm -f httpd-2.0.46.tar

 cd httpd-2.0.46

Now you should be in the directory that contains the source code.

Set compiler options (optional)

If you want you can set some compiler options, this is typically done to create optimized code. One very common thing to do is to set CFLAGS=-O2 or CFLAGS=-O3 (that's an Oh, not a Zero) that tells the compiler how much code optimization to do, setting it to a higher value does more optimization, but also takes longer to compile and may potentially cause unexpected things (not common). O2 is a fairly safe level to use. To do this type the following:

 
 export CFLAGS=-O2

You can also tell the compiler what kind of CPU you have to perform more optimizations, I'm not going to get into that here, but if your interested check out the GCC manual.

Run autoconf (configure)

Now you need to set the configuration options, and check that all libraries needed to compile are present. This is done with a script called configure, to find out what options you can set type the following:

 
 ./configure --help

You will see quite a few options there, we will set the prefix (the directory to install apache, we picked /usr/local/apache2) and also tell it which modules to compile and install. We will tell configure to compile and install all modules as shared DSO libraries, that way we can easily enable and disable them in the httpd.conf file. Here's how we ran configure:
 
 configure --prefix=/usr/local/apache2 --enable-mods-shared=all

Compile Apache

Now to compile apache we run make this compiles the source code into executable binaries.

 
 make

Installing Apache

The next step copies the binaries into the install directory, and sets up the modules.

 
 make install

Starting/Stopping/Restarting Apache

Now to start/stop apache use apachectl in the bin directory of your install dir.

 cd /usr/local/apache2/bin
 
 ./apachectl start
 
 ./apachectl stop
 
 ./apachectl restart

A script for init.d (optional)

Here's a script you can save to /etc/init.d/httpd it is a modified version of the one that came in the rpm for Apache 2.0.40

#!/bin/bash
#
# Startup script for the Apache Web Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#              HTML files and CGI.
# processname: httpd
# pidfile: /usr/local/apache2/logs/httpd.pid
# config: /usr/local/apache2/conf/httpd.conf

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache2/bin/apachectl
httpd=/usr/local/apache2/bin/httpd
pid=$httpd/logs/httpd.pid
prog=httpd
RETVAL=0


# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        daemon $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch /var/lock/subsys/httpd
        return $RETVAL
}
stop() {
        echo -n $"Stopping $prog: "
        killproc $httpd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/httpd $pid
}
reload() {
        echo -n $"Reloading $prog: "
        killproc $httpd -HUP
        RETVAL=$?
        echo
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status $httpd
        RETVAL=$?
        ;;
  restart)
        stop
        start
        ;;
  condrestart)
        if [ -f $pid ] ; then
                stop
                start
        fi
        ;;
  reload)
        reload
        ;;
  graceful|help|configtest|fullstatus)
        $apachectl $@
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $prog {start|stop|restart|condrestart|reload|status"
		echo $"|fullstatus|graceful|help|configtest}"
        exit 1
esac

exit $RETVAL

Next run chkconfig to setup runlevels for which httpd will run:
	
	chkconfig --add httpd
	
	chkconfig --level 2345 httpd on
	
	chkconfig --list
	

Uninstall old rpm packages (Optional)

If you have old rpm apache packages installed, you can check by running:

 rpm -q httpd
 
In my case it lists the following:
redhat-config-httpd-1.0.1-13
httpd-2.0.40-11.3
httpd-manual-2.0.40-11.3
To uninstall one of the rpm's type rpm -e packagename
 
 rpm -e httpd-2.0.40-11.3

It may tell you that you have other packages that depend on httpd, you will have to uninstall them first before you can remove the old httpd server.



apache linux redhat source rpm

Building Apache2 From Source on Linux (Redhat 8) was first published on September 21, 2004.

If you like reading about apache, linux, redhat, source, or rpm then you might also like:

Weekly Security Advisories Email

Advisory Week is a new weekly email containing security advisories published by major software vendors (Adobe, Apple, Microsoft, etc).