Deploying An Ubuntu Image with Nginx Server in Amazon EC2 Instance.
Published: 2024-02-08 04:18:13

Amazon EC2 instance is an Infrastruture as a Service(Iaas) in the cloud computing model. Amazon Elastic Compute provides resizeable compute capacity in the cloud. EC2 allows users to rent virtual machines, known as instances, and run applications on them. 

These instances are available in various configurations, allowing users to choose the amount of virtual CPUs, memory, storage, and networking capacity that best suits their needs.

In the next few steps we will deploy an Ubuntu instance in EC2 ,and install nginx server.

**PREREQUISITES:** 

To start ,we will need an aws account `www.aws.amazon.com`. 
Secondly is a Webterminal,for example **GitBash** in this exercise.
**STEPS**
1 Log into your console , on the search panel above type ec2 and select it from the drop-down menu.

![Select EC2 from the search pane](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcnj47prjx3x7ul192qa.png)
2 In the window that opens after step 1 , select Launch Instance.

![Click and select launch instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ku1ch8o3pfk5ncbak3i5.png)
3 Provide a unique name for your EC2 virtual machine `Hagital`,in the name and Tag section.

![Provide a Unique name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qvqza95a72jb00sp4r1s.png)
4 In the Application And OS Images,click on the browse more AMI and type Ubuntu in the search window.

![Search for Ubuntu AMI in the task window](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lf4y1payxnjx06kmdhrf.png)
5 Scroll down and select "Ubuntu Server 22.04 LTS (HVM), SSD Volume Type"

![Ubuntu Server22.04](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ruarvv3lnw6s2wsy4qs0.png)
6 Scroll down , in the instance type leave as default `t2.micro` free tier as shown .

![Select t2.micro free tier](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9aqgm13ewsbaolre3qql.png)
7 Scroll down to key pair login section,and select create a new key pair. We will create a key pair to grant us access when we try SSH into th machine from our web terminal.

![Select create a new key pair](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9orxzwmw9b8qhnzozkr.png)
8 Provide a unique name for the key,and leave the remaining settings as default. Click on the create key button below. Your unique private key will download into your host machine.

![Provide a unique name for your key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vn1an00cnghwo6en2hfi.png)
9 In the Networking Settings, leave default to create security group selected.Select all checkboxes to "Allow SSh from","Allow HTTPS traffic from the Internet" , and "Allow HTTP traffic from the Internet".

![Leave network setting as default,select create security group and tick all the checkboxes.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zvg1zd4s7w2ps9k0acoh.png)
10 We leave all other setting as default and click on the lauch Instance button to create the machine.
![Launch Image Instance to create ec2 machine.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kro79ovh6ls0t2ui9637.png)
If suucessfully launched, we get the below message

![instance created successfully](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ub3rqpo2zcqhm4e43t07.png)

**USING A WEB TERMINAL TO ACCESS THE EC2 INSTANCE AND CREATING A WEBSERVER(nginx)**

1 Click the hamburger icon on the left and select Instance. Select the instance created and it opens to a new window.

![Select instance from the hamburger icon to the left](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i9byf6jgata4a5qwcydg.png)
2 In the new window,click and select connect

![select connect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onult07php7gd28gshlo.png)
3 We can connect using EC2 instance manager,Session Manager,SSH clent or EC2 serial console. we are using SSh client to connect.Select SSH client and follow the below steps to connect.

![connect to ssh client](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kag5oxy9ez773eo8zs2x.png)

4 Open GitBash web terminal,and locate your private key downloaded ealier in the exercise. using the commant cd <Dir name>. Our is located at the download folder.

![locate the private key downloaded ealier in the exercise](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fx9b4hhg5wj7w3k164vu.png)
5 Run this command, if necessary, to ensure your key is not publicly viewable.
 chmod 400 "hagital-key-pair.pem"

![make the key private](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhmph6zk993a6d4yf69j.png)
6 Connect using the command below into your instance `
"ssh -i "hagital-key-pair.pem" ubuntu@ec2-3-85-111-48.compute-1.amazonaws.com" `

![Connecting into the instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ri9espse1qv56q952i6a.png)

![Successfully connected into the EC2 Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ye35fey0815v74qby9re.png)

**CONFIGURE NGINX WEBSERVER**
1 run the command sudo su to get admin access
2 run apt update to upgrade the instance.

![Update the instance packages](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v155v4mv1t339o2vhkbs.png)
3 Install Nginx using the command apt install nginx.When prompted type yes and continue.

![Nginx installed successfully](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ibg04yt7f8ovhdo1m0s.png)
4 Verify the Ip address of your machine using the command :"Ip addr"

![Verify Ip Addr](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5y3uoxl01lnnu0fw731.png)
5
Copy the Ip Address an paste in a browser the see your newly created webserver.

In conclusion , Amazon Ec2 is widely used for various purposes including hosting of websites,running applications,processing data and more. It forms a fundamental building block of many AWS solutions and is a vital solution.