How to Install Shlink on a VPS with Docker: Short Links on Your Own Domain



Long links have a bad habit: they show up at the worst moment. You print a QR code for a brochure, then realize the URL behind it runs as long as a sentence. You share a download link in a group chat, and it wraps across two lines that nobody can click. Or you use a free shortener, and one day the service changes its terms, shows ads before the redirect, or kills old links without warning. Your links live on someone else's server, under someone else's rules.

Shlink fixes that in the most sensible way: the URL shortener runs on your VPS, under your domain. Shlink is an open-source URL shortener with a REST API and CLI, per-link visit statistics, QR codes, custom slugs, and expiry dates. This guide installs it with Docker Compose: a Shlink container, MariaDB for the database, and the Shlink web client as the dashboard for managing links — then secures it behind Nginx with HTTPS.

Prerequisites

  • A Linux VPS with Docker and Docker Compose v2 installed.
  • Two subdomains already pointing at the VPS IP: one for the short links, for example short.example.com, and one for the dashboard, for example manage.example.com. This article uses those domains; replace them with yours.
  • A free MaxMind account for a GeoLite2 license key. Shlink uses this key to download the location database, so visit statistics can show the visitor's city and country. Sign up on the MaxMind site, then create a license key from your account dashboard.
  • SSH access to the VPS with sudo rights.

Check Docker and Compose first:

docker --version
docker compose version

Expected output: both commands print a version number without errors.

Step 1: create the working folder and the .env file

Create a folder at /opt/shlink and move into it:

sudo mkdir -p /opt/shlink
cd /opt/shlink

Create a .env file for every value that should not be written directly into compose.yml:

sudo nano .env

Fill it in with your own values:

DEFAULT_DOMAIN=short.example.com
IS_HTTPS_ENABLED=true
GEOLITE_LICENSE_KEY=paste-your-maxmind-license-key-here
DB_DRIVER=maria
DB_NAME=shlink
DB_USER=shlink
DB_PASSWORD=replace-with-a-strong-database-password
DB_HOST=db

What each variable does — all official names from Shlink's Docker documentation:

  • DEFAULT_DOMAIN is the short domain used by every shortened link. This value must match exactly the domain you point at the VPS.
  • IS_HTTPS_ENABLED=true tells Shlink that short links will be served over HTTPS, so generated URLs use https://.
  • GEOLITE_LICENSE_KEY is your GeoLite2 license key from MaxMind. Without it, Shlink still runs, but the location columns in visit statistics stay empty.
  • DB_DRIVER=maria selects MariaDB as the database. The official documentation recommends a separate database for production instead of the image's built-in SQLite. Other supported values: mysql, postgres, and mssql.
  • DB_HOST=db points at the database service name in the Compose file in the next step — not localhost, because inside a container, localhost means the container itself.

Step 2: write the Compose file for Shlink and MariaDB

Create compose.yml:

sudo nano compose.yml

Fill it with the following configuration:

services:
  shlink:
    image: shlinkio/shlink:stable
    restart: unless-stopped
    ports:
      - '8080:8080'
    env_file:
      - .env
    depends_on:
      - db
  db:
    image: mariadb:lts
    restart: unless-stopped
    env_file:
      - .env
    environment:
      MARIADB_DATABASE: ${DB_NAME}
      MARIADB_USER: ${DB_USER}
      MARIADB_PASSWORD: ${DB_PASSWORD}
      MARIADB_ROOT_PASSWORD: replace-with-a-mariadb-root-password
    volumes:
      - shlinkdb:/var/lib/mysql
volumes:
  shlinkdb:

What each part does:

  • shlink uses the official shlinkio/shlink:stable image, which always points at the latest stable release. Port 8080 inside the container is published to host port 8080 for now — Nginx takes over later through a reverse proxy.
  • db uses the mariadb:lts image. The shlinkdb volume keeps all link and visit data even if the container is removed and recreated.
  • env_file makes both services read the same variables from .env, so the database password is written in one place only. Note: MARIADB_ROOT_PASSWORD must hold a different value from DB_PASSWORD — each has its own job.

