Shlink is an open-source self-hosted URL shortener, which allows you to shorten URLs and serve them under your own short domain. Using your own URL shortner service instead of third-party service like bit.ly can increase brand awareness. This tutorial will be showing you how to install Shlink on Ubuntu 18.04 with Apache or Nginx web server.

Shlink Features

  • Visit stats: Track all the visits to your short URLs, including stats like location, browser or referrer.
  • Email tracking: Generate a 1px transparent image that can be used to track emails.
  • Third party integrations: Easily make third party tools use shlink to shorten URLs by using a single-request API endpoint.
  • Custom Slugs: Make your shortened URLs use a custom slug to easily identify campaigns.
  • QR codes: Generate QR codes on the fly pointing to your short URLs
  • Previews: Get previews in image format for any short URL
  • Tags: Tag your short URLs and classify them for later analytics
  • Limited access: Limit access to short URLs, by date range and/or maximum number of visits.

Prerequisites of installing Shlink on Ubuntu 18.04 Server

Shlink is written in PHP and relies on MySQL/MariaDB or PostgreSQL database server, so you need to set up a LAMP stack or LEMP stack. If you prefer Apache web server, then set up LAMP stack.

If you prefer Nginx web server, then set up LEMP stack.

You also need a domain name. I registered my domain name from NameCheap because the price is low and they give whois privacy protection for free. In this tutorial I use my lnux.be domain name as example. Without further ado, let’s install Shlink on Ubuntu 18.04 server.

Step 1: Download Shlink onto Your Ubuntu 18.04 Server

Go to the Shlink Github page to see the latest stable version. You can download the latest stable version (1.18.1) by executing the following command on your server.

wget https://github.com/shlinkio/shlink/releases/download/v1.18.1/shlink_1.18.1_dist.zip

Note: If a new version comes out, simply replace 1.18.1 with the new version number.

The file will be saved as shlink_1.18.1_dist.zip. Use unzip command to unzip it to /var/www/ directory.

sudo apt install unzip

sudo unzip shlink_1.18.1_dist.zip -d /var/www/

Now the files are stored under /var/www/shlink_1.18.1_dist/, we rename it.

sudo mv /var/www/shlink_1.18.1_dist /var/www/shlink

Then make the web server user (www-data) as the owner of this directory.

sudo chown -R www-data:www-data /var/www/shlink/

Step 2: Create a MariaDB Database and User for Shlink

Now we need to log in to MariaDB console and create a database and user for Shlink. By default, the MaraiDB package on Ubuntu uses unix_socket to authenticate user login, which basically means you can use username and password of the OS to log into MariaDB console. So you can run the following command to login without providing MariaDB root password.

sudo mysql -u root

Next,create a new database for Shlink using the following command. This tutorial names it shlink, you can use whatever name you like for the database.

CREATE DATABASE shlink;

The following command will create a database user and password, and at the same time grant all permission of the new database to the new user so later on Shlink can write to the database. Replace red texts with your preferred database name, username and password.

GRANT ALL ON shlink.* TO 'shlink'@'localhost' IDENTIFIED BY 'password';

Flush privileges table and exit MariaDB console.

FLUSH PRIVILEGES;

EXIT;

Step 3: Run the Shlink Install Script

Go to the /var/www/shlink/bin/ directory.

cd /var/www/shlink/bin/

There’s a PHP script named install, we run the script as the www-data user.

sudo -u www-data php ./install

Then the setup wizard will ask you to enter database details. So I choose MySQL as the database type, then enter database name, user and password. The host is localhost and port is 3306.

Next, select the scheme type (https) and hostname for your URL shortner.

Then slect the language and configure the application. I simply press Enter to use the default settings.

Step 4: Create Apache Virtual Host or Nginx Config File for Shlink

Apache

If you use Apache web server, create a virtual host for Shlink.

sudo nano /etc/apache2/sites-available/shlink.conf

