Compilation and installation of Apache HTTPD from source code

Most Linux distributions provide a specific version of Apache HTTPD binary packages from their main repositories. The downside is that this version is often not updated till the next distribution’s major release.

To take advantage of features available only from a newer version or to build a customized binaries, compilation and installation of Apache HTTPD from the source code is straightforward.

This tutorial describes the compilation and installation steps of Apache HTTPD under RPM-based (Fedora, CentOS, …) and Deb-based (Debian, Ubuntu, …) Linux distributions. The latest available version (2.4.x) is used.

Apache HTTPD source code

The directory /usr/local/src/ is generally used for build-related activities (requires root privileges). It is used in this tutorial as the build directory.

The source code of Apache HTTPD is available as a tar file from: http://httpd.apache.org/download.cgi.

The following are the steps to download and extract the source code in the build directory (The version numbers may be changed as needed):

cd /usr/local/src/
wget https://downloads.apache.org//httpd/httpd-2.4.43.tar.gz
tar -xzf httpd-2.4.43.tar.gz
ls -l
cd httpd-2.4.43
pwd
ls

Installation of build dependencies

The compilation of Apache HTTPD requires the installation of the development tools (C compiler, linker, …) and some additional libraries.

The required libraries are:

  • apr
  • apr-util
  • openssl
  • expat
  • prce

Most Linux distributions provide these libraries from the main repositories. If the system includes an old version of a library, its source code may be downloaded and compiled separately. A later section presents how to download and build the source code of the libraries apr and apr-util as part of the Apache HTTPD compilation process.

On RPM-based (Fedora, Centos, …) Linux distributions, the libraries development packages may be installed using the following commands:

yum group install development
yum install openssl-devel expat-devel pcre-devel apr-devel apr-util-devel

On Deb-based (Debian, Ubuntu, …) Linux distributions, the libraries development packages may be installed using the following command:

apt install build-essential libssl-dev libexpat-dev libpcre2-dev libapr1-dev libaprutil1-dev

Build configuration options

After the download of Apache HTTPD source code and the installation of the build dependencies, the build configuration options may be set (compiler options, installation directory, enable/disable modules, …).

Because Apache HTTPD uses the Autoconf build system, it provides a script called configure to set the build configuration options and generate the Makefile files.

Unless some tweaks are needed, the defaults may be kept. The default is to compile most of the modules and to set the installation directory to /usr/local/apache2/.

To use the defaults, the configure script is called without arguments:

cd /usr/local/src/httpd-2.4.43
./configure

To list all the possible options, the script configure is called with the –help argument:

cd /usr/local/src/httpd-2.4.43
./configure --help

The following sections present some useful build configuration options.

Installation directory: –prefix option

The installation directory is different from the build directory. It is where the resulting binaries and configuration files will be placed once the compilation succeeds and the command make install is executed (presented in the installation section).

The –prefix option is used to select a different installation path. This option is useful if we need to build and install different versions of Apache HTTPD.

The following example presents how to set the installation directory to /opt/apache-2.4.43/:

cd /usr/local/src/httpd-2.4.43
./configure --prefix=/opt/apache-2.4.43/

How to enable/disable a module: –enable-MODULE / –disable-MODULE

Most of the Apache HTTPD modules are built by default as dynamic libraries. The –enable-MODULE option may be used to enable a module or change its build type from dynamic to static. The –disable-MODULE option is used to disable a module from being built. MODULE is replaced by the actual module name.

The following example shows to build the module rewrite as a static library:

cd /usr/local/src/httpd-2.4.43
./configure --enable-rewrite=static

Compiler options

When necessary, compiler options are passed to the configuration script as environment variables.

The variables are summarized in the following table:

CCUsed to select the C compiler command. It defaults to cc (the GNU compiler) but clang is an option too
CFLAGSUsed to pass C compiler flags
LDFLAGSUsed to pass linker flags
LIBSUsed to pass libraries to the linker
CPPFLAGSUsed to pass (Objective) C/C++ preprocessor flags
CPPUsed to pass C preprocessor parameters

The following example presents how to pass an optimization flag to the C compiler:

cd /usr/local/src/httpd-2.4.43
export CFLAGS=-o2
./configure

Compilation and installation

After the execution of the configure script to set the build configuration options and generate the Makefile files, the compilation process is started with the make command.

The following example presents how to start the compilation process:

cd /usr/local/src/httpd-2.4.43
./configure
make

After the compilation succeeds, the installation into the installation directory may be started with the following command:

make install

The installation places all the necessary binaries and configuration files in the installation directory /usr/local/apache2. The executable files are placed in /usr/local/apache2/bin/. The configuration files are placed in /usr/local/apache2/conf/.

How to list the compiled modules

The compiled modules may be listed using the following command:

/usr/local/apache2/bin/httpd -M

Start and stop the service

The new build of Apache HTTPD may be started using the command /usr/local/apache2/bin/apachectl.

To start the service:

/usr/local/apache2/bin/apachectl start

To stop the service:

/usr/local/apache2/bin/apachectl stop

Building apr and apr-util from source code

The required libraries for the compilation of Apache HTTPD may be installed from the Linux distribution’s main repositories, but in some situations, Apache HTTPD requires a more recent version of a library that is not available from these repositories. In this case, the library’s source code may also be downloaded and compiled alongside Apache HTTPD.

The libraries apr and apr-util are two of the core libraries that Apache HTTPD relies on for its internal work to ensure portability across many operating systems.

The Apache HTTPD source code includes a subdirectory that is called srclib. It is used to hold the libraries that may be built and used by the server instead of using the system libraries.

To integrate the apr and apr-util libraries source code as part of the compilation process of Apache HTTPD, their source code is downloaded and extracted inside the subdirectory srclib.

The following example presents how to download and extract the source code of the two libraries apr and apr-util. When the tar file is extracted, the version number should be stripped out.They are available for download from the official website: https://apr.apache.org/download.cgi.

cd /usr/local/src/httpd-2.4.43/srclib/
wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
tar -xzf apr-1.7.0.tar.gz
mv apr-1.7.0 apr
ls

wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
tar -xzf apr-util-1.6.1.tar.gz
mv apr-util-1.6.1 apr-util
ls

cd /usr/local/src/httpd-2.4.43
./configure
make
make install

How to upgrade from version 2.x to 2.y

The update of an Apache HTTPD that is installed from the source code is possible. The recommended practice is to use a separate installation directory for each version and copy the configuration sub-directory from the old version to the new directory.

The following example presents how to update an already installed version 2.4.0 that is installed in /usr/local/apache-2.4.0, to a newer version 2.4.43 that will be installed in /usr/local/apache-2.4.43.

To download, compile and install the new version:

cd /usr/local/src/
wget https://downloads.apache.org//httpd/httpd-2.4.43.tar.gz
tar -xzf httpd-2.4.43.tar.gz
cd httpd-2.4.43
./configure --prefix=/usr/local/apache-2.4.43
make
make install

Stop the service from the old version:

/usr/local/apache-2.4.0/bin/apachectl stop

Copy the configuration directory from the old version’s directory to the new version’s one:

rm -rf  /usr/local/apache-2.4.43/conf/
cp -rp /usr/local/apache-2.4.0/conf/ /usr/local/apache-2.4.43/

Start the service from the new version:

/usr/local/apache-2.4.43/bin/apachectl start

References

Compilation steps: http://httpd.apache.org/docs/2.4/install.html

Configuration option: http://httpd.apache.org/docs/2.4/programs/configure.html