Step 3: start the stack and create the first API key

Start the full stack:

sudo docker compose up -d
sudo docker compose ps

Expected output: shlink and db both show as running (or Up). On first start, Shlink creates its own tables in MariaDB and downloads the GeoLite2 database using your license key. That takes a few seconds; if the log shows an error, wait a moment and check again:

sudo docker compose logs --tail=50 shlink

Shlink has no login page with a password. Access to the API and the web client is controlled by API keys. Create the first key through the CLI available inside the container:

sudo docker compose exec shlink shlink api-key:generate

Expected output: a single line containing the API key, for example a1b2c3d4-.... Copy that key somewhere safe — you will need it in the next step, and it cannot be shown again once the terminal is closed. If this key ever leaks, generate a new one with the same command, then revoke the old one from the web client.

Do a quick check that the Shlink API answers:

curl -s http://127.0.0.1:8080/rest/v3/short-urls --header "X-Api-Key: paste-your-api-key-here" | head -c 300

Expected output: a JSON response listing short URLs — empty for now, so something like {"shortUrls": {"data": []}, ...}. What matters is that the response is JSON, not an HTML error page.

Step 4: add the web client as the dashboard

Shlink itself is an API with no web interface. To manage links from a browser, add the official web client service to the same compose.yml. Open the Compose file again:

sudo nano compose.yml

Add a webclient service after the shlink service:

  webclient:
    image: shlinkio/shlink-web-client:stable
    restart: unless-stopped
    ports:
      - '8081:8080'
    environment:
      SHLINK_SERVER_URL: https://short.example.com
      SHLINK_SERVER_API_KEY: paste-the-api-key-from-step-3
      SHLINK_SERVER_NAME: My Shlink

Three things to understand about this configuration:

  • The web client is a static app that runs entirely in your browser. That is why SHLINK_SERVER_URL must hold the public Shlink URL — https://short.example.com — not an internal Docker address, because your browser is the one calling that URL, not the web client container.
  • SHLINK_SERVER_API_KEY makes the web client connect right away without typing the key in every time. The trade-off: anyone who can open this dashboard automatically has Shlink API access. So the dashboard must always sit behind HTTPS and, where possible, be access-restricted — do not leave a dashboard with an embedded API key open to the internet without extra protection.
  • The port inside the web client image is 8080, mapped here to host port 8081 so it does not clash with Shlink.

Restart the stack so the new service comes up:

sudo docker compose up -d
sudo docker compose ps

Expected output: three containers — shlink, db, and webclient — all running.



Step 5: put Nginx in front with HTTPS

Do not leave Shlink and its dashboard on plain HTTP and raw ports. Create one Nginx file serving both subdomains. Create /etc/nginx/sites-available/shlink:

server {
    listen 80;
    server_name short.example.com;
    location / {
        proxy_pass http://127.0.0.1:8080;
        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;
    }
}
server {
    listen 80;
    server_name manage.example.com;
    location / {
        proxy_pass http://127.0.0.1:8081;
        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;
    }
}

Enable the configuration and issue certificates for both domains:

sudo ln -s /etc/nginx/sites-available/shlink /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
sudo certbot --nginx -d short.example.com -d manage.example.com

Expected output: nginx -t reports test is successful, and Certbot reports both certificates were issued.

Step 6: create your first short link and check the stats

Open https://manage.example.com in a browser. The Shlink dashboard shows the server already configured from step 4. Create your first short link:

  1. Click Create short URL (or the add-link button).
  2. Paste the long URL into the Long URL field, for example a blog article address.
  3. Fill in Custom slug with a short, memorable word, for example november-promo. Leave it empty and Shlink generates a random code instead.
  4. Add a tag if you want to group links, for example blog or brochure.
  5. Click Save.

Now test that link from the terminal, the way a visitor would experience it:

