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
- 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
- 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