How to setup EC2 instance and install Nginx

Ankit dwivedi
3 min readOct 24, 2024

Hey there! 👋 Today, I’ll walk you through setting up a web server using Nginx on AWS EC2. Don’t worry if you’re new to this — I’ll explain everything in simple terms.

What it contains?

  1. Creating Your EC2 Instance
  2. Security Setup
  3. Connecting to Your Instance
  4. Installing and Configuring Nginx
  5. Security Hardening
  6. Performance Optimization

Step 1: Creating Your EC2 Instance

First, let’s create your EC2 instance. Think of this as renting a computer in the cloud!

Basic Setup

  1. Log into AWS Console
  2. Go to EC2 Dashboard
  3. Click “Launch Instance”

Instance Details

  • Name: ubuntu-nginx-server
  • Operating System: Ubuntu Server 22.04 LTS
  • Instance Type: t2.micro (Perfect for starting out — it’s free tier eligible!)
  • Storage: 8 GB gp2 (default)

Key Pair Setup

# Create a new key pair in AWS console
Name: ubuntu-nginx-keypair
Type: RSA
Format: .pem

Step 2: Security Setup

Security is super important! Let’s set it up right.

Creating a Security Group

Name: nginx-security-group
Description: Security group for Nginx web server

Important Security Rules

# Inbound Rules
SSH (Port 22): Your IP only
HTTP (Port 80): Anywhere (0.0.0.0/0)
HTTPS (Port 443): Anywhere (0.0.0.0/0)

# Outbound Rules
All Traffic: Anywhere (0.0.0.0/0)

đź’ˇ Pro Tip: You can attach multiple security groups to one instance, and each security group can be used with multiple instances. This gives you great flexibility in managing access!

Step 3: Let’s Connect to Our EC2 Instance from terminal!

First things first, let’s SSH into our EC2 instance. If you’re using a .pem file:

# Before connecting to EC2 instance, secure your key file from terminal
chmod 400 ubuntu-nginx-keypair.pem
# After that connect to EC2 instance.
ssh -i your-key.pem ubuntu@your-instance-ip

Step 4: Installing and Configuring Nginx

Update Your System

sudo apt update
sudo apt upgrade -y

Nginx Installation

sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx

Why These Steps Matter:

  • Installation gets the latest stable version
  • Starting the service makes it immediately available
  • Enabling ensures it starts automatically after reboots
  • Systematic approach prevents configuration issues

Basic Nginx Configuration

server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm;
server_name _;

location / {
try_files $uri $uri/ =404;
}
}

Why Each Configuration Matters:

  • listen: Handles both IPv4 and IPv6 connections
  • root: Defines where website files are stored
  • index: Specifies default files to serve
  • server_name: Catches all domain requests
  • try_files: Provides proper file handling

Step 5: Security Hardening

# Security Headers
server_tokens off;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";

Why These Security Measures Matter:

  • server_tokens: Hides Nginx version from potential attackers
  • X-Frame-Options: Prevents clickjacking attacks
  • X-XSS-Protection: Stops cross-site scripting attacks
  • X-Content-Type-Options: Prevents MIME-type sniffing

DDoS Protection

client_max_body_size 10M;
client_body_timeout 12;
client_header_timeout 12;

Why These Protections Matter:

  • Prevents large upload attacks
  • Controls server resource usage
  • Protects against slow HTTP attacks
  • Maintains server stability

Step 6: Performance Optimization

Gzip Compression

gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;

Why Compression Matters:

  • Reduces bandwidth usage significantly
  • Improves page load times
  • Saves money on data transfer
  • Better user experience

FastCGI Cache

fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

Why Caching Matters:

  • Dramatically improves response times
  • Reduces server load
  • Better handling of traffic spikes
  • Optimizes resource utilization

Final Steps and Verification

Test your configuration:

sudo nginx -t

Restart Nginx:

sudo systemctl restart nginx

Why These Final Steps Matter:

  • Ensures configuration is valid
  • Prevents service disruptions
  • Applies all changes properly
  • Confirms server is running correctly

Wrapping Up

Congratulations! 🎉 You now have a properly configured Nginx server running on AWS EC2. Your server is:

  • Secure with proper firewall rules
  • Optimized for performance
  • Protected against common attacks
  • Ready to serve your web content!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Ankit dwivedi
Ankit dwivedi

Written by Ankit dwivedi

Software developer ? ( Flutter || Dart || IOS ||C# || Nodejs || Kotlin || Java || Android ) : Writing

No responses yet

Write a response