Skip to content

LafaekStreet Backend - Deployment

Quick Start

Deploy and update the backend from your Mac using these two scripts:

bash
cd lafaekstreet_backend

# First-time deploy to AWS Lightsail
./new_deploy_server.sh

# Update existing server
./new_update_server.sh

See DEPLOY_GUIDE.md for full documentation.


Architecture

Mobile App (Flutter)
      |
      | HTTPS (port 443)
      v
+---------------------+
|   Amazon Lightsail   |
|   Ubuntu 22.04 LTS   |
|                       |
|  Nginx (reverse proxy)|  <-- SSL termination, rate limiting, security headers
|       |               |
|  Gunicorn + Uvicorn   |  <-- 2 async workers, port 8000
|       |               |
|  FastAPI Application  |  <-- API endpoints, background tasks
+---------------------+
      |         |        |         |          |
      v         v        v         v          v
  Aiven     Aiven     AWS S3   AWS Bedrock  Hedera
  PostgreSQL Valkey   (images)  (AI/Nova)   (blockchain)

What Runs Where

ComponentLocationPurpose
FastAPI + GunicornLightsailAPI server (2 async workers)
NginxLightsailReverse proxy, SSL, rate limiting
Fail2BanLightsailBrute-force protection
PostgreSQLAiven (managed)Database
Valkey/RedisAiven (managed)Cache + rate limiting
S3AWS (managed)Image storage
BedrockAWS (managed)AI damage detection + embeddings
KMSAWS (managed)Encryption keys
HederaHedera TestnetBlockchain verification

Deployment Scripts

new_deploy_server.sh - First-Time Deploy

Run from your Mac. Creates Lightsail instance, uploads code, configures everything:

  1. Creates SSH key + Lightsail instance
  2. Uploads code via SCP
  3. Installs system deps (Python, Nginx, Certbot, Fail2Ban)
  4. Sets up Python venv + pip dependencies
  5. Configures systemd service (Gunicorn + Uvicorn)
  6. Configures Nginx (reverse proxy, rate limiting, security headers)
  7. Configures Fail2Ban + log rotation
  8. SSL certificate via Let's Encrypt

new_update_server.sh - Update Server

Run from your Mac. Three update options:

OptionWhat it does
1 - Env onlyUpdates .env file, restarts service
2 - Full updateCode + .env + deps, with backup/rollback/health check
3 - Code onlyUpdates code + deps, keeps existing .env on server

Safety features: auto backup, import validation, health check, auto-rollback on failure.


Prerequisites

  1. AWS CLI configured: aws configure
  2. .env file in lafaekstreet_backend/ directory
  3. Run scripts from lafaekstreet_backend/ directory

Configuration

Default values in the scripts (edit if needed):

Instance:  lafaekstreet-api
Region:    us-east-1
Bundle:    micro_3_0 (1 GB RAM, 2 vCPUs, 40 GB SSD)
Domain:    api.lafaekstreet.com
SSH Key:   ~/.ssh/lafaekstreet-key.pem
App Dir:   /home/ubuntu/lafaekstreet (on server)

Quick Reference Commands

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

# Service management (on server)
sudo systemctl status lafaekstreet
sudo systemctl restart lafaekstreet

# Logs (on server)
sudo journalctl -u lafaekstreet -f
sudo tail -f /var/log/lafaekstreet/error.log

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

# Security check (on server)
bash ~/lafaekstreet/deployment/verify_security.sh

# System resources
free -h && df -h

Server Files

FileLocationPurpose
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
Backups~/backups/Pre-update backups

Nginx Details

Optimized for Timor-Leste's slow connections:

SettingValueReason
client_max_body_size12M10MB image limit + overhead
client_body_timeout120sSlow upload connections
proxy_send_timeout (uploads)300s5 min for image pre-upload
rate_limit10 req/s per IPDDoS protection
server_tokensoffHide Nginx version
keepalive32 connectionsConnection reuse

Troubleshooting

See DEPLOY_GUIDE.md for detailed troubleshooting.

Quick fixes:

bash
# Service won't start
sudo journalctl -u lafaekstreet -n 50

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

# 502 Bad Gateway
sudo systemctl start lafaekstreet
sudo ss -tlnp | grep 8000

# SSL
sudo certbot --nginx -d api.lafaekstreet.com

Cost

ServiceCostNotes
Lightsail Instance$5/month512MB RAM, 1 vCPU, 20GB SSD
Aiven PostgreSQL$0Free tier
Aiven Valkey$0Free tier
AWS S3~$0.50Pay-per-use
AWS Bedrock~$2Pay-per-use
AWS KMS~$1Key storage
Total~$8.50/month

Other Docs

Built for Timor-Leste