How to Self-Host a URL Shortener on Your VPS and Track Every Click

How to Self-Host a URL Shortener on Your VPS and Track Every Click

Long links look messy in print, in bios, and in chat messages, and they tell you nothing about who clicked them. A short link fixes the first problem, and a self-hosted shortener fixes the second: when you self-host a URL shortener on VPS hardware you already run, every click lands in a database you own, with the date, the referrer, and the country attached. No third-party service keeps a copy of your traffic, and no free plan caps how many links you can make. This guide sets up Shlink, a free open-source shortener, with Docker Compose behind Nginx and SSL.

The whole setup takes about an hour. You need a VPS with Docker installed, a domain or subdomain for the short links, and a way to point that domain at your server. The shortener itself uses little RAM, so a 1 GB VPS handles it alongside other small services.

Why Self-Host a Shortener Instead of Using a Free Service

Free shorteners work, but they come with limits you do not control. A free plan can cap links per month, retire old links, or show an interstitial ad page before the redirect. Some shut down and take your links with them. Your own shortener has none of that: links stay live as long as your server does, and the click data stays in your database.

There is also a trust angle. A link on your own domain, like go.yourdomain.com/sale, looks better in a printed flyer or a client message than a generic short domain you do not own. People click links they recognize, and you can see exactly how many did.

Shlink is a good fit because it is a single-purpose tool with a web admin panel, an API, and built-in click tracking. It shortens links, counts clicks, and gets out of the way.

How to Self-Host a URL Shortener on VPS in One Afternoon

The plan is short: pick a short domain, install Shlink with Docker Compose, put Nginx in front with a free SSL certificate, then create your first short links and check the click data. Each step is easy to verify before moving on.

Keep the shortener on its own subdomain, such as go.yourdomain.com. Do not put it on your main domain: a short domain keeps links short, and it keeps the shortener separate from your main site if anything goes wrong.

Pick a Short Domain and Point It at Your VPS

Buy a short domain or use a subdomain of a domain you already own. A short domain costs roughly what a normal domain costs, usually somewhere in the range of a few dollars to around twenty dollars a year depending on the extension, so check official pricing before you buy. Shorter is better: every character in the domain repeats in every link you share.

Add a DNS A record for the subdomain that points at your VPS IP address:

go.yourdomain.com  A  203.0.113.10

Wait for the record to spread before you continue. Check it with dig go.yourdomain.com +short or an online DNS checker. If it still shows the old address, wait a few minutes and try again. The SSL step later will fail if DNS is not ready, so do not skip this check.

Install Shlink with Docker Compose

Shlink ships as a Docker image with a small database, and the shlink docker setup below keeps both in one Compose file. If Docker is not on your server yet, follow our guide to installing Docker Compose on Ubuntu 24.04 first.

Create a folder for the shortener and write the Compose file:

sudo mkdir -p /opt/shlink
sudo nano /opt/shlink/docker-compose.yml
services:
  shlink:
    image: shlinkio/shlink:stable
    restart: unless-stopped
    environment:
      DEFAULT_DOMAIN: go.yourdomain.com
      IS_HTTPS_ENABLED: "true"
      DB_DRIVER: postgres
      DB_HOST: db
      DB_NAME: shlink
      DB_USER: shlink
      DB_PASSWORD: a-strong-password-here
      GEOLITE_LICENSE_KEY: ""
    ports:
      - "127.0.0.1:8080:8080"
    depends_on:
      - db

  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: shlink
      POSTGRES_USER: shlink
      POSTGRES_PASSWORD: a-strong-password-here
    volumes:
      - shlink-db:/var/lib/postgresql/data

volumes:
  shlink-db:

Two details matter here. First, Shlink listens only on 127.0.0.1, so it is not reachable from the internet directly; Nginx will pass traffic to it. Second, the database password appears twice and both copies must match, so generate one strong password and paste it in both places.

Start the stack and check that both containers run:

cd /opt/shlink
sudo docker compose up -d
sudo docker compose ps
sudo docker compose logs shlink --tail 20

Shlink prints an API key in its first-run logs. Copy it now, because you need it to log in to the web admin panel. If you miss it, generate a new one with sudo docker compose exec shlink shlink api-key:generate.

Put Nginx in Front and Add SSL

Nginx takes public traffic on port 443 and passes it to Shlink on port 8080. If you already run several Docker apps behind one Nginx, the same pattern works: our guide to Nginx Proxy Manager for multiple Docker sites covers the managed alternative, but a plain server block is enough here.

server {
    listen 80;
    server_name go.yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        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;
    }
}

Save it as /etc/nginx/sites-available/go.yourdomain.com, link it into sites-enabled, test, and reload. Then add a free SSL certificate with Certbot. Our guide to installing a free Let’s Encrypt certificate on Nginx walks through the Certbot command; the short version is:

sudo ln -s /etc/nginx/sites-available/go.yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
sudo certbot --nginx -d go.yourdomain.com

Open https://go.yourdomain.com after Certbot finishes. You should see the Shlink admin login. Paste the API key you copied earlier to get in.

Create Short Links and Track Clicks

In the Shlink admin panel, paste a long URL, pick a custom slug if you want one, and save. Shlink gives you a short link at once, for example https://go.yourdomain.com/sale. Share that link anywhere you would have shared the long one.

Every click on a short link shows up in the Shlink dashboard with the date and time, the referrer, the browser, and the country. That is the core of track clicks url shortener data: not just a total count, but where the clicks came from and when they arrived. Open a link yourself from your phone and from a desktop browser, then check the dashboard to confirm both visits appear before you share the link widely.

For links you share in different places, make one short link per place. Use /sale-ig for Instagram, /sale-email for a newsletter, and /sale-print for a flyer, all pointing at the same page. The click counts then tell you which channel works, instead of lumping all traffic into one number.

Shlink also has an API, so you can create links from a script instead of the panel. That is handy if you generate links in bulk, for example one per product page. The API key you used to log in is the same key scripts use.

Read the Click Data Without Drowning in It

Click counts are easy to misread. A link with 200 clicks and zero sales is not doing better than a link with 20 clicks and three sales. Check the click data next to the outcome you care about: orders, signups, or replies. Shlink tells you the clicks; your shop or form tells you the rest.

Watch for bot traffic too. A sudden burst of clicks from one country at 3 a.m. with no referrer is usually a crawler, not a crowd of buyers. Shlink marks bot visits where it can, so filter them out before you judge a campaign.

Keep the Shortener Fast and Backed Up

Shlink is light, but two habits keep it healthy. First, back up the database volume. The links and the click history live in Postgres, so a backup of the shlink-db volume plus the Compose file is enough to rebuild the whole service. Run a nightly database dump and copy it off the server.

sudo docker compose exec db pg_dump -U shlink shlink | gzip > /var/backups/shlink.sql.gz

Second, update the image on a schedule. Check for a new Shlink release every month or two, pull it, and restart the stack. A shortener that redirects traffic for your business is not a service you want running an old version with a known bug.

cd /opt/shlink
sudo docker compose pull
sudo docker compose up -d

Test one short link after every update. If it redirects to the right page, the update worked. If it does not, the logs will tell you why: sudo docker compose logs shlink --tail 50.

That is the whole setup. One subdomain, one Compose file, one Nginx block, and you own every click your links ever get.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.