I Ran Out of Vercel's Free Tier, So I Moved My FastAPI Backends to a Free Oracle Cloud VM
Why I Did This
I hit the limits of Vercel's free tier, so I moved my backends off it and kept only the frontends there.
Vercel is fantastic for Next.js and static frontends. But I was also running FastAPI backends there as serverless functions, and once I had a few projects, the free limits started to pinch. Cold starts, execution limits and usage caps add up fast when every side project has its own API.
So I split things up. The frontends stay on Vercel, where they shine. The backends now live on an Oracle Cloud free-tier VM that I control completely. It took me one afternoon, a few confusing errors, and a lot of reading logs. This is the guide I wish I had before starting.
By the end, every backend gets its own clean HTTPS address like api.journal.abhishekg.com.np, starts automatically on boot, restarts itself if it crashes, and costs me nothing.
The Setup at a Glance
The root domain points to Vercel, and each backend gets its own api.<service> subdomain pointing to the VM.
abhishekg.com.np -> Vercel (frontend, SSL handled by Vercel)
api.journal.abhishekg.com.np -> Oracle VM -> nginx (HTTPS) -> uvicorn on 127.0.0.1:8001
api.inside.abhishekg.com.np -> Oracle VM -> nginx (HTTPS) -> uvicorn on 127.0.0.1:8002On the VM, three tools do all the work:
nginx receives every request, handles HTTPS, and forwards it to the right app.
systemd runs each FastAPI app, starts it on boot, and restarts it if it crashes.
certbot gets a free Let's Encrypt certificate for each subdomain and renews it automatically.
The apps themselves only listen on 127.0.0.1, so nothing reaches them except through nginx.
Problem 1: nginx Worked, but Only from Inside the VM
Oracle's Ubuntu images ship with their own iptables rule that rejects everything except SSH, and it sits above ufw.
I installed nginx and allowed it in ufw. curl localhost showed the "Welcome to nginx!" page, but the browser and curl <public-ip> both failed. ufw looked perfect:
Nginx Full ALLOW AnywhereThe real answer showed up when I listed the actual iptables rules:
sudo iptables -L INPUT -n --line-numbers4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
6 ufw-before-logging-input ...Rule 5 rejects everything before the ufw chains (6 onwards) are even checked. iptables stops at the first match, so my ufw rules never ran.
The Fix
I disabled ufw (Oracle's rules plus the cloud firewall are enough) and inserted HTTP and HTTPS rules above the REJECT:
sudo ufw disable
sudo iptables -I INPUT 5 -p tcp -m state --state NEW --dport 80 -j ACCEPT
sudo iptables -I INPUT 6 -p tcp -m state --state NEW --dport 443 -j ACCEPTThen I made them survive reboots. netfilter-persistent was missing on my VM (installing ufw had removed it), so:
sudo apt install iptables-persistent # answer Yes to both save promptsDon't forget the cloud side too. In the Oracle console, open your VCN, go to the subnet's Security List (and any Network Security Group on the VNIC), and add ingress rules for TCP 80 and 443 from 0.0.0.0/0.
One more gotcha: testing your own public IP from inside the VM isn't reliable on Oracle. Always test from your laptop with:
curl -v http://<public-ip>In the browser, type http:// explicitly, because browsers often try HTTPS first.
Problem 2: certbot Failed with a 404
My first certificate request failed because the domain was still pointing at my old host, not the VM.
Domain: abhishekg.com.np
Type: unauthorized
Detail: 64.29.17.65: Invalid response from http://abhishekg.com.np/.well-known/acme-challenge/...: 404The clue was the IP in the error. Let's Encrypt connected to 64.29.17.65, which wasn't my VM at all. The A record for the root domain still pointed to Vercel, so Vercel answered with a 404.
Always check what the world sees before asking for a certificate:
dig +short abhishekg.com.np @1.1.1.1The Real Decision: Vercel Root, VM Subdomains
This made me think about what I actually wanted. My frontend should stay on Vercel, so the root domain must keep pointing there. The backends only need their own subdomains.
TypeNamePoints ToA@Vercel's IP (from Vercel → Domains)CNAMEwwwcname.vercel-dns.comAapi.journalVM's public IPAapi.insideVM's public IP
Vercel issues the root domain's certificate itself. The VM gets one certificate per subdomain, and Let's Encrypt only checks that subdomain's own record, so the two never interfere.
I deleted the root certificate I had just issued on the VM, since renewals would fail once the root pointed back to Vercel:
sudo certbot delete --cert-name abhishekg.com.npWhy not a wildcard certificate? *.abhishekg.com.np only covers one level, so it wouldn't cover api.journal.abhishekg.com.np anyway. One small certificate per service is simpler and renews automatically.
If you use Cloudflare for DNS, keep these VM records on DNS only (grey cloud), because Cloudflare's free SSL doesn't cover two-level subdomains either.
Organizing nginx So It Stays Clean
I wiped the default config and split everything into shared snippets plus one small file per site.
/etc/nginx/
├── snippets/
│ ├── acme-challenge.conf # Let's Encrypt verification
│ ├── ssl-params.conf # shared TLS settings
│ ├── security-headers.conf # shared headers
│ └── proxy-fastapi.conf # shared proxy settings
└── sites-available/ # symlinked into sites-enabled/
├── 00-default.conf # drops unknown hosts and the raw IP
├── 10-http-redirect.conf # all HTTP -> HTTPS, serves ACME
├── api.journal.abhishekg.com.np.conf
└── api.inside.abhishekg.com.np.confFirst, the cleanup:
sudo rm -f /etc/nginx/sites-enabled/*
sudo rm -f /etc/nginx/sites-available/default
sudo mkdir -p /var/www/certbot/.well-known/acme-challengeIn /etc/nginx/nginx.conf, inside http { }, I enabled server_tokens off; (hides the nginx version) and server_names_hash_bucket_size 64; (needed for long domain names).
The Shared Snippets
The Let's Encrypt challenge snippet lets certbot verify any subdomain through one folder:
# /etc/nginx/snippets/acme-challenge.conf
location ^~ /.well-known/acme-challenge/ {
root /var/www/certbot;
default_type text/plain;
}# /etc/nginx/snippets/ssl-params.conf
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;# /etc/nginx/snippets/security-headers.conf
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Strict-Transport-Security "max-age=31536000" always;# /etc/nginx/snippets/proxy-fastapi.conf
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "";
proxy_connect_timeout 5s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;The Two Base Sites
The catch-all drops anything that isn't one of my domains, like bots scanning the raw IP:
# /etc/nginx/sites-available/00-default.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
server_name _;
ssl_reject_handshake on;
return 444;
}The redirect site covers every subdomain at any depth (that's what the leading dot in .abhishekg.com.np does). It answers certbot's challenges and sends everything else to HTTPS:
# /etc/nginx/sites-available/10-http-redirect.conf
server {
listen 80;
listen [::]:80;
server_name .abhishekg.com.np;
include snippets/acme-challenge.conf;
location / {
return 301 https://$host$request_uri;
}
}Enable both sites and reload nginx:
sudo ln -s /etc/nginx/sites-available/00-default.conf /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/10-http-redirect.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginxBecause certbot uses this webroot folder, it never touches my nginx files. No messy "managed by Certbot" lines anywhere.
One systemd Template for Every FastAPI App
Instead of writing a service file per app, I wrote one template, fastapi@.service, and each app only needs a small env file.
The %i in the template is replaced by whatever comes after @. So fastapi@journal runs the code in /srv/journal with settings from /etc/fastapi/journal.env.
sudo useradd --system --no-create-home --shell /usr/sbin/nologin fastapi
sudo mkdir -p /etc/fastapi# /etc/systemd/system/fastapi@.service
[Unit]
Description=FastAPI service: %i
After=network.target
[Service]
Type=simple
User=fastapi
Group=fastapi
WorkingDirectory=/srv/%i
EnvironmentFile=/etc/fastapi/%i.env
ExecStart=/srv/%i/.venv/bin/uvicorn ${APP_MODULE} \
--host 127.0.0.1 --port ${PORT} --workers 1 \
--proxy-headers --forwarded-allow-ips=127.0.0.1 \
--no-access-log
Restart=on-failure
RestartSec=3
# Keep resource use low
MemoryMax=256M
CPUQuota=50%
# Hardening
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=tmpfs
ReadWritePaths=/srv/%i
[Install]
WantedBy=multi-user.targetReload systemd:
sudo systemctl daemon-reloadA few choices worth explaining:
One worker and a 256 MB cap keep each app small, so several fit on a free-tier VM.
--no-access-logsaves resources, since nginx already logs every request.A dedicated
fastapiuser with a read-only system means a bug in one app can't damage the server.ProtectHome=tmpfsinstead oftrue. I learned this one the hard way (see Problem 4).
Problems 3 and 4: The App Wouldn't Start
Both bugs were small configuration mistakes, and the logs pointed straight at them once I knew where to look.
Problem 3: "'' is Not a Valid Integer"
After starting the service, nothing was listening on port 8001. The logs said:
Referenced but unset environment variable evaluates to an empty string: APP_MODULE, PORT
Error: Invalid value for '--port': '' is not a valid integer.When I pasted my app's own variables into /etc/fastapi/journal.env, I accidentally replaced the two lines the template depends on. The env file must always start with them:
APP_MODULE=app.main:app
PORT=8001
# the app's own variables below
DATABASE_URL=...APP_MODULE depends on where main.py lives. In app/main.py it's app.main:app. In the repo root it's just main:app.
Problem 4: PermissionError on a Postgres Key I Never Had
The app started, but the first database request crashed:
PermissionError: [Errno 13] Permission denied: '/home/fastapi/.postgresql/postgresql.key'I don't use client certificates at all, so this was confusing. It turns out asyncpg checks for an optional key at ~/.postgresql/postgresql.key when connecting over SSL. Normally the file is missing, and asyncpg quietly skips it.
But ProtectHome=true made /home completely inaccessible. So instead of "file not found", asyncpg got "permission denied", and crashed.
Switching to ProtectHome=tmpfs still hides every real home folder, but shows an empty /home instead of a locked one:
sudo sed -i 's/^ProtectHome=true/ProtectHome=tmpfs/' /etc/systemd/system/fastapi@.service
sudo systemctl daemon-reload
sudo systemctl restart fastapi@journalSince it's in the template, every future service gets the fix for free.
A Side Note on uv
The VM didn't have python3-venv, so I installed uv and ran uv sync.
One thing to watch: if uv downloads its own Python, it lands in your home folder, which the service can't reach. Pointing it at the system Python avoids that:
uv sync --python /usr/bin/python3
readlink -f .venv/bin/python # should print a path under /usr/binHow I Add a New Service Now
Adding a backend is now eight short steps, and here's the exact run for api.inside.abhishekg.com.np, whose main.py sits in the repo root.
Each service gets the next free port. To see which ones are taken:
grep -h ^PORT /etc/fastapi/*.env1. DNS
Add an A record api.inside pointing to the VM's IP, then wait until it resolves:
dig +short api.inside.abhishekg.com.np @1.1.1.12. Code
sudo mkdir -p /srv/inside
sudo chown ubuntu:fastapi /srv/inside
git clone git@github.com:<you>/<repo>.git /srv/inside3. Dependencies
With a pyproject.toml, use uv sync. With a requirements.txt, create the venv and install from it:
cd /srv/inside
uv sync --python /usr/bin/python3
# or: uv venv --python /usr/bin/python3 && uv pip install -r requirements.txt
sudo chmod -R g+rX /srv/inside4. Env File
The two required lines first, then the app's own variables:
sudo nano /etc/fastapi/inside.envAPP_MODULE=main:app
PORT=8002Set the permissions:
sudo chmod 600 /etc/fastapi/inside.env5. Start It
sudo systemctl enable --now fastapi@inside
sleep 10
curl -I http://127.0.0.1:8002/docs # expect 200The sleep matters. My first curl failed only because the app was still starting, which took about 8 seconds on this VM.
6. Certificate
sudo certbot certonly --webroot -w /var/www/certbot \
-d api.inside.abhishekg.com.np \
--deploy-hook "systemctl reload nginx"7. nginx Site
# /etc/nginx/sites-available/api.inside.abhishekg.com.np.conf
upstream inside_backend {
server 127.0.0.1:8002;
keepalive 4;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name api.inside.abhishekg.com.np;
ssl_certificate /etc/letsencrypt/live/api.inside.abhishekg.com.np/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.inside.abhishekg.com.np/privkey.pem;
include snippets/ssl-params.conf;
include snippets/security-headers.conf;
access_log /var/log/nginx/inside.access.log;
error_log /var/log/nginx/inside.error.log;
client_max_body_size 10m;
location / {
proxy_pass http://inside_backend;
include snippets/proxy-fastapi.conf;
}
}Enable the site and reload nginx:
sudo ln -s /etc/nginx/sites-available/api.inside.abhishekg.com.np.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx8. Test from Your Laptop
curl -I https://api.inside.abhishekg.com.np/docsIf the frontend on Vercel calls this API, remember to allow its origin in FastAPI's CORSMiddleware. Otherwise the browser blocks requests even though curl works fine.
Deploying an update later is one line:
cd /srv/inside && git pull && uv sync && sudo systemctl restart fastapi@insideLogs, Resources and Daily Commands
App errors go to journald, and every HTTP request is logged by nginx.
# App logs: tracebacks, startup, prints
sudo journalctl -u fastapi@journal -f
sudo journalctl -u fastapi@journal --since today -p err
# Request logs: who called what, status codes
sudo tail -f /var/log/nginx/journal.access.log
sudo tail -f /var/log/nginx/journal.error.logA quick rule for reading them:
502 in nginx means the app is down.
500 means the app is running but threw an error.
Either way, the traceback is in journalctl.
To watch resource use per service, systemd-cgtop works like top but groups everything by service (press m to sort by memory):
systemctl status fastapi@journal --no-pager | grep -E 'Memory|CPU|Tasks'
sudo systemd-cgtop
free -hA FastAPI app with SQLAlchemy and asyncpg usually sits around 80 to 150 MB, and nginx uses only a few MB. With each app capped at 256 MB, a 1 GB free-tier VM comfortably fits four or five services.
Everyday Commands I Keep Handy
sudo systemctl restart fastapi@journal # after deploying new code
systemctl list-units 'fastapi@*' # all services at a glance
sudo certbot renew --dry-run # check that renewals workWhat I Learned
The frontend on Vercel and backend on my own VM split now costs me nothing, and adding a new API takes about ten minutes.
If I had to sum up the lessons:
ufw isn't the whole firewall on Oracle Cloud. Check the real iptables rules and the cloud Security List.
Read the IP in error messages. My certbot failure was a DNS problem, not an nginx problem.
Logs answer almost everything. Every bug here was explained in one line of
journalctl.Templates beat copy-paste. One systemd template and a few nginx snippets keep every new service consistent.
Hardening has side effects.
ProtectHome=trueis good security, until a library quietly looks inside a home folder.
If you're hitting free-tier limits too, I hope this saves you an afternoon of debugging.
Tagged
Written by
Abhishek Ghimire
Writes here about engineering, technology, and the things worth building.