AWS & Deployment
Moving from "Mild Hosting" to a global server.
Creating your First EC2
EC2 (Elastic Compute Cloud) is basically a rented Virtual Machine.
The Firewall (Security Groups)
By default, AWS (and most cloud providers) blocks everything. It's a "White-list" model. If you don't explicitly allow it, it's denied.
Imagine buying a house with no doors or windows. You are safe, but you can't get in. You need to carve out holes (ports) for specific things.
The Danger of "Closed"
If you launch a server without opening Port 22 (SSH), you lock yourself out forever. You can't login to fix it.
Manual Labor
Installing Nginx isn't enough. You must manually go to AWS Console → Security Groups → Inbound Rules and add Port 80 (HTTP) and 443 (HTTPS).
Essential Ports to Open:
- Port 22 (SSH): So YOU can control the server (Command Line).
- Port 80 (HTTP): So USERS can view your website.
- Port 443 (HTTPS): So traffic is encrypted (Green lock icon).
Server Setup & Deployment
Now that the ports are open, let's set up the environment. Choose your runtime:
1. Connect via SSH
ssh -i "key.pem" ubuntu@your-ipConnecting a Domain Name
Scenario: You bought sellclothes.com on GoDaddy/Namecheap. You want it to point to your new AWS server.
1. The "A Record"
Maps a Name to an IP Address.
- Type: A
- Name: @ (root)
- Value: 13.234.xx.xx (Your VM Public IP)
2. The "CNAME"
Maps a Name to another Name.
- Type: CNAME
- Name: www / api
- Value: sellclothes.com
The "Port Mismatch" Problem
Users will type sellclothes.com in their browser. Browsers automatically connect to Port 80 (HTTP).
But wait! Your Node app is running on Port 3000/4000 locally. Your Python app is on Port 8000.
If users go to Port 80, and your app is on Port 4000, they will see a "Site Can't Be Reached" error.
How do we bridge this gap without forcing users to type sellclothes.com:4000? Read the next section.
The Port 80 Problems
When users type `google.com`, the browser silently adds `:80`. It goes to Port 80 by default. So why is this hard?
Problem 1: The "Sudo" Trap
Ports below 1024 are privileged. You need `sudo` (Administrator) to use them.
Risk: If you run your Node app as root, and it has a bug, the attacker gets Root Access to your entire server. Game over.
Problem 2: The "One Port Rule"
A port can only be used by ONE application at a time.
Imagine you have 2 startups on one cheap server:
- SellClothes.com
- SellShoes.com
Both domains want to send traffic to Port 80. But only one app can listen there.Who gets the port?
The Solution: Nginx Reverse Proxy
What is Nginx?
Nginx is open-source software for web serving, reverse proxying, caching, and more. It was originally designed for maximum performance and stability. Think of it as a professional traffic controller.
We need a "Reverse Proxy". Someone who stands at Port 80, takes ALL the traffic, and intelligently hands it off to your internal apps (running on 3000, 4000, etc.).
(SellClothes.com)
(SellShoes.com)
Practical Guide: Setting it Up
Enough theory. Here is exactly what you type in your terminal to set this up.
1. Install Nginx
sudo apt update && sudo apt install nginx -y2. Open Config File
We need to edit the main configuration file.
sudo vi /etc/nginx/nginx.conf3. The Minimal Config
Delete everything inside http { ... } and replace it with this to route your two shops:
events {
# Events Directive
}
http {
# 1. SellClothes.com -> Port 3000
server {
listen 80;
server_name sellclothes.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
# 2. SellShoes.com -> Port 8080
server {
listen 80;
server_name sellshoes.com;
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
}4. Restart Nginx
Always reload after changing config.
sudo systemctl restart nginxSecure your Application
Now that your HTTP site is live, you need to secure it with HTTPS (SSL/TLS). If you haven't done that yet, check out our guide on Certificate Management.
Go to: Certificate Management