Mastodon: Comprehensive Guide to Installing and Using Mastodon

Introduction

Mastodon is a free, open-source social networking server based on the ActivityPub protocol. It allows users to create their own communities, interact with others, and enjoy a decentralized social media experience. This guide will walk you through the steps of signing up for a Mastodon account, installing Mastodon on your server, and getting started with its API.

1. Signing Up for a Mastodon Account

Choosing a Website

To join it, you need to select a website, also known as an instance. Each instance has its own community, policies, and moderation rules. Think of it like choosing an email provider or a gaming server for your new character. The instance you choose will host your account, profile, and home feed.

Understanding a Website’s Policies

Before signing up, it’s crucial to understand the policies and terms of use of your chosen instance. These are usually listed on the /about/more page, accessible by clicking “learn more” on the landing page while not logged in.

Signup Modes

It instances can have different signup modes:

  • Open Signup: Register immediately by providing your username, email, and password.
  • Server Invites: The registration form is disabled, and you need an invite link to join.
  • Approval-Based Registration: Fill out a registration form, including a reason for joining, and await moderator approval.

Your Username and Your Domain

Mastodon usernames consist of two parts: the local username (e.g., alice) and the domain of the website (e.g., example.com). When sharing your username with others, always include the domain, e.g., @a>alice@example.com</a, to ensure people can find you easily.

2. Installing Mastodon

Preparing Your Machine

Disable Password-Based SSH Login

Ensure you log in using SSH keys and not passwords to prevent lockout. Many hosting providers allow uploading a public key and setting up key-based root login automatically.

Edit /etc/ssh/sshd_config and set PasswordAuthentication to no. Restart the SSH service if changes are made:

systemctl restart ssh.service

Update System Packages

Keep your system updated:

apt update && apt upgrade -y

Install fail2ban

Install fail2ban to block repeated login attempts:

apt install fail2ban

Edit /etc/fail2ban/jail.local with the following configuration:

[DEFAULT] destemail = your@email.here sendername = Fail2Ban [sshd] enabled = true port = 22 mode = aggressive

Restart fail2ban:

systemctl restart fail2ban

Install a Firewall

Install iptables-persistent and configure it to allow only SSH, HTTP, and HTTPS ports:

apt install -y iptables-persistent

Edit /etc/iptables/rules.v4:

*filter # Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0 -A INPUT -i lo -j ACCEPT -A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT # Accept all established inbound connections -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow all outbound traffic - you can modify this to only allow certain traffic -A OUTPUT -j ACCEPT # Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL). -A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -p tcp --dport 443 -j ACCEPT # (optional) Allow HTTP/3 connections from anywhere. -A INPUT -p udp --dport 443 -j ACCEPT # Allow SSH connections # The -dport number should be the same port number you set in sshd_config -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT # Allow ping -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT # Log iptables denied calls -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 # Reject all other inbound - default deny unless explicitly allowed policy -A INPUT -j REJECT -A FORWARD -j REJECT COMMIT

Load the configuration manually:

iptables-restore < /etc/iptables/rules.v4

If your server uses IPv6, edit /etc/iptables/rules.v6:

*filter # Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0 -A INPUT -i lo -j ACCEPT -A INPUT ! -i lo -d ::1/128 -j REJECT # Accept all established inbound connections -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow all outbound traffic - you can modify this to only allow certain traffic -A OUTPUT -j ACCEPT # Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL). -A INPUT -p tcp --dport 80 -j ACCEPT -A INPUT -p tcp --dport 443 -j ACCEPT # (optional) Allow HTTP/3 connections from anywhere. -A INPUT -p udp --dport 443 -j ACCEPT # Allow SSH connections # The -dport number should be the same port number you set in sshd_config -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT # Allow ping -A INPUT -p icmpv6 -j ACCEPT # Log iptables denied calls -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7 # Reject all other inbound - default deny unless explicitly allowed policy -A INPUT -j REJECT -A FORWARD -j REJECT COMMIT

Load the IPv6 rules manually:

ip6tables-restore < /etc/iptables/rules.v6

3. Writing an App for Mastodon

Getting Started with the API

Introduction to REST

It provides access to its data via a REST API, using HTTP for requests and JSON for payloads.

Understanding HTTP Requests and Responses

The Mastodon API uses standard HTTP methods:

  • GET: Read a resource.
  • POST: Send information to the server.
  • PUT | PATCH: Update a resource.
  • DELETE: Remove a resource.

Using cURL for API Requests

Examples using cURL, a command-line utility for making HTTP requests:

GET Request:

curl https://mastodon.example/endpoint

POST Request:

curl -X POST -d 'key=value' https://mastodon.example/endpoint

Sending JSON Data:

curl -X POST -H 'Content-Type: application/json' -d '{"key":"value"}' https://mastodon.example/endpoint

Providing Parameters

Parameters can be provided as query strings, form data, or JSON.

Query Strings:

curl https://mastodon.example/endpoint?q=test&n=0

Form Data:

curl -X POST -d 'q=test' -d 'n=0' https://mastodon.example/endpoint

JSON Data:

curl -X POST -H 'Content-Type: application/json' -d '{"parameter":"value"}' https://mastodon.example/endpoint

Data Types

Arrays:

curl -X POST -d 'array[]=foo' -d 'array[]=bar' https://mastodon.example/endpoint

Nested Parameters:

curl -X POST -d 'source[privacy]=public' -d 'source[language]=en' https://mastodon.example/endpoint

Booleans:

curl -X POST -H 'Content-Type: application/json' -d '{"enabled":true}' https://mastodon.example/endpoint

Files:

curl -X POST -F 'file=@filename.jpg' https://mastodon.example/endpoint

Handling Responses

The REST API returns JSON responses with HTTP status codes indicating the result:

  • 200: OK
  • 4xx: Client error (e.g., 401 Unauthorized, 404 Not Found)
  • 5xx: Server error (e.g., 503 Unavailable)

4. Understanding the Mastodon Backend

Technical Overview

It is a Ruby on Rails application with a React.js front-end. Dependencies include Ruby, Node.js, PostgreSQL, and Redis. The development environment should have these dependencies installed natively.

Setting Up a Development Environment

Follow the "Installing from source" page in the Mastodon documentation to install dependencies. The development environment automatically creates an admin account with the email code>admin@localhost:3000</code and password mastodonadmin.

Environments

It comes with configurations for development, testing, and production environments. The default environment is development, which reloads Ruby code automatically and shows stack traces in the browser.

Also, read the relevant blog here: https://hyscaler.com/resources/install-detectron2-on-ubuntu/

Conclusion

This comprehensive guide has walked you through the essential steps of getting started with it, from choosing the right instance and signing up for an account, to preparing your server for installation and making use of Mastodon’s API. By understanding the different signup modes and the importance of selecting a suitable instance, you can better integrate into its community.

The detailed instructions on server preparation and security measures ensure a robust and secure environment for your Mastodon instance also found on this site: github.com. Additionally, exploring the API provides you with the tools to develop and interact with Mastodon programmatically, allowing for custom applications and integrations. Finally, insights into the backend setup and development environment lay the groundwork for contributing to Mastodon’s continuous improvement.

By following this guide, you are well on your way to becoming a proficient Mastodon user, admin, and developer. Whether you are setting up a personal instance, creating a community, or developing applications that enhance the Mastodon experience, the knowledge gained here will serve as a solid foundation for your endeavors. Enjoy your journey into the decentralized world of Mastodon!