Installing the Polr URL Shortener on Ubuntu 20.04

Polr is a versatile, free, and open-source URL shortener developed using PHP, offering a straightforward and user-friendly platform to create and manage links. Hosting your own URL shortener allows you to brand your URLs and have control over your data. Polr comes packed with features, such as a robust API, URL forwarding, customizable permissions, themes, and more.

This guide will walk you through the process of installing Polr with Apache and Let’s Encrypt SSL on Ubuntu 20.04.

Prerequisites

  • A server running Ubuntu 20.04.
  • A valid domain name pointing to your server IP.
  • A root password configured on the server.

Getting Started

First, update your system packages to the latest version by executing the following command:

apt-get update -y

Once the packages are updated, proceed to the next step.

Install Apache, MariaDB, and PHP

You’ll need to install the Apache server, MariaDB database, PHP, and additional PHP extensions on your server. Use the following command to install these packages:

apt-get install apache2 mariadb-server php libapache2-mod-php libapache2-mod-wsgi php-gd php-curl php-xml php-xmlrpc php-curl php-imagick php-mbstring php-zip php-intl -y

With all the packages installed, you can move on to creating a database for Polr.

Create a Database for Polr

Polr uses MariaDB/MySQL as its database backend. To set it up, start by creating a database and user for Polr.

Log into MariaDB with the command:

mysql

Then, execute the following commands to create a database and user:

MariaDB [(none)]> CREATE DATABASE polrdb;
MariaDB [(none)]> GRANT ALL ON polrdb.* TO 'polr' IDENTIFIED BY 'password';

Flush the privileges and exit MariaDB with the commands:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

Next, you will install Composer.

Install Composer

Composer is needed to install Polr’s PHP dependencies. Run the following command to install Composer:

curl -sS https://getcomposer.org/installer | php

Ensure Composer is installed by moving its binary to a system location:

mv composer.phar /usr/local/bin/composer

Verify the Composer installation with:

composer -V

Now, you can proceed to install Polr.

Install Polr

To install Polr, clone the latest version from the Git repository with this command:

git clone https://github.com/cydrobolt/polr.git --depth=1 /var/www/html/polr

Once downloaded, navigate to the Polr directory and install the required dependencies:

cd /var/www/html/polr
composer install --no-dev -o

Copy the sample environment file:

cp .env.setup .env

Set appropriate permissions for the Polr directory:

chown -R www-data:www-data /var/www/html/polr
chmod -R 755 /var/www/html/polr

Next, configure Apache for Polr.

Configure Apache for Polr

Create an Apache virtual host configuration file for Polr:

nano /etc/apache2/sites-available/polr.conf

Add these lines:

<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html/polr/public
ServerName polr.example.com
<Directory /var/www/html/polr/public/>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/polr-error_log
CustomLog /var/log/apache2/polr-access_log common
</VirtualHost>

Save and close the file. Enable the virtual host file and the rewrite module:

a2enmod rewrite
a2ensite polr.conf

Restart the Apache service to apply changes:

systemctl restart apache2

Check the Apache service status with:

systemctl status apache2

Access the Polr web interface by navigating to http://polr.example.com.

Database configuration

Application settings

URL shortener settings

Captcha settings

Fill in your database name, username, password, application settings, Polr URL, admin username, email, password, and click Install. Once complete, you’ll see this screen:

Setup complete

Navigate to the homepage to access the Polr dashboard:

Polr URL Shortener

Secure Polr with Let’s Encrypt Free SSL

To secure Polr, install the Certbot Let’s Encrypt client to integrate SSL certificates for your domain.

Start by installing Certbot:

apt-get install python3-certbot-apache -y

Proceed to install a Let’s Encrypt Certificate for your domain using:

certbot --apache -d polr.example.com

Provide your email address and agree to the terms of service during the process:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): admin@example.com

Follow the prompts to complete the installation, selecting option 2 to redirect HTTP traffic to HTTPS. After setup, you will see:

Congratulations! You have successfully enabled https://polr.example.com

Your Polr website is now secured with Let’s Encrypt free SSL and accessible at https://polr.example.com.

Conclusion

You have successfully installed Polr with Apache and secured it with Let’s Encrypt SSL on your Ubuntu 20.04 server. You’re now ready to host your own link shortener using Polr. Reach out with any questions!

FAQs

  • What is Polr? Polr is an open-source URL shortener that provides a simple interface for creating and managing shortened URLs.
  • Why use Polr? Hosting your own URL shortener lets you control your data and brand your URLs.
  • Is this setup guide applicable for other Linux distributions? This guide is specific to Ubuntu 20.04, but the steps should be similar for other Debian-based distributions.
  • How can I troubleshoot installation issues? Check the Apache and MariaDB service status, ensure your domain is correctly pointed, and review logs for any errors.
  • Can I customize the Polr interface? Yes, Polr offers themes and customization options within its settings.