curl -I https://short.example.com/november-promo

Expected output: an HTTP/2 302 response with a Location header pointing at the long URL you entered. The 302 code means a temporary redirect — Shlink's default — and browsers follow it automatically.

Back in the dashboard, open that link's detail page and click the short link once from your browser. The Visits count goes up, and after a few seconds the statistics show the visitor's approximate location, browser, and operating system — as long as your GeoLite2 key is valid and Shlink managed to download its location database.

Troubleshooting

The shlink container restarts repeatedly, or the log is full of database errors.
Check three things in order: (1) the db container is running, (2) DB_DRIVER=maria and DB_HOST=db in .env are correct, (3) the password in .env matches the one the db service uses. Check the log with sudo docker compose logs --tail=50 shlink. If you changed the password after the first start, MariaDB does not follow — remove the shlinkdb volume and start over, or change the password directly inside the database.

A short link returns 404.
Almost always because DEFAULT_DOMAIN does not match the domain visitors use. Check the DEFAULT_DOMAIN value in .env and make sure that domain points at the VPS and Nginx forwards it to port 8080. Links created before the domain was fixed do not update themselves — recreate those links.

The web client shows a server connection error.
The web client calls SHLINK_SERVER_URL from your browser, so that URL must be publicly reachable with a valid HTTPS certificate. Check: (1) https://short.example.com opens and answers, (2) its certificate is valid, (3) SHLINK_SERVER_API_KEY matches the key from step 3. If you change the API key, update the value in compose.yml too, then run sudo docker compose up -d.

Visit statistics show no location.
That means the GeoLite2 database did not download. Check GEOLITE_LICENSE_KEY in .env — the MaxMind key must still be active — then check the Shlink log at startup for download errors. Fix the key, then restart with sudo docker compose up -d.

The dashboard opens, but anyone can create links.
That is the consequence of the API key embedded in the web client. Do not advertise the dashboard subdomain widely. If the dashboard is only for you, add password protection to the manage.example.com Nginx block with auth_basic, or restrict access through a firewall and VPN.

Summary table

ComponentRolePortNotes
shlinkURL shortener API8080Image shlinkio/shlink:stable, DEFAULT_DOMAIN is mandatory
dbMariaDB for links and statsInternal to DockerImage mariadb:lts, volume shlinkdb
webclientWeb dashboard for managing links8081Image shlinkio/shlink-web-client:stable, API key embedded
GeoLite2Visitor location database—Needs a free MaxMind license key
NginxReverse proxy + HTTPS80 / 443Two subdomains: short and manage

Pre-finish checklist

  • The shlink, db, and webclient containers are all running.
  • The first API key has been created and stored somewhere safe.
  • https://short.example.com serves redirects, and curl -I on one short link answers 302.
  • The dashboard at https://manage.example.com opens over valid HTTPS and connects to the Shlink server.
  • One short link with a custom slug has been created and its visit statistics are recorded.
  • GEOLITE_LICENSE_KEY is valid, so visitor locations appear in the statistics.
  • The shlinkdb volume is covered by your backup plan, since it holds every link and visit record.

For maintenance, run sudo docker compose pull && sudo docker compose up -d from /opt/shlink periodically to update the images. Because the data lives in the shlinkdb volume, containers are safe to recreate — but test one short link after each update, before you print it on material that cannot be taken back.

Which long link do you share most often and wish had a short version? Would short links on your own domain change how you share them? Tell us in the comments, and if any step above fails on your VPS, paste the error message so it can be discussed together.

Read also: the guide to self-hosted bookmarks with Karakeep complements Shlink — Karakeep stores long links worth reading later, while the companion guide in this batch is the Firefly III guide to self-hosted personal finance on a VPS.

Diskusi

Ada pertanyaan atau pengalaman yang ingin dibagikan?

0komentar

Komentar ditinjau sebelum ditampilkan. Mari berdiskusi dengan sopan dan sesuai topik.