๐Ÿ–ฅ๏ธ How to Install NGINX on AWS EC2 (Ubuntu) โ€“ Complete Step-by-Step Guide

NGINX with Ubuntu on AWS

๐Ÿงพ 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 *