}

How to install Apache2 from source code using git on Linux

Created:

Introduction

Step 1: Install requirements

First we will install some requirements:

sudo apt-get install git svn libtool-bin libpcre3-dev

Step 2: Download the code

We are going to download the code using the github mirror. Since the default cloned branch is trunk we will switch to the 2.4.25 tag.

git clone https://github.com/apache/httpd.git
cd httpd
git checkout tags/2.4.25

Now we are going to compile apache2 code. Remember to add the --prefix=PREFIX to the configure if you wan to install apache2 in a different location.

svn co http://svn.apache.org/repos/asf/apr/apr/trunk srclib/apr
./buildconf
./configure
make
make install

Step 3: Adding apache2 as a service on boot

There are several ways to do this, we are going to explain one that is very used. To start Apache2 at boot we need to create a file and add it to the /etc/init.d directory. Create a new file called /etc/init.d/apache2 with the contents

#!/bin/sh
case "$1" in
  start)
    echo "Starting Apache ..."
    /usr/local/apache2/bin/apachectl start
  ;;
  stop)
    echo "Stopping Apache ..."
    /usr/local/apache2/bin/apachectl stop
  ;;
  graceful)
    echo "Restarting Apache gracefully..."
    /usr/local/apache2/bin/apachectl graceful
  ;;
  restart)
    echo "Restarting Apache ..."
    /usr/local/apache2/bin/apachectl restart
  ;;
  *)
    echo "Usage: '$0' {start|stop|restart|graceful}" > &2
    exit 64
  ;;
  esac
exit 0

Add execute permission to the new file:

chmod +x /etc/init.d/apache2

Test the new script using:

sudo /etc/init.d/apache2 start
psax | grep httpd

If everything was done correctly the output should be:

 1533 ?        Ss     0:00 /usr/local/apache2/bin/httpd -k start
 1534 ?        Sl     0:00 /usr/local/apache2/bin/httpd -k start
 1535 ?        Sl     0:00 /usr/local/apache2/bin/httpd -k start
 1536 ?        Sl     0:00 /usr/local/apache2/bin/httpd -k start
 1619 pts/0    S+     0:00 grep --color=auto httpd