Web Development

How to Setup Nginx Server on AWS EC2: Complete Step-by-Step Guide

Learn how to deploy and configure Nginx on AWS EC2 with this comprehensive guide. From server setup to SSL configuration, get your web server running in minutes.

Super Admin

Super Admin

Author

March 10, 2026
4 min read
19 views
Listen to this article alloy
0:00
0:00
How to Setup Nginx Server on AWS EC2: Complete Step-by-Step Guide

🚀 Setup Nginx on AWS EC2

Your Complete Guide to Web Server Deployment

Setting up Nginx on AWS EC2 is one of the most fundamental skills for web developers and DevOps engineers. This powerful combination provides a robust, scalable foundation for hosting websites and applications. Whether you're deploying your first web server or adding to your infrastructure, this guide will walk you through every step.

✅ What You'll Learn

  • Launch and configure an EC2 instance
  • Install and setup Nginx web server
  • Configure security groups and firewall rules
  • Setup SSL certificates for HTTPS
  • Optimize Nginx for production use

📋 Prerequisites

🔧 Technical Requirements

  • AWS account with EC2 access
  • Basic knowledge of Linux commands
  • SSH client (Terminal, PuTTY, etc.)

💰 Cost Estimate

  • EC2 t2.micro: ~$8.50/month
  • Free tier eligible: 750 hours/month
  • Additional storage: $0.10/GB/month

🔥 Step 1: Launch Your EC2 Instance

⚠️ Important Note

Always choose the latest stable Ubuntu or Amazon Linux AMI for better security and performance.

🖥️ Instance Configuration

  1. Navigate to EC2 Dashboard: Log into AWS Console → EC2 → Launch Instance
  2. Choose AMI: Select "Ubuntu Server 22.04 LTS" (Free tier eligible)
  3. Instance Type: Select t2.micro for free tier or t3.small for better performance
  4. Key Pair: Create new key pair or select existing one
  5. Storage: 8GB General Purpose SSD (increase if needed)

🛡️ Security Group Setup

Create a new security group with these rules:

Type Port Source Description
SSH 22 Your IP SSH Access
HTTP 80 0.0.0.0/0 Web Traffic
HTTPS 443 0.0.0.0/0 Secure Web Traffic

🔌 Step 2: Connect to Your Instance

💻 SSH Connection Command

ssh -i "your-key.pem" ubuntu@your-ec2-public-ip

Replace:

📦 Step 3: Install Nginx

🔄 Update System Packages

sudo apt update && sudo apt upgrade -y

⚡ Install Nginx

sudo apt install nginx -y

🚀 Start and Enable Nginx

sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx

✅ Verification

Visit http://your-ec2-public-ip in your browser. You should see the Nginx welcome page!

⚙️ Step 4: Configure Nginx

📝 Create Your Website Directory

sudo mkdir -p /var/www/your-domain.com/html
sudo chown -R $USER:$USER /var/www/your-domain.com/html
sudo chmod -R 755 /var/www/your-domain.com

📄 Create a Sample HTML File

sudo nano /var/www/your-domain.com/html/index.html

Add this sample content:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Your Nginx Server!</title>
</head>
<body>
    <h1>🎉 Nginx Server is Running!</h1>
    <p>Your server is successfully configured.</p>
</body>
</html>

🔒 Step 5: Setup SSL with Let's Encrypt

📋 Install Certbot

sudo apt install certbot python3-certbot-nginx -y

🔐 Obtain SSL Certificate

sudo certbot --nginx -d your-domain.com -d www.your-domain.com

🔄 Auto-renewal Setup

sudo crontab -e

Add this line for automatic renewal:

0 12 * * * /usr/bin/certbot renew --quiet

🚀 Step 6: Performance Optimization

⚡ Essential Nginx Optimizations

🗜️ Enable Gzip Compression

Reduces bandwidth usage by up to 70%

📦 Browser Caching

Improves load times for returning visitors

⚡ Worker Processes

Optimize based on CPU cores

📝 Edit Nginx Configuration

sudo nano /etc/nginx/nginx.conf

🔧 Step 7: Testing and Monitoring

🧪 Essential Tests

  • Configuration Test: sudo nginx -t
  • Reload Configuration: sudo systemctl reload nginx
  • Check Status: sudo systemctl status nginx
  • View Logs: sudo tail -f /var/log/nginx/access.log

📊 Monitor Your Server

📈 Performance

Use tools like htop, iotop

🛡️ Security

Monitor access logs regularly

💾 Storage

Check disk usage with df -h

🎉 Congratulations!

You've successfully set up Nginx on AWS EC2 with SSL encryption. Your web server is now ready for production use!

Pro Tip: Regular backups, monitoring, and security updates are crucial for maintaining a healthy web server. Consider setting up automated monitoring and backup solutions for production environments.

Tags

Share this Article

Super Admin

Written by

Super Admin

Author at BuildMyAiStoreNow

Link copied to clipboard!

Customer Support