How to Shorten Your Links With Your Own Domain


Click here if you use cPanel (shared hosting)

Click here if you use CentOS (VPS/Dedicated server)

Contact us if you want us to do it for you


Demo of YOURLS admin interface (stats, reports)

Demo of YOURLS admin interface (stats, reports)

Maybe you are tired of using bit.ly or goo.gl or maybe you just want your own branded short links. You can shorten your own links (for free!) on your own domain using YOURLS. It’s a free and open source, self-hosted, link-shortening application. In this tutorial, we’ll show you how to install YOURLS on CentOS and shorten your links with a custom domain. You can either use your own server (VPS) or a Shared hosting account. We have instructions for both scenarios.

Requirements

  • A short domain (obviously). We recommend getting a domain from Namecheap. They have the biggest portfolio of TLD extensions. They have 370+ new gTLD extensions, you can find all of them here. FYI: in this tutorial, we’ll use examp.le as an example short domain. You’ll need to update your configs accordingly.
  • A CentOS VPS. Since it’s a self-hosted application, you’ll need a server to host it on. YOURLS doesn’t require many resources, so you can host it on a $5 VPS from A2Hosting or DigitalOcean
  • Apache (with mod_rewrite enabled). Feel free to use some other web server like Nginx.
  • PHP 5.3+
  • MySQL 5+
  • cURL (if you plan on using the API)
  • Alternatively, you can even use a Shared hosting account. You can get shared cPanel hosting from A2Hosting. They have SSH access and great 24/7 support. Installation instructions for a Shared hosting environment are below

Now that we got the requirements out of the way, let’s continue to the installation.

YOURLS CentOS (VPS) Installation instructions

Before you start: these instructions are for a Server (VPS/Cloud/Dedicated). If you have a Shared hosting account (cPanel), click here.

Login to your server as a root user or just use sudo for each command.

As always, before you do anything, you should update your system. Run the following command to update CentOS:

yum update

1. Install and configure MariaDB

In this tutorial, we’ll be using MariaDB instead of MySQL. If you already have MariaDB (or MySQL) installed on your server, then skip this step.

To install MariaDB on CentOS 7, run the following command:

yum install mariadb-server

Then, start MariaDB and enable it to boot on startup:

systemctl enable mariadb
systemctl start mariadb

Run this script to secure your MariaDB. Just enter the following:

mysql_secure_installation

Enter the data requested by the script and you are done.

Now, we will create a database for our YOURLS.

First, login to MariaDB as root:

mysql -u root -p

And create a database and a user:

CREATE DATABASE thrurls;
CREATE USER thruser@localhost IDENTIFIED BY 'enter_a_strong_password_here';
GRANT ALL PRIVILEGES ON thrurls.* TO thruser@localhost IDENTIFIED BY 'enter_a_strong_password_here';

Don’t forget to enter a strong password and replace any names with something of your choice.

Flush the privileges:

FLUSH PRIVILEGES;

Exit MariaDB:

exit;

You are done with your database config. Don’t forget your config (DB name, username, password etc.), you’ll need them for later. Let’s move on to PHP.

2. Install PHP and required modules

If you already have PHP and the required modules already installed on your server, skip this step.

To install PHP, PHP-FPM, PHP-MySQL and other modules on your CentOS, run:

yum install php php-mysql php-fpm php-cli php-common php-curl

This will install PHP and some other required modules.

That’s it with PHP. Let’s move on to our web server (Apache)

3. Install and configure Apache (httpd)

If you already have Apache installed on your server, skip this step.

To install Apache, run:

yum install httpd

After the installation is complete, start the Apache web server:

systemctl start httpd.service

And enable Apache to start on boot:

systemctl enable httpd.service

Now restart Apache for the changes to take effect:

systemctl restart httpd.service

3.1. Create an Apache virtual host for your new YOURLS installation

We are not quite done with Apache. Even if you have Apache installed, you’ll still need to add your new domain in Apache. Don’t forget to point your short domain to your server IP!

First, create a new directory for your YOURLS installation:

mkdir -p /var/www/examp.le/public_html

Update the permissions:

chmod -R 755 /var/www

Create virtual hosts directories (if you don’t already have them):

mkdir /etc/httpd/sites-available
mkdir /etc/httpd/sites-enabled

To finish the virtual hosts directories setup, run:

nano /etc/httpd/conf/httpd.conf

And add the following line at the end of the file:

