How To Setup a Web Server and Nginx with Amazon EC2
Tags: aws, ec2, webserver, cloud
Published: 2022-10-26 17:04:05

---
title: How To Setup a Web Server and Nginx with Amazon EC2
published: true
description: 
tags: #AWS #EC2 #webserver #cloud
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2022-10-26 16:27 +0000
---

I recently signed up for the [HNG Internship 9](https://internship.zuri.team/hngi9) for the backend track. The internship starts on 27th October 2022.
The first task (ungraded) was to set up a web server and host a simple website that displays ‘Hello World'. 
This is a step-by-step process of how I did the task.
### What is Amazon EC2?
Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure and resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers.
### What is Nginx?
Nginx is a web server used as a reverse proxy, load balancer, mail proxy and HTTP cache.
#### Set Up an AWS Account and Configure a Security Group
1. Create an AWS Account, [sign up](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all) and get a 12 months free tier account.
2. Open the Amazon EC2 console https://console.aws.amazon.com/ec2/

![EC2 Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqun6x9ucg1e57k0kfxr.png)
3. On the EC2 Dashboard, select ‘Security Group’.
4. Select ‘Create Security Group’.
![Create Security Group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/st2h637a16azq1upohbh.png)
5. Enter a simple name that you will remember for future use. Leave VPC as it is.
![Basic Details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0dtr0r62p03r7rzs4mls.png)
6. Add some inbound rules.
    - Rule 1 - Type: HTTP, Source: Anywhere IPV4
    - Rule 2 - Type: HTTPS, Source: Anywhere IPV4
    - Rule 3 - Type: SSH, Source: MyIP
![Inbound Rules](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dasrdep2tq4rihmdnhok.png)
7. Leave everything else as it is and click ‘Create Security Group’

#### Create a Key Pair
1. Move back to the EC2 dashboard.
2. Click on ‘Key Pairs’ and select ‘create key pair’.
![Create Key Pair](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ja9ot9tunns7n85v6hsd.png)
3. Assign a suitable name for your key pair and leave everything else as selected. **Choose .ppk only if you intend to use PuTTY to connect to your instance.** 
4. Click on ‘Create key pair’.

![Create key pair](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlzrmpr3m1idd80la42z.png)
- A download dialogue box will pop up. Download the private key and store it on your computer. **Note that if you do not download the key, you have to create a new key pair.**

#### Launch an Instance
1. Move back to the EC2 dashboard and choose instances.
2. Choose ‘Launch Instance’. Give it an appropriate name.
3. For Application and OS Images, select one of ‘Amazon Linux (HVM). Click on ‘Browse more AMIs’ for more options.
![AMIs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y2msq49fcv47ntqz1iop.png)
4. For instance type, select t2.micro.
5. For the key pair, select the one that you created earlier.
6. Select 'existing security group' and select the one you created earlier.
7. Leave everything else as it is and click on Launch instance. It will take a few minutes before the status check is complete. Once the status check indicates **2/2 checks passed**, you can now connect to your EC2 instance.

#### Connect to your Instance
1. Fire up your local terminal and cd into the folder where you stored your private key. `cd Downloads` in my case.
2. Run `chmod 400 <filename.pem>` to change permission on the file to protect it against accidental overwriting.
3. Connect to the instance by running `ssh -i <privatekey.pem> username@publicipaddress`
In my case: `ssh -i keytest.pem ec2-user@3.86.234.246`
![Connect to Instance Error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/besfw18uj5xvdvmz0vmb.png)
4. If you get a **connect to host 3.86…. port 22: Connection timed out** error, open the EC2 Console and choose security group. Select the security group you created or are using and edit inbound rules. Remove the IP on the SSH rule, choose MyIP and save the rules.
5. Run the command in step 3. Everything should work well now. If you get more errors, check out more troubleshooting options from [here](https://www.s-squaresystems.com/how-to-troubleshoot-ssh-connection-issues-with-aws-ec2/#:~:text=Any%20timeout%20is%20related%20to,firewall%20is%20blocking%20the%20connection.).
![Connect to Instance Success](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/75v8jmoe6ds870sexju2.png)
6. Gain root access to your server by running `sudo su -`
![Root Access](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1b993zc99w6remvvfazl.png)
7. Install HTTPD package: `yum install -y httpd`
8. Enable your server: `chkconfig httpd on`
![Install HTTPD](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/it4zkx31e2zo20xdpvwk.png)

#### Install and Configure Nginx
1. Install EPEL: `yum install -y epel-release`
2. Update Yum: `yum update -y`
3. Install Nginx: `sudo amazon-linux-extras install nginx1`
4. Check Nginx Version: `nginx -v`
5. Start Nginx: `systemctl start nginx`
6. Enable Nginx: `systemctl enable nginx`
7. Check Nginx Status: `systemctl status nginx`
![Install & Enable Nginx](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ou0p911zlk0e7xke14wi.png)
8. Copy the Public IP to your web Browser to view the site's content.
![Site Content](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xiwx48nn8hy5r7seo1v1.png)
9. Write to the index.html file Using cat: `cat > /usr/share/nginx/html/index.html`
`Hello World`
![After Editing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4thba5cxijnkhi7jpuj8.png)
10. Refresh your site to see the changes.
![Site Refresh](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/14f9rf5suxwajwlkcq8b.png)

#### Terminate Your Instance
1. In the EC2 dashboard, choose Instances. 
2. In the list of instances, select the instance.
3. Choose Instance state, Terminate instance.
4. Choose Terminate when prompted for confirmation.