Install Your Own URL Shortener

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


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.



  • 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.


  1. Create the database
    mysql> CREATE DATABASE yourlsdb;

  3. Create the user & Grant
    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

  3. Unzip the code in directory you want as root
    $ sudo mkdir -p /var/www/
    $ cd /var/www/
    $ unzip <path to>/

  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
      $ 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
      /** YOURLS installation URL, no trailing slash */
      define( 'YOURLS_SITE', '' );

    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 **/
      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 {
  rewrite ^(.*)$1 permanent;

server {
    listen   80;

    access_log /var/log/nginx/;
    error_log /var/log/nginx/;

    root   /var/www/;
    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; 

    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_index index.php;

    fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
    fastcgi_param  PATH_INFO        $path_info;
    fastcgi_intercept_errors on;

    root /var/www/;

Restart nginx

Go through setup

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

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.


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.

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=""> <s> <strike> <strong>