AWS Builder Center
Part 06: Connect EC2 with SSH and Deploy a Website

Part 06: Connect EC2 with SSH and Deploy a Website

Learn how to connect to an Amazon EC2 Linux instance using SSH and deploy your first website on a cloud server. This step-by-step AWS tutorial covers key pairs, security groups, SSH commands, Apache web server installation, and website deployment.

Series: AWS Zero to Cloud Builder (8 articles)

  1. …
  2. 5
    Part 06: Connect EC2 with SSH and Deploy a Website This article
Student

Connect EC2 with SSH and Deploy a Website

In the previous article, we learned how to host a static website using Amazon S3.
But what if you want more control over your server and want to install software, run commands, and manage your website directly?
This is where Amazon EC2 and SSH come in.
Amazon EC2 allows you to run virtual servers in the AWS Cloud, while SSH (Secure Shell) provides a secure way to connect to and manage your Linux server remotely.
In this article, we'll connect to an EC2 instance using SSH and deploy a simple website using the Apache web server.

What is SSH?

SSH stands for Secure Shell.
It is a network protocol that allows users to securely connect to another computer over a network.
With SSH, you can:
  • Access your EC2 instance remotely.
  • Execute commands on your cloud server.
  • Install and configure software.
  • Manage website files.
  • Troubleshoot server issues.
Think of SSH as remotely opening the terminal of your cloud computer.

What Do You Need Before Starting?

Before deploying your website, make sure you have:
  • An AWS account.
  • An Amazon EC2 Linux instance.
  • A downloaded  .pem  key pair file.
  • The public IPv4 address or public DNS of your instance.
  • An SSH client (Windows PowerShell or Terminal).
  • Basic knowledge of HTML.
For this tutorial, we'll use an Amazon Linux 2023 EC2 instance.

Step 1: Launch an EC2 Instance

First, we need a virtual server.
  1. Log in to the AWS Management Console.
  1. Search for Amazon EC2.
  1. Open the EC2 Dashboard.
  1. Click Launch Instance.
  1. Enter a name, such as  My-Web-Server .
  1. Select Amazon Linux 2023 AMI.
  1. Choose an instance type, such as  t3.micro  if available and suitable for your account.
  1. Create a new key pair or select an existing one.
  1. Download and securely save the  .pem  file.
  1. Configure your network settings and launch the instance.
Wait until the instance reaches the Running state and passes its status checks.

Step 2: Configure Security Group Rules

A Security Group acts like a virtual firewall for your EC2 instance.
For this website deployment, we need two inbound rules:
Type
Port
Source
SSH
22
My IP
HTTP
80
Anywhere (IPv4)
Important: Keep SSH access restricted to your own IP address instead of allowing everyone to connect. HTTP access can be public when you want visitors to access your website.
Also make sure your instance has a public IPv4 address and a suitable network route for internet access.

Step 3: Connect to EC2 Using SSH

Now comes the interesting part!
If you're using Windows:
  1. Open PowerShell.
  1. Navigate to the folder containing your downloaded  .pem  file.
  1. Copy your EC2 instance's Public IPv4 DNS address.
  1. Run the following command:
ssh -i "my-key.pem" ec2-user@YOUR-EC2-PUBLIC-DNS
Replace:
  •  my-key.pem  with your actual key file name.
  •  YOUR-EC2-PUBLIC-DNS  with your instance's public DNS address.
If you're using Amazon Linux 2023, the default SSH username is  ec2-user .
On your first connection, you may be asked to confirm the server's host key. Verify the fingerprint where appropriate, then enter  yes .
If the connection is successful, you'll see a terminal prompt from your EC2 instance.
Congratulations! You're now connected to your cloud server.

Step 4: Update Your EC2 Instance

Before installing the web server, update the system packages.
Run:
sudo dnf update -y
This command checks for and installs available package updates.

Step 5: Install Apache Web Server

Apache is a popular web server that can deliver website content to visitors.
Install Apache using:
sudo dnf install httpd -y
After installation, start the Apache service:
sudo systemctl start httpd
Enable it to start automatically when the instance boots:
sudo systemctl enable httpd
To check whether Apache is running, use:
sudo systemctl status httpd
If everything is working correctly, Apache should show an active status.

Step 6: Create Your First Website

Now let's create a simple HTML webpage.
Run:
sudo nano /var/www/html/index.html
Add the following HTML code:
<!DOCTYPE html>
<html>
<head>
<title>My First EC2 Website</title>
</head>
<body>
<h1>Hello from Amazon EC2!</h1>
<p>My first website is running on AWS.</p>
<p>Deployed using SSH and Apache.</p>
</body>
</html>
Save the file and exit the editor.
Your website's homepage is now stored in Apache's default document directory.

Step 7: Access Your Website

It's time to see your website online!
  1. Open the AWS EC2 Console.
  1. Select your running instance.
  1. Copy its Public IPv4 address.
  1. Open your browser.
  1. Enter the following URL:
http://YOUR-EC2-PUBLIC-IP
Replace the placeholder with your actual public IP address.
If your security group, network configuration, and Apache service are correctly configured, your website should appear in the browser.
Congratulations! Your first website is now running on an Amazon EC2 instance.

Common SSH Connection Problems

Sometimes, beginners face issues while connecting to an EC2 instance.
Here are a few common problems and their possible solutions:
1. Connection Timed Out
  • Check whether your EC2 instance is running.
  • Verify that inbound SSH traffic is allowed on port 22.
  • Make sure the SSH rule allows your current public IP.
  • Check the instance's network configuration.
2. Permission Denied (Publickey)
  • Verify that you're using the correct  .pem  file.
  • Check your SSH username.
  • Make sure the key pair matches the one selected when launching the instance.
3. Website Not Loading
  • Check whether Apache is running.
  • Verify that port 80 is allowed in your security group.
  • Confirm that your instance has public internet connectivity.
  • Make sure your  index.html  file is in the correct directory.

Important Security Tips

While working with EC2, security should always be a priority.
  • Never share your private  .pem  key file.
  • Restrict SSH access to trusted IP addresses.
  • Avoid storing sensitive information in public website directories.
  • Keep your operating system and software updated.
  • Use HTTPS for production websites.
  • Stop or terminate resources when you no longer need them, after checking that you don't need to preserve their data.

Why Is This Project Important?

This small project teaches you several important cloud computing concepts.
You'll understand:
  • How virtual servers work.
  • How to connect to a remote Linux machine.
  • How SSH authentication works.
  • How security groups control network access.
  • How to install and manage a web server.
  • How websites are deployed on cloud infrastructure.
These skills are useful when learning cloud computing, Linux administration, DevOps, and backend development.

Conclusion

Connecting to an EC2 instance using SSH and deploying a website is one of the best practical exercises for AWS beginners.
Instead of only learning theoretical concepts, you've now experienced how to access a cloud server, install Apache, create a webpage, and make it accessible through the internet.
Keep experimenting with EC2 and try deploying your own HTML and CSS projects.
What's Next?
In Part 07, we'll explore AWS IAM (Identity and Access Management) and understand how AWS controls users, roles, and permissions.
Keep learning, keep building, and keep exploring AWS!

Series: AWS Zero to Cloud Builder (8 articles)

  1. …
  2. 5
    Part 06: Connect EC2 with SSH and Deploy a Website This article
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article