<coded>


Deploy a Website on AWS EC2 with One Shell Script

September 18, 2022

Manually setting up servers is time-consuming. With AWS EC2, you can deploy websites in minutes — and even faster if you use a single shell script. In this tutorial, you’ll learn how to launch an Ubuntu EC2 instance, install Nginx, and serve a simple website.


🚀 Step 1: Launch an EC2 Instance

Log in to your AWS Console, go to EC2, and launch a new instance:

Connect via SSH:

ssh -i mykey.pem ubuntu@your-ec2-public-ip

⚡ Step 2: The Deployment Script

#!/bin/bash

# Update server
sudo apt update && sudo apt upgrade -y

# Install Nginx
sudo apt install nginx -y

# Set up website directory
sudo mkdir -p /var/www/myapp
echo "<h1>Hello from AWS EC2 🚀</h1>" | sudo tee /var/www/myapp/index.html

# Configure Nginx
cat <<EOL | sudo tee /etc/nginx/sites-available/myapp
server {
    listen 80;
    server_name _;
    root /var/www/myapp;
    index index.html;
    location / {
        try_files \$uri \$uri/ =404;
    }
}
EOL

# Enable site
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl restart nginx

Make it executable:

chmod +x deploy.sh

🖥️ Step 3: Run the Script

Run your deployment script:

./deploy.sh

Now open http://your-ec2-public-ip you’ll see your website live.


🎯 Why This Matters


✅ You just deployed a website on AWS EC2 with one shell script. Next step: secure it with Let’s Encrypt SSL and automate deployments with CI/CD.

Did you find this useful? Please rate this post: