How to Host a Website on AWS EC2 with User Data Scripts: Complete Guide
By Braincuber Team
Published on March 12, 2026
Stop SSH-ing into new servers to manually run apt-get or yum updates. We see D2C brands wasting hours configuring new EC2 instances by hand every time traffic spikes and they need to scale. That manual process is slow, error-prone, and entirely unnecessary. By using an AWS EC2 User Data script, you can automate the entire bootstrap process — from installing Apache to downloading your site's codebase — before you even see the instance launch screen. This complete tutorial shows you exactly how to do it.
What You'll Learn:
- How to launch an Amazon Linux 2 EC2 instance (Free Tier)
- Writing a bash script to fully automate Apache web server setup
- Injecting the script into the EC2 User Data field during launch
- Configuring Security Groups to allow public HTTP traffic
- Verifying deployment without ever using an SSH client
Why Manual Configuration is Obsolete
When you launch an EC2 instance, it's just a blank operating system. Traditionally, you would generate a key pair, launch PuTTY or Terminal, SSH into the box, run your package manager updates, install a web server, fetch your code from GitHub, unzip it, and restart the services. That takes 15 minutes if you don't make a typo. User Data scripts reduce that time to zero.
Zero-Touch Automation
Imagine a to-do list for your server. During boot-up, the metadata service executes your script as the root user. By the time the instance status checks pass, your website is already live and serving traffic.
Perfect Consistency
If you build a script that works once, it works a thousand times. There is no risk of forgetting a critical dependency or typing the wrong directory path. The deployment becomes completely repeatable.
| Metric | Manual SSH Setup | User Data Script |
|---|---|---|
| Time to Live | 10-15 minutes | ~2 minutes (Boot time) |
| Human Error Risk | High (typos, skipped steps) | Zero (after initial script testing) |
| Auto Scaling Support | Impossible | Fully Supported |
| Root Access Needed | Yes (via sudo) | No (Script authenticates itself implicitly) |
The Apache Bootstrapping Script
Before we launch the server, here is the bash script we're going to use. It updates the OS packages, installs the Apache HTTP server, downloads a sample CSS template, unpacks it into the public HTML folder, and ensures the whole thing boots on restart.
#!/bin/bash
# Update the system and install necessary packages
yum update -y
yum install -y httpd wget unzip
# Start Apache and configure it to boot automatically
systemctl start httpd
systemctl enable httpd
# Move to the web root and pull down a sample template
cd /var/www/html
wget https://www.free-css.com/assets/files/free-css-templates/download/page284/built-better.zip
# Extract the archive into the root directory
unzip built-better.zip -d /var/www/html/
mv /var/www/html/html/* /var/www/html/
# Delete the leftover ZIP file and empty extracted directory
rm -r /var/www/html/html
rm built-better.zip
# Restart the server to enforce changes
systemctl restart httpd
Step-by-Step Deployment Instructions
Follow these steps perfectly. If you miss the Security Group configuration, your instance will launch but the internet won't be able to reach it — a classic beginner mistake.
Select Your AMI and Instance Type
Open the AWS Console, search for EC2, and click Launch Instance. Give it a name like "Automated Webserver". Under OS Images, select Amazon Linux 2 AMI (Free tier eligible). It uses the yum package manager which matches our script. For Instance Type, pick t2.micro so you don't incur charges.
Configure HTTP Security Group Rules
Scroll down to Network Settings. You must check the box that says "Allow HTTP traffic from the internet". Without this, your firewall rejects all port 80 requests from web browsers. You can optionally allow SSH traffic, but thanks to our script, we practically won't need it.
Inject the User Data Script
Scroll all the way down and expand the Advanced details section. Find the large text box labeled User Data. Copy and paste the entire bash script provided in the section above. Ensure the #!/bin/bash line is at the very top. Click Launch Instance.
Access Your Live Website
Wait about 2 minutes for the instance status checks to read "2/2 checks passed." Select your instance from the list, copy the Public IPv4 Address from the summary tab, and paste it into your browser. You should immediately see the "Built Better" template running entirely on its own.
Debugging User Data Failures
If your site doesn't load, verify that Port 80 is open in your Security Group. If the group is fine, the script may have failed. To debug, SSH into the instance and read the log output by running cat /var/log/cloud-init-output.log. It will show exactly which line in your script errored out. Remember, User Data scripts are run as the root user automatically, so sudo isn't strictly necessary within the script.
Frequently Asked Questions
Can I run User Data scripts every time an instance stops and starts?
No, by default User Data scripts only run once — during the very first boot cycle. If you need a script to run on every reboot, you must configure a chron job or specific cloud-init directives.
Can I edit my User Data later?
Yes, you can edit it via the EC2 console, but you must stop the instance first. Remember that simply changing the script won't run it again unless you alter cloud-init state files.
Do I need to include 'sudo' in my script commands?
No, scripts entered as User Data are executed with root privileges automatically. Adding sudo won't hurt, but it is redundant.
Why isn't my EC2 website loading?
9 times out of 10, the security group assigned to the instance does not have an inbound rule allowing HTTP traffic over port 80. Check your VPC security groups immediately.
What OS should I use for this tutorial?
We strongly recommend Amazon Linux 2. If you choose Ubuntu, the package manager changes from yum to apt, and the Apache service name changes from httpd to apache2.
Tired of Manually Managing AWS Environments?
If you are manually deploying instances or struggling to implement highly available Auto Scaling groups, you are wasting operational budget. Our engineers implement automated Terraform pipelines for D2C brands that deploy entire self-healing infrastructure stacks with zero human intervention. Let us audit your deployment workflow today.
