Cloud & Hosting (AWS)

AWS EC2 Setup & Deployment Guide

Launch a virtual server on AWS, connect with SSH, deploy a Node.js app with Nginx, attach an Elastic IP and domain, and add free SSL.

Updated Official documentation ↗

1. Introduction & use cases

Amazon EC2 (Elastic Compute Cloud) gives you virtual servers (instances) in the AWS cloud. You choose the operating system (an AMI such as Ubuntu 24.04), the size (instance type, e.g. t3.small or ARM-based t4g.small), storage (EBS volumes) and the firewall (security group).

You get full root access – install anything, just like a VPS – and can resize, snapshot or replace the server at any time. EC2 is a good fit when you want control over the server or run software that is not serverless.

Where it is used

  • Hosting Node.js, Python, PHP, Java or Go applications
  • Self-hosted tools (Jenkins, GitLab runners, databases for testing)
  • Background workers and scheduled jobs
  • Lift-and-shift of existing servers to the cloud

2. Prerequisites

You needVersion / notes
AWS accountWith MFA on the root user and a non-root admin identity
SSH clientBuilt into macOS/Linux/Windows 10+ (ssh)
Domain (optional)For HTTPS on your own name
An app to deploye.g. from the Node.js guide

3. Local setup

  1. 1Launch an instance

    EC2 console → Launch instance: name it, choose Ubuntu Server 24.04 LTS, instance type t3.micro/t3.small (or t4g for ARM), and storage 20–30 GB gp3.

  2. 2Create a key pair

    In the launch wizard create a key pair (ED25519, .pem) and download it – AWS shows it only once. Protect the file.

    BASH
    mkdir -p ~/.ssh && mv ~/Downloads/my-ec2-key.pem ~/.ssh/
    chmod 400 ~/.ssh/my-ec2-key.pem
  3. 3Configure the security group

    Allow SSH (22) from My IP only, and HTTP (80) + HTTPS (443) from anywhere. Do not open app ports like 3000.

    OUTPUT
    Type    Port  Source
    SSH     22    My IP (e.g. 49.36.x.x/32)
    HTTP    80    0.0.0.0/0, ::/0
    HTTPS   443   0.0.0.0/0, ::/0
  4. 4Connect with SSH

    Use the instance’s public IP or DNS. The default user on Ubuntu AMIs is ubuntu.

    BASH
    ssh -i ~/.ssh/my-ec2-key.pem ubuntu@<PUBLIC_IP>
    sudo apt update && sudo apt upgrade -y

4. Create & run a project

  1. 1Allocate an Elastic IP

    A public IP changes when you stop/start the instance. EC2 → Elastic IPs → Allocate → Associate with your instance gives a fixed address for DNS.

  2. 2Install Node.js and run a sample app

    Same as any Ubuntu server.

    BASH
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
    source ~/.bashrc && nvm install --lts
    mkdir ~/app && cd ~/app && npm init -y
    cat > server.js <<'EOF'
    require('http').createServer((q, s) => s.end('Hello from EC2')).listen(3000);
    EOF
    node server.js &
    curl localhost:3000
  3. 3Use an IAM role instead of keys

    If the app needs AWS services (S3, SES …), attach an IAM role to the instance (Actions → Security → Modify IAM role). The SDKs pick up credentials automatically.

✅ When it works you should see output like Figure 1.

5. Development configuration

Environment variables

Store app settings in an env file on the instance (readable only by the app user) or in SSM Parameter Store / Secrets Manager, fetched at startup via the instance role.

.env
NODE_ENV=production
PORT=3000
AWS_REGION=ap-south-1
# no AWS keys here – the instance role provides credentials

Key dependencies

Package / toolUsed for
AMIUbuntu 24.04 LTS (or Amazon Linux 2023)
Instance typet3/t4g for small apps; m/c families for steady load
EBS gp3Root disk; snapshot for backups
Security groupInstance firewall
Elastic IPFixed public IP
SSM Session Manager (optional)Shell access without opening port 22

Folder structure

project structure
/home/ubuntu/
└── app/
    ├── server.js
    ├── package.json
    └── .env          # chmod 600
/etc/nginx/sites-available/example.com

Essential commands

CommandWhat it does
ssh -i key.pem ubuntu@IPConnect
scp -i key.pem file ubuntu@IP:~Copy files
aws ec2 describe-instances --output tableList instances
aws ec2 create-snapshot --volume-id vol-…Back up a volume
sudo systemctl status nginxCheck services on the instance

6. Production deployment

Server & hosting requirements

