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 <path to>/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.

1 comment to Install Your Own URL Shortener

  • Jo

    Hi!

    On debian squeeze I had to install
    2011-10-03 15:25:45 install libnet-daemon-perl 0.43-1
    2011-10-03 15:25:45 install libplrpc-perl 0.2020-2
    2011-10-03 15:25:45 install mysql-common 5.1.49-3
    2011-10-03 15:25:46 install libdbi-perl 1.612-1
    2011-10-03 15:25:47 install libmysqlclient16 5.1.49-3
    2011-10-03 15:25:48 install libdbd-mysql-perl 4.016-1
    2011-10-03 15:25:48 install mysql-client-5.1 5.1.49-3
    2011-10-03 15:25:50 install mysql-server-core-5.1 5.1.49-3
    2011-10-03 15:25:57 install mysql-server-5.1 5.1.49-3
    2011-10-03 15:26:00 install libhtml-template-perl 2.9-2
    2011-10-03 15:26:01 install mysql-server 5.1.49-3
    2011-10-03 15:26:01 install php5 5.3.3-7+squeeze3
    2011-10-03 15:39:42 install unzip 6.0-4
    2011-10-03 15:40:07 install zip 3.0-3
    2011-10-04 09:49:35 install php5-mysql 5.3.3-7+squeeze3
    2011-10-04 09:58:08 install nginx 0.7.67-3
    2011-10-04 10:10:07 install libcurl3 7.21.0-2
    2011-10-04 10:10:07 install libssh2-1 1.2.6-1
    2011-10-04 10:10:08 install php5-curl 5.3.3-7+squeeze3
    2011-10-04 10:17:51 install comerr-dev 2.1-1.41.12-4stable1
    2011-10-04 10:17:51 install curl 7.21.0-2
    2011-10-04 10:17:51 install krb5-multidev 1.8.3+dfsg-4squeeze1
    2011-10-04 10:17:51 install zlib1g-dev 1:1.2.3.4.dfsg-3
    2011-10-04 10:17:52 install libssl-dev 0.9.8o-4squeeze2
    2011-10-04 10:17:53 install libidn11-dev 1.15-2
    2011-10-04 10:17:53 install libkrb5-dev 1.8.3+dfsg-4squeeze1
    2011-10-04 10:17:53 install libssh2-1-dev 1.2.6-1
    2011-10-04 10:17:53 install pkg-config 0.25-1.1
    2011-10-04 10:17:54 install libldap2-dev 2.4.23-7.2
    2011-10-04 10:17:56 install libcurl4-openssl-dev 7.21.0-2

    and also define mysql.so by find / -name “mysql.so” and specifying it in /etc/php5/conf.d/mysql.ini
    or I got “ezSQL_mysql requires mySQL Lib to be compiled and or linked in to the PHP engine”

    PS I use apache instead of ngix.. is there a good reason to choose ngix?

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>