Skip to main content

Command Palette

Search for a command to run...

Deploy a Web Server on EC2: A Detailed Guide

Updated
7 min read
Deploy a Web Server on EC2: A Detailed Guide
K

Hi, I'm Keerthi Ravilla Subramanyam, a passionate tech enthusiast with a Master's in Computer Science. I love diving deep into topics like Data Structures, Algorithms, and Machine Learning. With a background in cloud engineering and experience working with AWS and Python, I enjoy solving complex problems and sharing what I learn along the way. On this blog, you’ll find articles focused on breaking down DSA concepts, exploring AI, and practical coding tips for aspiring developers. I’m also on a journey to apply my skills in real-world projects like predictive maintenance and data analysis. Follow along for insightful discussions, tutorials, and code snippets to sharpen your technical skills.

Welcome to the next part of our AWS Cloud series! In this article, we will explore how to set up a web server on an Amazon EC2 instance. As businesses increasingly adopt cloud technology, knowing how to host applications and websites effectively is essential. Configuring an EC2 instance will enhance your cloud skills and expand your options for deploying web content beyond static hosting solutions like S3. Whether you're a beginner or looking to strengthen your knowledge, this guide offers clear, step-by-step instructions to get your web server up and running quickly. Let’s get started!

What is EC2?

Amazon EC2 (Elastic Compute Cloud) is a flexible virtual server service that lets users run applications in the cloud. It offers adjustable compute capacity, allowing you to scale your infrastructure as needed. EC2 aims to make web hosting and application deployment easier, offering users the choice of operating system, instance type, and storage options that fit their projects best.

Prerequisites

Before we dive into the setup process, ensure you have the following prerequisites in place:

  1. An AWS account: If you don’t have one, sign up for a free account here AWS console.

  2. Basic knowledge of AWS services and the AWS Management Console.

  3. A local terminal or command prompt for connecting to your EC2 instance via SSH.

With these prerequisites met, you're ready to start setting up your web server on EC2!

Step 1: Launching an EC2 Instance

To begin, the first step is to create an EC2 instance, which functions as a virtual machine hosted on AWS. Follow these instructions:

  1. Access the AWS Management Console

    • Head over to the AWS Console and sign in using your credentials.

    • Once logged in, search for EC2 in the Services menu and select it to open the EC2 Dashboard.

  1. Launch a New Instance

    • On the EC2 Dashboard, click the Launch Instance button to start the process of creating your instance.

    • From the list of available AMIs, choose the Amazon Linux 2023 AMI, which is free-tier eligible and lightweight, making it ideal for new users.

  1. Configure Instance Settings

    • Instance Type: Choose the right Instance Type. To stay within the free-tier, select t2.micro. This instance type offers a good balance of performance and cost-efficiency for beginners and small-scale applications.

  • Configure Instance Details: Leave the default settings as they are, and ensure that Auto-assign Public IP is enabled. This setting assigns a public IP address to your instance, making it accessible over the internet.

  • Add Storage: The default storage setting of 8 GB General Purpose SSD (gp2) is adequate for most basic web hosting or application needs, so you can leave it unchanged.

  • Configure Security Group: Create a new security group and:

    • Add a rule for HTTP (port 80):

      • Allow inbound HTTP traffic by adding a rule that permits access on port 80 from anywhere (0.0.0.0/0). This will make your web server publicly accessible.
    • Add a rule for SSH (port 22):

      • For SSH access, create a rule that allows inbound traffic on port 22. However, for security reasons, restrict the source to your IP address only. This will prevent unauthorized access to your instance while allowing you to manage it securely.

  • Key Pair: You will need a key pair to securely connect to your EC2 instance via SSH. You can either select an existing key pair or create a new one. If you create a new key pair, be sure to download the .pem file when prompted, as you will need it to access your instance. Keep this file secure, as it is your main method of authentication.

  1. Launch the Instance

    After configuring the settings, review everything, then click Launch. Your EC2 instance will take a few minutes to spin up.

Step 2: Connecting to Your EC2 Instance

Once your instance is up and running, the next step is to connect to it using SSH to set up your web server.

  1. Obtain the Public IP Address

    • Navigate to the EC2 Dashboard and locate your running instance.

    • Copy the Public IPv4 address assigned to your instance. This is the IP address you’ll use to connect to the server and access it from your browser later.

  1. Connect Using SSH

    On your terminal (Linux/Mac) or Command Prompt (Windows with SSH installed), run the following command to connect to the instance:

     ssh -i your-key-file.pem ec2-user@your-public-ip-address
    

    for example:

     ssh -i MyKeyPair.pem ec2-user@13.232.211.2
    

    Replace <path-to-your-key-pair> with the location of your .pem file and <public-ip-address> with your instance's IP. When prompted about the host's authenticity, type yes to accept it. You will then be connected to your EC2 instance and ready to set up your web server.

