How to Deploy Grafana Loki on a VPS
Updated Nov 2026
verified on Ubuntu 26.04 · Nov 2026Self-host Grafana Loki on a VPS with Docker Compose, retention, and Caddy HTTPS with a password, then back up the log volume and restore it.
- A VPS (see Server sizing below)
- A fresh Ubuntu 26.04 server with root/sudo SSH access
- A domain or subdomain you can point at the server
What you are building
Grafana Loki is an AGPL-3.0 log store that indexes labels rather than the full text of each line. It is an HTTP API, not a website: you send logs to it with a collector such as Grafana Alloy and read them in Grafana. This guide runs a single Loki process with filesystem storage.
Loki has no authentication of its own (auth_enabled controls
multi-tenancy, not login). Anyone who can reach port 3100 can read and write
your logs, so here it listens on loopback only and Caddy adds HTTPS and a
password.
Server sizing
On the catalog's test box Loki measured about 47 MB of memory idle and about 200 MB of disk. That says little about your server under load: memory and disk follow how many log lines and label combinations you send.
Prepare the server
Update the system and install the tools this guide uses:
sudo apt-get update && sudo apt-get -y upgrade
sudo apt-get install -y curl openssl ufw dnsutils
If you're logged in as root, create a non-root sudo user and continue as that user:
adduser deploy
usermod -aG sudo deploy
Open SSH and the two ports the reverse proxy needs:
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw --force enable
A published Docker port goes around ufw, so the Compose file below decides
what is reachable: Loki gets no public port of its own.
Paid link — we earn a commission if you shop through it.
Install Docker
If Docker isn't on the server yet, the convenience script installs Docker Engine and the Compose plugin (the Docker and Compose on Ubuntu guide covers the details):
command -v docker >/dev/null || curl -fsSL https://get.docker.com | sudo sh
sudo docker compose version
Configure Loki
Create the project. Replace logs.example.com with your hostname:
mkdir -p ~/loki && cd ~/loki
DOMAIN=logs.example.com
[ -f .env ] || ( umask 077
{ echo "DOMAIN=$DOMAIN"
echo "LOKI_USER=loki"
echo "LOKI_PASSWORD=$(openssl rand -hex 24)"
} > .env )
The image ships a working single-process configuration. The file below is that
configuration without its unused ruler block, plus two additions: retention (without it Loki keeps logs forever,
until the disk fills) and usage reporting turned off. 744h is 31 days; change
it to what you need.
cd ~/loki
cat > loki-config.yaml <<'EOF'
auth_enabled: false
server:
http_listen_port: 3100
common:
instance_addr: 127.0.0.1
path_prefix: /loki
storage:
filesystem:
chunks_directory: /loki/chunks
rules_directory: /loki/rules
replication_factor: 1
ring:
kvstore:
store: inmemory
schema_config:
configs:
- from: 2020-10-24
store: tsdb
object_store: filesystem
schema: v13
index:
prefix: index_
period: 24h
compactor:
working_directory: /loki/compactor
retention_enabled: true
delete_request_store: filesystem
limits_config:
retention_period: 744h
analytics:
reporting_enabled: false
EOF
cat > docker-compose.yml <<'EOF'
services:
loki:
image: grafana/loki:3.7.9
restart: unless-stopped
command: -config.file=/etc/loki/config.yaml
volumes:
- ./loki-config.yaml:/etc/loki/config.yaml:ro
- loki_data:/loki
ports:
- 127.0.0.1:3100:3100
caddy:
image: caddy:2
restart: unless-stopped
ports: ["80:80", "443:443"]
volumes: ["./Caddyfile:/etc/caddy/Caddyfile:ro", "caddy_data:/data"]
depends_on: [loki]
volumes:
loki_data:
caddy_data:
EOF
Caddy stores a hash of the password, not the password itself:
cd ~/loki
. ./.env
HASH=$(printf '%s\n' "$LOKI_PASSWORD" | sudo docker run --rm -i caddy:2 caddy hash-password)
cat > Caddyfile <<EOF
$DOMAIN {
basic_auth {
$LOKI_USER $HASH
}
reverse_proxy loki:3100
}
EOF
( umask 077; echo "user = \"$LOKI_USER:$LOKI_PASSWORD\"" > curlrc )
Start it. Loki reports ready only after a short warm-up:
cd ~/loki
sudo docker compose up -d
code=000
for i in $(seq 1 40); do
code=$(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:3100/ready)
[ "$code" = 200 ] && break
sleep 3
done
echo "Loki /ready answered $code"; [ "$code" = 200 ]
HTTPS and the domain
Create an A record for your hostname pointing at the server's public IP and confirm it resolves before Caddy asks for a certificate:
dig +short logs.example.com
Caddy requests the certificate once the name resolves and renews it on its own
(see Automatic HTTPS with Caddy). The check expects 200 with the password and 401 without it:
. ~/loki/.env
code=000
for i in $(seq 1 30); do
code=$(curl -s -K ~/loki/curlrc -o /dev/null -w '%{http_code}' "https://$DOMAIN/ready")
[ "$code" = 200 ] && break
sleep 5
done
anon=$(curl -s -o /dev/null -w '%{http_code}' "https://$DOMAIN/loki/api/v1/labels")
echo "with password: $code, without: $anon"; [ "$code" = 200 ] && [ "$anon" = 401 ]
Send and read a log line
Push one line through the public address and query it back:
cd ~/loki
. ./.env
curl -sf -K curlrc -X POST "https://$DOMAIN/loki/api/v1/push" -H 'Content-Type: application/json' \
-d "{\"streams\":[{\"stream\":{\"job\":\"guide\"},\"values\":[[\"$(date +%s%N)\",\"restore me\"]]}]}"
sleep 5
curl -sf -K curlrc -G "https://$DOMAIN/loki/api/v1/query_range" --data-urlencode 'query={job="guide"}' | tee /tmp/q.json; echo
grep -q 'restore me' /tmp/q.json
In Grafana, add a Loki data source with URL https://logs.example.com,
enable Basic authentication and enter the user and password from
~/loki/.env. Point your collector's push URL at
https://logs.example.com/loki/api/v1/push with the same credentials.
Backups
Chunks, index and the write-ahead log all live in the loki_data volume. Stop
Loki so the files are consistent (collectors buffer and retry for a short
outage, but check yours), then archive the volume and the configuration:
cd ~/loki
sudo docker compose stop loki
( umask 077
sudo docker run --rm -v loki_loki_data:/loki:ro caddy:2 tar czf - -C /loki . > ~/loki-data.tar.gz
tar czf ~/loki-config.tar.gz .env curlrc loki-config.yaml docker-compose.yml Caddyfile )
sudo docker compose start loki
ls -lh ~/loki-data.tar.gz ~/loki-config.tar.gz
Copy both files off the server.
Restore
This deletes the current log volume. On a new server, unpack
loki-config.tar.gz into ~/loki first.
The block checks the archive first and stops at the first error.
(
set -e
cd ~/loki
tar tzf ~/loki-data.tar.gz >/dev/null
sudo docker compose rm -sf loki
sudo docker volume rm -f loki_loki_data
sudo docker compose create loki
sudo docker run --rm -i -v loki_loki_data:/loki caddy:2 tar xzf - -C /loki < ~/loki-data.tar.gz
sudo docker compose up -d
for i in $(seq 1 40); do curl -sf -o /dev/null http://127.0.0.1:3100/ready && break; sleep 3; done
sleep 10
curl -sf -G http://127.0.0.1:3100/loki/api/v1/query_range --data-urlencode 'query={job="guide"}' | tee /tmp/r.json
echo
grep -q 'restore me' /tmp/r.json
)
The query's default window is the last hour. For older data pass start and
end.
Upgrades
Read the release notes and the
upgrade guide, since
configuration keys change between versions. Back up, change the tag (this guide
pins 3.7.9), then:
cd ~/loki
sudo docker compose pull
sudo docker compose up -d
sudo docker compose logs --tail=20 loki
Troubleshooting
cd ~/loki && sudo docker compose logs --tail=100 loki caddy
Next steps
Compare providers in Best VPS for self-hosting.
Next steps
Common questions
How long does it take to follow "How to Deploy Grafana Loki on a VPS"?
We estimate about 25 minutes of hands-on time for "How to Deploy Grafana Loki on a VPS"; this is an editorial estimate, not a measurement.
Was the "How to Deploy Grafana Loki on a VPS" guide tested?
The "How to Deploy Grafana Loki on a VPS" guide was verified end-to-end on Ubuntu 26.04 (Nov 2026).