The definitive self-hosted URL shortener
Find a file
2020-01-07 18:15:17 +01:00
.github Added github funding 2019-10-22 19:37:35 +02:00
bin Updated to installer v4 2020-01-04 13:18:28 +01:00
config Enabled redis config option 2020-01-05 17:04:22 +01:00
data Replaced use of deprecated class by a non-deprecated one 2020-01-06 23:08:14 +01:00
docker Updated config to read redis from config.redis 2020-01-05 16:45:14 +01:00
docs/swagger Changed latitude and longitude to float 2019-12-31 23:37:49 +01:00
hooks Updated docker image build script so that it sets shlink version to 'latest' when source branch is develop 2019-12-09 18:16:57 +01:00
module Refactored Rest ConfigProvider so that it appends the health action with and without version 2020-01-07 18:07:51 +01:00
public Moved common bootstrapping code to run.php script 2019-12-27 14:02:43 +01:00
.dockerignore Updated docker image build so that it sets shlink's version 2019-08-16 19:42:39 +02:00
.gitattributes Deleted everything related with previews generation 2019-12-31 13:40:38 +01:00
.gitignore Added dev php config and removed .env dependencies 2019-12-17 21:20:24 +01:00
.phpstorm.meta.php Project migrated from zend to laminas 2020-01-01 21:13:09 +01:00
.scrutinizer.yml Increased scrutinizer timeout while waiting for code coverage, from 5 min to 10 min 2019-02-17 10:12:13 +01:00
.travis.yml Updated more references to now unsupported PHP versions 2019-12-29 16:09:05 +01:00
build.sh Simplified build script to exclude everything from dockerignore for rsync 2019-08-16 19:30:40 +02:00
CHANGELOG.md Updated changelog 2020-01-07 18:10:09 +01:00
composer.json Updated to installer v4 2020-01-04 13:18:28 +01:00
docker-compose.override.yml.dist Added mariadb container for development environment 2019-10-06 11:21:41 +02:00
docker-compose.yml Added dev php config and removed .env dependencies 2019-12-17 21:20:24 +01:00
Dockerfile Updated base image for all docerfiles to use PHP 7.4.1 2019-12-29 16:34:43 +01:00
indocker Removed custom AccessLogFactory by updating to zend-expressive-swoole 2.2 2018-12-05 21:26:19 +01:00
infection.json Increased MSI to 61% 2018-11-17 17:36:22 +01:00
LICENSE Created upgrade doc explaining how to upgrade from Shlinnk 1.x to 2.x 2020-01-01 11:17:44 +01:00
migrations.php Migrated migrations config file from yaml to plain PHP 2019-12-06 09:50:37 +01:00
phpcs.xml Added all docker stuff to the project 2019-08-16 18:38:26 +02:00
phpstan.neon Refactored and fixed unit tests 2019-11-26 22:12:52 +01:00
phpunit-api.xml Improved NotifyVisitToWebHooksTest to kill more mutants 2019-12-28 10:43:13 +01:00
phpunit-db.xml Improved NotifyVisitToWebHooksTest to kill more mutants 2019-12-28 10:43:13 +01:00
phpunit.xml.dist Deleted everything related with previews generation 2019-12-31 13:40:38 +01:00
README.md Project migrated from zend to laminas 2020-01-01 21:13:09 +01:00
UPGRADE.md Project migrated from zend to laminas 2020-01-01 21:13:09 +01:00

Shlink

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version License Paypal donate

A PHP-based self-hosted URL shortener that can be used to serve shortened URLs under your own custom domain.

This document references Shlink 2.x. If you are using an older version and want to upgrade, follow the UPGRADE doc.

Table of Contents

Installation

These are the steps needed to install Shlink if you plan to manually host it.

Alternatively, you can use the official docker image. If that's your intention, jump directly to Using a docker image

First, make sure the host where you are going to run shlink fulfills these requirements:

  • PHP 7.4 or greater with JSON, APCu, intl, curl, PDO and gd extensions enabled.
  • MySQL, MariaDB, PostgreSQL or SQLite.
  • The web server of your choice with PHP integration (Apache or Nginx recommended).

Download

In order to run Shlink, you will need a built version of the project. There are two ways to get it.

  • Using a dist file

    The easiest way to install shlink is by using one of the pre-bundled distributable packages.

    Go to the latest version and download the shlink_x.x.x_dist.zip file you will find there.

    Finally, decompress the file in the location of your choice.

  • Building from sources

    If for any reason you want to build the project yourself, follow these steps:

    • Clone the project with git (git clone https://github.com/shlinkio/shlink.git), or download it by clicking the Clone or download green button.
    • Download the Composer PHP package manager inside the project folder.
    • Run ./build.sh 1.0.0, replacing the version with the version number you are going to build (the version number is only used for the generated dist file).

    After that, you will have a shlink_x.x.x_dist.zip dist file inside the build directory, that you need to decompress in the location fo your choice.

    This is the process used when releasing new shlink versions. After tagging the new version with git, the Github release is automatically created by travis, attaching the generated dist file to it.

Configure

Despite how you built the project, you now need to configure it, by following these steps:

  • If you are going to use MySQL, MariaDB or PostgreSQL, create an empty database with the name of your choice.
  • Recursively grant write permissions to the data directory. Shlink uses it to cache some information.
  • Setup the application by running the bin/install script. It is a command line tool that will guide you through the installation process. Take into account that this tool has to be run directly on the server where you plan to host Shlink. Do not run it before uploading/moving it there.
  • Generate your first API key by running bin/cli api-key:generate. You will need the key in order to interact with shlink's API.

Serve

Once Shlink is configured, you need to expose it to the web, either by using a traditional web server + fast CGI approach, or by using a swoole non-blocking server.

  • Using a web server:

    For example, assuming your domain is doma.in and shlink is in the /path/to/shlink folder, these would be the basic configurations for Nginx and Apache.

    Nginx:

    server {
        server_name doma.in;
        listen 80;
        root /path/to/shlink/public;
        index index.php;
        charset utf-8;
    
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
    

    Apache:

    <VirtualHost *:80>
        ServerName doma.in
        DocumentRoot "/path/to/shlink/public"
    
        <Directory "/path/to/shlink/public">
            Options FollowSymLinks Includes ExecCGI
            AllowOverride all
            Order allow,deny
            Allow from all
        </Directory>
    </VirtualHost>
    
  • Using swoole:

    First you need to install the swoole PHP extension with pecl, pecl install swoole.

    Once installed, it's actually pretty easy to get shlink up and running with swoole. Run ./vendor/bin/mezzio-swoole start -d and you will get shlink running on port 8080.

    However, by doing it this way, you are loosing all the access logs, and the service won't be automatically run if the server has to be restarted.

    For that reason, you should create a daemon script, in /etc/init.d/shlink_swoole, like this one, replacing /path/to/shlink by the path to your shlink installation:

    #!/bin/bash
    ### BEGIN INIT INFO
    # Provides:          shlink_swoole
    # Required-Start:    $local_fs $network $named $time $syslog
    # Required-Stop:     $local_fs $network $named $time $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Description:       Shlink non-blocking server with swoole
    ### END INIT INFO
    
    SCRIPT=/path/to/shlink/vendor/bin/mezzio-swoole\ start
    RUNAS=root
    
    PIDFILE=/var/run/shlink_swoole.pid
    LOGDIR=/var/log/shlink
    LOGFILE=${LOGDIR}/shlink_swoole.log
    
    start() {
      if [[ -f "$PIDFILE" ]] && kill -0 $(cat "$PIDFILE"); then
        echo 'Shlink with swoole already running' >&2
        return 1
      fi
      echo 'Starting shlink with swoole' >&2
      mkdir -p "$LOGDIR"
      touch "$LOGFILE"
      local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
      su -c "$CMD" $RUNAS > "$PIDFILE"
      echo 'Shlink started' >&2
    }
    
    stop() {
      if [[ ! -f "$PIDFILE" ]] || ! kill -0 $(cat "$PIDFILE"); then
        echo 'Shlink with swoole not running' >&2
        return 1
      fi
      echo 'Stopping shlink with swoole' >&2
      kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
      echo 'Shlink stopped' >&2
    }
    
    case "$1" in
      start)
        start
        ;;
      stop)
        stop
        ;;
      restart)
        stop
        start
        ;;
      *)
        echo "Usage: $0 {start|stop|restart}"
    esac
    

    Then run these commands to enable the service and start it:

    • sudo chmod +x /etc/init.d/shlink_swoole
    • sudo update-rc.d shlink_swoole defaults
    • sudo update-rc.d shlink_swoole enable
    • /etc/init.d/shlink_swoole start

    Now again, you can access shlink on port 8080, but this time the service will be automatically run at system start-up, and all access logs will be written in /var/log/shlink/shlink_swoole.log (you will probably want to rotate those logs. You can find an example logrotate config file here).

