Install Your Own URL Shortener

A while back I acquired domain, zod.im with intent of setting up my own shortened URL service. While there is nothing wrong with the current offerings by bit.ly or TinyURL, I was academically intrigued by the notion of setting this up.

YOURLS

I went with open source package YOURLS. It was pretty easy to setup and has a nice interface. The documentation didn’t provide me everything I needed, but what it didn’t provide was easily discovered. Examples:

  • I don’t install MySQL databases on a daily, weekly, or monthly basis, so I had to go look that back up since the commands weren’t included directly in the setup documentation.
  • I front nginx as the web server and chose to look up changes to nginx configuration as the yourls installer provides just content for .htaccess
  • I use WordTwit for notifications of posts on this blog to send to Twitter. The current version did not have yourls support, but looks like the next Pro version might have more pluggable solution. So made updates to plugin to use this now.

Installation

Pre-Requisites

  • This assumes the starting point of installation already has nginx installed with fastcgi able to interpret php.
  • This also assumes MySQL is installed and operational.

MySQL

  1. Create the database

  2. mysql> CREATE DATABASE yourlsdb;

  3. Create the user & Grant

  4. mysql> CREATE USER 'yourlsdbuser'@'localhost' IDENTIFIED BY 'password';

    mysql> GRANT ALL PRIVILEGES ON yourlsdb.* TO yourlsdbuser@localhost;

Install YOURLS

  1. Download the code
  2. Check here for the latest version.

    $ wget http://code.google.com/p/yourls/downloads/detail?name=yourls-1.5.zip

  3. Unzip the code in directory you want as root

  4. $ sudo mkdir -p /var/www/zod.im/web
    $ cd /var/www/zod.im/web/
    $ unzip /yourls-1.5.zip

  5. Update the configuration
  6. The following is the minimum settings that need to be changed for the private linking to work. Read through the configuration for other changes to consider.

    1. Copy the sample configuration

    2. $ cp includes/config-sample.php user/config.php

    3. Update the database settings
    4. Use vi or whatever your favorite editor is to edit the new file and follow the information in the file.
      Change the database settings.

      ...
      define( 'YOURLS_DB_USER', 'dbuser' );

      /** MySQL database password */
      define( 'YOURLS_DB_PASS', 'dbpassword' );

      /** The name of the database for YOURLS */
      define( 'YOURLS_DB_NAME', 'yourls' );

      /** MySQL hostname */
      define( 'YOURLS_DB_HOST', 'localhost' );
      ...

    5. Update the base site URL

    6. /** YOURLS installation URL, no trailing slash */
      define( 'YOURLS_SITE', 'http://example.com' );

    7. Update the cookie hash
    8. As the comment shows go here to get a new random hash.

      /** A random secret hash used to encrypt cookies. You don't have to remember it, make it long and complicated. Hint: copy from http://yourls.org/cookie **/
      define( 'YOURLS_COOKIEKEY', 'qQ4KhL_pu|s@Zm7n#%:b^{A[vhm' );

    9. Update the username and password
    10. The username and passwords are in the configuration file with password in clear text. Since this isn’t secure, I recommend not using a password that works anywhere else.

      /** Username(s) and password(s) allowed to access the site */
      $yourls_user_passwords = array(
      'username' => 'password',
      'username2' => 'password2' // You can have one or more 'login'=>'password' lines
      );

Setup Nginx for Rewrite

Edit the nginx configuration for the domain. The configuration of YOURLS provides an .htaccess file with rewrite rules which is fine for Apache, but not so well with nginx.

server {
server_name www.zod.im www.zodim.com;
rewrite ^(.*) http://zod.im$1 permanent;
}

server {
listen 80;
server_name zod.im;

access_log /var/log/nginx/zod.im.access.log;
error_log /var/log/nginx/zod.im.error.log;

root /var/www/zod.im/web/;
index index.php index.html;

error_page 404 /404.php;

location / {
# If the file exists as a static file serve it directly without
# running all the other rewite tests on it
if (-f $request_filename) {
expires max;
break;
}

if (!-f $request_filename){
set $rule_0 1$rule_0;
}
if (!-d $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
rewrite ^/.* /yourls-loader.php last;
}
}
location ~ .php$ {
set $script $uri;
set $path_info "";

if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}

include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:5555;
fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /var/www/zod.im/web$fastcgi_script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_intercept_errors on;

root /var/www/zod.im/web;
}

Restart nginx

Go through setup

Open browser and go to the admin page, login and go through initial setup process.

http://example.com/admin/

You get two screens to see. First is initial welcome with button to install. Click this and get the second telling you it has created the database tables.

The second screen may complain about not being able to write the .htaccess file, but since nginx is used, that can easily be ignored.

Conclusion

The process to create your own personal shortening service can take a little as little as 15 minutes with YOURLS. It will probably take longer to figure out your short domain name and acquire it.