Step 3: Installing a Web Server (Apache or Nginx)

Now that you have successfully connected to your EC2 instance, the next step is to install and configure a web server. You can choose to set up either Apache or Nginx as your web server, depending on your preference and project requirements. Both servers are popular choices for serving web content and are easy to install and configure on an EC2 instance.

Option 1: Installing Apache

  1. Update the package manager:

     sudo yum update -y
    
  2. Install Apache:

     sudo yum install httpd -y
    

  3. Start the Apache service:

     sudo systemctl start httpd
    
  4. Enable Apache to start on boot:

     sudo systemctl enable httpd
    
  5. Verify Apache is running: Open your browser, and go to your instance’s public IP. You should see the default Apache test page.

Option 2: Installing Nginx

  1. Update the package manager:

     sudo yum update -y
    
  2. Install Nginx:

     sudo amazon-linux-extras install nginx1 -y
     ## if the above doesn't work. just install nginx with yum
     ## sudo yum install nginx -y
    

  3. Start the Nginx service:

     sudo systemctl start nginx
    
  4. sudo systemctl enable nginx

     sudo systemctl enable nginx
    

Step 4: Hosting Your Website

With your web server running, the next step is to serve your own HTML content.

  1. Navigating to the Web Directory

    • For Apache: The web root is /var/www/html/.

    • For Nginx: The web root is /usr/share/nginx/html/.

Use the following commands to navigate to the correct directory:

Apache:

    cd /var/www/html/

Nginx:

    cd /usr/share/nginx/html/
  1. Upload or Create a Basic HTML File

    You can either create a simple index.html file directly on the instance or upload your website files using SCP or other file transfer tools.

    To create a basic HTML file:

     sudo nano index.html
    

    Enter the following HTML content:

     <!DOCTYPE html>
     <html>
     <head>
     <title>Welcome to Keerthi's Webserver!</title>
     <style>
     html { color-scheme: light dark; }
     body { width: 35em; margin: 0 auto;
     font-family: Tahoma, Verdana, Arial, sans-serif; }
     </style>
     </head>
     <body>
     <h1>Welcome hashnode!!!</h1>
     <p>Thanks for following till here, now to get futher updates regarding my blogs follow me on hashnode</p>
    
     <p>Here is the my profile link
     <a href="https://hashnode.com/@keerthiravilla">@keerthiravilla</a>.<br/>
     Or if you are interested in non-technical writings, checkout my medium profile
     <a href="https://medium.com/@keerthiravillaSRK">keerthi.medium</a>.</p>
    
     <p><em>Thank you for reading!!.</em></p>
     </body>
     </html>
    

    Save the file and exit (Ctrl + X, then Y).

  2. Access Your Website

    Open a web browser and visit your instance’s public IP:

    http://[your-instance-public-IP]

If everything is set up correctly, you should now see your custom index.html page.

Step 5: Troubleshooting and Best Practices

If you don’t see your website, here are some troubleshooting steps:

  • Check Security Groups: Ensure your security group has an inbound rule allowing HTTP traffic (port 80) from anywhere.

  • Restart Web Server: Restart the web server if necessary:

    • Apache:

        sudo systemctl restart httpd
      
    • Nginx:

        sudo systemctl restart nginx
      
  • Check Web Server Logs: Look at the logs for errors:

    • Apache:

        sudo tail -f /var/log/httpd/error_log
      
    • Nginx:

        sudo tail -f /var/log/nginx/error.log
      

In this article, you discovered how to set up a web server on an AWS EC2 instance, enriching your understanding of cloud computing. This complements our previous article on static web hosting in S3 and gives you more options for your projects. As we continue this AWS Cloud series, we’ll explore more services, so stay tuned for exciting insights and practical tips!

AWS cloud

Part 3 of 10

Welcome to my AWS series on Hashnode!☁️. In this journey, I’ll dive into the powerful features of AWS, sharing insights, tutorials, and best practices. Join me as we level up our cloud skills and celebrate our growth in cloud computing! 🎉🚀

Up next

Building a REST API with AWS Lambda and API Gateway: A Beginner's Tutorial

In this article, we will explore the fundamentals of creating a well-designed API, which can streamline communication between different software systems, enabling seamless interactions and data sharing. We will utilize two key AWS services in this pr...

More from this blog

K

Keerthi's Dev Chronicles

26 posts

Welcome to Keerthi's Coding Blogs! Explore how Data Structures and Algorithms enhance cloud solutions on AWS. Join me to discover insights and tips. Let’s connect and learn together!