How to setup EC2 instance and install Nginx
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?
- Creating Your EC2 Instance
- Security Setup
- Connecting to Your Instance
- Installing and Configuring Nginx
- Security Hardening
- 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
- Log into AWS Console
- Go to EC2 Dashboard
- 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!