I write a lot of PowerShell scripts for application installations, and many download the install files from a URL. Because the URL is hardcoded in the scripts, if the URL changes, I need to update all the scripts. This exact issue happened to me a few years ago, and my solution at the time was to write a Python script to update the PowerShell scripts. You can read about that solution in my blog post, Script to make Scripts.
However, if my scripts had used a URL shortener instead, I could’ve edited the short URL to resolve the issue. The problem with most URL shorteners is that you can make short URLs all day long, but you can’t edit them, and if they let you edit them, it’s a paid feature, and they also start limiting how many hits your short URL can get. I am not a fan of any of that.
Fortunately, a URL shortener named Shlink exists. Shlink is a self-hosted open-source URL shortener. With Shlink, you can do whatever you want with your short URL, including editing them.
Once I got Shlink set up, I re-wrote my PowerShell scripts to use my Shlink short URLs. Now, if an install URL changes, I can edit the Shlink short URL, and I don’t need to update my PowerShell scripts.
In this post, I will show you step-by-step how to set up Shlink with Docker and Cloudflare Tunnels.
The Shlink setup will use a Cloudflare tunnel for external access and a MariaDB database for the Shlink database. I selected MariaDB as I am more familiar with it. However, Shlink does support other databases. We will also use the Shlink web client as a UI to easily manage the short links.
Prerequisites
- Domain for the short links.
- DNS for the domain hosted in Cloudflare.
- Docker host.
The Process
- Make a folder to store your docker configurations for Shlink.
I’ll call mine shlink.
.env
Setup
- Copy the
sample.env
file as a new .env
file.
- You will need to define the variables in the
.env
file.
- The variables are:
CONTAINER_NAME
is the name of your Shlink stack. There will be 4 containers spawned.
- The one with
_app
appended to it is the Shlink application image that runs all of Shlink.
- The one with
_db
appended to it is the MariaDB image for Shlink to store all the configurations.
- The one with
_web_client
append to it is the Shlink web client image that gives you a nice UI to manage your short URLs.
- The one with
_cf
appended to it is the Cloudflare tunnel image for the Shlink application.
DB_NAME
is the name of the MariaDB database that Shlink will use.
DB_ROOT_PWD
is the MariaDB root password.
DB_USER
is the database user that Shlink will use.
DB_USER_PWD
is the password for the database user that Shlink will use.
SHORT_DOMAIN
is the domain you will be using for your short URLs. (only include the FQDN (Fully Qualified Domain Name))
REDIRECT_DOMAIN
is where invalid short links, direct visits to the short domain, or 404 pages will be sent. (this needs to start with https://)
APP_PORT
is the port that the Shlink application will run on and the port you will use with the Cloudflare tunnel.
ADMIN_API
is the initial API key that will be used to configure your Shlink and will be preloaded to the Shlink Web Client.
WEB_CLIENT_PORT
is the port the Shlink web client will use.
CF_TUNNEL_B64
is the Base64 of your Cloudflare tunnel for Shlink.
For example, I will use the domain dxt.zip as the primary domain for Shlink. I will be redirecting all non-short URL traffic to my blog, thedxt.ca. I will use port 8788 for the Shlink application and port 8787 for the Shlink web client to manage Shlink.
…