Every link you shorten through bit.ly, tinyurl, or t.co becomes someone else’s data. They see which campaigns you run, which pages your audience clicks, and how your traffic moves over time. For a personal blog that may not matter much. For a business, a newsletter, or anything with real audience numbers behind it, it should matter a lot.

Shlink is the answer most homelab operators land on. It’s an open-source URL shortener written in PHP that you host yourself, on your own domain, with your own analytics database. You get short links that look like https://go.example.com/launch, a clean API, a web interface, QR code generation, and click analytics — and none of that data leaves your server.

This guide walks through a production-ready Docker Compose setup: Shlink plus its database, a small web client for managing links, and a reverse proxy for HTTPS.

Why self-host a URL shortener at all

The argument for self-hosted link shortening comes down to three things.

Ownership of the redirect. When a third-party shortener changes its pricing, shuts down, or starts injecting interstitial ads before the redirect, every link you’ve ever published breaks or degrades. Your own domain means the redirect lives as long as you keep paying for the domain.

Analytics you actually control. Shlink records visits, referrers, user agents, and coarse geolocation in your own PostgreSQL or MySQL database. You can query it directly, export it, or delete it. Nothing is sold, and there’s no retention policy imposed by someone else.

Branding. go.yourdomain.com/docs looks intentional. bit.ly/x7Kq2pL looks like spam. This matters more than people expect — a branded short link isn’t filtered as aggressively and readers are more willing to click it.

There’s a fourth, quieter benefit: a short-link domain is genuinely useful infrastructure. You’ll use it for internal dashboards, wiki links, QR codes on physical signage, and deep links in mobile apps.

What you’re deploying

A complete Shlink stack has three moving parts:

  • Shlink server — the core application. Handles short code creation, redirects, REST API, and analytics ingestion.
  • A database — PostgreSQL is the recommended production choice, though MySQL/MariaDB and SQLite are supported.
  • Shlink Web Client — an optional but very worthwhile React UI for creating and browsing links without touching the API by hand.

You’ll also want a reverse proxy. If you already run Traefik or Caddy, this slots into your existing setup; both are covered below.

Prerequisites

  • A Linux host with Docker and the Compose plugin installed
  • A dedicated domain or subdomain, e.g. go.example.com
  • DNS A/AAAA record pointing that hostname at your server
  • A reverse proxy handling TLS (or willingness to expose port 8080 directly — not recommended)

Using a subdomain rather than a path is important. Short codes are appended directly to the host, and hosting Shlink under example.com/s/ complicates redirects and caching for no benefit.

The Compose file

Create a directory, for example /opt/shlink, and add a docker-compose.yml:

services:
  shlink:
    image: shlinkio/shlink:stable
    container_name: shlink
    restart: unless-stopped
    environment:
      DEFAULT_DOMAIN: "go.example.com"
      IS_HTTPS_ENABLED: "true"
      GEOLITE_LICENSE_KEY: "${GEOLITE_LICENSE_KEY}"
      DB_DRIVER: "postgres"
      DB_HOST: "shlink-db"
      DB_PORT: "5432"
      DB_NAME: "shlink"
      DB_USER: "shlink"
      DB_PASSWORD: "${SHLINK_DB_PASSWORD}"
      INITIAL_API_KEY: "${SHLINK_INITIAL_API_KEY}"
      DEFAULT_REDIRECT_STATUS_CODE: "302"
      DEFAULT_REDIRECT_CACHE_LIFETIME: "90"
    volumes:
      - ./data:/home/shlink/data
    ports:
      - "8080:8080"
    depends_on:
      shlink-db:
        condition: service_healthy

  shlink-db:
    image: postgres:17-alpine
    container_name: shlink-db
    restart: unless-stopped
    environment:
      POSTGRES_DB: "shlink"
      POSTGRES_USER: "shlink"
      POSTGRES_PASSWORD: "${SHLINK_DB_PASSWORD}"
    volumes:
      - ./postgres:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U shlink -d shlink"]
      interval: 10s
      timeout: 5s
      retries: 5

  shlink-web:
    image: shlinkio/shlink-web-client:stable
    container_name: shlink-web
    restart: unless-stopped
    ports:
      - "8081:8080"
    depends_on:
      - shlink

Alongside it, a .env file holding the secrets:

SHLINK_DB_PASSWORD=change-me-to-something-long
SHLINK_INITIAL_API_KEY=change-me-too
GEOLITE_LICENSE_KEY=

Restrict permissions on that file — chmod 600 .env — because it holds database and API credentials.

Explaining the important settings

DEFAULT_DOMAIN is the hostname Shlink will use when generating short URLs. If you later move to a different domain, you can change this and old links keep a record of the domain they were created under.

IS_HTTPS_ENABLED: "true" tells Shlink to generate https:// short URLs. Set it correctly from the start; if you flip it later, previously created links retain their original scheme unless you edit them.

