Hosting a Portfolio Website on EC2 for $2/month

nitesh

nitesh rijal

How to host a portfolio website as low as 2 USD/month on ec2.

3 min read
·
May 22, 2023
--
Well there is no denying that as we are using complicated system to host our portfolio website. So, today I am going to guide how to host a website with our own code so that creativity comes first rather than being scared of making complicated system so let’s start with the journey how I hosted portfolio site rijalnitesh.com .
So, here I have used namecheap as Domain register or to register my domain.
Then I have launched ec2 instance with free-tier eligible service i.e. ubuntu server and t2.micro 1 virtual cpu and 1 GB RAM and 20 GB storage.
While allowing all rules in outbound rule and only allowing my ip in ssh port and any in http and https in inbound rule for keeping the ec2 more secure.
Now connect to your ec2 instance via ssh or via serial console. now let’s start the process .
#/bin/bashsudo apt update sudo apt upgrade -y ### install docker and nginx and certbot ###echo "#########################"echo "Installing docker "echo "########################"sudo apt-get install ca-certificates curl gnupg -ysudo install -m 0755 -d /etc/apt/keyringscurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpgsudo chmod a+r /etc/apt/keyrings/docker.gpgecho \  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \  sudo tee /etc/apt/sources.list.d/docker.list > /dev/nullsudo apt-get updatesudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -ysudo groupadd dockersudo usermod -aG docker $USERsudo apt install git -yecho "#####################"echo "Docker installation successfull"echo "#######################"echo "######################"echo "Installing nginx and certbot"echo "######################"# Install Nginxsudo apt install -y nginx# Install Certbotsudo apt updatesudo apt install -y certbot python3-certbot-nginx# Restart Nginxsudo systemctl restart nginx echo "######################"echo "Installation  of nginx and certbot successfull"echo "######################"
Now add your ec2 ip to DNS record of your registered domain in registar in my case namecheap. Wait for sometime as it requires time to propagate the DNS to ec2. Meanwhile we setup the static website we write Dockerfile which exposes on port 5173 in my case since I am using threejs to host my website so here’s the dockerfile and yaml file for this
# Use the official Node.js 16 image as the base imageFROM node:16# Set the working directory in the containerWORKDIR /appRUN npm install -g viteRUN npm i -g three# Copy package.json and package-lock.json to the containerCOPY package*.json ./# Install the app's dependenciesRUN npm install# Copy the app's source code to the containerCOPY . /app/# Expose the port that the app listens onEXPOSE 5173# Start the appRUN npm i viteRUN npm i threeRUN npm iRUN npm updateCMD [ "npm", "run", "dev" ]
and here’s my docker-compose file
version: '3'services:  app:    container_name: app    build:      context: .      dockerfile: Dockerfile    ports:      - "5173:5173"    volumes:      - .:/app    environment:      NODE_ENV: production
run docker compose up
docker-compose up --build -d
now let’s configure nginx we create new file in /etc/nginx/sites-available make a conf file as your-servername.conf in my case rijalnitesh.com.conf
server {  listen 443 ssl;  server_name rijalnitesh.com;  client_max_body_size 500M;   location / {    proxy_set_header   X-Forwarded-For $remote_addr;    proxy_set_header   Host $http_host;    proxy_set_header   Upgrade $http_upgrade;    proxy_set_header   Connection "upgrade";    proxy_pass http://localhost:5173;  }} server {  listen 5173;  server_name rijalnitesh.com;  return 301 https://$host$request_uri;}
Now run the following commands
sudo nginx -tsudo systemctl reload nginx
Now run the command to install ssl certificate
sudo certbot --nginx -d rijalnitesh.com
If everything goes well we are able to get site as mine
Thank you for reading my blog. I am always open to comments.
Like this project

Posted Jul 28, 2025

Guided hosting a portfolio website on EC2 for $2/month.

Likes

0

Views

1

Timeline

Jun 29, 2023 - Jun 30, 2023