Using YOURLS with DigitalOcean

Introduction

YOURLS (Your Own URL Shortener) is a self-hosted URL shortening service that gives you full control over your short links. This guide will walk you through the process of setting up YOURLS on a DigitalOcean server, which you can then integrate with the Quick.Link iOS app for convenient URL management on the go.

Prerequisites

Before we begin, make sure you have:

  • A DigitalOcean account
  • A domain name for your URL shortener (e.g., yourdomain.com)
  • Basic knowledge of command line operations
  • SSH client (built into macOS/Linux or PuTTY for Windows)

Step 1: Creating a DigitalOcean Droplet

  1. Log in to your DigitalOcean account
  2. Click on "Create" and select "Droplets"
  3. Choose Ubuntu 22.04 LTS as your operating system
  4. Select a plan (the $5/month Basic plan is sufficient for moderate usage)
  5. Choose a datacenter region closest to your target audience
  6. Add SSH keys for secure access (recommended) or select password authentication
  7. Give your Droplet a name (e.g., yourls-server)
  8. Click "Create Droplet"

Step 2: Connecting to Your Server

Once your Droplet is running, you'll need to connect to it via SSH:

ssh root@your_server_ip

Replace your_server_ip with the IP address of your Droplet, which you can find on the DigitalOcean dashboard.

Step 3: Initial Server Setup

First, let's update the system and create a non-root user for security purposes:

apt update && apt upgrade -y

# Create a new user
adduser yourls_user

# Grant administrative privileges
usermod -aG sudo yourls_user

# Set up basic firewall
ufw allow OpenSSH
ufw allow 'Nginx Full'
ufw enable

Next, switch to the new user:

su - yourls_user

Step 4: Installing Required Software

YOURLS requires a LAMP (Linux, Apache, MySQL, PHP) or LEMP (Linux, Nginx, MySQL, PHP) stack. We'll use Nginx for better performance:

sudo apt install nginx mysql-server php-fpm php-mysql php-curl php-xml php-gd php-mbstring unzip -y

Start and enable the services:

sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl start mysql
sudo systemctl enable mysql

Step 5: Configuring MySQL

Set up a secure MySQL installation:

sudo mysql_secure_installation

Follow the prompts to set a root password and secure your MySQL installation.

Next, create a database and user for YOURLS:

sudo mysql -u root -p

Once you're in the MySQL prompt, execute:

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

Remember to replace your_secure_password with a strong password.

Step 6: Configuring Domain Name

Point your domain to your DigitalOcean Droplet by adding an A record at your domain registrar:

  • Type: A
  • Host: short (for short.yourdomain.com) or @ (for yourdomain.com)
  • Value: Your Droplet's IP address
  • TTL: 3600 (or the default)

Step 7: Setting Up Nginx

Create a new Nginx server block for your YOURLS installation:

sudo nano /etc/nginx/sites-available/yourls

Add the following configuration (replace short.yourdomain.com with your domain):

server {
    listen 80;
    server_name short.yourdomain.com;
    root /var/www/yourls;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /yourls-loader.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Enable the site and restart Nginx:

sudo ln -s /etc/nginx/sites-available/yourls /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 8: Installing YOURLS

Now, let's download and set up YOURLS:

# Create directory for YOURLS
sudo mkdir -p /var/www/yourls
sudo chown -R $USER:$USER /var/www/yourls

# Download YOURLS
cd /tmp
wget https://github.com/YOURLS/YOURLS/archive/master.zip
unzip master.zip
cp -r YOURLS-master/* /var/www/yourls/

Step 9: Configuring YOURLS

Copy the sample configuration file:

cd /var/www/yourls
cp user/config-sample.php user/config.php

Edit the configuration file:

nano user/config.php

Update the following parameters:

// MySQL settings
define( 'YOURLS_DB_USER', 'yourlsuser' );
define( 'YOURLS_DB_PASS', 'your_secure_password' );
define( 'YOURLS_DB_NAME', 'yourls' );
define( 'YOURLS_DB_HOST', 'localhost' );
define( 'YOURLS_DB_PREFIX', 'yourls_' );

// Site settings
define( 'YOURLS_SITE', 'https://short.yourdomain.com' );
define( 'YOURLS_HOURS_OFFSET', '0' ); // Timezone offset

// URL shortening settings
define( 'YOURLS_URL_CONVERT', 36 ); // Base 36 encoding

// Admin settings
$yourls_user_passwords = [
    'admin' => 'your_admin_password' // Change this to a strong password
];

Set proper permissions:

sudo chown -R www-data:www-data /var/www/yourls

Step 10: Setting Up SSL with Let's Encrypt

Secure your site with SSL:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d short.yourdomain.com

Follow the prompts to complete the SSL setup.

Step 11: Completing the Installation

Visit https://short.yourdomain.com/admin/ to complete the YOURLS installation. Log in with the username and password you set in the configuration file.

Step 12: Integration with Quick.Link iOS App

To integrate with the Quick.Link iOS app, you'll need:

  1. Your YOURLS domain (e.g., https://short.yourdomain.com)
  2. Your admin username and password

In the Quick.Link app:

  1. Go to Settings > URL Shortener
  2. Select "YOURLS" as your provider
  3. Enter your YOURLS domain (include https://)
  4. Enter your admin username and password
  5. Test the connection

Step 13: Securing Your YOURLS Installation

Create a .htaccess file for additional security:

nano /var/www/yourls/.htaccess

Add the following rules:

# Prevent directory listing
Options -Indexes

# Protect config file

  Require all denied


# Restrict access to user directory

  Require all denied

Step 14: Maintenance Tips

Regular Updates

To keep your YOURLS installation secure, regularly update it:

cd /var/www/yourls
git pull
sudo chown -R www-data:www-data /var/www/yourls

Backup Procedure

Set up regular backups of your database:

# Create backup directory
mkdir -p ~/yourls_backups

# Backup database
mysqldump -u yourlsuser -p yourls > ~/yourls_backups/yourls_db_$(date +\%Y\%m\%d).sql

Consider automating this with a cron job:

echo "0 2 * * * mysqldump -u yourlsuser -p'your_secure_password' yourls > ~/yourls_backups/yourls_db_\$(date +\\\%Y\\\%m\\\%d).sql" >> /tmp/crontab
crontab /tmp/crontab
rm /tmp/crontab

Conclusion

You now have a fully functional YOURLS installation running on a DigitalOcean server, integrated with the Quick.Link iOS app. You can create custom short URLs, track analytics, and manage your links efficiently.

For more information and customization options, refer to the official YOURLS documentation.