Put the following text into the file. Replace lnux.be with your real domain name and don’t forget to set DNS A record for it.


  ServerName lnux.be
  DocumentRoot /var/www/shlink/public

  ErrorLog ${APACHE_LOG_DIR}/shlink_error.log
  CustomLog ${APACHE_LOG_DIR}/shlink_access.log combined

  
    Options FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    allow from all
  

Save and close the file. Then enable this virtual host with:

sudo a2ensite shlink.conf

Reload Apache for the changes to take effect.

sudo systemctl reload apache2

Nginx

If you use Nginx web server, create a virtual host for Shlink.

sudo nano /etc/nginx/conf.d/shlink.conf

Put the following text into the file. Replace lnux.be with your real domain name and don’t forget to set DNS A record for it.

server {
   listen 80;
   listen [::]:80;
   server_name lnux.be;

   root /var/www/shlink/public;
   error_log /var/log/nginx/shlink.error;
   access_log /var/log/nginx/shlink.access;

   index index.php index.html index.htm index.nginx-debian.html;

   location / {
     # try to serve file directly, fallback to app.php
     try_files $uri /index.php$is_args$args;
   }

   # redirect some entire folders
     rewrite ^/(vendor|translations|build)/.* /index.php break;

   location ~ .php$ {
     fastcgi_split_path_info ^(. .php)(/. )$;
     fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
     fastcgi_index index.php;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include fastcgi_params;
   }

}

Save and close the file. Then test Nginx configuration.

sudo nginx -t

If the test is successful, reload Nginx for the changes to take effect.

sudo systemctl reload nginx

Step 5: Install Required and Recommended PHP Modules.

Run the following command to install PHP modules required or recommended by Shlink

sudo apt install php-apcu php7.2-common php7.2-gd php7.2-json php7.2-curl php7.2-intl

Then restart Apache. (If you use Nginx, you don’t need to restart Nginx.)

sudo systemctl restart apache2

Step 6: Enabling HTTPS

To encrypt the HTTP traffic, we can enable HTTPS by installing a free TLS certificate issued from Let’s Encrypt. Run the following command to install Let’s Encrypt client (certbot) on Ubuntu 18.04 server.

sudo apt install certbot

If you use Apache, install the Certbot Apache plugin.

sudo apt install python3-certbot-apache

And run this command to obtain and install TLS certificate.

sudo certbot --apache --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d lnux.be

If you use Nginx, then you also need to install the Certbot Nginx plugin.

sudo apt install python3-certbot-nginx

Next, run the following command to obtain and install TLS certificate.

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email [email protected] -d lnux.be

Where

  • --nginx: Use the nginx plugin.
  • --apache: Use the Apache plugin.
  • --agree-tos: Agree to terms of service.
  • --redirect: Force HTTPS by 301 redirect.
  • --hsts: Add the Strict-Transport-Security header to every HTTP response. Forcing browser to always use TLS for the domain. Defends against SSL/TLS Stripping.
  • --staple-ocsp: Enables OCSP Stapling. A valid OCSP response is stapled to the certificate that the server offers during TLS.

The certificate should now be obtained and automatically installed.

Step 7: Creating Short Links

First, you need to create an API key with the following command.

sudo -u www-data php /var/www/shlink/bin/cli api-key:generate

Then go to https://app.shlink.io/ to add your server.

Once you add your server, you can create short links.

Note that this is just a web client. Short URLs are stored on your own server.

You can also generate short URLs from command line on your server.

sudo -u www-data /var/www/shlink/bin/cli short-url:generate

List short URLs.

sudo -u www-data /var/www/shlink/bin/cli short-url:list

Run the following command to see the help message.

sudo -u www-data php /var/www/shlink/bin/cli

Wrapping Up

I hope this tutorial helped you install Shlink on Ubuntu 18.04 server. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂

Rate this tutorial

[Total: 2 Average: 5]