How to create your own link shortener

25 Aug 2015

Have you ever wondered how to create your own link shortener, so you don't have to use sites like Bitly? Even if you haven't, I'm going to tell you how to create one anyway!

Firstly, you will need your own domain name, and a web server running Apache and MySQL.

  1. Open up Terminal and browse to the directory you would like the link shortener to be in.
  2. Run the command below (If it says you do not have git installed, run 'sudo apt-get install git' first) this will download the setup files.
    git clone https://github.com/YOURLS/YOURLS.git
  3. Before we start the setup, we need to create the database and user account in MySQL. Run the command below followed by the admin password.
    'mysql -r root -p'
  4. Once you have done that, run these commands one at a time (including the semicolon at the end!):
    create database yourls;

    Where it says PASSWORD, please change that to something of your choosing.

    GRANT ALL on yourls.* TO 'yourls'@'localhost' identified by 'PASSWORD';
    flush privileges;
  5. This command will create the config file.
    cp YOURLS/user/config-sample.php YOURLS/user/config.php
  6. Now to edit the config file, type:
    nano YOURLS/user/config.php
  7. Edit
    'your db user name'

    by navigating there using the keyboard and replace it with:

    'yourls'

    as thats the name of the user you created above. Please do the same with the password on the line below.

  8. Further down, under 'Site options'  you will see a heading called YOURS Installation URL. Please enter your domain name followed by the location you installed the config files.
  9. Keep scrolling down, and you will see a line mentioning YOURLS_COOKIEKEY'. Please visit http://yourls.org/cookie and replace that line with the text from that webpage.
  10. Next, under 'Usernames and Passwords' you will see a line similar to below:
    'username' => 'password',

    Please replace username and password with credentials you would like to use to login to the site.

  11. Now press Ctrl + X to exit, then press Y to save the changes.
  12. Finally, navigate to http://yourdomain.com/admin/ and it should prompt you to install YOURLS!
  13. Login with the credentials you entered above, and start creating short links.

If you experience a permissions error, you may need to run the below command to change the ownership of the files:

chmod -R www-data:www-data yourlslocation

Where 'yourlslocation' is the folder where you setup YOURLS.

If it says you need to make changes to the '.htaccess' file, please navigate back to the installation location and run the following command to create the file:

nano .htaccess

You will then need to paste the following text into the file:

# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /s/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /s/yourls-loader.php [L]
</IfModule>
# END YOURLS

In the above example, I have installed my link shortener at https://lew.im/s/ so please replace that with your installation location. If you installed it in the root directory, replace the /s/ with just a /.

Once everything is up and running you can create links like this: https://lew.im/s/z9sd. If you want to view stats, just add a plus to the end, like so: https://lew.im/s/z9sd+.

Please let me know in the comments or on Twitter (@LewisLebentz) if you need any hep!

Lewis Lebentz