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
Author
🚀 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
- Navigate to EC2 Dashboard: Log into AWS Console → EC2 → Launch Instance
- Choose AMI: Select "Ubuntu Server 22.04 LTS" (Free tier eligible)
- Instance Type: Select t2.micro for free tier or t3.small for better performance
- Key Pair: Create new key pair or select existing one
- 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:
your-key.pemwith your downloaded key file pathyour-ec2-public-ipwith your instance's public IP address
📦 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.
Written by
Super Admin
Author at BuildMyAiStoreNow