GEOLITE_LICENSE_KEY enables geolocation of visits. MaxMind’s GeoLite2 databases require a free license key. If you don’t have one, leave it blank — Shlink runs fine and simply records visits without country or city data. This is the one setting where you should treat the value as optional and not block your deployment on it.

DEFAULT_REDIRECT_STATUS_CODE controls whether redirects are 301 (permanent, aggressively cached by browsers) or 302 (temporary). For marketing links where you might want to update the destination, or where you want every click to hit your server for analytics, 302 is the sane default. Use 301 only for links that will never change.

DEFAULT_REDIRECT_CACHE_LIFETIME sets the Cache-Control max-age in seconds for redirects. A moderate value like 90 seconds gives browsers a little caching without starving your analytics.

INITIAL_API_KEY seeds the first API key on the very first boot. Without it, you’d have to run a CLI command to generate one. Generate both secrets with a password manager or openssl rand -hex 32.

Starting the stack

From /opt/shlink:

docker compose up -d
docker compose logs -f shlink

Watch the logs for the database migration step. Shlink applies schema migrations automatically at startup, which is one of its nicer operational properties. When you see the application boot message, verify it’s healthy:

curl -s http://localhost:8080/rest/health

That endpoint should return a small JSON payload reporting status: pass. It’s also the right endpoint to wire into Uptime Kuma or your monitoring system, so you learn about a broken shortener before your readers do.

Putting it behind HTTPS

Since Shlink itself speaks plain HTTP on 8080, terminate TLS at your existing proxy.

With Caddy, the whole configuration is three lines:

go.example.com {
    reverse_proxy shlink:8080
}

Caddy provisions a Let’s Encrypt certificate automatically. If both containers share a Docker network, use the service name shlink; otherwise use the host IP and the published port.

With Traefik, add labels to the shlink service:

labels:
  - "traefik.enable=true"
  - "traefik.http.routers.shlink.rule=Host(`go.example.com`)"
  - "traefik.http.routers.shlink.entrypoints=websecure"
  - "traefik.http.routers.shlink.tls.certresolver=letsencrypt"
  - "traefik.http.services.shlink.loadbalancer.server.port=8080"

Do the same for the web client on 8081, ideally on a hostname like shlink.example.com that is not the short domain. Keeping the admin UI off the domain people click through is both tidier and a small security win.

With the REST API, using the key you seeded:

curl -X POST https://go.example.com/rest/v3/short-urls \
  -H "X-Api-Key: $SHLINK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "longUrl": "https://devdailytips.com/self-host-shlink-docker-url-shortener/",
    "customSlug": "shlink",
    "tags": ["blog", "self-hosting"],
    "validSince": "2026-11-20T00:00:00+00:00"
  }'

The customSlug field lets you choose the short code instead of getting a random one — this is what makes branded links readable. Tags let you group campaigns so analytics stay meaningful as your link count grows.

Or skip the terminal entirely: open the web client at https://shlink.example.com, add your server by URL and API key, and create links through the form. It supports bulk creation, tag filtering, and per-link visit charts.

Reading the analytics

Shlink stores visits in dedicated tables, which means you’re never locked into a vendor dashboard. The API exposes aggregates:

curl -s "https://go.example.com/rest/v3/short-urls/shlink/visits" \
  -H "X-Api-Key: $SHLINK_API_KEY" | jq '.visits.data[] | {date, referer, userAgent, country}'

A few practical notes on interpreting this data. Visits are counted when the redirect is served, not when the page finishes loading. Bot traffic is filtered by Shlink’s default bot-detection rules, but you can tune that. Referrers are only populated when the browser sends them, so a shift toward “direct” traffic in your stats often means people are clicking from a private channel or an app that strips referrers — not that your data is broken.

If you’d rather see trends visually, Shlink can expose Prometheus metrics and there are community Grafana dashboards. That’s a natural next step once the shortener is running.

Operational hygiene

Back up the database. Your links and their click history live in PostgreSQL, and there is no cloud provider saving a copy for you. pg_dump on a schedule, stored off-host, is the entire story:

docker compose exec -T shlink-db pg_dump -U shlink shlink | gzip > shlink-$(date +%F).sql.gz

Keep the stack updated by pulling stable tags and recreating containers; migrations run automatically on boot. Finally, treat the API key like a password. Anyone holding it can read your click data and create links under your domain — which is a fine way to get a trusted domain flagged.

The takeaway

A self-hosted Shlink gives you something the free tiers of commercial shorteners never will: links that belong to you, analytics you can query with SQL, and a branded domain your audience can trust. The Compose stack is four services and maybe fifteen minutes of work, and once it’s running it’s close to zero-maintenance. If you already run a reverse proxy, you’re most of the way there — the only genuinely new piece is the database, and that’s a pattern you’ve deployed a dozen times before.