Deploying a Simple Java HTTP Server on AWS EC2

Sravya BollaSravya Bolla
3 min read

Working with cloud infrastructure doesn't always require a complex stack or heavyweight frameworks. Sometimes, a basic experiment like deploying a simple Java HTTP server can be a great way to understand the fundamentals of cloud deployment, networking, and remote server access.

Project Overview

In this mini-project, I deployed a simple Java-based “Hello World” web application on a remote AWS EC2 server. This served as a hands-on exercise to understand how cloud instances work, how to set up a server environment from scratch, and how to expose services over the internet securely.

Tools & Requirements

  • Java Development Kit (JDK): Java 17+

  • AWS Account

  • EC2 Instance (Ubuntu or Amazon Linux)

  • Security Group Configuration: Inbound traffic allowed on port 8080

Key Features

  • Java HTTP server running on port 8080

  • Hosted on a remote EC2 instance

  • Configured security group rules

  • Accessed the service via EC2 Public IP

Here's the simple Java HTTP server code that serves a "Hello, World!" message on port 8080:

javaCopyEditimport java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;

public class HelloWorldServer {
    public static void main(String[] args) throws IOException {
        int port = 8080;
        HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
        server.createContext("/", new MyHandler());
        server.setExecutor(null); // creates a default executor
        System.out.println("Server started on port " + port);
        server.start();
    }

    static class MyHandler implements HttpHandler {
        public void handle(HttpExchange exchange) throws IOException {
            String response = "Hello, World from SRAVYA!";
            exchange.sendResponseHeaders(200, response.getBytes().length);
            OutputStream os = exchange.getResponseBody();
            os.write(response.getBytes());
            os.close();
        }
    }
}

Step-by-Step Deployment Guide

1. Local Setup

  • Save the Java file as HelloWorldServer.java

  • Compile the code:

      javac HelloWorldServer.java
    
  • Run locally (for testing):

      java HelloWorldServer
    
  • Test in browser:
    Open http://localhost:8080

Remote Deployment on AWS EC2

Launch EC2 Instance

  • Choose Ubuntu or Amazon Linux

  • Open port 8080 in the security group

SSH into EC2

ssh -i your-key.pem ec2-user@<your-ec2-ip>

Install Java

Amazon Linux:

sudo yum update -y
sudo yum install java -y

Ubuntu:

sudo apt update
sudo apt install default-jdk -y

✅ Option 1: Transfer Java File from Local to EC2 via SCP

scp -i your-key.pem HelloWorldServer.java ec2-user@<your-ec2-ip>:/home/ec2-user/

🔹 Make sure:

  • Replace your-key.pem with your actual EC2 key pair file.

  • Replace <your-ec2-ip> with the public IP of your EC2 instance.

  • File will land in the /home/ec2-user/ directory.


✅ Option 2: Push to GitHub and Clone on EC2

  1. Create a new GitHub repository and push your files:

     git init
     git remote add origin https://github.com/your-username/your-repo.git
     git add .
     git commit -m "Initial commit"
     git push -u origin main
    
  2. SSH into EC2 and clone it:

     ssh -i your-key.pem ec2-user@<your-ec2-ip>
     git clone https://github.com/your-username/your-repo.git
     cd your-repo
    

    Compile and Run on EC2

     javac HelloWorldServer.java
     java HelloWorldServer
    

    Access the App

    Visit:

     http://<EC2-Public-IP>:8080
    

    you will see:

    Reflection

    This small-scale deployment taught me the basics of setting up a remote server from scratch, configuring network access, installing runtime environments, and making a service publicly available over the internet. It’s simple, yet powerful when you see your code served from a real server for the first time.

Hope you like it:)

0
Subscribe to my newsletter

Read articles from Sravya Bolla directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sravya Bolla
Sravya Bolla