Hosting an Apache Web Server via AWS EC2 using CentOS

James Williamson
7 min readOct 21, 2023

--

For this tutorial, we are going to use Amazon EC2 running a CentOS server in order to host an Apache web server!

Let’s start with what you need in order to do this:

  1. Free tier AWS account

2. Basic knowledge of EC2 and SSH Connections (However we will be going through what to do here)

3. Basic understanding of the CLI on your machine (we will be using PowerShell for Windows)

Part One: Use Case

You have been hired by the Level Up Bank to create a new web server using CentOS which will host the bank’s website and provide information about its services as well as allowing customers to access their accounts. Now, we won’t be building out the entire website as we aren’t web developers here, however, we need to create the space in which the developers can create the sight.

Let’s get started!

Part Two: Hosting a Website using Apache webserver on CentOS

The first thing we need to do is create an EC2 instance. Go to your EC2 management console inside of AWS and hit Launch Instance.

Once done, we will need to select a couple options in order to set up the instance correctly. The first thing we need to do is name our instance. As you can see I will name mine “LUIT_CENTOS_BANK.”

Once done in the AMI search bar, we need to search for the CentOS options available. Type in CentOS as shown above and press enter. This will bring you to a menu where you will select the AWS Marketplace AMI’s section and then find the CentOS Stream 8 (x86_64) distro.

When you click on it, it will bring up additional details and as you can see, this distro is free tier eligible which is perfect.

Once done, go back and click the orange select button on the right.

Back on the EC2 setup, you will see the instance type section marked as T2.micro which is perfect for what we will be doing today.

Next we will need to create a key pair in order to SSH into our instance. Clock on “Create new key pair.”

On the key pair menu, name your keys something easy to recognize, for this, I will name mine, CENTOSKEYS. All other options can stay the same. Click on “Create key pair.”

*ENSURE YOU KEEP TRACK OF THE FILE PATH YOU SAVE YOUR KEYS TO AS WE WILL NEED THAT INFO FOR THE SSH CONNECTION*

We also need to create a security group in order to allow access to our website. Under firewall, configure your settings as shown below.

Now we can launch our instance!

Since this may be your first time with subscribing to the marketplace due to using CentOS it may take a few minutes for the instance to boot up, however once it does, you can select it from your instances management console and hit connect.

Once you are on the connect menu, click over to SSH client and keep in mind again where you stored your keys, and another command on this screen pointed out which we will use to SSH into our instance. It is noted by the second arrow at the bottom.

So lets open up PowerShell. We will need to get into the path where we have our keys stored which is in the downloads folder for me. For that I will use the following command:

cd downloads

Once you are here, you can copy and paste the command from the SSH connect screen noted above by the arrow.

So here we run into our first problem! It is saying permission denied and doesn’t let you SSH.

This also caused me some problems, however the solution is simply changing the user in the SSH command as follows:

ssh -i "insertkeysname.pem" centos@ec2-3-85-203-186.compute-1.amazonaws.com

Ensure you replace the keys file name with your file name AND replace the ec2 instance connection route with your own which you can find on the SSH connection page.

Leaving the user as ec2-user won’t allow you permission into the instance so changing that portion of the command to “centos” allows us access. Once you have access it will look like this.

Alright now that we are here, we need to do a couple things before we can get a website up and running.

First, we need to update the server with the following command:

sudo yum update -y

This will take a while the first time you run it so just wait it out or go grab you a soda, water, or my personal favorite, coffee, and come back after a few minutes.

Next, we will install the Apache Web Server:

sudo yum install httpd -y

Now, let’s start the Web Server:

sudo systemctl start httpd

Finally, this command will automate apache to start up on instance launch:

sudo systemctl enable httpd

Alright! Lets see if what we just did worked! Back in the AWS console find the Public IPv4 address for your instance and copy and paste it into your search bar.

An BOOM! Our site is UP!

Let’s do one more thing to kick it up a notch!

Part Three: Replace the Server test page with a basic html file

Back over on your CLI, we will first need to install a html editor onto our instance. For this we can choose from a few Linux based options such as nano, vim, or vi. We will use nano for this example. Use the following command to install nano.

sudo yum install nano

Once you have done this, we will need to use a command in order to get into the file path where we will create our html file with nano. From your CLI type in the following:

cd /var/www/html/

Once you are in this file path, type the following to open up an editor and create the file name index.html.

nano index.html

You can copy and paste the following code into that window:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to LUIT!!</title>
</head>
<body>
<h1>Welcome to LUIT - Blue Team</h1>
</body>
</html>

Once done you will press “ctrl 0” to save and then “ctrl x” to exit. Press Y on the first option and then hit enter on the file name.

Back on the command line to verify your file in now in that file path type the following command to list all files in that path.

ls

Lastly we want to ensure the correct user and group are assigned to the overarching directory the file is held in, otherwise the webserver may not pull the html file.

Run the following command in order to ensure the proper ownership is established.

sudo chown -R centos:apache /var/www/

Now lets restart our server with the following command

sudo systemctl restart httpd

Finally, lets head over to our public IP using a web browser to check it out!

And there you go!! You now know how to create a basic HTML website using Apache and AWS EC2 with CentOS!!

Wrap Up

If tutorials like this pique your interest and you are enjoying the journey, please follow me here and connect with me on LinkedIn!

https://www.linkedin.com/in/james-williamson-phr/

See you next time.

--

--

James Williamson

Talent Acquisition Manager at a tech company looking to transition into working on the cool stuff I recruit people to work on! #DevOps