A PHP-based self-hosted URL shortener on Platform.sh

Alejandro Celaya 7c5825d1bc Removed custom AccessLogFactory by updating to zend-expressive-swoole 2.2 5 年之前
bin 2fc2ad98aa Updated config so that shlink logger dynamically uses standard output when running with swoole 5 年之前
config 7c5825d1bc Removed custom AccessLogFactory by updating to zend-expressive-swoole 2.2 5 年之前
data 743d052f55 Documented how to serve shlink using swoole 5 年之前
docs 4d2684be52 Updated swagger docs for visits including everything related to pagination 5 年之前
module 7c5825d1bc Removed custom AccessLogFactory by updating to zend-expressive-swoole 2.2 5 年之前
public 2fc2ad98aa Updated config so that shlink logger dynamically uses standard output when running with swoole 5 年之前
.env.dist 80d8c32881 Removed rest auth env vars 7 年之前
.gitattributes 79427d08d7 Added phpstan config file to export-ignore list 6 年之前
.gitignore bbe85cde31 Migrated to GeoLite2 for IP location resolution 5 年之前
.phpstorm.meta.php 2368b634e3 Moved command and app creation logic to a factory for install scripts 6 年之前
.scrutinizer.yml 6b9f9f0f44 Added scrutinizer config to enforce using the new environment 5 年之前
.travis.yml cfaecd93e4 Added swoole extension to travis 5 年之前
CHANGELOG.md 3faf6e967f Updated changelog adding v1.15 5 年之前
LICENSE 37502151ef Updated date in license 5 年之前
README.md f2d39ca55a Added missing comma 5 年之前
build.sh 8fc949898b Excluded GeoLite2 db from build process 5 年之前
composer.json 7c5825d1bc Removed custom AccessLogFactory by updating to zend-expressive-swoole 2.2 5 年之前
docker-compose.override.yml.dist 16590b2dbb Prepared project to support both swoole and regular app servers with fast cgi 5 年之前
docker-compose.yml 16590b2dbb Prepared project to support both swoole and regular app servers with fast cgi 5 年之前
func_tests_bootstrap.php 91442a3379 Ensured same testing database is used to generate with entities and to run functional tests 6 年之前
indocker 7c5825d1bc Removed custom AccessLogFactory by updating to zend-expressive-swoole 2.2 5 年之前
infection.json a705ef21a9 Increased MSI to 61% 5 年之前
migrations.yml 26fd61a3ed Created migrations to rename camel case columns to snake case 5 年之前
phpcs.xml c4afc7a923 Updated project to use external coding standard 5 年之前
phpstan.neon 4745a37549 Used a lower level on phpstan to avoid errors produced by Symfony 4.1.5 new phpdocs 5 年之前
phpunit-func.xml 84094a51a2 Implemented EditShortCodeAction 6 年之前
phpunit.xml.dist fa595f7aa3 Fixed non-existing keys not being set with default values in imported config 5 年之前

README.md

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.

Installation

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

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

Then, you will need a built version of the project. There are a few ways to get it.

  • Using a dist file

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

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

    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 generated dist file to it.

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

  • If you are going to use MySQL 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.
  • Expose shlink 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.1-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:

      Important! Swoole support is still experimental. Use it with care, and report any found issue.

      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. Just run ./vendor/bin/zend-expressive-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/zend-expressive-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).

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

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

Bonus

There are a couple of time-consuming tasks that shlink expects you to do manually, or at least it is recommended, since it will improve runtime performance.

Those tasks can be performed using shlink's CLI, so it should be easy to schedule them to be run in the background (for example, using cron jobs):

  • Resolve IP address locations: /path/to/shlink/bin/cli visit:process

    If you don't run this command regularly, the stats will say all visits come from unknown locations.

  • Update IP geolocation database: /path/to/shlink/bin/cli visit:update-db

    When shlink is installed it downloads a fresh GeoLite2 db file. Running this command will update this file.

    The file is updated the first Tuesday of every month, so it should be enough running this command the first Wednesday.

  • Generate website previews: /path/to/shlink/bin/cli short-url:process-previews

    Running this will improve the performance of the doma.in/abc123/preview URLs, which return a preview of the site.

Any of those commands accept the -q flag, which makes it not display any output. This is recommended when configuring the commands as cron jobs.

In future versions, it is planed that, when using swoole to serve shlink, some of these tasks are automatically run without blocking the request and also, without having to configure cron jobs. Probably resolving IP locations and generating previews.

Update to new version

When a new Shlink version is available, you don't need to repeat the entire process yourself. 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 directories 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.

The script will ask you for the location from previous shlink version, and use it in order to import the configuration. It will then update the database and generate some the assets neccessary for Shlink to function.

Right now, it does not import cached info (like website previews), but it will. For now you will need to regenerate them again.

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

Using a docker image

Currently there's no official docker image, but there's a work in progress alpha version you can find here.

The idea will be that you can just generate a container using the image and provide predefined config files via volumes or CLI arguments, so that you get shlink up and running.

Currently the image does not expose an entry point which let's you interact with shlink's CLI interface, nor allows configuration to be passed.

Using shlink

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

    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.

Shlink CLI Help

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.
 config
  config:generate-charset     Generates a character set sample just by shuffling the default one, "123456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ". Then it can be set in the SHORTCODE_CHARS environment variable
  config:generate-secret      Generates a random secret string that can be used for JWT token encryption
 short-url
  short-url:delete            [short-code:delete] Deletes a short URL
  short-url:generate          [shortcode:generate|short-code:generate] Generates a short URL for provided long URL and returns it
  short-url:list              [shortcode:list|short-code:list] List all short URLs
  short-url:parse             [shortcode:parse|short-code:parse] Returns the long URL behind a short code
  short-url:process-previews  [shortcode:process-previews|short-code:process-previews] Processes and generates the previews for every URL, improving performance for later web requests.
  short-url:visits            [shortcode:visits|short-code: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:process               Processes visits where location is not set yet
  visit:update-db             Updates the GeoLite2 database file used to geolocate IP addresses

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