To host a website on EC2 with Nginx, launch a Linux instance, open ports 80 and 443 in its security group, install Nginx, put your files in the web root and enable the service so it starts on boot. This produces a working site at the instance's public address in a few minutes. For a real domain you then point DNS at a stable address and add HTTPS.
This guide uses Amazon Linux 2023 and shows each step to host a website on ec2 nginx, with notes for Ubuntu where commands differ.
t3.micro or similar is enough for a small site) with an Amazon Linux 2023 or Ubuntu AMI.A security group is a virtual firewall. Without a rule for HTTP, the browser will time out even if Nginx runs perfectly. Add these inbound rules:
| Type | Port | Source | Why |
|---|---|---|---|
| SSH | 22 | Your IP only | Administration |
| HTTP | 80 | 0.0.0.0/0 | Public web traffic |
| HTTPS | 443 | 0.0.0.0/0 | Encrypted traffic |
Never open SSH to the whole internet if you can avoid it. The subnet also needs a route to an internet gateway for the instance to be reachable.
Connect with SSH, then install and start Nginx:
On Ubuntu, use sudo apt update && sudo apt install -y nginx and log in as ubuntu. Nginx on Ubuntu starts automatically after installation.
Open http://YOUR_PUBLIC_IP in a browser. The default Nginx welcome page confirms the server and the security group both work.
On Amazon Linux the default web root is /usr/share/nginx/html. A cleaner approach is to create your own directory:
To upload a built site from your computer, use scp -i my-key.pem -r ./dist/* ec2-user@YOUR_PUBLIC_IP:/var/www/mysite/, or rsync -avz for repeated deploys.
Nginx loads extra configuration from /etc/nginx/conf.d/*.conf. Create a file for your site:
Save it as /etc/nginx/conf.d/mysite.conf. Check the syntax before applying it, then reload without dropping connections:
If the page returns 403 Forbidden, the Nginx user cannot read your files: check that the directory and files are readable by others (chmod -R o+rX /var/www/mysite) and that every parent directory is executable. The default server block in nginx.conf on Amazon Linux may also answer first; if so, remove or change its server_name so yours matches.
Create an A record for your domain pointing to the instance's Elastic IP (or use Route 53). Once DNS resolves, get a free certificate from Let's Encrypt with Certbot:
The Certbot package name can differ by distribution and version; check the Certbot site for the current install method. Certbot edits your server block to listen on 443, redirects HTTP to HTTPS and sets up automatic renewal. Verify renewal with sudo certbot renew --dry-run.
If you plan to run several servers, consider terminating TLS on an Application Load Balancer with a free AWS Certificate Manager certificate instead.
Check three things: the security group allows port 80 from the internet, the instance has a public address and an internet gateway route, and Nginx is running (systemctl status nginx).
The default is /usr/share/nginx/html, but it is better to serve your own directory defined with root in a server block under /etc/nginx/conf.d/.
Not to test it, but you do for a domain. Without one, the public address changes when you stop and start the instance, breaking your DNS record.
For small sites, yes, with backups. For availability, run at least two instances behind a load balancer in different Availability Zones.
Hosting a site on EC2 with Nginx takes five steps: allow ports 80 and 443 in the security group, install Nginx, copy your files to a web root, add a server block and reload, then attach a domain and HTTPS. Test the configuration with nginx -t before every reload and give the instance an Elastic IP so DNS stays valid.