Skip to content

Deploying Simple-URL-Shortener

Introduction

Simple-URL-Shortener is a lightweight, self-hosted URL shortening service that allows you to create short, memorable links without relying on third-party services. Perfect for personal use, small businesses, or internal company links, it provides a straightforward solution for managing shortened URLs.

In an age where link tracking services collect extensive data about users, self-hosting your URL shortener gives you complete control over your links and the privacy of those who click them. Simple-URL-Shortener focuses on doing one thing well: creating and managing short URLs efficiently.

Key features include:

  • Lightweight Design: Minimal resource requirements for efficient hosting
  • Custom Short Codes: Choose your own memorable short codes or use auto-generated ones
  • Click Tracking: Basic analytics showing how often links are clicked
  • Simple Interface: Easy-to-use web interface for managing links
  • API Access: Programmatic link creation for integrations
  • Self-Hosted: Complete control over your data and privacy
  • Fast Redirects: Minimal latency for redirect operations
  • Database Options: Support for various database backends
  • No External Dependencies: All processing happens on your server
  • Open Source: Transparent codebase you can audit and modify

This guide walks through deploying Simple-URL-Shortener on Klutch.sh using Docker.

Why Deploy a URL Shortener on Klutch.sh

Deploying your own URL shortener on Klutch.sh provides several advantages:

Simplified Deployment: Klutch.sh handles the build and deployment process automatically.

Data Privacy: Keep all link data and click analytics on your own infrastructure.

HTTPS by Default: Automatic SSL certificates for secure link sharing.

Custom Domains: Use a short, branded domain for your links.

Persistent Storage: Your links and analytics survive restarts and redeployments.

Scalable Resources: Handle high traffic volumes when links go viral.

Always-On Availability: Your short links work 24/7 without maintenance.

Prerequisites

Before deploying on Klutch.sh, ensure you have:

  • A Klutch.sh account
  • A GitHub account with a repository for your configuration
  • Basic familiarity with Docker
  • (Optional) A short custom domain for your links

Deploying Simple-URL-Shortener on Klutch.sh

    Create a GitHub Repository

    Create a new GitHub repository for your URL shortener deployment configuration.

    Create Your Dockerfile

    Create a Dockerfile in your repository:

    FROM node:18-alpine
    WORKDIR /app
    RUN apk add --no-cache git
    RUN git clone https://github.com/phanison898/url-shortener.git .
    RUN npm install
    ENV PORT=3000
    ENV DATABASE_URL=${DATABASE_URL}
    ENV BASE_URL=${BASE_URL}
    EXPOSE 3000
    CMD ["npm", "start"]

    Alternatively, for a Python-based URL shortener:

    FROM python:3.11-slim
    WORKDIR /app
    RUN pip install flask flask-sqlalchemy
    COPY app.py .
    ENV FLASK_APP=app.py
    ENV DATABASE_URL=${DATABASE_URL}
    EXPOSE 5000
    CMD ["flask", "run", "--host=0.0.0.0"]

    Create Application Code (if using Python)

    Create an app.py file:

    from flask import Flask, redirect, request, jsonify
    from flask_sqlalchemy import SQLAlchemy
    import string
    import random
    import os
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///urls.db')
    db = SQLAlchemy(app)
    class URL(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    short_code = db.Column(db.String(10), unique=True, nullable=False)
    long_url = db.Column(db.Text, nullable=False)
    clicks = db.Column(db.Integer, default=0)
    with app.app_context():
    db.create_all()
    def generate_short_code():
    chars = string.ascii_letters + string.digits
    return ''.join(random.choice(chars) for _ in range(6))
    @app.route('/<short_code>')
    def redirect_url(short_code):
    url = URL.query.filter_by(short_code=short_code).first_or_404()
    url.clicks += 1
    db.session.commit()
    return redirect(url.long_url)
    @app.route('/api/shorten', methods=['POST'])
    def shorten():
    data = request.get_json()
    long_url = data.get('url')
    custom_code = data.get('code')
    short_code = custom_code or generate_short_code()
    url = URL(short_code=short_code, long_url=long_url)
    db.session.add(url)
    db.session.commit()
    return jsonify({'short_url': f"{request.host_url}{short_code}"})
    if __name__ == '__main__':
    app.run()

    Push Your Repository to GitHub

    Commit and push your files to GitHub.

    Create a New Project on Klutch.sh

    Navigate to the Klutch.sh dashboard and create a new project.

    Create a New App

    Create a new app and connect your GitHub repository.

    Configure Environment Variables

    Add environment variables:

    VariableValue
    DATABASE_URLYour database connection string
    BASE_URLYour short URL base (e.g., https://short.example.com)

    Configure HTTP Settings

    Set the traffic type to HTTP and configure the internal port (3000 for Node.js, 5000 for Python).

    Attach Persistent Storage

    If using SQLite:

    Mount PathRecommended SizePurpose
    /app/data1 GBSQLite database file

    Deploy Your Application

    Click Deploy to start the build and deployment process.

    Configure Custom Domain

    For short URLs, you’ll want a short custom domain. Configure this in your app settings.

    Start Creating Short Links

    Access your deployment and start creating shortened URLs.

Using Your URL Shortener

After deployment, you can create short links:

Via API:

Terminal window
curl -X POST https://your-shortener.klutch.sh/api/shorten \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com/very/long/path"}'

Response:

{"short_url": "https://your-shortener.klutch.sh/abc123"}

Additional Resources

Conclusion

Deploying a simple URL shortener on Klutch.sh gives you control over your links and privacy. With a custom short domain and self-hosted infrastructure, you can create professional, branded short URLs without relying on third-party services.

Whether for personal use, marketing campaigns, or internal company links, a self-hosted URL shortener provides the flexibility and control that commercial services cannot match.