Installing an Apache HTTP Server on CentOS 8
Hey, welcome to my journey! Have you ever wondered what happens when you go to a websites homepage such Google or Facebook? How does your computer or phone connects to the website and display content? Most likely that site is using one of the many forms of web servers including Apache, Nginx, or ever Tomcat. Today I’m going to show you how to install Apache onto a CentOS server.
If you’re new you may be asking what is Apache? Apache HTTP Server is a free and open-source web server that delivers web content through the internet intially launched in 1995. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web. What does that have to do with websites? Well, when you click on a link from Google, your web browser is sending a request to a server to display that information. Apache accepts this request and sends the requested data (text, images, videos) to be displayed as a web page to you.
CentOS stands for Community Enterprise Operating System. Its one of the many flavors of Linux distributions. Originally release in 2004, its highly customizable, stable and secure.
Ready to jump in? You’re a new admin and your team needs you to stand up a CentOS Linux server with Apache installed.
What you’ll need to get started:
- A connection to your cloud server by SSH (secure socket shell port 22) through your command terminal.
- Have access to an account with sudo privileges. (This allows a user to execute commands as the root or a different user).
- A server with CentOS 8 installed.
SSH to Start a Secure Connection
We use “SSH” in order to gain access to server remotely. It provides a secure connection between two endpoints.
In order to SSH you would use terminal on your local device and enter the command:
ssh user_name@host_ip_address
- ssh: instructs the system to make an secure connection with the host IP address.
- user_name: is your assigned credentials in order to access the host.
- host_ip_address: is the address for the machine being accessed.
Update packages on CentOS Server
You always want to check for package updates this ensures system stability, dependencies and system security.
Use the following command to check for available updates and packages:
$ sudo yum update
We’re using the sudo command in order to run these commands. Sudo escalates a user privileges for 5 minutes. In a dev environment you may not want to give user access to root for security issues since root is the most powerful user “the gatekeeper”.
The system will return a list of updates. You should see the following message showing updates are complete:
Install an Apache HTTP Server
To install Apache we’re going to use dnf instead of yum. DNF is the successor to YUM more powerful and it makes it easy to maintain groups of packages and capable of automatically resolving dependencies.
sudo dnf install httpd
As you can see the package is ready to be installed, enter y to confirm you want to install Apache HTTP. In cases of automation and you didn’t want confirmation you would use the following command:
sudo dnf install httpd -y
Once the installation is finished, run the following command to enable and start the server:
sudo systemctl enable httpd
sudo systemctl start httpd
“Enable” will allow the server to start at boot time. Let’s check the status of the service:
sudo systemctl status httpd
Ok! The server is up and running, lets see if we can access it.
Test the Website
Use the following command to retrieve your IP address if you don’t have an domain name associated with this server.
curl httpbin.org/ip
Enter that IP into your browser of choice and lets see if you can access the website.
Uh Oh! It looks like our Apache server isn’t allowing external connections. Lets fix that!
The Brick “Firewall”
With data breaches at a all time high, you wouldn’t want malicious actors gaining access to your server. Unfortunately, this is also blocking us from being able to reach it ourselves. Your Apache web server runs on port 80 (HTTP) by default. We need to open that port in order to gain access.
First let’s install the firewall:
sudo dnf install firewalld
Looks like mines was already installed. No big deal, let’s go ahead and start it.
sudo systemctl start firewalld
And let’s check the status.
Next, make the configuration change that we need:
sudo firewall-cmd --permanent --add-service=http
You should receive an success output.
Now to apply the changes, you’ll have to reload the firewall in order for outside connections to be made.
sudo firewall-cmd --reload
Is it up?
Go ahead and retry your public IP and lets see if we can access the web page.
Success! We can access the HTTP Server Test Page and its ready to be passed off to your team.
Now as an unexpected twist your team also needs you to automate this process and display an custom HTML page. Lets get started!
We all like organization so let’s make a directory specifically for scripts:
mkdir Scripts
Change into that directory using the cd command.
Using the touch command make a file called apache.sh.
Lets edit using:
vim apache.sh
This will open up a text editor which allows for you to write your script and use the following script to install Apache, firewall, and html page.
#!/bin/bash
# Update packages
sudo dnf update -y
# Install Apache Webserver
sudo dnf install httpd -y
# Enable Apache HTTP Web server
sudo systemctl enable httpd
# Start Apache server
sudo systemctl start httpd
# Install firewall
dnf install firewalld -y
# Start firewall
systemctl start firewalld
# Firewall Configuration
sudo firewall-cmd --permanent --add-service=http
# Reload Firewall
sudo firewall-cmd --reload
# Gives current user permissions to edit /var/www/html
sudo chown -R $USER:$USER /var/www
# Creates specified text in var/www/html
sudo echo "<html><head><title>Welcome to LUIT - Green Team</title></head><body><h1>Welcome to LUIT - Green Team</h1></body></html>" > /var/www/html/index.html
# Echo completion
echo Web Server active, with Level Up In Tech
In a nutshell what this scripts does is the same step we did earlier but in an automated fashion. It will also create a index.html file located in “/var/www/html” with the indicated message.
Use the following command to save your work:
wq!
Next you would have to make this script executable we can do that use this:
chmod u+x apache.sh
Your script will be green depending on your terminal indicating it is now executable.
To run this script we’ll use the following command:
./apache.sh
Using the same step as above retrieve your IP address:
curl httpbin.org/ip
There you have it, you’ve set up an Apache Web Server with a custom landing page for your team.
If you’ve made it here thanks for reading and feel free to connect with me on LinkedIn.