DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Emmanuel Kariithi
Emmanuel Kariithi

Posted on

How To Setup a Web Server and Nginx with Amazon EC2

I recently signed up for the HNG Internship 9 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 and get a 12 months free tier account.
  2. Open the Amazon EC2 console https://console.aws.amazon.com/ec2/

EC2 Console

  1. On the EC2 Dashboard, select β€˜Security Group’.
  2. Select β€˜Create Security Group’. Create Security Group
  3. Enter a simple name that you will remember for future use. Leave VPC as it is. Basic Details
  4. 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
  5. 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
  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

  • 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
  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
  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. Connect to Instance Success
  6. Gain root access to your server by running sudo su - Root Access
  7. Install HTTPD package: yum install -y httpd
  8. Enable your server: chkconfig httpd on Install HTTPD

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
  8. Copy the Public IP to your web Browser to view the site's content. Site Content
  9. Write to the index.html file Using cat: cat > /usr/share/nginx/html/index.html Hello World After Editing
  10. Refresh your site to see the changes. Site Refresh

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.

Top comments (0)

12 APIs That You Will Love

>> Check out this classic DEV post <<