Self-hosted URL Shortener with Shlink 🌱

What is Shlink?

A PHP-based self-hosted URL shortener that can be used to serve shortened URLs under your own custom domain. -https://github.com/shlinkio/shlink

Installing Shlink

  1. Log into the Linux device
  2. Run the following commands in a terminal:
    # update software repositories
    sudo apt update
    # install software updates
    sudo apt upgrade -y
    # install pre-requisites
    sudo apt install lsb-release apt-transport-https ca-certificates -y
    # add php gpg key
    sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
    # add php apt repository
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
    # update software repositories
    sudo apt update
    # install Apache HTTPD and MySQL
    sudo apt-get install apache2 mariadb-server mariadb-client -y
    # install PHP components
    sudo apt install php8.0 libapache2-mod-php8.0 php8.0-curl php8.0-intl php8.0-gd php8.0-pdo php8.0-fpm php8.0-common php8.0-mysql php8.0-xml php8.0-apcu -y
    # enable/disable apache2 modules
    sudo a2dismod mpm_prefork php8.0
    sudo a2enmod mpm_event rewrite proxy_fcgi setenvif php8.0
    sudo a2enconf php8.0-fpm
    # configure the MySQL database
    sudo su
    mysql_secure_installation
  3. Press Enter to login as root
  4. Type Y and press Enter to set a root password, type the password twice to confirm
  5. Type Y and press Enter to remove anonymous users
  6. Type Y and press Enter to disallow root login remotely
  7. Type Y and press Enter to remove the test database
  8. Type Y and press Enter to reload privilege tables
  9. Run the following command to login into MySQL:
    mysql -u root -p
  10. Authenticate with the root password set earlier
  11. Run the following commands to create the ShlinkIO database and database user
    # NOTE: update the password with a secure password
    CREATE DATABASE shlinkio;
    GRANT ALL ON shlinkio.* to 'shlinkio_rw'@'localhost' IDENTIFIED BY '$hlink10!';
    FLUSH PRIVILEGES;
    EXIT;
    exit
  12. Continue with the following commands to download and extract shlinkio
    # download latest shlink.io version
    sudo wget https://github.com/shlinkio/shlink/releases/download/v2.6.2/shlink2.6.2_php8.0_dist.zip
    # extract the zip archive
    sudo unzip shlink2.6.2_php8.0_dist.zip -d /var/www
    # rename the extracted folder
    sudo mv /var/www/shlink* /var/www/shlinkio
    # set the owner of the new shlinkio directory to www-data
    sudo chown -R www-data:www-data /var/www/shlinkio
    # run the shlinkio installer
    sudo -u www-data php /var/www/shlinkio/bin/install
  13. Type 0 for MySQL > Press Enter
  14. Type shlinkio for the database name > Press Enter
  15. Press enter to use localhost for the database host
  16. Press enter to accept the default MySQL port
  17. Type shlinkio_rw for the database username > Press Enter
  18. Type the shlinkio_rw password created earlier > Press Enter
  19. Press Enter at the Unix socket prompt
  20. Enter the default domain name to use for shortened URLs,
  21. Type 0 for http > Press Enter
  22. Press Enter to validate long URLs
  23. Press Enter to accept the default shortened URL length
  24. Press Enter to say no to having shlink resolve page titles
  25. Press Enter to track orphan visits
  26. Press Enter at the GeoLite2 license key prompt
  27. Press Enter to anonymize report IP addresses
  28. Enter 301 for the kind of redirect > Press Enter
  29. Press Enter to accept the default redirect cache value
  30. Press Enter to accept displaying a 404 for all 3 redirects options
  31. Press Enter to leave parameter name blank
  32. Type No to enable safety check > Press Enter
  33. Type /shlinkio for the path > Press Enter
  34. Press Enter at the integrations prompt
  35. With the installation completed, run the following command to create a shlinkio.conf apache configuration
    # download shlinkio web client
    sudo wget https://github.com/shlinkio/shlink-web-client/releases/download/v3.1.0/shlink-web-client_3.1.0_dist.zip
    # extract the zip archive
    sudo unzip shlink-web-client_3.1.0_dist.zip
    # move the extracted files to the web root
    sudo mv ./shlink-web-client*/* /var/www/html
    sudo nano /etc/apache2/sites-available/shlinkio.conf
  36. Paste the following configuration into shlinkio.conf

    Alias /shlinkio "/var/www/shlinkio/public/"
    <Directory /var/www/shlinkio/public>
    Options FollowSymlinks Includes ExecCGI
    AllowOverride All
    Require all granted
    Order allow,deny
    allow from all
    </Directory>

  37. Press CTRL+O, Enter, CTRL+X to write the changes to shlinkio.conf
  38. Continue with the following commands to enable the site and restart Apache:
    # enable the shlinkio site
    sudo a2ensite shlinkio
    # restart apache2 service for the changes to take effect
    sudo systemctl restart apache2
    # generate an API key
    sudo -u www-data php /var/www/shlinkio/bin/cli api-key:generate
  39. Copy the generated API key to the clipboard
  40. Open a web browser and navigate to http://DNSorIP
  41. Click the add your first server link
  42. Enter a name for the server, the URL should be http://DNSorIP/shlinkio and paste the copied API key from earlier