Transitioning My Personal Website to AWS EC2
Launching a Svelte website on AWS EC2 involves several clear, manageable steps. Here's a comprehensive guide to smoothly transition your website:
1. Creating an EC2 Instance
Start by logging into your AWS console and creating a new EC2 instance. Choose an Ubuntu-based AMI (like Ubuntu Server 22.04) with an appropriate instance type (e.g., t2.micro for small projects).
2. Establishing Security Groups
Define a Security Group for your EC2 instance, enabling:
- HTTP (port 80)
- HTTPS (port 443)
- SSH (port 22)
This ensures your website is accessible, secure, and manageable remotely.
3. SSH Key Pairs
Generate a secure SSH key pair via the AWS EC2 dashboard. Download the .pem
file and store it securely (usually in ~/.ssh
).
Set file permissions with:
chmod 400 your-key.pem
4. SSH Connection into the Server
Connect via SSH using your terminal:
ssh -i your-key.pem ubuntu@your-ec2-ip-address
5. Installing Docker and Docker Compose
Install Docker:
sudo apt update
sudo apt install -y docker.io
sudo usermod -aG docker ubuntu
Install Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
6. Building and Running Your Svelte Application
Create a Dockerfile for your Svelte application and use Docker Compose to build and run your app on port 3000:
Example docker-compose.yml
:
version: "3"
services:
app:
build: .
ports:
- "3000:3000"
Deploy using:
docker-compose up -d --build
7. Establishing Caddy as a Reverse Proxy
Install Caddy web server:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/caddy-stable-archive-keyring.gpg] https://dl.cloudsmith.io/public/caddy/stable/deb/debian any-version main" | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
Configure your Caddyfile (/etc/caddy/Caddyfile
):
yourdomain.com {
reverse_proxy localhost:3000
}
Reload Caddy:
sudo systemctl reload caddy
8. Creating Route53 DNS Records
Navigate to Route53 in the AWS console:
- Create a new hosted zone for your domain registered with Name.com.
- Add an "A" record pointing to your EC2 instance's IP address.
9. Updating Name Servers
Since your domain is registered with Name.com, update the NameServers provided by Name.com to the AWS NameServers listed in your Route53 hosted zone.
10. SSL Certificate Management
Caddy automatically manages SSL certificates using Let's Encrypt, simplifying certificate management.
For AWS resources like CloudFront, you can optionally create a certificate via AWS Certificate Manager (ACM).
Going Forward: Updating the Website
Future enhancement steps could include:
- Automating deployments using AWS CodeDeploy.
- Writing scripts to update Docker images and dependencies.
- Setting up CloudWatch Logs to monitor application usage metrics.
- Adding alerts to notify about SSL certificate expiration.
By following these clear steps, your Svelte site will be smoothly deployed, secure, and easy to maintain on AWS EC2.