Installing Apache on CentOS 8

Joshua Hunter
6 min readSep 24, 2022

--

Overview

Today we are going to be installing Apache Web Server on CentOS 8 Linux. Apache is an extremely popular open-source software that holds approximately 30% of the market share for web servers. There are numerous reasons why many choose to utilize Apache for their web server needs, a few being: it is free and open source, reliably stable, flexible, and easy to configure, as we will see in the following scenario.

Scenario

As a new Linux Admin, your team wants to test a webpage out on their test Linux server. They would like to see if they can access the page over the internet. Your job is to install the test web server that will serve up the webpage for your team.

Objectives:

  • Update all packages on the server
  • Install an Apache HTTP Web Server
  • Enable the Apache Web Server
  • Grab the public IP of your server and test the webpage over the internet.

Prerequisites

  • Slight familiarity with the Linux operating system
  • CentOS 8 installed in a virtual environment, instance, or physical machine
  • A user account with sudo privileges
  • Access to the internet

Step 1 — Installing Apache

First off we are going to want to make sure we update the packages on our system. Packages are the information necessary to install, update and run software on a Linux system. You can interact with packages using a package manager. The package manager for Centos 8 is yum.

We can update our packages using the following command with elevated sudo (superuserdo) privileges. You will be prompted for your password after initiating the command.

sudo yum update

After running the command all dependencies should be resolved and a complete status should be shown as well.

Next we will install Apache by running the following command with a -y to automatically confirm installation.

sudo yum install httpd -y

We will then run the following commands to “start” the Apache service on our server and “enable” Apache to start at system reboot as well.

sudo systemctl start httpdsudo systemctl enable httpd

We can confirm Apache is running and enabled with the following command

sudo systemctl status httpd

Press “q” to quit the status view.

Step — 2 Configuring the Firewall

We will now need to install and configure our firewall as it allows or blocks traffic based on a defined set of security rules. We will need to configure the firewall to allow web traffic to and from our server over HTTP (Unencrypted) and HTTPS (Encrypted).

This fist command will be familiar to us as we used it initially to install Apache.

sudo yum install firewalld -y

Our firewall does not come pre configured to allow HTTP and HTTPS so we will need to run the following commands to allow those services.

sudo firewall-cmd — permanent — add-service=httpsudo firewall-cmd — permanent — add-service=https

We will then run this command to allow our changes to take effect.

sudo firewall-cmd — reload

Let us now run the following commands to make sure the correct services are enabled and that our firewall is in an active state.

sudo firewall-cmd — permanent — list-allsudo systemctl status firewalld

We see that our firewall comes pre-enabled from the vendor so it will start if the system reboots. We can always run sudo systemctl enable firewalld.

Step 3 — Connecting to your website

You should now be able to access your website.

To do this we will need our server’s public IP address and enter it into our browser. Use the following command to get your public IP address.

curl -4 ifconf.co

Congratulations! If you are seeing the test page then you have properly configured your server and can now use it as you see fit.

Supplementary Steps — Customizing Your Page and Automating the Installation With Bash Scripts

Customizing Your Page

The test page tells us we can add our own content if we navigate to the following directory /var/www/html. We will use the built in text editor nano to input an html text file to alter what is displayed on our webpage.

First we will need to navigate to the proper directory.

cd /var/www/html

Next we will use sudo nano index.html to create and name a new text file in our current working directory.

Copy and paste the following text into the nano editor. Customize it as you see fit.

<!DOCTYPE html><title>Level Up In Tech</title><body><h1>Level Up In Tech</h1><p>Nothing can happen until you swing the bat.</p></body></html>`

Now press “ctrl o” then “enter” to save and “ctrl x” to exit the file.

Refresh your web page and you should now see your new content displayed.

Automating The Process

We can cut down significantly on install and configuration time of our Apache web server by utilizing very simple Bash Scripts. Bash Scripts at a high level are text files containing commands to be executed.

We will be creating 2 scripts. The first we will use to install Apache and the second will be for the firewall just in case we want to change the configuration separate of our Apache install.

We will use nano apachescript.sh to create and name a new text file in our current directory.

Copy and paste the following text into the nano text editor.

#!/bin/bashsudo yum updatesudo yum install httpd -ysudo systemctl start httpdsudo systemctl enable httpdsudo systemctl status httpd

#! — (shebang) tells the interpreter what language we want to use.

Now press “ctrl o” to save and “ctrl x” to exit the file. We can repeat these steps to create a script for the firewall.

nano firewallscript.shPaste into nano text editor#!/bin/bashsudo yum install firewalld -ysudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --permanent --add-service=httpssudo firewall-cmd --reloadsudo firewall-cmd --permanent --list-allsudo systemctl status firewalld`

We will use the following command to make our file executable from its current location.

Chmod +x apachescript.sh <or whatever you choose to name your file>

We can now execute the file from its current location by using the following.

./apachescript.sh

We should now see Apache running and enabled indicating that our script has run successfully.

--

--