DEV Community

Cover image for AWS: Creating a Linux EC2 Instance and Installing NGINX
John Ogbonna
John Ogbonna

Posted on

AWS: Creating a Linux EC2 Instance and Installing NGINX

What is an EC2 instance?

Amazon Elastic Compute Cloud (EC2) is a core service offered by Amazon Web Services (AWS) that provides scalable cloud computing. An EC2 instance is essentially a virtual server that allows you to run applications, host websites, or perform other computational tasks without the need for on-premises server hardware.

By offering various instance types and configurations, EC2 gives you the option to choose the right combination of CPU, memory, and storage to meet your specific needs. Whether you're hosting a small website or deploying large-scale applications, EC2 instances allow you to scale up or down* based on demand, ensuring cost-efficiency and performance.

In this guide, we’ll walk through creating a Linux-based EC2 instance and installing NGINX, a lightweight and powerful web server, to get your applications up and running in no time.

What is NGINX?

NGINX (pronounced "engine-x") is an open-source, high-performance web server and reverse proxy server that is known for its effectiveness, scalability, and speed. It was originally created to manage several concurrent connections, but it is now a common option for load balancing**, APIs, and contemporary online applications.

In addition to providing static files, NGINX is excellent at functioning as a reverse proxy***, caching requests, and effectively handling traffic. Its strong feature set and lightweight design make it a popular choice for web application performance optimization, server traffic balancing, and website hosting.

We'll go over how to install NGINX on a Linux-based AWS EC2 instance in this tutorial, which will allow you to swiftly and efficiently serve content or launch apps.

*EC2 scaling: EC2 instances have the ability to scale up and down (by adding/removing resources from an individual instance) or in an out (by removing or adding more individual instances). This is done in order for the system to only use as much compute power as is needed for the task at hand at any given time.

**Load Balancing: Load balancing is the process of distributing incoming network requests and traffic across multiple servers to ensure no single server becomes overwhelmed and that the correct servers are chosen to handle each task. This helps maintain high availability, reliability, and performance.

***Reverse proxy server: A reverse proxy server is a gateway that sits between clients (like web browsers) and backend servers. It routes client requests to the appropriate server and sends the server's response back to the client.

1. Creating a Linux EC2 instance:

  • In the AWS console, search for and Select EC2
    In the AWS console, search for and Select EC2

  • Select Launch Instance
    Select Launch Instance

  • Give the instance a name

  • Select Amazon Linux, as it is free tier eligible
    Give the instance a name

  • Scroll down and select instance type. t2.micro will keep you in the free tier

  • Then, click on "create new key pair". Here we will create a private key to be used when accessing the instance
    Scroll down and select instance type

  • Choose a name for the key pair

  • In this case, we will leave the rest on default. Conversely, you can also choose your key configuration at this step if you have different requirements. Click "OK"

Choose a name for the key pair

  • The key will be downloaded to your computer. Be sure to store it in a safe location on your local machine

  • Leave the default setting "Create Security group" and "Allow SSH traffic from anywhere"

  • Note the warning that recommends adding security group rules, rather than allowing access from anywhere. In this tutorial, we will allow access from anywhere, but in production or even development grade apps, it is recommended to restrict access using security group rules
    Security settings

    * Also note that you can check "Allow https traffic" as sometimes the browser defaults to https instead of http
  • At this stage, you can leave all other settings on default or change as personally needed. Click "Launch Instance"

  • Select View instances once the instance is created or navigate to instances in the EC2 portal. Select the newly created instance
    Select the newly created instance

  • Copy the public IPv4 address and store it
    Copy the public IPv4 address

  • Open a new terminal on your local machine (Terminal / iTerm for Mac, Powershell for Windows) and navigate to the folder where you stored the downloaded key

  • Run this command to ensure your key is not publicly viewable:

chmod 400 your-key-file.pem
Enter fullscreen mode Exit fullscreen mode
  • Example:
    Example of chmod command

  • SSH into your instance by running this command. Refer to the public IPv4 address you copied as "your-ec2-public-dns":

ssh -i your-key-file.pem ec2-user@your-ec2-public-dns
Enter fullscreen mode Exit fullscreen mode
  • Example:

Example of connection command

  • You should be greeted with a successful login screen similar to example shown
  • Type this command to update system once you are logged in:
sudo yum update -y
Enter fullscreen mode Exit fullscreen mode
  • To install NGINX, type this command:
sudo yum install nginx -y
Enter fullscreen mode Exit fullscreen mode
  • You may have to run this command if the above is not successful, then run the above command again:
sudo amazon-linux-extras enable nginx1
Enter fullscreen mode Exit fullscreen mode
  • Successful installation will look like this:
    NGINX successful installation

  • Start and enable NGINX:

sudo systemctl start nginx
sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode
  • The result should resemble:
    NGINX enabled

  • Go back the webpage with your EC2 instance selected and click open address
    Open Address EC2

  • If the page is taking a long time to load, go into the address bar and change the prefix to "http" from "https" if "allow https traffic" was not selected when creating the EC2 instance

  • You should be able to see the NGINX default splash page:
    NGINX default splash page

Congratulations! you've deployed an EC2 instance and installed NGINX on it!

Top comments (0)