Installing Nginx via EC2 User Data
Stand up an Nginx web server on a fresh EC2 instance with nothing but a user-data script, no manual SSH setup required.
Photo by Sigmund on Unsplash*
Nginx is an open-source web server that also handles reverse proxying, caching, load balancing, media streaming, and logging. Here's how to get it running on EC2 with zero manual setup, using nothing but the instance's user data.
A few terms, quickly
- Web server: hardware and software that serves data over HTTP
- Reverse proxy: a server that sits in front of your origin server, accepting public requests and relaying them while keeping your actual server hidden
- Caching: a copy of frequently requested data kept somewhere fast, so you're not re-fetching it every time
- Load balancing: spreads traffic across multiple servers so no single one gets overwhelmed
- Media streaming: delivers video and audio to clients as they request it
- Logging: records of application and server behavior for later review
What you need
An AWS account, plus a VPC, internet gateway, and public subnet already set up.
Installing Nginx
- Go to EC2 → Launch instances
- Choose Amazon Linux 2 AMI (64-bit x86)
- Pick an instance type —
t2.microis plenty for testing - Set the VPC and turn on Auto-assign Public IP
- Add this to User data:
#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install nginx1 -y
sudo systemctl enable nginx
sudo systemctl start nginx
- Leave storage at the default
- Add whatever tags you use for organization
- Create a security group allowing SSH and port 80
- Review and launch
- Create or select a key pair
- Visit the instance's public IP in a browser to confirm it's up
If it doesn't show up
- Check the instance's system log first
- SSH in and run
sudo systemctl status nginx - If it's still not right, check
/var/log/nginx/for the actual error
Where to go from here
From this baseline, it's worth digging into Nginx's config files and command set to start using it for what it's actually good at — reverse proxying, caching, load balancing, and streaming.
Practical guide: launch checklist
A condensed, copy-pasteable version of the steps above.
- Have the prerequisites ready first. An AWS account plus a VPC, internet gateway, and public subnet already set up.
- Start the launch. Go to EC2, choose Launch instances, and pick the Amazon Linux 2 AMI (64-bit x86).
- Pick a small instance type.
t2.microis plenty for testing. - Set networking. Choose the VPC and turn on Auto-assign Public IP.
- Paste the install script into User data:
#!/bin/bash
sudo yum update -y
sudo amazon-linux-extras install nginx1 -y
sudo systemctl enable nginx
sudo systemctl start nginx
- Leave storage at the default, add whatever tags you use for organization, and create a security group allowing SSH and port 80.
- Review, launch, and select a key pair.
- Confirm it's up by visiting the instance's public IP in a browser.
- If it doesn't show up: check the instance's system log first, SSH in and run
sudo systemctl status nginx, and if it's still not right check/var/log/nginx/for the actual error.