Self-hosting social networks

Cyril Šebek • 11/22/2024, 5:42:25 PM

In recent months, previously obscure social networks have gained significant traction. Some of these platforms are federated, making them self-hostable and giving users more control over their digital interactions.

Introduction

At the beginning of July 2024, with some free time thanks to the school year ending, I finally finished configuring my servers. Everything I needed was running smoothly—apps and services were performing as expected. This left me with some extra time to experiment. Despite avoiding social networks in the past, the current situation with Twitter (now known as X) made me even more certain of my disinterest. However, I remembered a project I discovered soem time ago—Mastodon. While researching, I also came across another rapidly growing platform, Bluesky.

Mastodon: Decentralized and Federated

Mastodon is a decentralized, federated social network. Unlike centralized platforms like Twitter/X, where one company owns all servers, Mastodon servers are community-driven. Anyone can host their own instance, known as a “home server,” while still interacting with others through the magic of federation. This interconnected network of servers runs on the ActivityPub protocol and is often referred to as the Fediverse.

Why self-host Mastodon

There are several reasons to self-host Mastodon. For hobby groups, communities, or companies, hosting your own instance provides autonomy and flexibility. For me, it boils down to freedom—absolute freedom. Since there’s no central authority, each server can establish its own rules about acceptable content, allowing users to enjoy an uncensored environment.

Setting up Mastodon

Self-hosting Mastodon isn’t overly complex, and numerous guides are available online. I followed the official guide and created a new Debian 12 LXC container on my Proxmox server. My public domain for Mastodon is mastodon.cyrilsebek.cz, and while I don’t own other domains, subdomains worked just fine. However, I quickly discovered one major challenge — Mastodon is a disk space guzzler. Initially, I allocated 20 GB of storage, thinking it would suffice. Within a day, the site stopped loading due to lack of space. After repeatedly increasing the disk size and watching it balloon to 100 GB, I realized something is wrong and I needed a better solution.

Automatic Deletion

Mastodon doesn’t automatically clean up its storage. It downloads and stores a significant amount of data, including content from the wider Fediverse. To manage this, I created a script that deletes unused or unnecessary data older than four days:

#!/bin/bash

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

RAILS_ENV=production /home/mastodon/live/bin/tootctl accounts prune;

RAILS_ENV=production /home/mastodon/live/bin/tootctl statuses remove --days 4;

RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove --days 4;

RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove --remove-headers --include-follows --days 0;

RAILS_ENV=production /home/mastodon/live/bin/tootctl preview_cards remove --days 4;

RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove-orphans;

I also scheduled it to run every three hours using a cron job:

0 */3 * * * /bin/bash /home/mastodon/purge-media.sh

This setup has kept my Mastodon instance stable and storage manageable at around 60GB.

Bluesky: Modern Alternative

Bluesky is another microblogging social network similar to Twitter/X. Unlike Mastodon, Bluesky operates on the ATProto protocol and is a younger platform. Initially developed under Twitter in 2019, it became independent in 2021 following Elon Musk’s acquisition of Twitter. Today, Bluesky has over 21.5 million users as of November 22, 2024, and is one of the most popular Twitter/X alternatives. It’s also federated, but the protocol works a bit differently. The original idea behind it was to explore the possibilites of federated social network.

Why self-host Bluesky

Bluesky allows users to set up their own Personal Data Server (PDS), making it simple to manage and maintain control over data. The installation process is remarkably straightforward compared to Mastodon. A single script handles most of the setup, with minimal manual configuration required. You can find the detailed guide on Bluesky’s GitHub page.

Nginx vs. Caddy

One challenge I faced during setup was the automatic script assuming it was the sole service on the domain or IP address. By default, the script uses Caddy as a reverse proxy and manages SSL/TLS certificates. While this is convenient for less technical users, it didn’t suit my setup. I rely on Nginx to manage multiple services on my homelab, each using subdomains. To resolve conflicts, I configured Nginx to forward requests to the appropriate backend without interfering with SSL/TLS. Here’s my solution using Nginx’s stream module:

stream {
    # Define a map to route based on SNI
    map $ssl_preread_server_name $backend {
        bsky.cyrilsebek.cz 10.69.13.212:443;
        default 127.0.0.1:8443; # Fallback backend or drop the connection
    }

    server {
        listen 443;
        listen [::]:443;

        # Enable SNI inspection
        ssl_preread on;

        # Route traffic to the correct backend
        proxy_pass $backend;
    }
}

Conclusion

Both Mastodon and Bluesky are excellent options for self-hostable social networks. But you don’t need to selfhost either of them to use them. Mastodon offers unparalleled freedom and community-driven management, while Bluesky shines in simplicity and ease of setup. For users seeking a straightforward Twitter/X alternative, Bluesky is likely the better choice. However, Mastodon’s decentralized, federated structure provides more control and aligns with the ethos of a truly independent network. Ultimately, I’ve decided to use both. Each platform has its strengths, and self-hosting gives me the freedom to explore them fully.