Install Apache from Source Code - Overview
Apache is a free and open-source cross-platform web server software. Cross-platform software is a type of software application that works on operating systems including Linux and Windows.
In this article, We are going to install an Apache HTTP server from source code on Rocky Linux 8. It is easy to install Apache server from the default repositories with the following command
[root@testserver ~]# dnf install httpd
However, in this tutorial, we will explain to you how to install Apache from the source code. Installing from source gives you a customization option at the same time it takes a lot of effort, while installation from binary is easier, but you may not be able to customize as you wish.
Advantages of installing from source:
- We can install the latest version and can always stay updated.
- We can install it in any location as per our needs
- We can add some features which may not be provided in the default repository.
- It helps us to remove any unwanted feature during the installation
- We can also have multiple versions of the same program installed
If you are a beginner to Apache web server, Always consider installing from default repositories first. You can try installing from source only if you know exactly why you need to do this.
Lab Environment and Prerequisites
In this tutorial, we will be installing the Apache HTTP server on Rocky Linux 8. Please refer the article for installation Install Rocky Linux 8 [Step-by-Step with Screenshots]
Server Machine:
OS: Rocky Linux release 8.4 (Green Obsidian)
Kernel: 4.18.0-305.3.1.el8_4.x86_64
Apache Server Version: 2.4.48
Step 1: Install pre-requisite packages
The following packages are required to get files and to build the server.
[root@testserver ~]# dnf install wget make gcc apr apr-util apr-devel apr-util-devel pcre-devel openssl-devel redhat-rpm-config
Step 2: Download Apache Source Code
We can download the latest version of Apache from its official website. Refer Downloading the Apache HTTP Server
wget
is the utility for non-interactive download of files from the Web. In the example, we are downloading httpd-2.4.48.tar.gz
to the current working directory. Once downloaded, extract the package by running the tar command as follows.
[root@testserver ~]# wget https://downloads.apache.org//httpd/httpd-2.4.48.tar.gz [root@testserver ~]# tar -zxvf httpd-2.4.48.tar.gz
Step 3: Configure the Apache source tree
Now, change the directory and run the configure command as shown below. You can change the default Apache installation directory path, here we have mentioned /usr/local/
apache directory path. configure
is a script that is generally provided with the source of most Linux packages. It contains code that will “patch” and localize the source distribution so that it will compile and load on your local Linux system. I have also enabled a couple of modules with configure.
[root@testserver ~]# cd httpd-2.4.48/ [root@testserver httpd-2.4.48]# ./configure --prefix=/usr/local/apache
Once configure
has done its job. The output will end like below:
Step 4: Build and install Apache from Source Code
make
utility is to build the software. This runs a series of tasks defined in a Makefile
to build the finished program from its source code.
The make install
command will copy the built program, and its libraries and documentation, to the correct locations.
[root@testserver httpd-2.4.48]# make [root@testserver httpd-2.4.48]# make install
If the make install completes successfully, the end of the output will be similar to below. Now, we have completed installing the Apache web server from the source code.
Installing man pages and online manual
mkdir /usr/local/apache/man
mkdir /usr/local/apache/man/man1
mkdir /usr/local/apache/man/man8
mkdir /usr/local/apache/manual
make[1]: Leaving directory '/root/httpd-2.4.48'
The following are important files and directories that were created after the installation.
/usr/local/apache/htdocs/
- Default Document root/usr/local/apache/conf/httpd.conf
– Main Apache config file/usr/local/apache/
- Where all the Apache2 related files are stored/usr/local/apache/conf.d/
- All the new config files can be added in this directory are included in the main config file/usr/local/apache/modules/
- Location for Apache module config files/usr/local/apache/logs/
- Log files are under this folder/usr/local/apache/bin
- Apache executables/Binaries
Step 5: Start Apache Service
apachectl
is a front end to the Apache HTTP server. It is designed to help the administrator control the functioning of the apache2 daemon. Once the build and installs are completed without any error, Let us start the Apache web server using apachectl
[root@testserver httpd-2.4.48]# /usr/local/apache/bin/apachectl start
Step 6: Configure Firewall
You should be able to access the default URL http://SERVER_IP_OR_HOSTNAME/
. If you have a firewall enabled, enable HTTP service on firewalld. If you plan to use HTTPS then the same also can be enabled.
[root@testserver ~]# firewall-cmd --zone=public --permanent --add-service=http success [root@testserver ~]# firewall-cmd --reload success
Step 7. Add Apache executables to PATH
Apache is successfully installed on the server from its source code. However, We cannot directly use the commands like httpd
or apachectl
without specifying the full path. We can fix this by adding a script file /etc/profile.d/httpd.sh
with the following content.
# vi /etc/profile.d/httpd.sh
pathmunge /usr/local/apache/bin
Once the file is created, log out from the current session and re-login to reload the profile. You should be able to run the Apache binaries without a full path.
Step 8. Configure Apache as systemd service
To manage the Apache service using systemctl, we need to add the service to systemd. Let us create a file /etc/systemd/system/httpd.service
and add the following contents to it.
[Unit] Description=The Apache HTTP Server [Service] Type=forking ExecStart=/usr/local/apache/bin/apachectl start ExecReload=/usr/local/apache/bin/apachectl graceful ExecStop=/usr/local/apache/bin/apachectl stop PrivateTmp=true [Install] WantedBy=multi-user.target
Once the file is created, we can manage the service using systemctl
. In the below example, we are enabling httpd service to start on reboot.
[root@testserver ~]# systemctl daemon-reload
[root@testserver ~]# systemctl enable httpd
Removed /etc/systemd/system/multi-user.target.wants/httpd.service.
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /etc/systemd/system/httpd.service.
Start the httpd service and check the status.
[root@testserver ~]# systemctl start httpd
Check the status of the service
Step 9. Test Apache HTTP web server
In the end let us create a test file under the default document root and test if the web server is serving fine. I just created a test file under /usr/local/apache/htdocs/
and add some contents to it.
[root@testserver ~]# vi /usr/local/apache/htdocs/testfile.html
<h2>YAY! HTTPD is Serving</h2>
When you access the URL http://SERVER_IP_OR_HOSTNAME/testfile.html
it should work. The snippet as shown below
Summary
After this article, You have a fully working Apache HTTP server in the latest version installed on your system. This process might take some time, but you will have full control over the service.