IncludeOptional sites-enabled/*.conf

Now, create a virtual host file for your short domain:

nano /etc/httpd/sites-available/examp.le.conf

And add the following:

<VirtualHost *:80>
    ServerName examp.le
    ServerAlias examp.le
    DocumentRoot /var/www/examp.le/public_html
    <Directory /var/www/examp.le/>
      AllowOverride All
      Order Deny,Allow
      Allow from all
    </Directory>
    ErrorLog /var/www/examp.le/error.log
    CustomLog /var/www/examp.le/requests.log combined
</VirtualHost>

Enable the virtual hosts file with:

ln -s /etc/httpd/sites-available/examp.le.conf /etc/httpd/sites-enabled/examp.le.conf

Restart Apache for the changes to take effect:

systemctl restart httpd.service

4. Download, install and configure YOURLS

Here’s the actual installation of YOURLS.

First, navigate to your directory for your short domain:

cd /var/www/virtual/examp.le/public_html

clone (download) YOURLS from their official GitHub repo:

git clone https://github.com/YOURLS/YOURLS

Copy the sample configuration file:

cp user/config-sample.php user/config.php

Open up the configuration file:

nano /var/www/virtual/examp.le/public_html/user/config.php

And update the lines according to your needs. The code itself is self-explanatory and there’s good documentation for each line, so you should change any setting based on your setup. The lines you must update are:

define( 'YOURLS_DB_USER', 'thruser' );
define( 'YOURLS_DB_PASS', 'the_strong_password_you_entered_in_step1' );
define( 'YOURLS_DB_NAME', 'thrurls' );
define( 'YOURLS_SITE', 'http://examp.le' );
define( 'YOURLS_COOKIEKEY', 'modify this text with something random - you don't have to remeber it' );
$yourls_user_passwords = array(
    'yourlsadminusername' => 'password',
    );

Save and close the file.

To finish the installation, go to

http://examp.le/admin

and follow the steps to finish the installation (click on “Install YOURLS”)

You are done with the installation. Now let’s finish the configuration.

5. .htaccess config

Create (or edit) your .htaccess file:

nano /var/www/virtual/examp.le/public_html/.htaccess

and add the following

# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^examp.le$
RewriteRule ^/?$ https://www.example.com [R=301,L]
</IfModule>

With this, you are forcing non-www. What’s the point of a short domain if you plan on using it with www? You’ll also redirect everyone to your www.example.com homepage if someone visits examp.le

6. Optional plugins

YOURLS has a lot of plugins that you can install (optionally). The most used plugin is Random Keywords, which allows you to generate random short URLs (examp.le/aGshF) instead of generic URLs (examp.le/1).

6.1. How to install a YOURLS plugin

We’ll use Random Keywords for our example.

First, create a new folder for your plugin:

mkdir /var/www/examp.le/public_html/user/plugins/random-keywords

Navigate to the new folder:

cd /var/www/examp.le/public_html/user/plugins/random-keywords

Download/extract/clone the plugin files to the directory:

git clone https://github.com/YOURLS/random-keywords

Go to your plugins admin area (http://examp.le/admin/plugins.php) and activate the plugin.

That’s it. You are done installing YOURLS on CentOS 7.

Wanna set up SSL (https) on your short domain? Got stuck somewhere? Get our support and we’ll install and configure YOURLS with SSL for you, just contact us.


In an attempt to cover the most popular setups, along with our CentOS server instructions, we’ll let you know how to install YOURLS on a shared hosting account. Most beginners are probably already using a shared hosting account with cPanel. So if you have cPanel, these instructions are for you. If you have a CentOS VPS, ignore these and just do the instructions above.

You may have a different setup based on your hosting plan. Contact our support and we’ll install YOURLS on your hosting plan.

YOURLS installation instructions on a shared hosting plan

If you have access to cPanel, then you can install YOURLs with a single click via Softaculous. Just navigate to your cPanel, go to Softaculous and find YOURLs. Fill in the required data (username, password, domain) and that’s it. YOURLS is installed.

If you want to do it manually, you can. Here are the instructions:

1. Create a database and a database user

Whatever panel you are using (probably cPanel), there’s an area in your panel where you can manage your databases. Go to the Database section and first, create a database. Name it whatever you want. For this tutorial, we’ll name it

sh2r3dh0s7_yourlsdb

Next, create a user and add the user to your newly created database with all privileges. For this tutorial, we’ll name the user:

sh2r3dh0s7_yourlsuser

2. Download, configure and upload YOURLS

Download the latest version of YOURLS and extract it to your desktop. Go to the ‘user’ folder and rename the file ‘config-sample.php’ to ‘config.php’

Now, open up ‘config.php’ in Notepad or whatever editor you use and edit the following settings:

define( 'YOURLS_DB_USER', 'sh2r3dh0s7_yourlsuser' );
define( 'YOURLS_DB_PASS', 'the_strong_password_you_user_for_your_database' );
define( 'YOURLS_DB_NAME', 'sh2r3dh0s7_yourlsdb' );
define( 'YOURLS_SITE', 'http://examp.le' );
define( 'YOURLS_COOKIEKEY', 'modify this text with something random - you don't have to remeber it' );
$yourls_user_passwords = array(
    'yourlsadminusername' => 'password',
    );

The setup is much similar to our VPS setup. You just have to edit the ‘config.php’ file to match your data. All the lines are pretty self-explanatory. When you are done editing the file, save it.

The next step is to upload all the files and folders you just downloaded for YOURLS (including the new updated config.php file) via FTP. You can use the online file manager via your control panel or you can use an FTP client like FileZilla. Just upload all the folders and files to your root (public_html) directory.

After you are done uploading the files, navigate to http://examp.le/admin and finish the installation.

3. .htaccess config

Create a .htaccess file (you can do it via Notepad, just open up an empty file and save it as .htaccess) and add the following content:

# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^examp.le$
RewriteRule ^/?$ https://www.example.com [R=301,L]
</IfModule>

With this, you are forcing non-www. What’s the point of a short domain if you plan on using it with www? You’ll also redirect everyone to your www.example.com homepage if someone visits examp.le

Save the file and upload it to your public_html directory (or wherever you previously uploaded the YOURLS files)

4. Optional plugins

YOURLS has a lot of plugins that you can install (optionally). The most used plugin is Random Keywords, which allows you to generate random short URLs (examp.le/aGshF) instead of generic URLs (examp.le/1).

4.1. How to install a YOURLS plugin

We’ll use Random Keywords for our example.

Go to your file manager or FTP client and navigate to ‘public_html/user/plugins’. Then, create a folder for your new plugin, ex: ‘random-keywords’ and open up the folder.

Download the plugin files from GitHub and upload them to the folder you just created (ie. public_html/user/plugins/random-keywords)

Go to your plugins admin area (http://examp.le/admin/plugins.php) and activate the plugin.

And that’s it.


 

This may seem like a lot of configurations, but the whole process is pretty easy and should take you no longer than 20 minutes. If you use Softaculous, you can do it in under a minute! If you need any help with YOURLS, feel free to contact our support.

 

Leave a comment

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