How to configure a domain on EC2 and install an SSL certificate

Linux commands

1. Point Your Domain to Your EC2 Instance

Identify the EC2 instance you want to associate with your domain

2. Update DNS Records

  • Log in to your domain registrar (e.g., GoDaddy, Namecheap, Hostinger).
  • Go to DNS settings and create an A record
  • Host: @
  • Value: Your EC2 public IP
  • TTL: Default

3. Install Nginx on EC2

If you haven't installed Nginx yet, do so with:

sudo apt update && sudo apt install -y nginx

4. Update server_name in the following configuration.

server {
      server_name your-domain-name;

      root /var/www/html/myapp/.next;

      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;
      }
  }

5. Install SSL Certificate Using Let's Encrypt (Certbot)

Install Certbot and Nginx Plugin

sudo apt install -y certbot python3-certbot-nginx

Obtain and Install SSL Certificate

sudo certbot --nginx -d [your-domain-name]

Enable Auto-Renewal for SSL

sudo certbot renew --dry-run

Restart Nginx server

sudo systemctl restart nginx