Finally access to https://app.shlink.io and configure your server to start creating short URLs.

Bonus

Geo-locating visits to your short links is a time-consuming task. When serving Shlink with swoole, the geo-location task is automatically run asynchronously just after a visit to a short URL happens.

However, if you are not serving Shlink with swoole, you will have to schedule the geo-location task to be run regularly in the background (for example, using cron jobs):

The command you need to run is /path/to/shlink/bin/cli visit:locate, and you can optionally provide the -q flag to remove any output and avoid your cron logs to be polluted.

Update to new version

When a new Shlink version is available, you don't need to repeat the entire process. Instead, follow these steps:

  1. Rename your existing Shlink directory to something else (ie. shlink ---> shlink-old).
  2. Download and extract the new version of Shlink, and set the directory name to that of the old version (ie. shlink).
  3. Run the bin/update script in the new version's directory to migrate your configuration over. You will be asked to provide the path to the old instance (ie. shlink-old).
  4. If you are using shlink with swoole, restart the service by running /etc/init.d/shlink_swoole restart.

The bin/update will use the location from previous shlink version to import the configuration. It will then update the database and generate some assets shlink needs to work.

Important! It is recommended that you don't skip any version when using this process. The update tool gets better on every version, but older versions might make assumptions.

Using a docker image

Starting with version 1.15.0, an official docker image is provided. You can learn how to use it by reading the docs.

The idea is that you can just generate a container using the image and provide custom config via env vars.

Once shlink is installed, there are two main ways to interact with it:

  • The command line. Try running bin/cli and see all the available commands.

    All of those commands can be run with the --help/-h flag in order to see how to use them and all the available options.

    It is probably a good idea to symlink the CLI entry point (bin/cli) to somewhere in your path, so that you can run shlink from any directory.

  • The REST API. The complete docs on how to use the API can be found here, and a sandbox which also documents every endpoint can be found in the API Spec portal.

    However, you probably don't want to consume the raw API yourself. That's why a nice web client is provided that can be directly used from https://app.shlink.io, or you can host it yourself too.

Both the API and CLI allow you to do the same operations, except for API key management, which can be done from the command line interface only.

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  help                Displays help for a command
  list                Lists commands
 api-key
  api-key:disable     Disables an API key.
  api-key:generate    Generates a new valid API key.
  api-key:list        Lists all the available API keys.
 db
  db:create           Creates the database needed for shlink to work. It will do nothing if the database already exists
  db:migrate          Runs database migrations, which will ensure the shlink database is up to date.
 short-url
  short-url:delete    Deletes a short URL
  short-url:generate  Generates a short URL for provided long URL and returns it
  short-url:list      List all short URLs
  short-url:parse     Returns the long URL behind a short code
  short-url:visits    Returns the detailed visits information for provided short code
 tag
  tag:create          Creates one or more tags.
  tag:delete          Deletes one or more tags.
  tag:list            Lists existing tags.
  tag:rename          Renames one existing tag.
 visit
  visit:locate        Resolves visits origin locations.

This product includes GeoLite2 data created by MaxMind, available from https://www.maxmind.com