Run your own URL shortener on Raspberry Pi

Long URLs are hard to remember. With a self-hosted URL shortener, you only need to remember your own domain name and a short string. Here’s how to set up and use one on your Raspberry Pi.

Why do you need a URL shortener?

URLs are long, unwieldy things and can be hard to remember. The URL for this post, for example, is That’s 69 characters, and if you consider it worth sharing as part of a social media post with a character limit set, that’s 69 characters that you could use to wax lyrics with comments, and your own point of view on the article. Shorter URLs give you more space to write.

A URL shortener takes a long URL, assigns it a new, shorter URL, and redirects traffic to the original long URL. For example, by clicking on will send a request to TinyURL, which will redirect the request to, the profile page of this author. By using a URL shortener, you saved a whopping 41 characters.

They’re also really handy if you plan on giving someone a web address — or even writing it down — rather than sending a link.

Other great reasons to use a URL shortener include branding and marketing, click tracking, and app deep linking.

Why Host a URL Shortener on Raspberry Pi?

There are many commercial URL shortening services. Some are fully paid, others are completely free, while a good portion operate on a “Freemium” model, offering basic features at no cost, while charging more than basic usage.

You may have heard of or even used URL shortening services such as TinyUR, Bitly or BL.INK. These services host your collections of shortened URLs, but they are vulnerable and not particularly portable. If you violate the Terms of Service, your collection could be gone in the blink of an eye. If the service provider raises the fees to a level you’re not comfortable with, you can either accept the exorbitant price increase or lose your collection of URLs.

And then there’s the fact that URLs like certainly shorter than the alternative, are neither particularly easy to remember nor particularly short.

By running your URL shortener on a Raspberry Pi, you’re not vulnerable to price gouging, terms and conditions vagaries, and you can customize the URL however you like. We chose Policy like our URL shortener because it’s easy to install, simple to use, and comes with great support.

Choose a short domain name

The main feature of a URL shortener is that the resulting URL should be, well, short, and to achieve that, you need a short domain name. All the good .com domains are gone, but there are hundreds of other top-level domains you can use as a base.

We chose It is only seven characters, easy to remember and linked to the main website. We could have chosen an equally effective,, or The possibilities for a good domain name are endless.

Register the domain (ours costs $1.98 per year), then visit the Advanced DNS section. Create a new record, choosing A like kind, @ as host, and for value, put your own personal IP address. Set the TTL as low as possible.

Configure your Raspberry Pi with Polr

The first thing you need to do is follow our guide on setting up your Raspberry Pi as a server. Once you’ve done that, login with Secure Shell (SSH), then update and upgrade all installed packages:

sudo apt update
sudo apt upgrade

Change directory, clone Polr GitHub repository and set permissions:

cd /var/www/
sudo git clone https:
sudo chmod -R 755 polr

Now give ownership of the poll Apache user directory:

chown -R www-data polr
img alt="Terminal output for cloning polr and setting permission " height="435" src="" width="1809" />

Move into the poll directory, then become the root user and install the Composer tool and its dependencies:

cd polr
sudo su
curl -sS https:
php composer.phar install

You will see a warning: “Do not run Composer as root/super user!” However, it is safe to ignore this. After this point, Composer may generate errors relating to missing PHP modules; install them with:

apt-get install php-mbstring php-curl php-dom

And then repeat the previous command. When asked, “Do you trust ‘kylekatarnls/update-helper’ to run the code and want to enable it now?” hit Yes on your keyboard then Come back. You will be returned to the command prompt. Exit the root shell, then change directory:

cd /etc/apache2/sites-available/

Enable the Apache rewrite mod:

a2enmod rewrite

And use nano to create a new Apache configuration file:

sudo nano polr.conf

Inside, paste:

<VirtualHost *:80>
DocumentRoot "/var/www/polr/public"
<Directory "/var/www/polr/public">
Require all granted
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all

…Replacement with your own domain name. Save and exit nano with CTRL+O then Ctrl+X. Activate the new configuration file, then restart Apache with:

sudo a2ensite polr.conf
sudo service apache2 restart

Polr requires a database to work. Open MariaDB with:

sudo mariadb

Add the following entries, one line at a time:

CREATE USER polr IDENTIFIED BY 'topsecretpassword';
GRANT USAGE ON *.* TO [email protected] IDENTIFIED BY 'topsecretpassword';
GRANT ALL privileges ON polr.* TO [email protected];

Return to the Polr configuration directory and copy the .env case:

cd /var/www/polr
cp .env.setup .env

You can now run the setup wizard through your browser at http://your-domain/setup/

Configure Polr through a browser

You will be faced with a bewildering array of options and text input fields against a beautiful polar background. Here you will tell your Raspberry Pi URL shortener what it is and how you want it to work. Some of these fields will be pre-populated.

Leave the Database host and Database Port with their default values, but modify the Database username at poll and the password of whatever you have set in MariaDB. If you have followed our instructions to the letter, it will be topsecret password. The app URL is your domain name.

The rest of the options relate to how you want your short URLs formatted, whether you need a password to use the site, and whether you want your URLs to be public. Remember to set a reasonable administrator username and a secure password. You can also choose from a variety of themes, including Google Material Design, Newspaper, and Midnight Black. When you’re done, hit the green Install button at the bottom of the page.

Secure your URL shortener

Man-in-the-middle attacks aren’t fun for anyone (except, maybe, the man himself). Secure your Polr installation by using Certbot to retrieve and install Let’s Encrypt certificates and keys.

sudo certbot

Enter your email address when prompted and select the name for which you want to enable HTTPS. Certbot will retrieve and install Let’s Encrypt SSL keys and certificates. Now restart Apache:

sudo service apache2 restart

When you reload the page in your browser, the connection to your URL shortener will be secure. To shorten a URL, simply paste the URL into the text box, then click Shorten. Or click Link options to customize the short link.

Use your Raspberry Pi URL shortener with care!

Running your own URL shortener is a great way to keep track of which links you find useful and which ones you’d like to share with others. However, since your URL shortener is on the public Internet, be careful not to use it to link to illegal, ethically questionable, or pirated material.

A URL shortener is just one of many internet-related projects you can run on a Raspberry Pi. Others include the ability to host your photo gallery, password manager, and even a server personal cloud storage with a variety of useful applications.

Comments are closed.