How to Install Shlink URL Shortener on Ubuntu 20.04 Server

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 can increase brand awareness. This tutorial will be showing you how to install Shlink on Ubuntu 20.04 with Apache or Nginx web server.

install shlink on ubuntu_ self hosted url shortener

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.
  • Third-party imports: Import your existing short URLs from third parties like
  • Command-line and web interface.

Prerequisites of installing Shlink on Ubuntu 20.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 free for life. In this tutorial, I use my domain name as an example. Without further ado, let’s install Shlink on Ubuntu 20.04 server.

Step 1: Download Shlink onto Your Ubuntu 20.04 Server

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


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

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

sudo apt install unzip

sudo mkdir -p /var/www/

sudo unzip -d /var/www/

Now the files are stored under /var/www/shlink2.6.2_php8.0_dist/, we rename it to make it simpler.

sudo mv /var/www/shlink2.6.2_php8.0_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 the MariaDB console and create a database and user for Shlink. By default, the MariaDB package on Ubuntu uses unix_socket to authenticate user login, which basically means you can use the username and password of the OS to log into the 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.


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.



Step 3: Install PHP8.0 and Some Extensions

Since we downloaded the PHP 8 version of Shlink, we need to install PHP8. Ubuntu 20.04 repository includes PHP7.2. To install PHP8.0 on Ubuntu 20.04, we need to add a PPA.

sudo apt install software-properties-common

sudo add-apt-repository ppa:ondrej/php -y

Then install PHP8.0 and the extensions required by Shlink.

sudo apt install php-apcu php8.0 php8.0-fpm php8.0-mysql php8.0-gd php8.0-common php8.0-curl php8.0-intl php8.0-gmp php8.0-xml

If you use Apache web server, you need to run the following commands to make it use PHP8.0-FPM.

sudo a2dismod mpm_prefork
sudo a2enmod mpm_event proxy_fcgi setenvif
sudo a2enconf php8.0-fpm

Then restart Apache.

sudo systemctl restart apache2

Step 4: Run the Shlink Install Script

Run the PHP install script as the www-data user.

sudo -u www-data php8.0 /var/www/shlink/bin/install

The setup wizard will ask you to enter database details. So I choose MariaDB as the database type, then enter the database name, user and password. The host is localhost and the port is 3306. Shlink can also connect to the database server via Unix socket. If you use MariaDB, the socket location is /var/run/mysqld/mysqld.sock on Ubuntu server.


Next, enter the default domain for your URL shortener and select the scheme type (https). Then answer several simple questions.

shlink url shortner

If you want to analyze visitors’s geolocation, you need to use your own GeoLite2 license key, which is free. To obtain a license key, create an account at MaxMind. Maxmind will send you an email. Click the link in the email to set a password, then log in to your MaxMind account. Next, select My License Key on the left bar.

maxmind license key

Click the Generate New License Key button.

maxmind generate new license key

Give your license key a name. Then choose No, because we don’t need to use the geoipupdate program. Then click the Confirm button.

shlink geolite2 license key

Once the license key is created, copy the license key and paste it in the Shlink setup wizard. Then you can choose to anonymize visitor’s IP addresses and set the redirect type (302 or 301).

Then configure redirects for anomaly cases. When visitors hit my Shlink’s base URL (, they will be redirected to my website. You can also create a custom URL for 404 not found page.

shlink url redirects

Finally, configure the application. I simply press Enter to use the default settings.


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


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 with your real domain name and don’t forget to set DNS A record for it.

<VirtualHost *:80>
  DocumentRoot /var/www/shlink/public

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

  <Directory /var/www/shlink/public>
    Options FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    allow from all

Include /etc/apache2/conf-available/php8.0-fpm.conf


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


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 with your real domain name and don’t forget to set DNS A record for it.

server {
   listen 80;
   listen [::]:80;

   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/php8.0-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 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 20.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

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


  • --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 php8.0 /var/www/shlink/bin/cli api-key:generate

Then go to to add your server.

shlink create short link

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

shlink create short url

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 20.04 server. You may also want to learn how to use multiple versions of PHP on Ubuntu.

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: 1 Average: 5]

One Response to “How to Install Shlink URL Shortener on Ubuntu 20.04 Server

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community ( for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded.