
Ollama is easy to install, and that convenience can bite you. If you bind it to 0.0.0.0 and open the port, you’ve published an unauthenticated AI endpoint to the internet.
This walkthrough shows how to run Ollama behind Caddy so the model API stays private. You’ll terminate HTTPS at the edge, require basic auth, and add firewall rules that don’t invite trouble.
When you’re done, you’ll have a clean URL like https://ollama.example.com proxying back to 127.0.0.1:11434.
Caddy terminates TLS and handles authentication. Ollama never listens publicly.
What you’ll build (and what you won’t)
- Build: A private Ollama endpoint on a VPS, reachable only via Caddy with HTTPS + basic auth.
- Build: A firewall policy that only exposes ports 22/80/443 to the world.
- Won’t build: A public “free for all” API, or a full multi-tenant LLM platform.
If you want a browser UI later, add Open WebUI.
HostMyCode has a practical guide: Run Open WebUI On a VPS With Caddy and HTTPS.
Prerequisites and sizing (CPU, RAM, disk)
This setup runs fine on a CPU-only VPS. A GPU helps, but you don’t need one for a small private endpoint.
- OS: Ubuntu 24.04 LTS or Debian 12 (commands below assume Ubuntu).
- CPU: 4 vCPU minimum for acceptable latency with small/medium quantized models.
- RAM: 8–16 GB recommended for a typical 7B–8B class model in quantized form.
- Disk: 50 GB+ SSD/NVMe if you plan to keep multiple models. Model files add up quickly.
- Network: A stable public IP and a DNS record you control.
If you’re picking a box specifically for a private LLM endpoint, start with a HostMyCode VPS.
If you’d rather offload OS updates and incident response, a managed VPS hosting plan is the better match.
Step 1: Point DNS to your VPS
Create a DNS record for the endpoint. Example:
- Type: A
- Name:
ollama - Value:
<your_vps_ip>
Use a domain you already control, or register one via HostMyCode domains.
Then wait for DNS to propagate. This is often minutes, but it can take longer depending on TTL.
Quick check from your laptop:
dig +short ollama.example.com
Step 2: Secure SSH before you expose anything else
Lock down SSH before you bring up web-facing services. Use key-based login and disable password auth.
If you haven’t done that yet, follow Set Up SSH Keys And Disable Password Login On VPS.
One rule: open a second terminal and confirm key-based login works before changing SSH settings.
Step 3: Install Ollama (local-only binding)
SSH into the server:
ssh ubuntu@<your_vps_ip>
Install Ollama using the official installer method:
sudo apt-get update
sudo apt-get install -y curl ca-certificates
curl -fsSL https://ollama.com/install.sh | sh
Confirm the service is present:
systemctl status ollama --no-pager
Critical: Ollama must listen on localhost only. It should be unreachable from the internet.
Most installs bind to 127.0.0.1:11434 by default. Verify it:
sudo ss -lntp | grep 11434 || true
You should see 127.0.0.1:11434 (or localhost:11434), not 0.0.0.0:11434.
If it’s listening on all interfaces, force an explicit bind address with a systemd override.
sudo systemctl edit ollama
Add:
[Service]
Environment="OLLAMA_HOST=127.0.0.1:11434"
Reload and restart:
sudo systemctl daemon-reload
sudo systemctl restart ollama
Test locally on the VPS:
curl -s http://127.0.0.1:11434/api/tags
Step 4: Pull a model (pick something realistic)
Start small. On a typical VPS, a lighter quantized model behaves better than something huge that times out under load.
Example:
ollama pull llama3.1:8b
If disk space matters, keep your model list short. To see what’s installed:
ollama list
Quick interactive test on the server:
ollama run llama3.1:8b "Write a short Nginx health check explanation."
Honest limitation: on CPU-only VPS hardware, long contexts and multiple concurrent users will feel slow.
The goal here is a controlled private endpoint, not a public API service.
Step 5: Install Caddy
Caddy will sit in front of Ollama. It will handle TLS and keep the configuration manageable.
On Ubuntu, install it from the recommended repo packages.
sudo apt-get 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
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt-get update
sudo apt-get install -y caddy
Confirm it runs:
systemctl status caddy --no-pager
Step 6: Create a Basic Auth password hash for Caddy
Caddy’s basic auth uses a hashed password. Generate one like this:
caddy hash-password --plaintext 'use-a-long-random-password-here'
Copy the hash (it starts with $2a$ or similar). You’ll paste it into the Caddyfile next.
Step 7: Configure Caddy as an HTTPS reverse proxy to Ollama
Edit the Caddyfile:
sudo nano /etc/caddy/Caddyfile
Replace its contents with this (adjust domain, user, and hash):
ollama.example.com {
encode zstd gzip
# Basic auth keeps casual scanning out.
# Use a long random password and rotate it periodically.
basicauth {
ollama_user $2a$12$REPLACE_WITH_YOUR_HASH
}
# Keep request sizes sane.
request_body {
max_size 25MB
}
# Proxy to local-only Ollama
reverse_proxy 127.0.0.1:11434 {
header_up Host {host}
header_up X-Forwarded-Proto {scheme}
}
}
Validate configuration:
sudo caddy validate --config /etc/caddy/Caddyfile
Reload Caddy:
sudo systemctl reload caddy
If DNS is correct and ports 80/443 are reachable, Caddy will automatically obtain and renew TLS certificates.
Step 8: Open the right firewall ports (and only those)
Don’t default to “open everything.” You only need SSH plus HTTP/HTTPS for Caddy.
If you use UFW:
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose
Do not open port 11434. Ollama should stay on localhost only.
If you want to reduce noisy brute-force traffic after the basics are working, add CrowdSec.
HostMyCode has a step-by-step: Install CrowdSec on Ubuntu VPS to Block Bruteforce.
Step 9: Test the endpoint over HTTPS
From your local machine, confirm auth works and the endpoint responds:
curl -u ollama_user:'your-password' https://ollama.example.com/api/tags
You should get JSON listing available models.
Then try a small generation request (short prompt, short output):
curl -u ollama_user:'your-password' https://ollama.example.com/api/generate \
-H 'Content-Type: application/json' \
-d '{"model":"llama3.1:8b","prompt":"Give me a one-paragraph server uptime status update template.","stream":false}'
If it stalls, the model may be too heavy for your CPU/RAM. The box might also be swapping.
Check the basics:
free -h
uptime
sudo journalctl -u ollama -n 50 --no-pager
Step 10: Add practical safeguards (timeouts, logs, and access control)
Basic auth helps, but you still need to think like an operator.
Decide who can reach this endpoint and what should happen when requests spike.
Option A: Restrict by IP (good for admin-only use)
If you only access the endpoint from a fixed office IP or a VPN exit node, add an allowlist in your Caddyfile:
ollama.example.com {
@allowed remote_ip 203.0.113.10 198.51.100.25
handle @allowed {
basicauth {
ollama_user $2a$12$REPLACE_WITH_YOUR_HASH
}
reverse_proxy 127.0.0.1:11434
}
handle {
respond "Forbidden" 403
}
}
This rejects unknown IPs before the basic auth challenge ever appears.
Option B: Put the endpoint on a private network
If you don’t need public access, don’t provide it.
Bind Caddy to a private interface, or make it reachable only over a VPN.
For Open WebUI, HostMyCode covers the Tailscale approach here: Open WebUI With Tailscale on a VPS. The same pattern works for an Ollama endpoint.
Basic operational logging
Caddy logs are usually enough to spot request volume and auth failures:
sudo journalctl -u caddy -n 100 --no-pager
For Ollama behavior (model loads, errors):
sudo journalctl -u ollama -n 100 --no-pager
Common problems and fast fixes
1) Caddy can’t get certificates
- Make sure
ollama.example.comresolves to your VPS IP. - Make sure ports 80 and 443 are open at the VPS firewall and any upstream firewall.
- Check logs:
sudo journalctl -u caddy -n 200 --no-pager
2) Ollama is reachable from the internet (bad)
This means you bound Ollama to 0.0.0.0 and/or opened port 11434.
- Force localhost bind using the systemd override shown earlier (
OLLAMA_HOST=127.0.0.1:11434). - Remove firewall rules that expose 11434.
- Verify again:
ss -lntp | grep 11434
3) Requests fail with 413 (payload too large)
If your client sends large prompts, raise Caddy’s request body limit. In the Caddyfile:
request_body {
max_size 50MB
}
Reload Caddy after changes:
sudo systemctl reload caddy
4) Slow responses or timeouts
- Use a smaller model and keep prompts shorter.
- Make sure the server isn’t swapping:
free -h. - Consider upgrading CPU/RAM, or moving to a GPU machine for heavier usage.
If you’re regularly hitting limits, hardware becomes the difference between “it responds” and “it’s usable.”
For sustained workloads, look at HostMyCode dedicated servers.
How to use the endpoint from apps and tools
Any client that supports Ollama can usually point at your reverse proxy URL. You only need:
- Base URL:
https://ollama.example.com - Auth: HTTP basic auth credentials (store them as secrets, not in code)
If you add a UI later, Open WebUI is a common choice. Install it, then point it at this secured endpoint.
For persistence and storage layout, see Run Open WebUI With Persistent Storage on a VPS.
Security checklist (quick operator pass)
- Ollama listens on
127.0.0.1:11434, not on public interfaces. - Firewall allows inbound
80/tcpand443/tcp, and blocks11434/tcp. - Caddy has basic auth enabled (and optionally IP allowlisting).
- SSH uses keys, password login disabled.
- System updates applied regularly.
Summary: a private Ollama endpoint you can actually operate
You’ve kept Ollama bound to localhost and put Caddy at the edge for TLS and basic auth.
If you want the simplest safe way to run Ollama behind Caddy, this is it.
You expose fewer surfaces, avoid surprises, and keep a configuration you can reason about later.
As usage grows, stability comes down to sizing. A right-sized HostMyCode VPS works well for internal tools, while managed VPS hosting makes sense if you don’t want to own every patch cycle and alert.
If you’re planning to run private AI services on real infrastructure, start with a VPS that gives you predictable CPU, RAM, and disk performance. HostMyCode VPS is a solid base for Ollama, reverse proxies, and companion services like Open WebUI.
If you want help hardening the OS, tightening firewall rules, and keeping the endpoint maintained, use managed VPS hosting and keep the AI stack private by default.
FAQ
Should I expose Ollama directly on port 11434?
No. Bind Ollama to 127.0.0.1 and put a reverse proxy in front. You get TLS, authentication, and better control over access.
Is basic auth “enough” for a private LLM endpoint?
For a small internal endpoint, basic auth plus HTTPS is a reasonable baseline. For stronger control, add IP allowlisting or require VPN access.
Can I put this behind Nginx instead of Caddy?
Yes, but Caddy’s HTTPS automation and clean config reduce footguns for operators. If you already run Nginx everywhere, keep it consistent—just don’t skip authentication and firewall rules.
What’s the simplest way to monitor uptime?
Monitor the HTTPS endpoint with a health check. If you want a self-hosted monitoring UI, see Run Uptime Kuma With Docker Compose on a VPS and point it at https://ollama.example.com/api/tags using basic auth.