Automating an Apache web server with an Amazon EC2 instance: A step-by-step guide

Aalok Trivedi
10 min readDec 11, 2022

Welcome! In a previous article, I detailed how to install an Apache web server on a CentOS 8 cloud server. I highly recommend quickly reviewing this article, as many of the techniques will apply to this walkthrough.

Intro

Nowadays, Amazon Web Services (AWS) and “the cloud” is all the rage, and for good reason! Fast, reliable, and affordable cloud computing has been a game-changer for deploying and scaling applications. There’s no longer the need to house, maintain, and scale your servers or figure out how to allocate resources to different services. AWS does all this for us, so we can focus on delivering the best product to our customers.

I’m not going to lie, AWS is vast, robust, and can be intimidating, but we all have to begin somewhere, right? In this walkthrough, we’ll start with the basics and use the AWS management console to deliver a simple website with an Apache web server on an EC2 instance.

Scenario

Your company, Brainiac, wants to start shifting from using on-premises servers to using servers in the cloud. Rather than purchasing all the infrastructure in a data center, they ask you to create an EC2 instance in AWS to host their new website.

Steps we’re going to take

  1. Create a bash script that installs an Apache web server and creates a simple HTML file.
  2. Launch an AWS EC2 instance inside the newly created VPC.
  3. SSH into the EC2 instance using a command line terminal and make sure everything is running smoothly.

Prerequisites:

  • An AWS account with an IAM user set up.
  • Basic knowledge of the Linux OS and commands.
  • Basic knowledge of bash scripting.
  • Access to a command line terminal or shell.

Terminology

This topic often hits you with vocabulary overload, and it can be overwhelming, so here is an overview of some of the terms we’ll be discussing.

AWS (Amazon Web Services)

AWS is a cloud computing service by Amazon that provides dozens of cloud services that allow customers to easily deploy applications without the need for expensive on-premises server infrastructure and maintenance. Find out more about AWS here.

EC2 (Elastic Compute Cloud)

