DEV Community

Onkar Vhatkar
Onkar Vhatkar

Posted on

1

Deploying an EC2-Based Web Server: A Practical Guide

Problem Statement:

When deploying a web application, developers often require a reliable and scalable web server. Hosting a website or web application on AWS EC2 is a common solution, but configuring the web server properly can be challenging for beginners. The goal of this project is to set up an EC2 instance, install Apache or Nginx, and ensure it serves web content efficiently.

Debugging Challenges

During the deployment process, several issues may arise:

  1. SSH Connection Issues: Misconfigured security groups or incorrect key pairs may prevent SSH access.
  2. Package Installation Failures: Package repositories may be outdated, requiring updates before installation.
  3. Service Not Starting: Apache or Nginx might fail to start due to incorrect permissions, missing dependencies, or conflicts.
  4. Firewall Restrictions: If the firewall or security group settings are misconfigured, the web server might not be accessible.
  5. File Path Issues: The default web directory may vary depending on the web server, leading to content not being served correctly.

Solution Approach

Step 1: Launch an EC2 Instance

  1. Navigate to AWS Management Console → EC2.
  2. Click Launch Instance and configure:
    o Name: MyWebServer
    o AMI: Amazon Linux 2 or Ubuntu
    o Instance Type: t2.micro (Free Tier eligible)
    o Key Pair: Create or use an existing key pair.
    o Security Group: Allow SSH (22) and HTTP (80) traffic.

  3. Click Launch.

Step 2: Connect to the EC2 Instance

ssh -i your-key.pem ec2-user@your-instance-public-ip
Enter fullscreen mode Exit fullscreen mode
  1. (For Ubuntu, use ubuntu instead of ec2-user)

Step 3: Install Apache or Nginx
Apache Installation

sudo yum update -y  # Amazon Linux
sudo apt update -y  # Ubuntu
sudo yum install httpd -y  # Amazon Linux
sudo apt install apache2 -y  # Ubuntu
sudo systemctl start httpd  # Amazon Linux
sudo systemctl enable httpd  # Amazon Linux
sudo systemctl start apache2  # Ubuntu
sudo systemctl enable apache2  # Ubuntu

Enter fullscreen mode Exit fullscreen mode

Verify installation:

sudo systemctl status httpd  # Amazon Linux
sudo systemctl status apache2  # Ubuntu

Enter fullscreen mode Exit fullscreen mode

Nginx Installation

sudo yum install nginx -y  # Amazon Linux
sudo apt install nginx -y  # Ubuntu
sudo systemctl start nginx
sudo systemctl enable nginx

Enter fullscreen mode Exit fullscreen mode

Verify installation:

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

Step 4: Configure Firewall

Allow HTTP traffic:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

Enter fullscreen mode Exit fullscreen mode

(For Ubuntu, security groups handle firewall settings in AWS.)

Step 5: Deploy a Web Page

sudo echo "<h1>Welcome to My Web Server</h1>" > /var/www/html/index.html  # Apache
sudo echo "<h1>Welcome to My Web Server</h1>" > /usr/share/nginx/html/index.html  # Nginx

Enter fullscreen mode Exit fullscreen mode

Step 6: Access the Web Server
Open a browser and navigate to:

http://your-instance-public-ip
You should see "Welcome to My Web Server" displayed.

Step 7: Configure Auto Start (Optional)
Ensure the web server starts automatically on reboot:

sudo systemctl enable httpd  # Apache
sudo systemctl enable nginx  # Nginx

Enter fullscreen mode Exit fullscreen mode

Conclusion

By following these steps, we successfully set up an EC2-based web server running Apache or Nginx. This approach is widely used in hosting websites, web applications, and reverse proxy configurations. Debugging common issues such as SSH connectivity, package installation, and firewall settings ensures a smooth deployment experience.
By learning in public, sharing our debugging challenges and solutions, we not only solidify our understanding but also help others facing similar issues. Happy deploying!

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

Top comments (1)

Collapse
 
surinder_saini_ea48d6b7dc profile image
Surinder Saini

👍

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️