πŸ–₯️ How to Install NGINX on AWS EC2 (Ubuntu) – Complete Step-by-Step Guide

🧾 Prerequisites

Before we dive in, make sure you have:

  • An AWS account
  • Basic understanding of EC2
  • A key pair (for SSH access)
  • A domain name (optional but helpful)

πŸ”§ Step 1: Launch an EC2 Instance

  1. Go to the AWS EC2 Dashboard.
  2. Click β€œLaunch Instance”.
  3. Choose Ubuntu Server 22.04 LTS (or 20.04).
  4. Select a free-tier eligible type like t2.micro.
  5. Create or use an existing key pair.
  6. Under Security Group, allow these:
    • SSH (port 22) – to access your server
    • HTTP (port 80) – for web traffic
    • HTTPS (port 443) – for SSL later

Click β€œLaunch” and wait for the instance to be ready.


πŸ”— Step 2: Connect to the Server via SSH

Use your .pem key to SSH into your instance:

ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ip

Replace /path/to/your-key.pem and your-ec2-public-ip with your actual key file and public IP.


πŸ“¦ Step 3: Update the Server

Once connected, always update your server packages:

sudo apt update && sudo apt upgrade -y

🌐 Step 4: Install NGINX

Let’s install the NGINX web server:

sudo apt install nginx -y

Now start and enable NGINX:

sudo systemctl start nginx
sudo systemctl enable nginx

Check status:

sudo systemctl status nginx

πŸ”“ Step 5: (Optional) Allow NGINX Through UFW Firewall

If you’re using UFW (Ubuntu’s firewall):

sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status

🌍 Step 6: Test in Your Browser

Open your browser and go to:

http://your-ec2-public-ip

You should see the default NGINX welcome page β€” that means it’s working! βœ…


πŸ“ Step 7: Create Your Own Website

Want to serve your own content?

  1. Create a directory:
sudo mkdir -p /var/www/yourdomain.com
sudo chown -R $USER:$USER /var/www/yourdomain.com
  1. Add an index.html file:
echo "<h1>Hello from NGINX on AWS!</h1>" > /var/www/yourdomain.com/index.html
  1. Create a new NGINX config:
sudo nano /etc/nginx/sites-available/yourdomain.com

Paste:

server {
listen 80;
server_name yourdomain.com www.yourdomain.com;


root /var/www/yourdomain.com;
index index.html;

location / {
try_files $uri $uri/ =404;
}
}
  1. Enable it and reload NGINX:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

πŸ” Step 8: Add SSL with Let’s Encrypt (Free HTTPS)

To secure your site with HTTPS:

sudo apt install certbot python3-certbot-nginx -y

Run Certbot:

sudo certbot --nginx

Follow the prompts to install SSL automatically. πŸŽ‰


βœ… Final Words

You now have NGINX running on an AWS EC2 instance β€” serving your own website, secured with HTTPS.


πŸ” Summary Checklist

TaskStatus
βœ… Launch EC2 with Ubuntuβœ”οΈ
βœ… Install NGINXβœ”οΈ
βœ… Set up security groupsβœ”οΈ
βœ… Deploy a custom siteβœ”οΈ
βœ… Enable SSL with Certbotβœ”οΈ

πŸ’¬ Need Help?

Drop your questions in the comments.

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *