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:
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
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"
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
* 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
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
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
- Example:
- 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
- To install NGINX, type this command:
sudo yum install nginx -y
- 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
sudo systemctl start nginx
sudo systemctl enable nginx
Go back the webpage with your EC2 instance selected and click open address
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
Top comments (0)