How to Install Polr Link Shortener on Debian 10
Polr is a free and open-source URL shortener that allows you to host your own link shortener. It is written in PHP and Lumen, and uses MariaDB to store its data. With Polr, you can create new links or look up existing links without breaking your existing code.
In this tutorial, we will show you how to install the Polr Link Shortener software on Debian 10 server.
Prerequisites
- A server running Debian 10.
- A valid domain name pointed with your server IP. in this tutorial, we will use polr.example.com domain.
- A root password is configured on your server.
Getting Started
Before starting, it is a good idea to update your system with the latest version. You can update your system with the following command:
apt-get update -y
apt-get upgrade -y
After updating the system, restart it to implement the changes.
Install LAMP Server
First, you will need to install the Apache webserver, MariaDB server, PHP and other packages in your system. You can install all of them using the following command:
apt-get install apache2 mariadb-server libapache2-mod-php php php-pdo php-mysql php-mbstring php-tokenizer php-json php-curl git wget unzip -y
Once all the packages are installed, start the Apache and MariaDB service and enable them to start after system reboot.
systemctl start apache2
systemctl start mariadb
systemctl enable apache2
systemctl enable mariadb
Once you are done, you can proceed to the next step.
Configure MariaDB Database
By default, the MariaDB root password is not configured in Debian 10. So, you will need to set it first.
To do so, log in to the MariaDB shell with the following command:
mysql
Once login, set your MariaDB root password with the following command:
MariaDB [(none)]> SET PASSWORD FOR 'root'@'localhost' = PASSWORD("newpassword");
Next, create a database and user for Polr 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 shell with the following command:
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;
Once you are finished, you can proceed to the next step.
Install Composer
Next, you will need to install the Composer in your system. Composer is a dependency manager for PHP used to install all PHP dependencies for your project.
First, download the Composer with the following command:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
Next, you will need to verify the data integrity of the downloaded script by comparing the script SHA-384 hash with the latest hash.
To do so, download the signature of the latest Composer installer from the Git repository and store it in a variable named HASH:
HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
Next, verify the installation script with the following command:
php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
If the hashes match, you will see the following output:
Installer verified
Finally, install the Composer using the following command:
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
Once the installation has been completed, you should see the following output:
All settings correct for using Composer Downloading... Composer (version 1.9.2) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
Once you are finished, you can proceed to the next step.
Install Polr
First, download the latest version of the Polr from the Git repository to the Apache web root directory:
cd /var/www/html
git clone https://github.com/cydrobolt/polr.git --depth=1
Once downloaded, change the directory to polr and install all required dependencies with the following command:
cd polr
composer install --no-dev -o
Next, enable the web-based installer by renaming the following file:
mv .env.setup .env
Next, give proper permissions to the polr directory as shown below:
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 Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/polr_error.log CustomLog ${APACHE_LOG_DIR}/polr_access.log combined </VirtualHost>
Save and close the file when you are finished. Then, enable the Apache virtual host file and rewrite module with the following command:
a2ensite polr.conf
a2enmod rewrite
Finally, restart the Apache service to implement the changes:
systemctl restart apache2
At this point, Polr is installed and configured. You can now proceed to the next step.
Access Polr Web Interface
Now, open your web browser and type the URL http://polr.example.com. You will be redirected to the Polr setup wizard as shown below:
Provide your database information, application settings, admin account settings and click on the Install button. Once the installation has been completed, you should see the following page:
Click on the login button. You will be redirected to the Polr login page:
Provide your admin username, password and click on the LOGIN button. You should see the following page:
Now, click on the ADMIN => DASHBOARD in the top right corner. You should see the Polr dashboard in the following page:
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 python-certbot-apache -y
Once installed, you can run the following command to install Let's Encrypt Certificate for your domain polr.example.com.
certbot --apache -d polr.example.com
Durring 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
Congratulations! you have successfully installed Polr on Debian 10 server and secured it with Let's Encrypt SSL. You can now host your own link shortener using the Polr web interface. Feel free to ask me if you have any questions.