1. Introduction

Apache httpd, often shortened to Apache, is a free and open-source web server that listens to HTTP requests and responds with the appropriate content. Additionally, websites are often hosted with Apache.

Apache httpd server listens for incoming requests from clients on a specific port (usually port 80), processes it, and sends the requested resource, such as a web page or file, back to the client, enabling them to view the content on their browser.

Another advantage to Apache httpd is that it’s cross-platform compatible. It runs on various Linux distributions, Windows, and other operating systems.

In this tutorial, we’ll explore how to set up an Apache httpd server on a Linux machine.

2. Direct Installation With Package Manager

Package managers are tools for managing software installations, upgrades, and removal. Most Linux distributions come preinstalled with package managers such as YUM, DNF, and APT.

On Debian-based operating systems like Ubuntu, the Apache HTTP server (httpd) operates under the alias apache2. Debian’s package naming guidelines and the need to avoid conflicts with other Apache-related packages necessitated this renaming.

On the other hand, for RHEL-based OS such as CentOS or Fedora, the Apache HTTP Server goes by httpd.

2.1. Install the Apache httpd Server

We can install the latest version of the Apache httpd server on the Linux machine.

Let’s install it on a Debian-based OS:

$ sudo apt install apache2

Again, let’s install it on a RHEL-based OS:

$ sudo yum install httpd

These commands install the Apache httpd server on the Linux machine.

2.2. Start the Apache httpd Server

By default, the Apache httpd server doesn’t start listening for requests. We must activate the server manually.

Let’s start the server on a Debian-based OS:

$ sudo systemctl start apache2

On the other hand, let’s also start it on a RHEL-based OS:

$ sudo systemctl start httpd

These commands start the Apache web server on the Linux machine.

2.3. Verify the Installation

Now, we’ve successfully started the Apache web server. The Apache httpd server listens on port 80 of the Linux machine. We can send a curl request to this port to confirm that the server is running:

$ curl localhost:80
<html>
  <body>
    <hr>
    <br />
    <center>
       <h1>Apache2 Default Page</h1>
    </center>
    <br />
    <hr>
    <br />
  </body>
</html>

The excerpt above shows the beginning of an HTML document with the body “Apache2 Default Page“. However, the full output would likely include more HTML tags and content depending on the server configuration.

Alternatively, we can use the web browser to check if the service is running by entering the IP address of the Linux machine. The Apache httpd server will display a default web page.

3. Building Apache httpd from Source Code

The Apache httpd server can be built from the open-source code. One advantage of building the Apache HTTP server from the source code is that we can use the latest version of Apache as soon as it’s available, therefore bypassing the wait for our Linux distribution to package and release it.

In this section, we’ll build and set up the Apache httpd server from the source code. All instructions in this section have been tested on a CentOS 7 server with root access.

3.1. Prerequisites

Before building the web server, we need the following packages installed on the Linux machine:

Therefore, let’s create a directory to unpack and install these packages into:

$ mkdir /opt/software
$ cd /opt/software

Let’s download the APR package version 1.7.4 using wget:

$ wget https://dlcdn.apache.org/apr/apr-1.7.4.tar.gz

Additionally, let’s download the APR-Util package version 1.6.3:

$ wget https://dlcdn.apache.org/apr/apr-util-1.6.3.tar.gz

Let’s also download the PCRE package version 10.37 from Sourceforge:

$ wget https://sourceforge.net/projects/pcre/files/pcre2/10.37/pcre2-10.37.tar.gz

Finally, we can use the package manager to install the Development Tools and expat-devel packages:

$ sudo yum group install "Development Tools" -y
$ sudo yum install expat-devel

This way, we’ve downloaded the necessary packages needed to set up the Apache httpd server.

3.2. Download Apache httpd Source Code

In the same directory, let’s download the Apache httpd source code:

$ wget https://dlcdn.apache.org/httpd/httpd-2.4.58.tar.gz

To confirm that we have all the packages downloaded, let’s list the contents of the directory:

$ ls -l /opt/software
total 13484
-rw-r--r--. 1 root root 1122147 Apr 16 2023 apr-1.7.4.tar.gz
-rw-r--r--. 1 root root 556623 Feb 1 2023 apr-util-1.6.3.tar.gz
-rw-r--r--. 1 root root 9825177 Oct 19 09:09 httpd-2.4.58.tar.gz
-rw-r--r--. 1 root root 2299767 May 26 2021 pcre2-10.37.tar.gz

As seen above, all necessary packages, including the httpd source code, have been downloaded.

3.3. Extract and Configure the Downloaded Packages

Notably, the downloaded packages are gzipped tarballs. So, we’ll have to extract them:

$ tar -xf apr-1.7.4.tar.gz
$ tar -xf apr-util-1.6.3.tar.gz
$ tar -xf httpd-2.4.58.tar.gz
$ tar -xf pcre2-10.37.tar.gz

On completion, the extracted folders will be in the directory:

$ ls -l
total 13500
drwxr-xr-x. 28 501 games 4096 Apr 13 2023 apr-1.7.4
-rw-r--r--. 1 root root 1122147 Apr 16 2023 apr-1.7.4.tar.gz
drwxr-xr-x. 20 501 games 4096 Jan 31 2023 apr-util-1.6.3
-rw-r--r--. 1 root root 556623 Feb 1 2023 apr-util-1.6.3.tar.gz
drwxr-xr-x. 13 504 games 4096 Oct 16 15:06 httpd-2.4.58
-rw-r--r--. 1 root root 9825177 Oct 19 09:09 httpd-2.4.58.tar.gz
drwxr-xr-x. 7 1169 1169 4096 May 26 2021 pcre2-10.37
-rw-r--r--. 1 root root 2299767 May 26 2021 pcre2-10.37.tar.gz

Firstly, let’s configure the APR package:

$ cd /opt/software/apr-1.7.4
$ ./configure --prefix=/opt/apache24/apr

Here’s a breakdown of the last command. We’re executing a script named configure located in the current directory. We pass the –prefix flag to the configure script to specify the installation directory, which in this case is /opt/apache24/apr.

The command will generate a Makefile in the current directory:

$ ls
apr-1-config build.conf emacs-mode Makefile README
apr-config.in build-outputs.mk encoding Makefile.in README.cmake
apr.dep CHANGES file_io Makefile.win shmem
apr.dsp CMakeLists.txt helpers memory strings
apr.dsw config.layout include misc support
apr.mak config.log libapr.dep mmap tables
apr.pc config.nice libapr.dsp network_io test
apr.pc.in config.status libapr.mak NOTICE threadproc
apr.spec configure libapr.rc NWGNUmakefile time
atomic configure.in libtool passwd tools
build docs LICENSE poll user
buildconf dso locks random

Let’s build and install the APR package using the Makefile:

$ make && make install

Let’s confirm that the APR package has been installed in the chosen path:

$ ls -l /opt/apache24/apr
total 0
drwxr-xr-x. 2 root root 26 Feb 28 12:23 bin
drwxr-xr-x. 2 root root 108 Feb 28 12:23 build-1
drwxr-xr-x. 3 root root 19 Feb 28 12:23 include
drwxr-xr-x. 3 root root 140 Feb 28 12:23 lib

Secondly, let’s navigate to the APR-Util folder and configure it:

$ cd /opt/software/apr-util-1.6.3
$ ./configure --prefix=/opt/apache24/apr-util --with-apr=/opt/apache24/apr

As before, we’re running the configure script from the current directory. The –prefix flag sets the installation directory to /opt/apache24/apr-util.

However, there’s a slight change. The –with-apr flag instructs the configure script to use our already installed APR library as a dependency by specifying the installation path.

Again, this command creates a Makefile. Let’s use it to build and install the APR-Util package:

$ make && make install

Let’s check if the package was installed:

$ ls -l /opt/apache24/apr-util
total 0
drwxr-xr-x. 2 root root 26 Feb 28 12:34 bin
drwxr-xr-x. 3 root root 19 Feb 28 12:34 include
drwxr-xr-x. 3 root root 164 Feb 28 12:34 lib

Finally, let’s move to the PCRE folder and configure it:

$ cd /opt/software/pcre2-10.37
$ ./configure --prefix=/opt/apache24/pcre

Again, we execute the configure script with the –prefix flag to specify the installation path for the PCRE library. Hence, the last step is to build and install the PCRE package using the generated Makefile:

$ make && make install

Let’s confirm that the PCRE library was installed:

$ ls -l /opt/apache24/pcre
total 0
drwxr-xr-x. 2 root root 60 Feb 28 12:49 bin
drwxr-xr-x. 2 root root 41 Feb 28 12:49 include
drwxr-xr-x. 3 root root 268 Feb 28 12:49 lib
drwxr-xr-x. 4 root root 28 Feb 28 12:49 share

Therefore, we’ve installed and configured the APR, APR-Util, and PCRE packages.

3.4. Set up Apache httpd Server

Now, we can set up our Apache httpd server. Let’s navigate to the httpd folder and configure it:

$ cd /opt/software/httpd-2.4.58
$ ./configure --prefix=/opt/apache24 --with-apr=/opt/apache24/apr --with-apr-util=/opt/apache24/apr-util --with-pcre=/opt/apache24/pcre/bin/pcre2-config

Here’s a breakdown of the last command. We’re executing the configure script in the current directory. Firstly, the –prefix flag sets the installation directory, in this case to /opt/apache/24. Then, –with-apr and –with-apr-util specify the locations of the APR package and APR-Util library, respectively. Finally, the –with-pcre flag instructs the configure script to use the PCRE library by specifying its installation path.

Hence, let’s build and install httpd using the generated Makefile:

$ make && make install

Therefore, we’ve successfully installed the Apache httpd server.

3.5. Start the Service

To start the service, we have to make changes to the httpd.conf file. Let’s navigate to the Apache configuration directory:

$ cd /opt/apache24/conf
$ ls
extra httpd.conf magic mime.types original

As apparent from the output, this directory contains the httpd.conf file, which is the main configuration file for the Apache httpd server. We’ll make changes to this file so we can start the server. Let’s open the file in Vi:

$ vi httpd.conf

Let’s uncomment the following line and replace the value of ServerName with our server’s IP address or Fully Qualified Domain Name (FQDN):

#ServerName www.example.com:80

Next, we save and exit the file.

Finally, let’s move into the /opt/apache24/bin directory to start the server:

$ cd /opt/apache24/bin
$ ls
ab         checkgid   envvars-std   htdbm     httpd       rotatelogs
apachectl  dbmmanage  fcgistarter   htdigest  httxt2dbm
apxs       envvars    htcacheclean  htpasswd  logresolve
$ ./apachectl -k start

We start the Apache httpd server by executing the apachectl script located in the bin directory.

3.6. Verify the Installation

To confirm that the web server is running, let’s check all running processes related to the Apache httpd server:

$ ps aux | grep httpd
root      2642  0.0  0.4  71040  2256 ?        Ss   20:12   0:00 /opt/apache24/bin/httpd -k start
daemon    2643  0.0  0.4 360004  2204 ?        Sl   20:12   0:00 /opt/apache24/bin/httpd -k start
daemon    2648  0.0  0.4 360004  2204 ?        Sl   20:12   0:00 /opt/apache24/bin/httpd -k start
daemon    2666  0.0  0.4 360004  2204 ?        Sl   20:12   0:00 /opt/apache24/bin/httpd -k start
root      2735  0.0  0.1 112812   984 pts/0    S+   20:21   0:00 grep --color=auto httpd

Here’s how this command works. First, we use ps to get a list of all running processes on the Linux machine. Then, the listed output is piped (|) to the grep command. Finally, grep filters out any process that doesn’t contain the httpd pattern.

Additionally, we can enter the server’s IP address into the local browser. The Apache httpd server will load a default web page that says ‘It works!’.

4. Conclusion

In this article, we’ve learned how to set up the Apache httpd server on a Linux machine. In particular, we discussed two ways of setting up the Apache httpd server: using a package manager or building it directly from the source code.

We explored different aspects of the Apache httpd server, such as its function, configuration, and commands. Hence, by understanding the discussed methods, we can successfully set up the Apache httpd server on our machine.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.