How To Install Polr URL Shortener on Ubuntu 20.04
Polr is a free and open-source URL shortener written in PHP. It provides a simple and user-friendly interface to create and manage links. You can host your own URL shortener, brand your URLs, and gain control over your data. It provides a ton of features including, robust API, URL forwarding, customize permissions, themes, and more.
In this post, we will show you how to install Polr with Apache and Let's Encrypt SSL on Ubuntu 20.04.
Prerequisites
- A server running Ubuntu 20.04.
- A valid domain name pointed with your server IP.
- A root password is configured on the server.
Getting Started
First, update the system packages to the updated version by running the following command:
apt-get update -y
Once all the packages are updated, you can proceed to the next step.
Install Apache, MariaDB, and PHP
First, you will need to install the Apache server, MariaDB database, PHP, and other PHP extensions to your server. You can install all of them with the following command:
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
Once all the packages are installed, you can proceed to the next step.
Create a Database for Polr
Polr uses MariaDB/MySQL as a database backend. So you will need to create a database and user for Polr.
First, log in to the MariaDB with the following command:
mysql
Once log in, create a database and user with the following command:
MariaDB [(none)]> CREATE DATABASE polrdb;
MariaDB [(none)]> GRANT ALL ON polrdb.* TO 'polr' IDENTIFIED BY 'password';
Next, flush the privileges and exit from the MariaDB with the following command:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
Once you are done, you can proceed to the next step.
Install Composer
Next, you will need to install Composer in your server to install the PHP dependencies required for Polr.
Run the following command to install the Composer:
curl -sS https://getcomposer.org/installer | php
You should get the following output:
All settings correct for using Composer Downloading... Composer (version 2.1.3) successfully installed to: /root/composer.phar Use it: php composer.phar
Next, move the Composer binary to the system location using the following command:
mv composer.phar /usr/local/bin/composer
Next, verify the Composer version using the following command:
composer -V
You should see the following output:
Do not run Composer as root/super user! See https://getcomposer.org/root for details Continue as root/super user [yes]? yes Composer version 2.1.3 2021-06-09 16:31:20
Install Polr
Next, you will need to download the latest version of Polr from the Git repository. You can download it with the following command:
git clone https://github.com/cydrobolt/polr.git --depth=1 /var/www/html/polr
Once the download is completed, change the directory to Polr and install all required dependencies with the following command:
cd /var/www/html/polr
composer install --no-dev -o
Next, copy the sample environment file with the following command:
cp .env.setup .env
Next, set proper permission to the Polr directory with the following command:
chown -R www-data:www-data /var/www/html/polr
chmod -R 755 /var/www/html/polr
Once you are finished, you can proceed to the next step.
Configure Apache for Polr
Next, you will need to create an Apache virtual host configuration file for Polr. You can create it with the following command:
nano /etc/apache2/sites-available/polr.conf
Add the following lines:
<VirtualHost *:80> ServerAdmin [email protected] 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 then enable the Apache virtual host file and rewrite module with the following command:
a2enmod rewrite
a2ensite polr.conf
Next, restart the Apache service to apply the changes:
systemctl restart apache2
You can now verify the status of the Apache service using the following command:
systemctl status apache2
You should get the following output:
? apache2.service - The Apache HTTP Server Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2021-06-17 12:09:42 UTC; 16s ago Docs: https://httpd.apache.org/docs/2.4/ Process: 27602 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS) Main PID: 27617 (apache2) Tasks: 6 (limit: 2353) Memory: 26.6M CGroup: /system.slice/apache2.service ??27617 /usr/sbin/apache2 -k start ??27618 /usr/sbin/apache2 -k start ??27619 /usr/sbin/apache2 -k start ??27620 /usr/sbin/apache2 -k start ??27621 /usr/sbin/apache2 -k start ??27622 /usr/sbin/apache2 -k start Jun 17 12:09:42 ubuntu2004 systemd[1]: Starting The Apache HTTP Server...
Access Polr Web Interface
You can now access the Polr web interface using the URL http://polr.example.com.
Provide your database name, username, password, application settings, Polr URL, admin username, email, password and click on the Install button. Once the Polr has been installed, you should get the following screen:
Click on the home page. You should see Polr dashboard on the following screen:
Secure Polr with Let's Encrypt Free SSL
First, you will need to install the Certbot Let's Encrypt client to download and install the SSL certificate for your domain.
You can install it with the following command:
apt-get install python3-certbot-apache -y
Once installed, you can run the following command to install the Let's Encrypt Certificate for your domain polr.example.com.
certbot --apache -d polr.example.com
During the installation, you will be asked to provide your email address and accept the term of service as shown below:
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): [email protected] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y Obtaining a new certificate Performing the following challenges: http-01 challenge for polr.example.com Enabled Apache rewrite module Waiting for verification... Cleaning up challenges Created an SSL vhost at /etc/apache2/sites-available/polr-le-ssl.conf Deploying Certificate to VirtualHost /etc/apache2/sites-available/polr-le-ssl.conf Enabling available site: /etc/apache2/sites-available/polr-le-ssl.conf Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Next, type 2 and hit Enter to download and install a free SSL certificate for your domain. Once the installation has been completed successfully. You should get the following output:
Enabled Apache rewrite module Redirecting vhost in /etc/apache2/sites-enabled/polr.conf to ssl vhost in /etc/apache2/sites-available/ polr-le-ssl.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://polr.example.com You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=polr.example.com - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Your Polr website is now secured with Let's Encrypt free SSL. You can access it using the URL https://polr.example.com.
Conclusion
In the above guide, you learned how to install Polr with Apache and Let's Encrypt SSL on Ubuntu 20.04 server. You can now host your own link shortener using Polr. Feel free to ask me if you have any questions.