How to launch an Apache Web Server from the CLI

Marshall Hubbard
Nerd For Tech
Published in
6 min readFeb 15, 2022

--

Alright guys, welcome back to my blog! I decided to take you guys back to the basics and show you how to install an Apache web server directly from the CLI. If you have been following my page for a while, this is actually going to be a simple task compared to my previous projects.

The objective: Your team wants to test a webpage out on their new CentOS 7 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.

Before we dive into this, let’s start with some definitions so that we are all on the same page.

  • What is Apache? Apache is a popular open-source, cross-platform web server that is, by the numbers, the most popular web server in existence. It’s actively maintained by the Apache Software Foundation. Here are a few companies using Apache to host their website: Cisco, IBM, Salesforce, General Electric, Adobe, VMware, Xerox, LinkedIn, Facebook, Hewlett-Packard, AT&T, Siemens, eBay, and many more (source).
  • What is CentOS7? CentOS (from Community Enterprise Operating System; also known as CentOS Linux) is a Linux distribution that provides a free and open-source community-supported computing platform, functionally compatible with its upstream source, Red Hat Enterprise Linux (RHEL). (Source)
  • What is the CLI? The command line interface is a program on your computer that allows you to create and delete files, run programs, and navigate through folders and files. On a Mac, it’s called Terminal, and on Windows, it’s Command Prompt. (Source)

Alright, I believe that we are good to go so let’s go ahead and get this party started!

Step 1- Update your server

Update

First things first, figure out what linux flavor you want to use because that might change the commands for this project when you do it. I personally use CentOS 7 for majority of my projects and I ssh into them through a cloud playground server so that I am not using my own host terminal for projects.

But if you are you using a CentOS 7 server, first thing I want you to do is make sure that your server is up to date as far as packages. To do this, you need to be the root user or you can just execute the command as the superuser like I did here: sudo yum -y update

This will take a second but eventually it will say “Complete!” at the bottom of your terminal letting you know that the update was a success but as you can see from my screenshot, it removed my older kernel and installed a newer version. It also updated a couple different files. This is a good habit to get into whenever you are doing a project whether it is with AWS, Python, Terraform or just from the CLI itself. Always run a command to make sure that you are using the latest version.

Step 2- Set up your Firewall

Depending on the server that you use, you can probably skip this step. If you are using Ubuntu, a firewall typically comes preinstalled on your server. But since I like to make things difficult for myself (I have no idea why), I chose to use CentOS 7 and install firewalld myself. Luckily, this is a fairly easy step and I will show you how to do it.

First off we are going to run the command- sudo yum install firewalld. This command will begin the installation process of installing your firewall on to the server.

Next thing we will do is enable the firewall with the command systemctl enable firewalld. This command will then enable the firewall to start everytime the server is booted up. The next command we will execute is systemctl start firewalld. This command will immediately start the firewall. The next command that I personally like to use for any system is to check the status of whatever I just got done installing. Worst thing to do in my opinion is to work through a assignment just to get to the end of all of your troubleshooting just to realize that it was never installed properly and is not currently running. I used the command systemctl status firewalld to show the status of the firewall and to make sure that it was currently active and running. You can see the results in green on the screenshot. We will come back to finish off our firewall after we install Apache.

Step 3- Install Apache

Installing Apache is actually the easiest part of this project. Start off by using command sudo yum install httpd. This command will start on the installation process of installing Apache to your terminal and will only take a few seconds to complete. If you are using a cloud server, you might be signed out while the server reboots itself and get a return at the end such as “connection lost”. No worries, give it a couple seconds and then SSH back into your server. Your server might just be rebooting.

Like I always say, double check to make sure the service that you are installing is actually installed before you continue and wonder why nothing works. Ask me how I know…. Checking to make sure Apache is installed is done by executing the command httpd -v

Step 4- Apache and the Firewall

Now we will allow the default port for HTTP (80) and HTTPS (443) through the firewall by using the command sudo firewall-cmd — permanent — add-port=80/tcp for HTTP and sudo firewall-cmd — permanent — add-port=443/tcp for port 443. Once this is done, we have permanently allowed Apache to operate on ports 80 and port 443 through Firewalld.

The next step is to run the command sudo systemctl start httpd to start up Apache. Then run the command sudo systemctl enable httpd to make sure Apache starts when the server is first started. Last step is to check the status of Apache with the command sudo systemctl status httpd. Once this is done and you see it return that Apache is active and running, proceed to the next step because we are almost done.

Step 5- Check your work

After about a minute or two, check to see if the Apache web server is working by placing the public IP address into your web browser and see what comes up. If you followed the steps, you should get this page showing that the Apache web server is active.

--

--

Marshall Hubbard
Nerd For Tech

LE Professional transitioning to DevOps. Currently learning at Level Up In Tech.