Launching an AWS EC2 Instance and Installing an Apache Server

Cedric Watkins
7 min readOct 25, 2022

Objective: We will be launching an AWS EC2 instance (virtual server), and install an Apache web server on it via a script that we’ll create within or server

What is Amazon Web Services (AWS)?

  • Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform, offering over 200 fully featured services from data centers globally. Millions of customers — including the fastest-growing startups, largest enterprises, and leading government agencies — are using AWS to lower costs, become more agile, and innovate faster.

What is Amazon EC2?

  • Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the Amazon Web Services (AWS) Cloud. Using Amazon EC2 eliminates your need to invest in hardware up front, so you can develop and deploy applications faster. You can use Amazon EC2 to launch as many or as few virtual servers as you need, configure security and networking, and manage storage. Amazon EC2 enables you to scale up or down to handle changes in requirements or spikes in popularity, reducing your need to forecast traffic.

What you’ll need?

  • An AWS account with Admin privileges
  • A basic understanding of the Linux Environment (Majority of the Cloud run on the Linux OS)
  • A terminal so that you can ssh into your EC2

Step 1: Enter EC2 on the AWS dashboard via aws.amazon.com search bare and select EC2

Step 2: Within the EC2 Dashboard your going to press the launch tab button and this will initiate the creation process for your EC2

Step 3: Here you will give your EC2 a name and select your Amazon Machine Image(AMI) in this case we chose Ubuntu

  • An Amazon Machine Image is a special type of virtual appliance that is used to instantiate (create) a virtual machine within EC2. It serves as the basic unit of deployment for services delivered using EC2. Whenever you want to launch an instance, you need to specify AMI. To launch instances, you can also use different AMI’s. If you want to launch multiple instances from a single AMI, then you need multiple instances of the same configuration.

Step 4: We’ll select our instance type and create a key pair

  • For Instance type we’ll choose “t2.micro” it’s the free tier version and supply us with just enough computing resources to complete our task.
  • A key pair, consisting of a public key and a private key, is a set of security credentials that you use to prove your identity when connecting to an Amazon EC2 instance. Amazon EC2 stores the public key on your instance, and you store the private key. For Linux instances, the private key allows you to securely SSH into your instance
  • We’ll give our key pair a name and leave it’s encryption and file format set to their defaults and click the “create key pair” button and it’ll download the file onto your computer.

Step 5: Next we’ll work on establishing our network settings for our Instance

  • Our Network will be established in our region default Virtual Private Cloud(VPC) and Subnet.
  • Amazon Virtual Private Cloud (Amazon VPC) enables you to launch AWS resources into a virtual network that you’ve defined. This virtual network closely resembles a traditional network that you’d operate in your own data center, with the benefits of using the scalable infrastructure of AWS.
  • A subnet is a range of IP addresses in your VPC. You can launch AWS resources into a specified subnet. Use a public subnet for resources that must be connected to the internet, and a private subnet for resources that won’t be connected to the internet.
  • For our Security Group we’ll create a new one that will allow inbound ssh traffic(port 22) from any IP address, and http(port 80) traffic into our Instance.
  • *** For the sake of this project we’ll allow traffic from anywhere, but as warned below this is not the most secure way to manage who’s able to gain access to your server***
  • Now that we are done with configuring our Instance select the “Launch Instance” button

Step 6: Now we will ssh into our Instance by clicking the connect button and navigating to the “SSH Client” Tab.

  • Once at the “SSH Client” Tab you’ll see an SSH command provided to you, copy that and navigate to your terminal
  • In your terminal you will navigate to the directory that your private key file is saved in by using the “cd” command and paste the ssh command we copied earlier

Step 7: Create our Script for our Apache installation

  • Now that we are in our Instance we will begin creating our script by entering the vim command following the name of the script we’re creating.
  • Vim is an open source text editor made available on Linux and MacOS systems. This text editor will allow us to create scripts such as the one we’re about to create so we can automate our installation process for our Apache Web Server
vim apache_script.sh
  • Enter the letter “i” and this will allow us to actually start typing into our file. Once that is done we’ll enter these commands below.
  • Save and exit the editor by pressing the “esc” key following the “:wq” keys
#!/bin/bash
apt update -y
apt install -y apache2
systemctl start apache2
systemctl enable apache2
echo "<html><body><image src="http://www.quickmeme.com/img/94/940e1841103905d4fd0395560d426f7c006ff7c97f896f39e0ad3af33b9f8746.jpg" alt="quickmeme.com" width="625" height="415"></body></html>" > /var/www/html/index.html
  • The “#!/bin/bash” at the beginning of our script will inform our system the this file is being executed as a bash script
  • The beautiful thing about bash scripts, is that your able to run multiple commands at a time, and they all will be executed when the script is called. You can easily run single commands in the terminal, but in order to automate that process and run multiple at a time, a bash script is needed.
  • Ok, so now our script is created and when you enter the long listing command “ls -l” in your terminal you’ll notice that our file isn’t executable.
  • The “chmod” command allows us to modify the permissions for our file for either the user (owner of the file), Group(a group of users associated with the file), and everyone else
  • The letters represent:
  • r: Read. Allows the file to be opened.
  • w: Write. Allows for modifications to be made to the file.
  • x: Execute. Allows the file if it is a script to be ran.
  • As seen above we’ll modify the users permissions by entering the following command giving us read, write, and execute permissions to this file
chmod u=rwx apache_script.sh
  • All we have to do in order to execute our script is enter the sudo command giving us “super user do” capabilities and call our script using the “sh” command
sudo sh apache_script.sh
  • Whewww…….. look we’re almost done all we have to do now is copy the public IPv4 address of our Instance, and enter it into our web browser. If you were brave enough to copy my script above you should see the image below when the page is fully loaded up.

Thank you for following along with this walk through not only were you able to launch and navigate one of Amazon Web Services top resources but you were able to create a script that will automate the Installation of an Apache Web Server. I strongly encourage you to not stop here, there’s so much more that can be done within “AWS” infrastructure. I wish you the best and Great Work my friend.

--

--

Cedric Watkins

Hello World, I’m simply an individual who loves to learn and take on challenges follow my journey as I develop/share my Cloud technology skills.