Khaled Ezzat

Mobile Developer

Software Engineer

Project Manager

Blog Post

Free Supabase BaaS: Self-Host with Edge Functions on VPS

Supabase has rapidly emerged as a powerful open-source alternative to Firebase, offering developers a suite of tools built on top of the rock-solid foundation of PostgreSQL. While its managed cloud platform provides an excellent and easy entry point, the true power of open-source lies in the freedom to run it yourself. This article explores the compelling proposition of self-hosting the entire Supabase stack, including the Deno-based Edge Functions, on your own Virtual Private Server (VPS). We will delve into how you can achieve this powerful setup for free, bypassing the limitations of managed free tiers and gaining complete control over your data, infrastructure, and scalability. This is your guide to building a production-ready Backend-as-a-Service without the recurring monthly bill.

Why Self-Host Supabase? The Allure of Full Control

Opting to self-host Supabase is a strategic decision that shifts the balance of power from the platform provider to you, the developer. The most immediate benefit is sovereignty over your data and infrastructure. When you run Supabase on your own VPS, your PostgreSQL database, authentication services, and storage files reside in a server environment you manage. This eliminates vendor lock-in and gives you the freedom to choose your server’s geographic region, a critical factor for data compliance regulations like GDPR. Furthermore, self-hosting allows you to completely bypass the limitations inherent in the official managed free tier. Forget about projects being paused due to inactivity, restrictive database sizes, or limited API request quotas. Your only constraints are the resources of your VPS.

Beyond breaking free from limitations, self-hosting unlocks a deeper level of customization. You gain direct, unfettered access to the underlying PostgreSQL database. This means you can install any trusted Postgres extension you need, perform fine-grained performance tuning, and implement complex backup and replication strategies that go beyond the offerings of the managed platform. While this guide focuses on leveraging free VPS tiers, it’s crucial to understand that this model is also incredibly cost-effective at scale. As your application grows, the predictable cost of a more powerful VPS will often be significantly lower than the equivalent paid tiers on a managed service.

Securing Your Free VPS: The Foundation of Your Stack

Before deploying any application, you must first build a secure foundation. The “free” in “free VPS” is meaningless if your server is vulnerable to attack. Fortunately, several cloud providers offer “Always Free” tiers that are more than capable of running a Supabase instance for development or small-to-medium production workloads. Oracle Cloud’s Free Tier is a popular choice due to its generous offerings, including Ampere A1 ARM-based instances with multiple cores and ample RAM. Alternatives include the free tiers from AWS EC2 and Google Cloud Platform, though their terms can be more restrictive.

Once you’ve provisioned your virtual machine (typically running a Linux distribution like Ubuntu), the next step is server hardening. This is not optional. Follow these essential security practices:

  • Create a Non-Root User: Immediately create a new user account with `sudo` privileges. You will perform all subsequent actions from this account, disabling the root login to reduce the attack surface.
  • Implement a Firewall: A firewall is your first line of defense. Using a simple tool like `ufw` (Uncomplicated Firewall) on Ubuntu, you can set a default policy to deny all incoming traffic and explicitly allow only the necessary ports, such as SSH (port 22), HTTP (port 80), and HTTPS (port 443).
  • Use SSH Key Authentication: Passwords can be cracked. Disable password-based authentication for SSH entirely and rely solely on SSH keys. This method is vastly more secure and ensures that only machines with a corresponding private key can access your server.

Only after these hardening steps is your server truly ready to host your Supabase stack securely.

Deploying Supabase with Docker: A Step-by-Step Overview

The officially supported and most straightforward method for self-hosting Supabase is through Docker. This containerization approach encapsulates each component of the Supabase stack—from the database to the API gateway—into isolated, manageable services. The primary prerequisite is to install Docker and Docker Compose on your hardened VPS.

The deployment process is methodical:

  1. Clone the Official Repository: Begin by cloning the Supabase Docker setup files directly from their GitHub repository. You can do this with the command: `git clone –depth 1 https://github.com/supabase/docker`.
  2. Configure Your Environment: Navigate into the new `docker/` directory. Here you will find a file named `.env.example`. Copy this to a new file named `.env`. This file is the control panel for your entire stack.
  3. Generate Secure Secrets: This is the most critical configuration step. The `.env` file contains placeholders for essential secrets like `POSTGRES_PASSWORD`, `JWT_SECRET`, and the `ANON_KEY` and `SERVICE_ROLE_KEY` for the API. Do not use the default example values. Use a strong password generator to create unique, complex strings for each of these secrets. Compromising these keys would mean compromising your entire application.
  4. Launch the Stack: Once your `.env` file is populated with secure secrets, you can bring your Supabase instance to life with a single command: `docker-compose up -d`. This command tells Docker to pull all the necessary images and start all the services (Kong, GoTrue, PostgREST, Realtime, Storage, etc.) in the background.

After a few minutes, your private, fully functional Supabase backend will be running on your VPS.

Enabling Edge Functions and Securing with a Reverse Proxy

A base Supabase installation is powerful, but modern applications demand serverless logic. The self-hosted stack includes the Deno-based Edge Functions service, allowing you to deploy custom TypeScript code that runs close to your data. This service is part of the `docker-compose.yml` configuration and runs alongside the other components. You can deploy functions to your new instance using the Supabase CLI, but you must first configure it to point to your self-hosted domain instead of the official Supabase cloud.

However, you should never expose the myriad of ports from your Docker containers directly to the internet. This is insecure and impractical. The professional solution is to set up a reverse proxy. A web server like Nginx or Caddy is installed on the host VPS and acts as a single, secure entry point for all web traffic. It then intelligently routes incoming requests to the correct internal Supabase service based on the URL path. For example, a request to `https://your-domain.com/auth/v1/` would be forwarded by the reverse proxy to the internal GoTrue authentication service running on its specific Docker port.

Finally, to make your service production-ready, you must enable SSL/TLS. No modern application should operate over unencrypted HTTP. A reverse proxy makes this simple. Using a free tool like Certbot with Nginx, you can automatically obtain and renew SSL certificates from Let’s Encrypt. This ensures all communication between your users’ clients and your Supabase backend is fully encrypted and secure, completing your professional, self-hosted setup.

In conclusion, self-hosting a Supabase and Edge Functions stack on a free VPS is not only possible but also a strategically sound decision for developers seeking ultimate control and cost-efficiency. We’ve walked through the entire process, starting with the compelling reasons to self-host, such as data sovereignty and the removal of platform limitations. We then established a secure foundation by hardening a free-tier VPS, a non-negotiable first step. From there, we detailed the Docker-based deployment, emphasizing the critical importance of securing your configuration secrets. Finally, we elevated the setup to a production-grade service by integrating a reverse proxy for secure traffic management and enabling SSL/TLS encryption. While this path requires more initial setup than a one-click managed solution, the reward is immense: a powerful, scalable, and entirely free backend infrastructure that you truly own.

Tags: