Posted in

How to Host Your Own URL Shortener on Ubuntu Server

In this guide, we’ll walk through how to set up a self-hosted URL shortener using open-source software on an Ubuntu server. Whether you’re a developer, marketer, or just tech-curious, this step-by-step tutorial will get you up and running in no time.


🧰 What You’ll Need

  • An Ubuntu server (VPS or local, version 20.04 or later)
  • A domain name (optional, for branded short links)
  • A non-root user with sudo privileges
  • Basic terminal knowledge

🚀 Step 1: Choose a URL Shortener Platform

We’ll use YOURLS (Your Own URL Shortener) — a lightweight, open-source PHP app that’s fast, customizable, and packed with analytics.

🔗 GitHub: https://github.com/YOURLS/YOURLS


⚙️ Step 2: Install Apache, PHP, and MySQL

First, update your system:

sudo apt update && sudo apt upgrade -y

Then install the LAMP stack:

sudo apt install apache2 mysql-server php php-mysql php-curl php-mbstring php-xml php-cli unzip curl -y

Enable Apache and MySQL to start on boot:

sudo systemctl enable apache2
sudo systemctl enable mysql

🗃️ Step 3: Set Up the MySQL Database

Log into MySQL:

sudo mysql -u root -p

Run these commands to create a database and user:

CREATE DATABASE yourls;
CREATE USER 'yourlsuser'@'localhost' IDENTIFIED BY 'strongpassword';
GRANT ALL PRIVILEGES ON yourls.* TO 'yourlsuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

📥 Step 4: Download and Configure YOURLS

  1. Download YOURLS:
cd /var/www/
sudo git clone https://github.com/YOURLS/YOURLS.git yourls
cd yourls
sudo cp user/config-sample.php user/config.php
  1. Edit the config file:
sudo nano user/config.php

Update these lines:

define( 'YOURLS_DB_USER', 'yourlsuser' );
define( 'YOURLS_DB_PASS', 'strongpassword' );
define( 'YOURLS_DB_NAME', 'yourls' );
define( 'YOURLS_SITE', 'http://yourdomain.com' ); // Or your IP
define( 'YOURLS_HOURS_OFFSET', 0 );
define( 'YOURLS_PRIVATE', true );
define( 'YOURLS_USER', array(
    'admin' => 'change_this_password',
));

Save and exit.


🌐 Step 5: Configure Apache

Create a virtual host config:

sudo nano /etc/apache2/sites-available/yourls.conf

Paste:

<VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /var/www/yourls

    <Directory /var/www/yourls>
        Options Indexes 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:

sudo a2ensite yourls.conf
sudo a2enmod rewrite
sudo systemctl restart apache2

🔐 (Optional) Step 6: Add HTTPS with Let’s Encrypt

Install Certbot:

sudo apt install certbot python3-certbot-apache -y

Secure your domain:

sudo certbot --apache -d yourdomain.com

🧪 Step 7: Finish Installation in Browser

Go to:

http://yourdomain.com/admin

You’ll see the YOURLS installer. Click the button to install, and you’re done!

Login with the username/password you set in config.php.


✅ Features of YOURLS

  • Shorten links manually or automatically
  • Analytics: track clicks, referrers, geolocation
  • API access for developers
  • Bookmarklets and browser extensions
  • Plugins for added features

🎯 Bonus: Enable Public Shortening (Optional)

If you want users to shorten URLs without logging in:

define( 'YOURLS_PRIVATE', false );

But be careful—this opens your site to spam and abuse.


📦 Wrapping Up

Congrats! You now have a fully functional, self-hosted URL shortener. Whether you’re branding links for marketing campaigns or creating an internal tool, hosting YOURLS on Ubuntu gives you freedom, privacy, and flexibility.

Leave a Reply

Your email address will not be published. Required fields are marked *