Build Your Own Branded URL Shortener: A Step-by-Step Guide to Hosting Shlink on a VPS
Introduction: Why You Should Stop Relying on Third-Party URL Shorteners
In modern digital marketing and corporate communications, links are the primary bridges connecting your audience to your content. However, relying on generic, third-party URL shortening services like Bitly or TinyURL poses significant strategic disadvantages. Not only do generic links dilute your brand identity, but they also expose your sensitive traffic analytics to external platforms and limit your customization options.
By establishing a self-hosted, branded URL shortening system using Shlink on a personal Virtual Private Server (VPS), your business can regain absolute data ownership, enhance brand recognition, and gain access to enterprise-grade tracking features—all at a fraction of the cost of premium third-party subscriptions.
What is Shlink and Why is it the Ideal Choice?
Shlink is an open-source, self-hosted URL shortener written in PHP that functions as a robust microservice. Unlike traditional platforms, Shlink is headless by design, providing a powerful REST API to generate, manage, and track short URLs.
Key advantages of utilizing Shlink for your business infrastructure include:
- Absolute Data Privacy: All click tracking, visitor IP addresses, user-agent data, and referral metrics are stored securely on your own database.
- Multi-Domain Support: Serve short links from multiple branded domains using a single server instance.
- Advanced Analytics: Track detailed visitor demographics, including geolocation, device types, and traffic sources.
- High Performance: Built to be lightweight and highly scalable, ensuring near-instantaneous redirections.
Combined with Shlink Web Client, a modern and intuitive user interface, your team can easily manage links without ever touching the command line after the initial setup.
Prerequisites for Installation
Before proceeding with the deployment, ensure you have the following components ready:
- A Linux VPS: A server running Ubuntu 22.04 LTS or 24.04 LTS with at least 1 vCPU and 1GB RAM.
- A Domain Name: A dedicated short domain (e.g.,
yourbrand.co) with access to its DNS management console. - Docker and Docker Compose: The most efficient, reliable, and isolated method to deploy Shlink and its dependencies.
Step-by-Step Deployment Guide
Step 1: Point Your DNS Records
Log in to your domain registrar or DNS provider (such as Cloudflare) and create an A Record pointing your short domain (or subdomain, e.g., lnk.yourbrand.com) to the public IPv4 address of your VPS.
Note: Allow a few minutes for DNS propagation before proceeding to verify that the server is reachable via the domain name.
Step 2: Install Docker and Docker Compose
Connect to your VPS via SSH and update your system packages. Execute the following commands to install Docker:
sudo apt update && sudo apt upgrade -y
sudo apt install docker.io docker-compose -y
sudo systemctl enable --now dockerStep 3: Configure the Environment File
Create a dedicated directory for your Shlink installation and navigate into it:
mkdir ~/shlink && cd ~/shlinkShlink requires an API key and encryption secrets to run securely. Create an environment file named .env to store these configurations securely:
SHORT_DOMAIN=lnk.yourbrand.com
IS_HTTPS_ENABLED=true
GEOLITE2_LICENSE_KEY=your_maxmind_license_key
DB_PASSWORD=your_secure_db_passwordStep 4: Create the Docker Compose File
To orchestrate the Shlink service, a database (PostgreSQL), and a reverse proxy (Caddy) for automatic SSL termination, define a docker-compose.yml file:
version: '3.8'
services:
shlink-db:
image: postgres:15-alpine
environment:
POSTGRES_DB: shlink
POSTGRES_USER: shlink
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- shlink_db:/var/lib/postgresql/data
restart: always
shlink-web:
image: shlinkio/shlink:stable
depends_on:
- shlink-db
environment:
- DEFAULT_DOMAIN=${SHORT_DOMAIN}
- IS_HTTPS_ENABLED=${IS_HTTPS_ENABLED}
- DB_DRIVER=postgres
- DB_USER=shlink
- DB_PASSWORD=${DB_PASSWORD}
- DB_NAME=shlink
- DB_HOST=shlink-db
- GEOLITE2_LICENSE_KEY=${GEOLITE2_LICENSE_KEY}
restart: always
caddy:
image: caddy:2-alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile
- caddy_data:/data
- caddy_config:/config
depends_on:
- shlink-web
restart: always
volumes:
shlink_db:
caddy_data:
caddy_config:Step 5: Configure the Reverse Proxy (Caddyfile)
Create a Caddyfile in the same directory to automatically provision Let's Encrypt SSL certificates and forward traffic to Shlink:
lnk.yourbrand.com {
reverse_proxy shlink-web:8080
}Step 6: Launch the System
Execute the following command to download the images and launch your branded URL shortener system in the background:
docker-compose up -dTo generate your initial API key to connect to the administration interface, run:
docker-compose exec shlink-web shlink api-key:generateCopy the generated key securely; it will be required to manage your links.
Connecting the Shlink Web Client
With the backend running smoothly, navigate to the official, secure web interface hosted by the Shlink team at app.shlink.io. Rest assured, all configurations are stored locally in your browser's local storage; your API key is never transmitted to their servers.
Click on "Add a server" and input the following details:
- Name: Your Brand Shortener
- URL:
[https://lnk.yourbrand.com](https://lnk.yourbrand.com) - API Key: The key generated in Step 6.
Once connected, you will be greeted by a sleek dashboard where you can start generating short links, creating custom slugs, attaching tags, and reviewing comprehensive analytics immediately.
Conclusion and Best Practices
Congratulations! You have successfully decoupled your link management infrastructure from restrictive third-party SaaS platforms. By deploying Shlink on your personal VPS, you now possess a fully branded, secure, and private ecosystem for your corporate links.
To maintain peak performance and operational security, ensure you implement regular automated backups of your PostgreSQL database volume and periodically update your Docker images to receive the latest security patches.
