Skip to content

LafaekStreet Backend - Deployment Guide

How to deploy and update the FastAPI backend on AWS Lightsail from your Mac.


Scripts Overview

ScriptPurposeRun From
new_deploy_server.shFirst-time full deploymentYour Mac
new_update_server.shUpdate existing serverYour Mac

Both scripts are located in: lafaekstreet_backend/


Architecture

Your Mac                          AWS Lightsail (Ubuntu 22.04)
────────                          ─────────────────────────────
new_deploy_server.sh  ──SSH/SCP──>  Nginx (port 80/443)
new_update_server.sh                   │
                                  Gunicorn + Uvicorn (port 8000)

                                  FastAPI Application

                          ┌────────┬───┴───┬──────────┬────────┐
                          v        v       v          v        v
                       Aiven    Aiven    AWS S3    Bedrock   Hedera
                       Postgres  Valkey  (images)   (AI)   (blockchain)

Prerequisites

Before running any script, make sure you have:

  1. AWS CLI configured (aws configure)
  2. .env file in the lafaekstreet_backend/ directory
  3. Run scripts from the lafaekstreet_backend/ directory
bash
cd /path/to/LafaekStreet/lafaekstreet_backend

First-Time Deployment

Run the deploy script

bash
cd lafaekstreet_backend
./new_deploy_server.sh

What it does (step by step)

StepActionDetails
1SSH Key SetupCreates ~/.ssh/lafaekstreet-key.pem if not exists
2Lightsail InstanceCreates lafaekstreet-api instance (or uses existing)
3NetworkConfigures firewall: ports 22, 80, 443, 8000
4Wait for SSHWaits until instance is accessible
5Package CodeCreates tar.gz excluding .git, venv, pycache, etc.
6UploadUploads code to instance via SCP
7System DepsInstalls Python, Nginx, Certbot, Fail2Ban, etc.
8App SetupCreates venv, installs pip dependencies, log/backup dirs
9Systemd ServiceGunicorn + Uvicorn workers, auto-restart, logging
10NginxReverse proxy, rate limiting, security headers, upload timeouts
11Fail2BanSSH + Nginx brute-force protection
12Log Rotation14-day retention, daily rotation
13Start ServicesStarts app and verifies it's running
14SSL CertificateCertbot with Let's Encrypt (requires DNS configured)

Configuration (edit in script if needed)

bash
INSTANCE_NAME="lafaekstreet-api"
BUNDLE_ID="micro_3_0"           # 1 GB RAM, 2 vCPUs, 40 GB SSD
AVAILABILITY_ZONE="us-east-1a"
REGION="us-east-1"
DOMAIN="api.lafaekstreet.com"
KEY_NAME="lafaekstreet-key"

After deployment

Instance Name: lafaekstreet-api
SSH Key:       ~/.ssh/lafaekstreet-key.pem
App Directory: /home/ubuntu/lafaekstreet
Logs:          /var/log/lafaekstreet/
Backups:       /home/ubuntu/backups/

Updating the Server

Run the update script

bash
cd lafaekstreet_backend
./new_update_server.sh

Update Options

You'll be prompted to choose:

Option 1: Environment Variables Only

Updates .env file on the server and restarts the service.

  • Backs up existing .env on server (timestamped)
  • Uploads your local .env
  • Sets file permissions to 600
  • Restarts service
  • Verifies service is running

Use when: You changed API keys, database URLs, or config values.

Option 2: Full Application Update

Updates code + .env + dependencies with safety checks.

  • Creates backup on server (tar.gz)
  • Uploads all code from your Mac
  • Installs/updates pip dependencies
  • Tests application import (from app.main import app)
  • Restarts service
  • Health check (3 retries on /health)
  • Auto-rollback if anything fails
  • Reloads Nginx
  • Cleans old backups (keeps last 5)

Use when: You changed code AND environment variables.

Option 3: Code Only

Same as Option 2 but preserves the existing .env on the server.

  • Saves .env before extracting new code
  • Restores .env after extraction
  • All the same safety checks as Option 2

Use when: You only changed code, not environment variables.

Safety Features

FeatureDescription
Auto BackupBacks up current code before every update
Import TestValidates from app.main import app before restart
Health CheckHits /health endpoint with 3 retries
Auto RollbackRestores backup if import, restart, or health check fails
Backup CleanupKeeps only the last 5 backups

Useful Commands

From your Mac

bash
# SSH to server
ssh -i ~/.ssh/lafaekstreet-key.pem ubuntu@<INSTANCE_IP>

# Check service status
ssh -i ~/.ssh/lafaekstreet-key.pem ubuntu@<INSTANCE_IP> 'sudo systemctl status lafaekstreet'

# View live logs
ssh -i ~/.ssh/lafaekstreet-key.pem ubuntu@<INSTANCE_IP> 'sudo journalctl -u lafaekstreet -f'

# Restart service
ssh -i ~/.ssh/lafaekstreet-key.pem ubuntu@<INSTANCE_IP> 'sudo systemctl restart lafaekstreet'

On the server (after SSH)

bash
# Service management
sudo systemctl start lafaekstreet
sudo systemctl stop lafaekstreet
sudo systemctl restart lafaekstreet
sudo systemctl status lafaekstreet

# Logs
sudo journalctl -u lafaekstreet -f              # Live app logs
sudo tail -f /var/log/lafaekstreet/error.log     # Error log
sudo tail -f /var/log/nginx/lafaekstreet_access.log  # Nginx access

# Health check
curl http://localhost:8000/health

# System resources
free -h && df -h

Troubleshooting

Service won't start

bash
# Check logs
sudo journalctl -u lafaekstreet -n 50

# Test import manually
cd ~/lafaekstreet && source venv/bin/activate
python -c "from app.main import app"

# Check .env exists
ls -la ~/lafaekstreet/.env

502 Bad Gateway

bash
# App not running
sudo systemctl start lafaekstreet

# Check port
sudo ss -tlnp | grep 8000

Health check fails after update

The script auto-rolls back. Check what changed:

bash
# View recent logs
sudo journalctl -u lafaekstreet -n 30

# Manual rollback (if needed)
ls ~/backups/  # find latest backup
sudo tar -xzf ~/backups/backup_YYYYMMDD_HHMMSS.tar.gz -C ~/lafaekstreet
sudo systemctl restart lafaekstreet

SSL issues

bash
# Re-run certbot
sudo certbot --nginx -d api.lafaekstreet.com

# Check cert expiry
sudo certbot certificates

# Test auto-renewal
sudo certbot renew --dry-run

Server Configuration Files

FileLocation on ServerPurpose
Systemd service/etc/systemd/system/lafaekstreet.serviceProcess management
Nginx config/etc/nginx/sites-available/lafaekstreetReverse proxy
Environment~/lafaekstreet/.envApp configuration
Log rotation/etc/logrotate.d/lafaekstreetLog cleanup
Fail2Ban/etc/fail2ban/jail.localBrute-force protection
App logs/var/log/lafaekstreet/Application logs
Nginx logs/var/log/nginx/lafaekstreet_*.logWeb server logs
Backups~/backups/Pre-update backups

Built for Timor-Leste