How to Install YOURLS URL Shortener on Ubuntu 20.04
You want a private, self‑hosted link shortener that doesn’t involve Google’s analytics or Facebook’s cookie wars? YOURLS gives you exactly that—plus a handy admin panel and an API you can call from your scripts. In this walk‑through you’ll get YOURLS up and running on Ubuntu 20.04 in under 30 minutes, with all the tweaks that keep it stable and secure.
Prerequisites: What You’ll Need
- A fresh or existing Ubuntu 20.04 server (root access or sudo privileges).
- A MySQL/MariaDB instance – I’ve seen folks forget to create a user for YOURLS, then spend extra time troubleshooting “access denied” errors.
- Apache 2 with mod_rewrite enabled, or Nginx + PHP‑FPM if you prefer that stack.
If your machine already runs a LAMP stack, just skip straight to the database setup.
Step 1: Update the System & Install Dependencies
sudo apt update && sudo apt upgrade -y sudo apt install apache2 php libapache2-mod-php php-mysql mysql-server git unzip -y
Fresh packages keep you away from the “I’m getting a PHP‑fatal error” nightmare. git lets us pull YOURLS straight from GitHub, and unzip is handy for any future packaging needs.
Step 2: Create a MySQL Database for YOURLS
sudo mysql -u root -p
Inside the MySQL shell:
CREATE DATABASE yours; GRANT ALL PRIVILEGES ON yours.* TO 'yoursuser'@'localhost' IDENTIFIED BY 'StrongPassword!'; FLUSH PRIVILEGES; EXIT;
YOURLS will keep all its links in a database. Giving it its own user prevents accidental data leaks into other sites and keeps permissions tight.
Step 3: Grab the Latest YOURLS Release
cd /var/www sudo git clone https://github.com/YOURLS/YOURLS.git yourls
Cloning instead of downloading a zip lets you stay on top of updates with git pull later. And it saves those annoying “download everything, unzip, move” steps.
Step 4: Configure YOURLS Settings
cd /var/www/yourls/ sudo cp yourls-config.php.dist yourls-config.php sudo nano yourls-config.php
Find the following lines and change them:
define( 'YOURLS_DB_USER', 'yoursuser' ); define( 'YOURLS_DB_PASSWD', 'StrongPassword!' ); define( 'YOURLS_DB_NAME', 'yours' ); define( 'YOURLS_SITE', 'https://short.example.com' ); // your domain define( 'YOURLS_ADMIN_USER', 'admin' ); define( 'YOURLS_ADMIN_PASSWD','YourAdminPassword' );
The yourls-config.php file is the heart of YOURLS. If you leave the defaults, you’ll be prompted for a password each time—no fun.
Step 5: Set Up Apache and PHP
sudo a2enmod rewrite sudo systemctl restart apache2
Create an Apache virtual host:
sudo nano /etc/apache2/sites-available/yourls.conf
Add this content (replace short.example.com with your real domain):
<VirtualHost *:80>
ServerName short.example.com
DocumentRoot /var/www/yourls
<Directory /var/www/yourls>
Options +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/yourls_error.log
CustomLog ${APACHE_LOG_DIR}/yourls_access.log combined
</VirtualHost>
Enable the site and reload Apache:
sudo a2ensite yourls.conf sudo systemctl reload apache2
mod_rewrite is mandatory for pretty URLs (/u/mycode). The vhost file isolates YOURLS from other sites you might host on the same server.
Step 6: Finish Installation & Test It Out
Navigate to http://short.example.com/admin/.
You should see the YOURLS admin login page. Log in with the credentials you set earlier.
Create a short link by pasting an URL (e.g., https://github.com/YOURLS/YOURLS) and clicking Shorten. You’ll get something like /u/abc123.
Visit that link to confirm it redirects properly.
A quick sanity check ensures the database connection, file permissions, and rewrite rules all work.
Common Pitfall I’ve Seen
People often forget to set proper ownership on the YOURLS directory:
sudo chown -R www-data:www-data /var/www/yourls/
If you skip this, Apache will refuse to write into the cache or uploads folders, and the admin panel will break with “cannot create cache file” errors.
You’ve now got a fully functional, private URL shortener on Ubuntu 20.04 that can be extended with plugins, integrated into your own scripts, or exposed through its API. Feel free to tweak the .htaccess rewrite rules for HTTPS only, or add Nginx if you’re comfortable with that stack.