Set up an EC2 instance and install NGINX

This exercise walks through setting up an EC2 instance on AWS and installing an NGINX server.

Objective

Set up an EC2 instance on AWS. Then install and start the NGINX server.

Description

Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the Amazon Web Services (AWS). Using Amazon EC2 eliminates your need to invest in hardware upfront, so you can develop and deploy applications faster Amazon EC2 provides Virtual computing environments, known as instances. In simple words, an instance can be thought of as a server.

In this exercise, you will be setting up an EC2 instance on AWS. To perform the tasks for this exercise, you will need to set up an AWS account.

Once you set up an EC2 instance, you will install the NGINX server on it. Nginx is a high-performance web server that can also be used as a reverse proxy, load balancer, mail proxy, and HTTP cache. Nginx can be easily installed on the EC2 instances.

 

Acceptance criteria

  • Launch an EC2 instance
  • Choose an AMI (Ubuntu instance with Free tier)
  • SSH into your instance (using EC2 instance connect)
  • Install NGINX on this instance
  • Start NGINX server

 

Hints

  • EC2 instances are created from Amazon Machine Images (AMI). The machine images are like templates that are configured with an operating system and other software, which determine the user’s operating environment.
  • Use the following command to install NGINX
    sudo apt install nginx

     

    Solution

Introduction

An EC2 instance is a type of virtual machine that allows users to access the Amazon Elastic Compute Cloud (EC2).

EC2 instances can be used to run web applications, hosting services, and other compute-intensive tasks. EC2 instances are created from pre-configured images called Amazon Machine Images (AMIs), which contain all the necessary software and settings needed to run the instance. Users can select from a variety of different instance types, each of which offers different amounts of CPU, memory, and storage resources. ec2 instances can also be customized with additional storage volumes, network interfaces, and security groups. Once an ec2 instance has been launched, it can be accessed via the Amazon EC2 console or the Amazon EC2 API.

NGINX is a popular open-source server that is known for its speed, efficiency, and ease of use. It works by utilizing a unique reverse proxy configuration, essentially serving as a go-between for web browsers and the various other servers that power websites. NGINX is capable of handling lots of traffic simultaneously, making it an excellent choice for websites that receive high levels of traffic or have complex content. Additionally, NGINX provides extensive support for caching, allowing websites to store commonly accessed data close to the people who need it most. Overall, NGINX is an extremely versatile tool that can help power all kinds of websites and online applications.

 

Getting started with EC2

Create an account or log in to AWS to get started. Once logged into the AWS console, click “Services” on the navbar and click EC2 under the “Compute” section.

Next, Go to the “Launch instance” section in the EC2 dashboard and launch an EC2 instance by clicking the “Launch instance” button.

Launch EC2 instance

This will open up a page with a workflow of steps to set up the instance. Select “Ubuntu Server 18.04 LTS (HVM), SSD Volume Type” with free tier eligible from the options as shown in the image below.

Go to step 2 and select t2.micro ( with the option of “free tier available”)

selecting EC2 image size

Next click “Review and Launch” This will open up Step 7 to review the instance. Click launch.

review ec2 instance

A modal opens up asking you to select a key pair. You can use the key pair you have created before or create a new one now. Create new key pair:

key pair

Download the key pair and save them in a secure place.

Your instance will be launched and you can find its details by going to the Instances section in your EC2 dashboard.

You can see the status as Initializing while the instance is getting prepared for launch.

Initializing instance:

initializing instance

Once the status check is completed you will see checkmarks

Public IPV4 DNS is the address from which you can access the server. If you go to the link right now, you will see a 502 error.

Accessing the server:

Go to the EC2 dashboard and click on the instances. Select the instance that was just created from the list of instances (if this was the first time, there will be only one entry shown).

This will open a windowpane below with several tabs. Click on the security and the item listed under “Security groups”. This will open another window to add security rules.

Click “EDIT INBOUND RULES”

Add the following rules for HTTP and save

HTTP inbound rules:

http inbound rules - ec2 instance

Save and go back to the instance.

Connecting to your instance:

There are several ways to connect to your created instance such as:

  • EC2 instance connect
  • Session manager
  • SSH client

We will be using the EC instance connect here.

Click on the Actions button above the list of instances and click “Connect”. The “Connect to instance” window opens up.

Actions > Connect:

actions - connect

As we will be using “EC2 instance connect”, click connect from the bottom of the page.

Connect to instance screen:

Connect to instance screen

 

Now, you will be connected to your instance through a browser-based console.

connected_ec2

 

Installing NGNIX:

Install Nginx using these commands

sudo apt install nginx

Next, verify the installed version with the command

nginx -v

It will print the version of NGINX. Example:

nginx version: nginx/1.18.0 (Ubuntu)

 

Nginx is now installed but it is not running.

To start Nginx use the following command:

Sudo service nginx start

Now go to the public IPv4 DNS address and you will see the “Welcome to Nginx “ page. Or try doing curl localhost:80 to see the default Nginx page.

Welcome NGINX screen:

welcome to nginx screen

To stop service, use the following command. This will stop the NGINX server.

Sudo service nginx stop

You have now successfully installed NGINX server on your EC2 instance.