Gary Young, Junior
5 min readMay 9, 2023
Apache | Amazon EC2

What is Amazon Web Services?

Amazon Web Services (AWS), Inc. is a diverse cloud platform that offers hundreds of services to help you host and maintain your applications or business using infrastructure-as-a-service (IaaS), platform-as-a-service (PaaS), and software-as-a-service (SaaS). They focus on functionality, security, and reliability, allowing you to focus on innovation and reachability, using their global cloud infrastructure. AWS allows you to create different types of virtual servers, called instances, that can be tailored to fit your specific need. Elastic Compute Cloud (EC2) is one such instance that provides dynamic compute cloud capacity, meaning it automatically scales, or adjusts the number of instances, depending on the demand of your project. This is important when it comes to pricing, as you essentially pay for only what you need. Now that you know a little bit about what AWS is and what it can do, let’s get started with an example.

Prerequisites

  • Active AWS account (create a free one here)
  • Basic SSH knowledge
  • Basic Linux command line knowledge
  • Basic BASH scripting knowledge
  • Access to a terminal

I have two parts in this demonstration: Step 1 includes creating and launching an AWS EC2 instance, including how to adjust security settings, Step 2 includes SSHing into the instance you created and installing Apache using a BASH script.

Step #1: Launching an AWS EC2 instance

First, make sure you are logged into your AWS Admin user account. If you are logged into your root user account (the initial account you set up), make sure to create a second user (you can call it anything but it will be used as an administrator role) to work from. You want to protect and reserve your root user account, and would not use it for day-to-day activity. This is why we create a general Admin user, to use for our everyday operations. You can find instructions on how to create a user here. Now that you are logged into your Admin account, use the search bar at the top to search for EC2, then click it to open. This will bring you to the EC2 dashboard.

EC2 Dashboard

To note: you need to use a region closest to you. Be aware that I am using the North Virginia region as you can see, from the screenshot. A region is a geographic location of Amazon servers, and you want to use the one closest to you and your clients. To launch your EC2 instance, click the orange “Launch Instance” button, then click “Launch Instance” again from the drop-down toggle.

Instance tag & AMI selection

Select the type of Amazon Machine Image (AMI) you want to use. The AMI contains the software configuration including operating system, application server, and applications needed to launch your instance. In this case, we will use the Amazon Linux AMI.

key pair is essential
Name key pair; key pair type: RSA; key file format: .pem

A key pair consists of a private and public key, which are used together to SSH to your instance. Select “Create new key pair,” then name your key pair and click the orange button “Create key pair.” This will automatically download your key pair to your Downloads folder. RSA (for further information on RSA public-key cryptosystem).

Select a t2.micro instance, we enjoy being in the free tier service. 🥹

Instance type
Network settings

Network Settings, includes your firewall settings. In this example, I want to allow SSH traffic from my IP address, so I’ve selected “Allow SSH traffic from”. I also want to allow inbound traffic on HTTPS from all IP addresses, and I’ve also selected “Allow HTTP traffic from the internet.”

Your instance has been created.

Part #2: SSH into your EC2 instance and install Apache with a BASH script

Make sure that your instance is running. Then click the check box next to the instance you want to work with, and click “Connect.”

You have a few ways to connect to your instance. SSH option we will be using. Also, note that you will likely need to change the permissions of your key file to make sure it is not viewable to the public.

Here I SSH’d into the EC2 with an error message appearing

To address the error message I will recommend that we enter this command.

]$ ssh -i keyname.pem ec2-user@ip.address
common warning, ⚠️ “unprotected private key file”

We successfully logged in to the EC2 instance.

HERE!

FOR HELP. REFER TO THE LINKED VIDEOS!

Linux/Mac user
Windows user

Create scripts

sudo yum install -y httpd

Then

systemctl start httpd
systemctl enable httpd

You just installed and updated Apache. Your public IPv4 address is on the Instances page, to see your custom Apache landing page.

Everything is up and running
[ec2-user@ip.address ~]$ touch filename.sh

[ec2-user@ip.address ~]$ vim filename.sh
Vim BASH script

For root access, to run the BASH script.

[ec2-user@ip.address ~]$ sudo su

Using the “chmod” command, you’ll need to enable execution of the file. The “chmod +x” command grants execute permissions. Once complete, execute the file using “./” and the file name.

[root@ip-172-31-22-183 ec2-user]# chmod +x filename.sh

[root@ip-172-31-22-183 ec2-user]# ./filename.sh

Confirm web server is working

As you see our web server is up!

Thank you for stopping by!