Home ยป ๐Ÿ”— How to Host Your Own URL Shortener on Ubuntu Server
Posted in

๐Ÿ”— How to Host Your Own URL Shortener on Ubuntu Server

Want to ditch Bitly and TinyURL and run your own branded URL shortener? Hosting your own URL shortening service is not only possibleโ€”itโ€™s simple, secure, and gives you complete control over your data and links.

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.

Need help customizing YOURLS or integrating it into another project? I can help write a follow-up guide or API integration tutorialโ€”just let me know

Leave a Reply

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