Ever since the internet invaded mobile devices, lengthy links have been an issue. With only 140 characters in a tweet or 160 in a text, there can’t be any wasted space. This is where the link shortener comes in.
History of Link Shorteners
This is definitely not the first time anyone has ever had the idea of creating their own link shortener. You are probably familiar with the big players in this arena: Bit.ly, Google’s goo.gl, Twitter’s t.co. And there are more services popping up every day.
So why create your own?
The question seems analogous to self-hosting your website or using a pre-existing service. It basically boils down to responsibility and control. If you use someone else’s service, you aren’t responsible for keeping the service up and making sure it doesn’t break (or get hacked).
But on the other hand, you have limited control over the service. Essentially you can only do what the service allows you to do.
If you host your own website (or link shortener), you are responsible for keeping it up, but you have the control to make it look and act however you want it to.
Plus, it’s a lot of fun!
How I Created My Link Shortener in 3 Steps
So, here’s how I did it.
Step 1: Pick and register a domain
The thing about a link shortener is that you want the domain name to be short as well. A domain like “reallyawesomelinkshortener.com.au” would be terrible. If the domain of the link shortener itself is longer than the link you’re trying to share, you’re doing it wrong.
The difficulty here is that short domain names are very popular. Good, short, unregistered domains are increasingly rare, especially in the .com TLD. Because of this, a lot of new sites are opting for international TLDs: e.g. .ly, .co, etc.
Another thing to keep in mind is that though they may not be popular in regular domains, hyphens might make a short domain easier to find.
Keeping these two things in mind, I found the domain for my shortener and registered it through NameCheap: a-n.pw.
The “a-n” is for “Avid Netizen.” The .pw TLD is actually the international TLD for the Pacific island country Palau, but it has been rebranded as “professional web.”
NameCheap registers .pw domains for $3.98.
Step 2: Set up hosting
I set up my link shortener on a VPS hosted by Digital Ocean. I already had this VPS, so that cost wasn’t included in the total, but if you wanted to copy me, you could get the same VPS for $5.
(Alternatively, you could get a shared hosting account through iPage for less than $2 a month.)
Once you have your hosting, just configure your host’s DNS nameservers in your registrar account.
Step 3:Install YOURLS
YOURLS is an open source link shortening web software (the name is an abbreviation of “Your Own URL Shortener”). Not only does YOURLS shorten links, it also includes a fairly sophisticated analytics package, allowing you to see how many times your short link has been clicked as well as where the clicks are coming from — both geographically and in terms of referring web pages.
The software runs on PHP and MySQL, so it should work on pretty much any host that can support WordPress, and installation is a snap. Simply download the zip file or tarball and extract it in your web root, edit the config file to match your system, set up your database, and run the install.
I recently changed my web server software from Apache to Nginx, so I had a little bit more configuration to do than normal. The most difficult part was figuring out the configuration file. Thankfully, the installation instructions linked to this article on how to do it. This is how the nginx configuration should look:
server { listen 178.33.177.161:80; server_name yourdomainna.me; access_log off; error_log /var/log/nginx/yourdomainna.me.error.log; root /var/www/yourdomainna.me.webroot; location / { try_files $uri $uri/ /yourls-loader.php; location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } }
I set up that configuration file, but due to an issue with php-fpm, I was still getting 502 errors. If you run into the same problem, this article should sort you out.
Short Links and You
Now that I have my own link shortener set up, I’m still pondering whether to make it publicly available or not.
With any kind of public service there’s a risk of being overrun by spammers. If this goes on too long, there’s a good chance your ISP will get mad and shut you down. There are plugins to mitigate the risk, but it’s still something anyone considering this should think about.
The good news is that even if I don’t open my service, now you know how to make your own.
If you need help with this or any other web development issue, feel free to contact me at AvidNetizen.com.
Leave a Reply