Amazon EC2 (is a cost-effective web service that provides users with compute capacity to build and launch applications quickly. It’s one of AWS’ foundational compute services that allows you to rent and manage virtual servers in the cloud.

EC2s provides elastic loading balancing, so it automatically distributes incoming application traffic. It also provides auto-scaling, so it adds or replaces instances based on demand. Find more info on EC2 here.

VPC (Virtual Private Cloud)

A vpc is a foundational service that allows you to create a secure private network in the AWS cloud where you launch your resources. Think of it as your own private slice of the Amazon cloud. An EC2 instance is launched Inside this VPC, along with a public (or private) subnet and internet gateway to allow traffic to and from the internet. Find more info on VPCs here.

Apache

Apache is a free, open-source service that allows users to create HTTP servers and deliver websites and web applications. Find more info on Apache here.

Here is a helpful, visual diagram of what we’re essentially creating:

Diagram courtesy of A Cloud Guru

Alright, Let’s get started!

Step 1: Create a bash script to install an Apache web server

Let’s first prepare our script that will install and run Apache. Creating a bash script is a great way to eliminate repetitive, manual tasks and speed up the process. What this script will do:

  1. Install Apache
  2. Enable and start Apache
  3. Create an index.html file
  4. Write some basic HTML to the index.html file

I will use VS Code but feel free to use your favorite text editor. Start a new file and name it ‘apache-install.sh’ Don’t forget the .sh extension to make it an executable file.

As always, we start bash scripts with the ‘shebang’ interpreter to let the shell know this is an executable bash script.

#!/bin/bash

The first command we need to run is to install Apache.

#install apache
sudo yum install -y httpd

Now enable and start the Apache server.

#enable and start apache
sudo systemctl enable httpd
sudo systemctl start httpd

Apache creates a ‘www’ directory inside root, in which all the web files will live, so we have to navigate to that directory. Let’s also create empty directories to store our CSS and Scripts.

#navigate to the html folder that apache creates on install
cd /var/www/html

sudo mkdir CSS
sudo mkdir Scripts

Almost there! Time to create the index.html file. Remember, this will live under /var/www/html. We also have to change the file permissions for the index.html file so we can read and write to it.

#create index.html file and change file permissions
touch index.html
sudo chmod 775 index.html

Finally, create the HTML and redirect it into the index.html file

sudo echo '<html> <body> <h1> Hello! Apache server successfully started! </h1> </body> </html>' > index.html

And we’re done! We just created a script that will install an Apache web server with a custom index.html file! Save this file. We’ll need it later.

Here is the full script:

#!/bin/bash

#install apache
sudo yum install -y httpd

#enable and start apache
sudo systemctl enable httpd
sudo systemctl start httpd

#navigate to the html folder that apache creates on install
cd /var/www/html

sudo mkdir Css
sudo mkdir Scripts

#create index.html file and change file permissions
touch index.html
sudo chmod 775 index.html

sudo echo '<html> <body> <h1> Hello! Apache server successfully started! </h1> </body> </html>' > index.html

In the next step, we’ll create an EC2 server instance on AWS.

Step 2: Launch a new EC2 instance

Alright! We’re halfway there. Now that we have our script ready, it’s time to create and launch our EC2 server instance. Remember, an EC2 instance is our virtual cloud server where we can deploy our applications efficiently and use its computing power as needed.

Setting up an instance

Sign into your AWS account (Make sure, you’re signing in as an IAM user, NOT root) and navigate to EC2 services. The easiest way to find it is to use the search bar at the top.

Make sure the N. Virginia (us-east-1) region is selected (top right corner).

To launch a new instance, click the orange ‘Launch Instance’ dropdown button (and then ‘Launch instance’ again).

Give the server a name. I’ll be naming it ‘brainiac-web-server.

Each EC2 instance comes with several options for what OS image you want your server to be configured to. We’re going to stick with Amazon Linux, which is included in a free tier account.

The instance type will be t2.micro. More than enough for this demo and free tier eligible (We’ll get up to 750 hours of free computing services).

Next, we will create a key pair to securely connect to the instance via ssh. Create a new key pair.

Give the key pair a name and keep the rest of the options to their defaults.

This will download the key pair to your local machine as a .pem file. Move this file to another location on your machine for easy access. We’ll be using this later to ssh into our instance.

Under ‘Network settings,’ we should see that a VPC and subnet have already been attached to this instance. AWS provides a default VPC and subnet, so let’s stick with those. We can create your own VPC and subnet, but we won't be covering that topic for this scenario.

We also want to make sure ‘Auto-assign public IP’ is enabled. This allows access to the IP address, in which our website will deploy. If this is disabled by default, click the edit button to enable it.

Now we have to create security group rules to allow proper inbound traffic to our server and website.

There should already be an ssh option selected. However, we don’t want to allow just anyone to ssh into our server. We only want to allow known IP addresses, so change the ‘Source type’ to ‘My IP.’

We also need to allow inbound traffic through HTTP/HTTPS, so the public can reach our site! Let’s add new security rules for HTTP and HTTPS and set the source type to anywhere.

One more step!

Remember that bash script we created? It’s time to insert the script so the EC2 instance will run it on launch.

Open the ‘Advanced details’ menu and scroll alllllll the way down to the bottom. There will be a ‘user data’ text box. Paste your script here.

Launching and viewing an instance

Done! Now we’re ready to launch our EC2 instance!

Once you’ve received the success message, let’s ‘View all instances.’

All instances instance will be shown here. if the Instance state says ‘pending,’ give it a second until it says ‘running.’

Click on the instance to see the details. Take particular note of the ‘Public IPv4 address.’ This is where our new website will be delivered.

Congrats! We’ve just deployed our first EC2 instance! Next, we will SSH into it to make sure everything is running properly.

Step 3: SSH into our EC2 instance

AWS makes SSH-ing into our environment simple and straightforward. Remember that key-pair file we downloaded earlier? Get it ready. We’ll be using it to SSH into our instance.

Let's select our instance and hit ‘Connect.

There are multiple ways to connect to our instance, but we will be connecting through SSH, so navigate to the ‘SSH client’ tab.

As you can see, AWS does a wonderful job of walking us through the process. All of our info is already there! We just have to basically copy and paste!

Changing permissions for the key-pair file

The first step is to change the file permissions to our key-pair. If we don't do this, the key-pair info is susceptible to leeching out, so anyone can read it. We want to change it to ‘400,’ so only the owner can ‘read’ it.

Let’s open the terminal (or command line tool of preference) and navigate to the key-pair file on your local machine. I moved my file to /User/aaloktrivedi/AWS/Keys, so that’s what I will ‘cd’ to.

cd /User/aaloktrivedi/AWS/Keys

Remember, we haven’t logged into an other servers or environments. This is just on our local machine.

Now copy and paste the command from step three. Remember to use the file name you created.

chmod 400 brainiac-web-server-key.pem

SSH into the instance

Now ssh using the .pem file (you can copy and paste what was already provided).

ssh -i "brainiac-web-server-key.pem" ec2-user@ec2-3-91-100-157.compute-1.amazonaws.com

We’ll be prompted to confirm the connection. Type, ‘yes’ to continue.

🎉🎉🎉🎉🎉🎉Hooray!🎉🎉🎉🎉🎉🎉

We’ve successfully ssh-ed into our EC2 instance!

Check the Apache web server status

Now to see if our script worked and installed Apache correctly, let’s run a status check:

sudo systemctl status httpd

Great! Apache is up and running!

Check the public IP address

Last step! To see if our web page was delivered correctly to the public IPv4 address, let’s go to it!

Remember, the public IP address can be found on the main EC2 instance page (Tip: ‘Copy’ the IP address, don’t use the ‘Open address’ link).

Success!

Congratulations! We’ve successfully created an Amazon EC2 instance, Installed an Apache web server, and delivered our website to the world!

Some clean-up: On the free tier option, we have 750 hours of free computing time. As long as the instance is running, it will be using up that time, so if you're not actively using the instance, you can stop it by selecting it and ‘Stop’ the instance under the ‘Instance state’ dropdown.

Thank you!

Thank you for following me on my cloud computing journey. I hope this article was helpful and informative. Please give me a follow as I continue my journey, and I will share more articles like this!

--

--