How To Install YOURLS on Ubuntu 22.04 LTS
In this tutorial, we will show you how to install YOURLS on Ubuntu 22.04 LTS. For those of you who didn’t know, YOURLS stands for Your Own URL Shortener, which is a free and open-source PHP script that lets you create custom URL shortening services. It allows you to create short and customized URLs, track click statistics and keep control of your data.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of the YOURLS (Your Own URL Shortener) on Ubuntu 22.04 (Jammy Jellyfish). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.
Prerequisites
- A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for YOURLS.
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install YOURLS on Ubuntu 22.04 LTS Jammy Jellyfish
Step 1. Before we begin the installation process, it’s a good practice to update the system to ensure we have the latest software packages. To do so, open the terminal and run the following command:
sudo apt update sudo apt upgrade sudo apt install wget apt-transport-https gnupg2
Step 2. Installing LAMP Stack on Ubuntu 22.04.
YOURLS requires a web server, a database, and PHP to run. If you do not have LAMP Stack installed, you can follow our guide here.
Step 3. Configuring MariaDB.
By default, MariaDB is not hardened. You can secure MariaDB using the mysql_secure_installation
script. you should read and below each step carefully which will set a root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MariaDB:
mysql_secure_installation
Configure it like this:
- Set root password? [Y/n] y - Remove anonymous users? [Y/n] y - Disallow root login remotely? [Y/n] y - Remove test database and access to it? [Y/n] y - Reload privilege tables now? [Y/n] y
Next, we will need to log in to the MariaDB console and create a database for YOURLS. Run the following command:
mysql -u root -p
This will prompt you for a password, so enter your MariaDB root password and hit Enter. Once you are logged in to your database server you need to create a database for Yourls installation:
MariaDB [(none)]> create database yourlsdb character set utf8mb4; MariaDB [(none)]> grant all on yourlsdb.* to 'yourls'@'localhost' identified by 'your-strong-passwd'; MariaDB [(none)]> flush privileges; MariaDB [(none)]> exit;
For additional resources on installing MariaDB, read the post below:
Step 4. Installing YOURLS on Ubuntu 22.04.
By default, the YOURLS is not available on Ubuntu 22.04 base repository. Now run the following command below to download the latest version of YOURLS from the official website:
wget https://github.com/YOURLS/YOURLS/archive/refs/tags/1.9.2.tar.gz
Next, extract the downloaded archive:
sudo tar -xvzf 1.9.2.tar.gz sudo mv YOURLS-1.9.2/* /var/www/yourls/ sudo rm -rf YOURLS-1.9.2/
After that, change the ownership of the YOURLS directory to the Apache user:
sudo chown -R www-data:www-data /var/www/yourls sudo chmod -R 755 /var/www/yourls sudo chmod 777 /var/www/yourls/user/config.php
Next, copy user/config-sample.php
to user/config.php
:
cp user/config-sample.php user/config.php nano user/config.php
Edit the following lines to match your database configuration:
* ** MySQL settings - You can get this info from your web host */ /** MySQL database username */ define( 'YOURLS_DB_USER', 'yourls' ); /** MySQL database password */ define( 'YOURLS_DB_PASS', 'your-strong-passwd' ); /** The name of the database for YOURLS */ define( 'YOURLS_DB_NAME', 'yourlsdb' ); /** MySQL hostname. ** If using a non standard port, specify it like 'hostname:port', eg. 'localhost:9999' or '127.0.0.1:666' */ define( 'YOURLS_DB_HOST', 'localhost' ); /** MySQL tables prefix */ define( 'YOURLS_DB_PREFIX', 'yourls_' );
Set website URL for YOURLS:
/** YOURLS installation URL -- all lowercase, no trailing slash at the end. ** If you define it to "http://sho.rt", don't use "http://www.sho.rt" in your browser (and vice-versa) */ define( 'YOURLS_SITE', 'http://yourls.your-domain.com' );
Next, set up the user and password:
/** Username(s) and password(s) allowed to access the site. Passwords either in plain text or as encrypted hashes ** YOURLS will auto encrypt plain text passwords in this file ** Read http://yourls.org/userpassword for more information */ $yourls_user_passwords = array( 'admin' => 'meilana', 'jmutai' => 'meilana-Strong-Password', // You can have one or more 'login'=>'password' lines );
Save and close the file.
Step 5. Configure Apache Virtual Host.
Now create the virtual host configuration file for YOURLS:
nano /etc/apache2/sites-available/yourls.conf
Add the following file:
<VirtualHost *:80> ServerName yourls.your-domain.com DocumentRoot /var/www/yourls <Directory /var/www/yourls/> Options FollowSymlinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/yourls.your-domain.com_error.log CustomLog ${APACHE_LOG_DIR}/yourls.your-domain.com_access.log combined </VirtualHost>
Save and close the file, then restart the Apache so that the changes take place:
sudo a2enmod rewrite sudo a2ensite yourls.conf sudo systemctl restart apache2
For additional resources on installing and managing Apache, read the post below:
Step 6. Secure YOURLS with Let’s Encrypt SSL.
First, install the Certbot client using the following command below:
sudo apt install certbot python3-certbot-apache2
Next, get your SSL certificate with Let’s Encrypt by following these steps:
certbot --apache -d yourls.your-domain.com
Let’s Encrypt certificates have 90 days of validity, and it is highly advisable to renew the certificates before they expire. You can test automatic renewal for your certificates by running this command:
sudo certbot renew --dry-run
Step 7. Configure Firewall.
Now we set up an Uncomplicated Firewall (UFW) with Apache to allow public access on default web ports for HTTP and HTTPS:
sudo ufw allow OpenSSH sudo ufw allow 'Apache Full' sudo ufw enable
Step 8. Accessing YOURLS Web Interface.
Once successfully installed, now open your web browser and access the YOURLS Web UI using the URL https://yourls.your-domain.com
. You will be redirected to the following page:
Congratulations! You have successfully installed YOURLS. Thanks for using this tutorial for installing YOURLS (Your Own URL Shortener) on Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the YOURLS website.