Hosting your own social network using Mastodon and Exoscale

February 10, 2023 
Denis ArnstDenis Arnst
 
tutorial

For various reasons, more and more people are turning away from classic social media apps to alternatives like Mastodon, which is a self-hosted alternative to Twitter. Mastodon can be used as a blogging tool for yourself, your friends and company, or as a full social network.

Installing Mastodon on Exoscale

Running your own server is easy. On top it is fully compatible with Exoscale services.

Requirements for installation:

  • An Exoscale instance with Ubuntu or Debian
  • Your own (sub-)domain to be used for your Mastodon installation pointing to the Exoscale instance
  • A certificate for your domain, can be easily generated with Let’s Encrypt
  • An email delivery service or an SMTP server
  • PostgresSQL database: Self-hosted or leveraging our Managed PostgreSQL DBaaS
  • Redis®: Self-hosted or Managed Valkey offering

Optionally you can use an Exoscale Object Storage bucket for media files.

By using our DBaaS offering you take advantage of automatic security-critical updates and backups. Regardless of wether you install the database yourself or use our managed offerings, you should definitly consider using the Exoscale Object Storage for media files. This way you are free in terms of size of data and can store them cost-effectively.

Tips and Tricks for the installation

Create an Exoscale instance (Medium is sufficient for starters) and make sure to follow the official Mastodon documentation closely after reading the full article here on the Exoscale blog. Don’t forget to set up a security group allowing Port 80/TCP, 443/TCP for web traffic, and Port 22 exclusively for your IP for SSH.

!!! note “SMTP server” Outgoing connections on SMTP are not allowed by default on Exoscale, as outlined in our Security Group Documentation. Alternatively, you can use SMTP services like MailGun, SendGrid, etc. If you just plan to do some testing without needing e-mail functionality, you can select in the installer that you want to use a local SMTP service and that you do not want to test it.

In case you just get a blank page (possibly with the Mastodon logo) after installation, most likely nginx does not have permission to enter the directory for the static files. You can quickly fix it by running chmod a+x /home/mastodon. Also make sure the check the nginx error logs in /var/log/nginx.

Preparing an Exoscale Managed PostgreSQL instance

Create a new PostgreSQL Database in the Exoscale Portal. For the beginning, you can use our hobbyist-2 offering. Make sure that the IP of the Exoscale instance is entered as IP filter and wait until the status of the database is Running.

Copy the Service-URI from the Portal and use it to connect to the database via PSQL like this:

psql postgres://avnadmin:thepassword@mastodon-exoscale-databaseuuid.aivencloud.com:21699/defaultdb?sslmode=require

Be aware that the psql command requires the postgresql and postgresql-contrib packages which can be installed by apt. When you are connected, create a new user called “mastodon” with permission to create new databases:

-- Use a securly generated random password
CREATE USER mastodon with encrypted password 'SECURERANDOMPASSWORD' CREATEDB;

The Mastodon interactive setup wizard - as explained in the official docs - will automatically create the database using the given user.

Also, create an empty postgres database. This is required as the setup checks the existence of the database to determine whether the connection to it works:

create database postgres;

When the installer asks, simply insert the hostname and the port of your database (as shown in the Exoscale Portal) and the credentials of the just-created mastodon user.

Using Exoscale Object Storage

As mentioned using an Object Storage is fully optional. Nevertheless, it is highly recommended as the amount of media in your instance can quickly rise. By connecting an S3-compatible object storage bucket you don’t have to care about increased demands and it is by far more cost-effective.

Replace de-fra-1 with the zone where your bucket is located. Replace also the name of the bucket and the access/secret keys. The S3_REGION directive is ignored when specifying an endpoint. Finally issue systemctl restart mastodon*.

Using Exoscale for Caching

Using Redis® is even easier. Simply start an instance in the Exoscale Portal, set up the IP filter and wait until it is running.

Issue systemctl restart mastodon* again. Check mastodon-streaming for errors (i.e. systemctl status mastodon-streaming.service -n 100).

Keeping the instance running is pretty straightfoward as with the setup we have just established, the application is stateless, and data resides in the SOS for files and the rest in the database which comes with built-in backup strategies as standard on Exoscale DBaaS.

Start your Mastodon network

Installation of Mastodon is straight-forward. With the tips and tricks mentioned and following the official documentation, you are ready to start your own Mastodon network. Once you have the instance fully set up, you can try posting, uploading images or following others (even following users from other instances). If you want to go a step further you can federate your instance with the world and have a full social network timeline!

LinkedIn Bluesky