RequirementRecommendation
Instancet3.small (2 GB RAM) for a typical Node/Python app
NetworkingElastic IP, security group 80/443 open, 22 from your IP
Process managerPM2 or systemd
Web serverNginx + Let’s Encrypt
BackupsScheduled EBS snapshots (Data Lifecycle Manager)
MonitoringCloudWatch metrics + alarms

Deployment steps

  1. 1Install Node.js on the server

    Install the current LTS release with nvm (or the NodeSource packages). Use the same major version as on your computer.

    BASH
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
    source ~/.bashrc
    nvm install --lts
    node -v && npm -v
  2. 2Get the code and build it

    Clone the repository on the server (or upload a build artifact from CI). Install exact dependency versions with npm ci and build.

    BASH
    git clone https://github.com/you/your-app.git /home/ubuntu/app
    cd /home/ubuntu/app
    npm ci --omit=dev
  3. 3Keep the app running with PM2

    PM2 restarts the app if it crashes and starts it again after a server reboot. pm2 startup prints one command – run it with sudo, then pm2 save.

    BASH
    sudo npm install -g pm2
    pm2 start server.js --name app
    pm2 startup          # run the command it prints
    pm2 save
    pm2 logs app        # live logs
    pm2 restart app     # after each deploy
  4. 4Install Nginx as a reverse proxy

    Nginx receives requests on ports 80/443 and forwards them to your app on 127.0.0.1:3000. Replace example.com with your domain.

    /etc/nginx/sites-available/example.com
    server {
        listen 80;
        server_name example.com www.example.com;
    
        location / {
            proxy_pass http://127.0.0.1:3000;
            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
  5. 5Enable the site and reload Nginx

    Link the config into sites-enabled, test it and reload. nginx -t must say syntax is ok before you reload.

    BASH
    sudo apt install -y nginx
    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl reload nginx
  6. 6Point the domain to the Elastic IP

    Add an A record for example.com (and www) to the Elastic IP in Route 53 or your registrar.

    OUTPUT
    # A  example.com   13.233.x.x (Elastic IP)
    dig +short example.com
  7. 7Add free HTTPS with Let’s Encrypt

    Certbot gets a free SSL certificate, adds it to the Nginx config and redirects HTTP to HTTPS. Certificates renew automatically (a systemd timer runs twice a day); test renewal once with --dry-run.

    BASH
    sudo apt install -y certbot python3-certbot-nginx
    sudo certbot --nginx -d example.com -d www.example.com
    sudo certbot renew --dry-run
  8. 8Automate backups and alarms

    Create a Data Lifecycle Manager policy for daily EBS snapshots, and a CloudWatch alarm on CPUUtilization and StatusCheckFailed with an email (SNS) notification.

🗺 The whole path from your code to the live site is drawn in the deployment diagram.

7. Images & screenshots

Illustrations drawn for this guide from the commands above. Product dashboards change often, so for screens such as cloud consoles follow the step text and the official documentation.

terminal$ ssh -i ~/.ssh/my-ec2-key.pem ubuntu@13.233.x.xWelcome to Ubuntu 24.04 LTS (GNU/Linux 6.8.0-1012-aws x86_64)ubuntu@ip-172-31-5-20:~$ node server.js &[1] 2841ubuntu@ip-172-31-5-20:~$ curl localhost:3000Hello from EC2
Figure 1: terminal output of the sample AWS EC2 project (section 4).
1Launch Ubuntuinstance2Elastic IP +security group3Node + PM24Nginx + domain5HTTPS + snapshots
Figure 2: production deployment flow (section 6).
/home/ubuntu/└── app/ ├── server.js ├── package.json └── .env # chmod 600/etc/nginx/sites-available/example.com
Figure 3: project folder structure (section 5).

8. Common errors & solutions

WARNING: UNPROTECTED PRIVATE KEY FILE! / permission denied

The .pem file is too open. Run chmod 400 key.pem (Windows: remove inherited permissions so only your user can read it).

SSH times out

Security group does not allow port 22 from your current IP (it changes on mobile/home networks), or you used the wrong IP after a restart. Update the rule or use an Elastic IP.

Permission denied (publickey)

Wrong username (ubuntu for Ubuntu, ec2-user for Amazon Linux) or wrong key file.

Site not reachable on port 80/443

Security group lacks HTTP/HTTPS rules, Nginx is not running, or UFW blocks it (sudo ufw allow 'Nginx Full').

Instance slows down over time (t3)

Burstable instances spend CPU credits. Check CPUCreditBalance; move to a larger or non-burstable type, or enable unlimited mode.

Best practices

  • Restrict SSH to your IP or use Session Manager instead of port 22.
  • Attach IAM roles; never store AWS keys on instances.
  • Snapshot volumes automatically and test restores.
  • Keep the OS patched (unattended-upgrades).
  • For more than one server, add a load balancer and auto scaling group.