Explainer: Create EC2 instance in AWS with Apache2 Web Server installed

EC2 in AWS

Whatever your computing, storage, or memory requirements may be, Amazon can fulfill them all through its service called EC2, better known as Elastic Compute Cloud service. It’s an on-demand instance that lets you quickly set up reliable and scalable infrastructures coupled with the ability to increase or decrease the capacity within minutes. So, if you are planning to come up with your own WordPress site or a personal blog or just looking to test an application, you can test a prototype of the model by creating an EC2 instance in AWS and also installing a web server (Apache2) alongside it. We have come up with a detailed guide that will help you get started!

How to create an EC2 instance in AWS with Apache Web Server installed?

Firstly, it is important to note here that to run your applications or software on EC2 instance in AWS you should have an active AWS account. By active AWS account, we mean a working email ID and other important details to signup for the service. If you have them set up already, just log in to get access to the console. Then, follow the instructions given below.

First Part

Under the Recently Visited heading at the top, choose EC2 option.

AWS Console
AWS Console

Instantly, the EC2 dashboard screen will appear, displaying the number of instances running, Elastic Ips in use, Security groups, etc. Hit the Launch Instance button. The action will take you to the instance launching page where you must add various details. We will take a look at them one by one. Let’s through them quickly, one by one.

EC2 Dashboard
EC2 Dashboard

Kindly give a name to your instance under the empty Name field. In our case, we’ve named it as “Test” instance.

Name your EC2 Instance
Name your EC2 Instance

The next step is to select an appropriate image from the offered flavors like Ubuntu, Windows, Amazon Linux, SUSE, RHEL etc. Since the purpose of our work is to launch an instance for an experiment, we selected Ubuntu Server 22.04 (free version). Within Ubuntu, you get options to select virtual CPU (vCPU) and RAM. So, we went ahead with “t2micro” as our instance, available in AWS free tier.

You can also go for higher configuration instances depending on the workload type you would be running or the environment in which you will use it example, Dev, Prod, or testing.  As part of the free tier, AWS offers 750 hours per month of free usage on selected instances. 750 hours is equivalent to more than a month’s time. This gets renewed every month until 1 year. Post that limit, you don’t get free service and you will be charged for resources on a “pay as you go” basis.

EC2 Instance Type
EC2 Instance Type

After selecting the appropriate instance configuration, you will have to create a Key-pair.

Keypair Login
Keypair Login

A key pair is a combination of Private and Public keys that works together in combination and not standalone. The private key is stored in the AWS while the public key is available for download. When you will log in to your instance through a secured shell (SSH) AWS will request you to share the key and only then allow you to access the instance. This is available for download once only and if you lose it there is no way you can get it back. You’ll need to launch the entire instance again. So, store this key in an appropriate location.

Name Keypair
Name Keypair

The next step is to create network settings, in our case, we would be launching our instance in the default network instead of creating a custom network. The Network section consists of VPC, Subnets, IPs, and route tables. In the default selection category, we make use of default VPC, subnet, and route tables. These can be customized depending on the needs of a user. Moreover, it is highly recommended not to use default settings for production-grade applications. Just make sure to check the “Allow SSH traffic from” box. Then, hit the drop-down button next to the option and set it to “Anywhere” (0.0.0.0/0).

Create Security Group
Create Security Group

Proceeding further, configure the Storage quota. This is the volume that will be attached to your instance other than RAM. Here too we would be using the default option. Gp2 stands for general purpose 2 and you can add more storage if required by accessing the Add new volume option.

Configure Storage
Configure Storage

Now, review your configuration of the instance in the image below. Hit the “Launch instance” button.

Summary
Summary

If you see a Success message, it means your instance is ready. Click the blue hyperlink in the bracket to navigate your instance page.

Success
Success

The green tick and the Running status under the Instance State indicate the successful launch.

Instance State Running
Instance State Running

You can now see all the information of your running instance, including the public IP through which you can access it on the web.

Instance Summary with Public Address
Instance Summary with Public Address

Scrolling down you can see other important tabs like networking, security, storage, and monitoring.

Second Part

Now we move to the second part where we would be connecting the instance either through the AWS console on the web, SSH through Termius in MAC, or SSH through Putty if your system is running on Windows. Click the Connect button to open a new tab in your web browser. This will validate your keys automatically and allow you to access the instance as the image shown below.

Connect Button
Connect Button

Type Clear in the console to erase all the above information. Then add the following command.

Sudo apt install apache2

Subdo apt Install Apache2
Subdo apt Install Apache2

Once you hit Enter, you’ll go to the web where you’ll find the necessary Apache repository. Click Y to move forward.

Apache Repository
Apache Repository

The Done text indicates the installation is complete.

Done Text
Done Text

Third part

Third part is accessing your server over the internet, but before you do that there is one important step you need to complete. Go to the Security tab of the instance and add a rule in inbound rule, key in the following parameters,

  1. Allow all traffic
  2. Custom 0.0.0.0/0
  3. Click save

Then, copy the Public IP and paste it into the URL field of a separate tab in the browser.

Address in the browser
Address in the browser

Finally, you will be redirected to the Apache page.

Apache
Apache

Congrats you have successfully launched your first webserver.

Also read: How to transfer your running LightSail instance to AWS EC2 and How to migrate your running instances from Classic Load Balancer to Application Load Balancer in AWS.