Hosting a Static Website on AWS EC2: A Step-by-Step Guide

Hosting a Static Website on AWS EC2: A Step-by-Step Guide


In today's digital era, having a website is essential for businesses and individuals alike. While there are many ways to host a website, Amazon Web Services (AWS) provides a reliable and scalable infrastructure for hosting websites of all sizes. In this tutorial, we will explore how to host a static website on an AWS EC2 instance using the http-server package and the process manager pm2. So let's dive in and get started!


Before we begin, make sure you have the following rerequisites in place:

  1. An AWS account with access to the EC2 service.

  2. An EC2 instance is set up and running with a public IP address.

  3. Secure Shell (SSH) access to your EC2 instance.

  4. A static website with all the necessary HTML, CSS, and JavaScript files ready for deployment.

Step 1: Connect to your EC2 Instance

To get started, open a terminal or command prompt on your local machine and connect to your EC2 instance using SSH. Use the following command, replacing your-instance-ip with the public IP address of your EC2 instance:
ssh -i your-key.pem ec2-user@your-instance-ip

Step 2: Install Node.js and NPM

Since we'll be using http-server and pm2 to host our static website, we need to have Node.js and NPM (Node Package Manager) installed on our EC2 instance. Execute the following commands to install them:
sudo yum update -y
curl -o-
source ~/.bashrc
nvm install node
These commands will update the package manager, install NVM (Node Version Manager), and use NVM to install the latest stable version of Node.js.

Step 3: Install and Configure http-server

Next, we need to install the http-server package globally on our EC2 instance. This package allows us to create a simple HTTP server to serve our static website files. Execute the following command to install http-server:
npm install -g http-server
Once installed, navigate to the root folder of your static website using the cd command:
cd /path/to/your/website
Now, start the http-server with the following command:
http-server -p 8080
The -p flag specifies the port number on which the server will listen. Feel free to change it to any available port number.

Step 4: Test Your Website

To verify that your website is being served correctly, open a web browser and enter the following URL, replacing your-instance-ip with the public IP address of your EC2 instance:
If everything is set up correctly, you should see your static website rendered in the browser.

Step 5: Install and Configure pm2

While running the http-server directly in the terminal works fine, it has a limitation: if the terminal session ends or your SSH connection is interrupted, the server will stop. To overcome this, we can use pm2, a process manager for Node.js applications, to ensure our server keeps running even when we're not connected to the EC2 instance.

Install pm2 globally by executing the following command:
npm install -g pm2
Now, start your http-server using pm2 with the following command:

pm2 start http-server --name my-website -- -p 8080
This command starts the http-server process with the name my-website and listens on port 8080. You can change the process name and port number as per your preference.


Congratulations! You have successfully hosted your static website on an AWS EC2 instance using http-server and pm2. Now you have a reliable and scalable hosting solution for your website. Remember to keep your EC2 instance up-to-date with security patches and always monitor your server's resources to ensure optimal performance. Happy hosting!