Long URLs are difficult to remember. With a self-hosted URL shortener, all you have to remember is your own domain name and a short string of characters. Here’s how to set one 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. For example, the URL for this post is https://www.makeuseof.com/run-your-own-url-shortener-on-raspberry-pi/. That’s 69 characters, and if you feel it makes sense to share it as part of a social media post with a set character limit, that’s 69 characters you could use to pop in with comments and your own take on the article to rave about. Shorter URLs give you more room to write.
A URL shortener takes a long URL, assigns it a new, shorter URL, and redirects traffic to the original long URL. Click for example https://tinyurl.com/2p8xswau sends a request to TinyURL, which forwards the request https://www.makeuseof.com/author/david-rutland/, the profile page of this author. By using a URL shortener, you saved a whopping 41 characters.
They also come in very handy when you want to give someone a web address or even write it down instead of sending a link.
Other good reasons to use a URL shortener include branding and marketing, click tracking, and deep-linking to apps.
Why Host a URL Shortener on Raspberry Pi?
Many commercial URL shortening services are available. Some are fully paid for, others are completely free to use, while a fair proportion operate on a “freemium” model, offering basic features at no cost while charging for more than basic usage.
You may have heard of or even used URL shortening services like 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 no time. If the service provider increases the fees to a level that you are not comfortable with, you can either accept the exorbitant price increase or lose your URL collection.
And then there’s the fact that URLs like https://tinyurl.com/2p8xswaualthough certainly shorter than the alternative, are neither particularly easy to remember nor particularly short.
By running your on-URL shortener on a Raspberry Pi, you’re not vulnerable to price increases and vagaries of terms and conditions, and you can customize the URL as you see fit. We have chosen polar as our url shortener as it is easy to install and use and has great support.
Choose a short domain name
The main feature of a URL shortener is that the resulting URL should be short, and to achieve this 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 have chosen muo.lol. It consists of only seven characters, is easy to remember and refers to the main website. We could have chosen an equally effective muo.fun, muo.gay, muo.la or muo.wtf. The possibilities for a good domain name are endless.
Register the domain (ours costs $1.98 per year) and then visit the advanced DNS section. Create a new record and select A as a guy @ as host and as value enter your own home IP address. Set the TTL as low as possible.
Set up your Raspberry Pi with Polr
The first thing you need to do is follow our guide on how to set up your Raspberry Pi as a server. After that, login with Secure Shell (SSH) and update and update all installed packages:
sudo apt update
sudo apt upgrade
Change directory, clone Polr GitHub repository and set permissions:
sudo git clone https://github.com/cydrobolt/polr.git --depth=1
sudo chmod -R 755 polr
Now give ownership of the polar Directory to the Apache user:
chown -R www-data polr
img alt="Terminal output for cloning polr and setting permission " height="435" src="https://static0.makeuseofimages.com/wordpress/wp-content/uploads/2022/09/clone-polr.jpg" width="1809" />
Move into the polar directory, then make yourself root and install the composer tool and dependencies:
curl -sS https://getcomposer.org/installer | php
php composer.phar install --no-dev -o
You will see a warning: “Do not run Composer as root/superuser!” However, it is safe to ignore this. After this point, Composer may throw errors related 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 code and do you want to activate it now?” blow Y on your keyboard and then To return. You return to the command prompt. Exit the root shell, then change directories:
Enable the Apache Rewrite mod:
And use nano to create a new Apache conf file:
sudo nano polr.conf
In it, paste the following:
Require all granted
Options Indexes FollowSymLinks
Allow from all
…Substitute muo.lol with your own domain name. Save and exit nano with Ctrl + O then Ctrl + X Activate the new conf file and then restart Apache with:
sudo a2ensite polr.conf
sudo service apache2 restart
Polr needs a database to run. Open MariaDB with:
Add the following entries line by line:
CREATE DATABASE polr;
CREATE USER polr IDENTIFIED BY 'topsecretpassword';
GRANT USAGE ON *.* TO [email protected] IDENTIFIED BY 'topsecretpassword';
GRANT ALL privileges ON polr.* TO [email protected];
Change back to the Polr setup directory and copy the .env File:
cp .env.setup .env
You can now run the configuration wizard from your browser at http://your-domain/setup/
Set up Polr using a browser
You’ll be presented with a bewildering array of options and text entry fields against a beautiful polar background. This is where you tell your Raspberry Pi URL shortener what it is and how you want it to work. Some of these fields are pre-filled.
Let them database host and database port with their default values, but change those Database username to polar and the password for what you set in MariaDB. If you have followed our instructions exactly, this will be the case top secret password. The Application URL is your domain name.
The remaining options relate to how you want your shortened URLs formatted, whether you need a password to use the site, and whether you want your URLs to be public. Remember to set a sensible admin username and strong 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 To install button at the bottom of the page.
Secure your URL shortener
Man-in-the-middle attacks are not fun for anyone (except maybe the man himself). Secure your Polr installation by using Certbot to retrieve and install certificates and keys from Let’s Encrypt.
Enter your email address when prompted and select which name you want to enable HTTPS for. Certbot fetches and installs SSL keys and certificates from Let’s Encrypt. Now restart Apache:
sudo service apache2 restart
When you reload the page in your browser, the connection to your URL shortener is secure. To shorten a URL, just paste the URL into the text box and click on it Shorten. Or click Link Options to customize the shortcut.
Use your Raspberry Pi URL Shortener with care!
Running your own URL shortener is a great way to track links that you find useful and want to share with others. However, since your URL shortener is on the public internet, be careful not to use it to link to anything illegal, unethical, or pirated.
A URL shortener is just one of the 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 personal cloud storage server with a variety of useful apps.
This article was previously published on Source link