How to Host a Static Website on AWS EC2 with Nginx
To host a static website on AWS EC2 with Nginx, install Nginx on an Amazon Linux 2023 instance, upload your HTML/CSS files with scp, copy them to /var/www/mysite, and add a server block in /etc/nginx/conf.d/mysite.conf with root /var/www/mysite;. Test with sudo nginx -t, reload with sudo service nginx reload, and open http://<PUBLIC_IP> with port 80 allowed in the security group.
Friends, in the Nginx guide we built a one-page proposal website. Was the answer "yes"? Then now give her a real website – with photos, CSS and its own folder. Today we upload a complete website from the laptop to EC2 and host it properly with Nginx – the way companies do it.
मित्रांनो, Nginx guide मध्ये आपण एक पेज ची प्रपोजल वेबसाइट बनवली होती. उत्तर "हो" आलं? मग आता तिला खरी वेबसाइट द्या – फोटोज, CSS आणि स्वतःचा फोल्डर असलेली. आज आपण लॅपटॉप वरची पूर्ण वेबसाइट EC2 वर अपलोड करून Nginx ने प्रॉपर होस्ट करूया – जसं कंपनी मध्ये करतात.
दोस्तों, Nginx guide में हमने एक पेज की प्रपोज़ल वेबसाइट बनाई थी. जवाब "हाँ" आया? तो अब उसे असली वेबसाइट दो – फ़ोटोज़, CSS और अपने फ़ोल्डर वाली. आज हम लैपटॉप वाली पूरी वेबसाइट EC2 पर अपलोड करके Nginx से प्रॉपर होस्ट करेंगे – जैसे कंपनी में करते हैं.
Quick answer
From your laptop, upload the site; then on the server, copy it, add a server block and reload:
# laptop
scp -i mykey.pem -r ./mysite ec2-user@<PUBLIC_IP>:~/
# server (Amazon Linux 2023, Nginx already installed)
sudo mkdir -p /var/www/mysite && sudo cp -r ~/mysite/* /var/www/mysite/
sudo find /var/www/mysite -type d -exec chmod 755 {} \;
sudo find /var/www/mysite -type f -exec chmod 644 {} \;
# create /etc/nginx/conf.d/mysite.conf (Step 4), then:
sudo nginx -t && sudo service nginx reload
What do I need before hosting a static site?
- An Amazon Linux 2023 instance with Nginx installed and running — follow How to Install Nginx on Amazon Linux 2023.
- HTTP port 80 open in the security group — see How to Open Port 80 and 443 in an EC2 Security Group.
- Your website folder on your laptop, with an
index.htmlat the top (for examplemysite/index.html,mysite/css/style.css,mysite/images/). - Your key file and SSH access — see How to SSH into EC2.
How does a visitor reach your static site?
The browser on a laptop or mobile sends a Request over the internet. The security group lets it in on port 80, Nginx on EC2 answers with the page, and the Response travels back.
लॅपटॉप किंवा मोबाइल वरचा ब्राउझर इंटरनेट वरून Request पाठवतो. Security group ती port 80 वर आत येऊ देतो, EC2 वरचा Nginx पेज देऊन उत्तर देतो, आणि Response परत येतो.
लैपटॉप या मोबाइल का ब्राउज़र इंटरनेट से Request भेजता है. Security group उसे port 80 पर अंदर आने देता है, EC2 पर Nginx पेज के साथ जवाब देता है, और Response वापस आता है.
How do I host a static website on EC2 with Nginx?
Step 1 — Create a small site (or use your own)
No site yet? Create one on the server in ~/mysite:
mkdir -p ~/mysite/css
cat > ~/mysite/index.html <<'HTML'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My First Cloud Website</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header><h1>Namaste from AWS EC2!</h1></header>
<main><p>This static website is served by Nginx on an EC2 instance.</p></main>
</body>
</html>
HTML
echo 'body{font-family:Arial,sans-serif;margin:0;background:#f4f7fb} header{background:#0b3d6e;color:#fff;padding:20px;text-align:center} main{max-width:700px;margin:30px auto;background:#fff;padding:20px;border-radius:8px}' > ~/mysite/css/style.css
echo '<!DOCTYPE html><html><body><h1>404 - Page not found</h1><a href="/">Go home</a></body></html>' > ~/mysite/404.html
ls -R ~/mysite
Step 2 — Upload your files from the laptop
Run scp on your laptop, from the folder that contains mysite and your key:
scp -i mykey.pem -r ./mysite ec2-user@<PUBLIC_IP>:~/
Other ways: git clone your GitHub repository on the server (sudo yum install -y git), or drag and drop with WinSCP, MobaXterm or FileZilla (SFTP, port 22, same key). You cannot upload straight into /var/www/... because root owns it — upload to your home folder first.
Step 3 — Copy to the web folder and set permissions
sudo mkdir -p /var/www/mysite
sudo cp -r ~/mysite/* /var/www/mysite/
sudo find /var/www/mysite -type d -exec chmod 755 {} \;
sudo find /var/www/mysite -type f -exec chmod 644 {} \;
ls -la /var/www/mysite
Folders 755 and files 644 let Nginx read everything while only root can change it.
Step 4 — Add an Nginx server block
sudo tee /etc/nginx/conf.d/mysite.conf > /dev/null <<'EOF2'
server {
listen 80;
listen [::]:80;
server_name YOUR_SERVER_NAME;
root /var/www/mysite;
index index.html;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
access_log /var/log/nginx/mysite_access.log;
error_log /var/log/nginx/mysite_error.log;
}
EOF2
# put this server's public IP (or your domain) as server_name
sudo sed -i "s/YOUR_SERVER_NAME/$(curl -s https://checkip.amazonaws.com)/" /etc/nginx/conf.d/mysite.conf
Amazon Linux already has a default server { server_name _; } block in /etc/nginx/nginx.conf that serves /usr/share/nginx/html. Giving your block the exact public IP (or a domain) makes Nginx choose your block. Quick method instead: copy your files into /usr/share/nginx/html/ — no config needed.
Step 5 — Test, reload and open the site
sudo nginx -t && sudo service nginx reload
curl -s http://localhost -H "Host: $(curl -s https://checkip.amazonaws.com)" | head -5
Open http://<PUBLIC_IP> in your browser. Try http://<PUBLIC_IP>/no-such-page to see your custom 404 page. Your website is live! 🎉
Ravindra Bagale's Tip
Many students forget to update server_name when the public IP changes, and then Nginx's default welcome page shows up again. That's why you attach an Elastic IP first. Once the Elastic IP or domain is in place, change server_name and run sudo nginx -t && sudo service nginx reload.
Ravindra Bagale's Tip – मराठी
खूप स्टुडंट्स public IP बदलला की server_name अपडेट करायला विसरतात, आणि मग Nginx चं default welcome पेज परत दिसतं. म्हणून आधी Elastic IP अटॅच करा. Elastic IP किंवा डोमेन लावलं की server_name बदला आणि sudo nginx -t && sudo service nginx reload करा.
Ravindra Bagale's Tip – हिंदी
बहुत स्टूडेंट्स public IP बदलने पर server_name अपडेट करना भूल जाते हैं, और फिर Nginx का default welcome पेज वापस दिखता है. इसलिए पहले Elastic IP अटैच करो. Elastic IP या डोमेन लगते ही server_name बदलो और sudo nginx -t && sudo service nginx reload करो.
How do I update the website later?
Upload the changed files again and copy them over the old ones:
# laptop
scp -i mykey.pem -r ./mysite ec2-user@<PUBLIC_IP>:~/
# server
sudo cp -r ~/mysite/* /var/www/mysite/
With git: cd ~/mysite && git pull, then the same sudo cp. Static files need no reload — only config changes do. Want a proper name like mysite.com instead of an IP? Continue with How to Connect a GoDaddy Domain to AWS EC2.
Ravindra Bagale's Tip
💡 Before blaming the web server, run curl -I http://localhost from inside the server. If this works but the browser doesn't, the problem is outside – the security group, the IP, or you typed https. If it fails inside too, read sudo tail -n 20 /var/log/nginx/mysite_error.log. This one habit halves your troubleshooting time.
Ravindra Bagale's Tip – मराठी
💡 वेब सर्व्हर ला दोष देण्याआधी सर्व्हरच्या आतून curl -I http://localhost चालवा. हे चाललं पण ब्राउझर नाही, तर प्रॉब्लेम बाहेर आहे – security group, IP किंवा https टाइप केलं. आतून पण फेल झालं तर sudo tail -n 20 /var/log/nginx/mysite_error.log वाचा. ही एकच सवय troubleshooting चा वेळ अर्धा करते.
Ravindra Bagale's Tip – हिंदी
💡 वेब सर्वर को दोष देने से पहले सर्वर के अंदर से curl -I http://localhost चलाओ. ये चला पर ब्राउज़र नहीं, तो प्रॉब्लम बाहर है – security group, IP या https टाइप कर दिया. अंदर से भी फेल हुआ तो sudo tail -n 20 /var/log/nginx/mysite_error.log पढ़ो. ये एक आदत troubleshooting का टाइम आधा कर देती है.
How do I fix common static site errors?
Ghabru naka 😅 — every student meets these:
| Symptom | Likely cause | Fix |
|---|---|---|
| 403 Forbidden | No index.html; wrong permissions |
ls -la /var/www/mysite; folders 755, files 644; namei -l /var/www/mysite/index.html |
| 404 Not Found | Wrong root; filename case (Index.html) |
Check the root line; ls the exact names |
| Still the welcome page | Default block wins; server_name is wrong |
Put the exact public IP/domain in server_name; sudo nginx -T shows the full config |
| CSS/images not loading | Wrong relative path; file not uploaded | Browser DevTools (F12) → Network tab |
scp: Permission denied |
Uploading straight into /var/www |
Upload to ~/, then sudo cp |
| Browser timeout | Port 80 missing in the security group; no public IP | Add HTTP 80 from 0.0.0.0/0 |
| Site broke after stop/start | Public IP changed | Attach an Elastic IP; update server_name |
Learn it properly
This guide is the short path. The free AWS course explains every step in depth, with labs:
Got it? Upload, copy, permissions, server block, nginx -t and reload – those are the six steps of static hosting. Now, to send the link you need a proper domain, not an IP – we do that in the next guide.
समजलं का? Upload, copy, permissions, server block, nginx -t आणि reload – हेच static hosting चे सहा टप्पे. आता लिंक पाठवायची तर IP नको, प्रॉपर डोमेन हवं – पुढच्या गाइड मध्ये ते करूया.
समझ आया? Upload, copy, permissions, server block, nginx -t और reload – यही static hosting के छह स्टेप्स हैं. अब लिंक भेजनी है तो IP नहीं, प्रॉपर डोमेन चाहिए – अगली गाइड में वो करेंगे.
Frequently asked questions
How do I host a static website on EC2?
Install Nginx, open port 80 in the security group, upload your files with scp, copy them to /var/www/mysite, add a server block with root /var/www/mysite;, then run sudo nginx -t && sudo service nginx reload.
How do I upload my website files to EC2?
From your laptop: scp -i mykey.pem -r ./mysite ec2-user@<PUBLIC_IP>:~/. You can also use git clone on the server, or WinSCP, MobaXterm or FileZilla (SFTP on port 22).
Why do I get 403 Forbidden after uploading my site?
Usually there is no index.html in the folder, or the permissions are wrong. Set folders to 755 and files to 644, and check every folder in the path with namei -l /var/www/mysite/index.html.
Why does Nginx still show the welcome page?
The default server block in /etc/nginx/nginx.conf won. Put the exact public IP or domain in server_name of your block, then test and reload. Or simply copy your files into /usr/share/nginx/html.
Can I use S3 instead of EC2 for a static website?
Yes. S3 static website hosting needs no server at all. This guide uses EC2 because it teaches the web server, permissions and Linux skills you need for dynamic sites later.
How do I host more than one website on the same EC2 instance?
Give each site its own folder and its own server block with a different server_name (name-based virtual hosts), then point each domain to the same Elastic IP.