In this tutorial, we will show you how to install Yourls on Ubuntu 20.04 LTS. For those of you who didn’t know, YOURLS stands for Your Own URL Shortener. It is a small set free and open-source PHP script that will allow you to run your own URL shortening service. YOURLS allows you to have full control over your data, detailed stats, analytics, plugins, and more.
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 through the step-by-step installation of Yourls open-source URL shortening on Ubuntu 20.04 (Focal Fossa). You can follow the same instructions for Ubuntu 18.04, 16.04, and any other Debian-based distribution like Linux Mint.
Install Yourls on Ubuntu 20.04 LTS Focal Fossa
Step 1. First, make sure that all your system packages are up-to-date by running the following
apt commands in the terminal.
sudo apt update sudo apt upgrade
Step 2. Installing the LAMP stack.
A Ubuntu 20.04 LAMP server is required. If you do not have LAMP installed, you can follow our guide here.
Step 3. Configuring MariaDB for Yourls.
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:
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-password'; MariaDB [(none)]> flush privileges; MariaDB [(none)]> exit;
Step 4. Installing Yourls on Ubuntu 20.04.
Now we download the latest version of YOURLS from the Git repository:
cd /var/www/html git clone https://github.com/YOURLS/YOURLS.git
cd YOURLS/user/ cp config-sample.php config.php
Next, edit the configuration file and define your database settings:
Change the following lines that match with your database settings and also provide your domain name and admin password:
* ** 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-password' ); /** 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_' );
Once done, now 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 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' => 'Ngadimin', 'jmutai' => 'Admin-Strong-Password', // You can have one or more 'login'=>'password' lines );
Save and close the file then change the ownership and give proper permissions to the YOURLS directory:
chown -R www-data:www-data /var/www/html/YOURLS chmod -R 775 /var/www/html/YOURLS
Step 5. Configuring Apache for Yourls.
Now we create a new virtual host directive in Apache. For example, create a new Apache configuration file named ‘
yourls.conf’ on your virtual server:
touch /etc/apache2/sites-available/yourls.conf ln -s /etc/apache2/sites-available/yourls.conf /etc/apache2/sites-enabled/yourls.conf nano /etc/apache2/sites-available/yourls.conf
Add the following lines:
<VirtualHost *:80> ServerAdmin firstname.lastname@example.org DocumentRoot /var/www/html/YOURLS ServerName your-domain.com ServerAlias www.your-domain.com <Directory /var/www/html/YOURLS/> Options FollowSymLinks AllowOverride All Order allow,deny allow from all </Directory> ErrorLog /var/log/apache2/your-domain.com-error_log CustomLog /var/log/apache2/your-domain.com-access_log common </VirtualHost>
Now, we can restart the Apache webserver so that the changes take place:
sudo a2enmod rewrite sudo a2ensite yourls.conf sudo systemctl restart apache2.service
Step 6. Configure Firewall
In case, you enabled UFW firewall and firewall block requests of the apache web server, open a port in the firewall:
sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw reload
Step 7. Accessing Yourls Web Interface.
Yourls will be available on HTTP port 80 by default. Open your favorite browser and navigate to
http://yourls.your-domain.comand complete the required steps to finish the installation.
Congratulations! You have successfully installed Yourls. Thanks for using this tutorial for installing Yourls (Your Own URL Shortener) on Ubuntu 20.04 LTS Focal Fossa system. For additional help or useful information, we recommend you check the official Yourls website.