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
- Log into the Linux device
- 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 unzip -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 - Press Enter to login as root
- Type Y and press Enter to set a root password, type the password twice to confirm
- Type Y and press Enter to remove anonymous users
- Type Y and press Enter to disallow root login remotely
- Type Y and press Enter to remove the test database
- Type Y and press Enter to reload privilege tables
- Run the following command to login into MySQL:
mysql -u root -p
- Authenticate with the root password set earlier
- 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 - 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 - Type 1 for MariaDB > Press Enter
- Type shlinkio for the database name > Press Enter
- Press enter to use localhost for the database host
- Press enter to accept the default MySQL port
- Type shlinkio_rw for the database username > Press Enter
- Type the shlinkio_rw password created earlier ($hlink10!) > Press Enter
- Press Enter at the Unix socket prompt
- Enter the default domain name to use for shortened URLs,
- Type 0 for http > Press Enter
- Press Enter to validate long URLs
- Press Enter to accept the default shortened URL length
- Press Enter to say no to having shlink resolve page titles
- Press Enter to track orphan visits
- Press Enter at the GeoLite2 license key prompt
- Press Enter to anonymize report IP addresses
- Enter 301 for the kind of redirect > Press Enter
- Press Enter to accept the default redirect cache value
- Press Enter to accept displaying a 404 for all 3 redirects options
- Press Enter to leave parameter name blank
- Type No to enable safety check > Press Enter
- Type /shlinkio for the path > Press Enter
- Press Enter at the integrations prompt
- 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 - 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> - Press CTRL+O, Enter, CTRL+X to write the changes to shlinkio.conf
- 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 - Copy the generated API key to the clipboard
- Open a web browser and navigate to http://DNSorIP
- Click the add your first server link
- Enter a name for the server, the URL should be http://DNSorIP/shlinkio and paste the copied API key from earlier