Wednesday 8 October 2014

Building apache2 from source on a Linux machine

In this post, I will discuss about the steps used to compile and build apache2 on a Linux machine.

Download the source code to the directory "/usr/src"

=====
cd /usr/src/
wget https://www.pccc.com/downloads/apache/current/httpd-2.2.24.tar.bz2
=====

Extract the source code:
=====
tar -xvjf  httpd-2.2.24.tar.bz2
cd httpd-2.2.24
=======

Run configure to see more options:
======
./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
=====

Run the below commands:
====
make
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 init script to start and stop:
====
#!/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
=====

Now, run chkconfig to add the service to chkconfig list:
=====
chkconfig --add httpd

chkconfig --level 2345 httpd on

chkconfig --list
=====

References:
====
http://www.thegeekstuff.com/2008/07/install-apache-2-from-source-on-linux/
http://www.zrinity.com/developers/apache/apache2src.cfm
=====

No comments:

Post a Comment

Note: only a member of this blog may post a comment.