NetApp Closes Acquisition of Instaclustr Read the announcement
  • Technical
  • PostgreSQL
Create a Mastodon server with Instaclustr

Twitter is doing great! The new owner claims that daily active users are at an all-time high, a successful subscription model has just been launched and previously dormant accounts are being given a new life — just ignore everything else that everyone is saying about the company... 

Still, the current Twitter regime may not be for everyone. It is a great time to look at the landscape and see what other competitors are out there 

Today we will look at Mastodon, an open-source social network platform.  

We will then explore how we can start our own Mastodon server and use the Instaclustr Managed Platform for PostgreSQL® to host the storage back-end. 

What is Mastodon?

Mastodon is an open-source solution for creating social networks by federating different instances together and presenting a single home feed using the ActivityPub protocol.  

Mastodon users register and “belong” to their specific Mastodon server but can immediately start sharing stories, interacting and following other users across the federated network.  

The important way it differs from Twitter is that it is not a centralized social media controlled by a single owner. Instead, administration and content moderation policies are implemented by the content owners themselves. 

On the technical side, Mastodon is built on a combination of Ruby, NodeJS & ReactJS for the application, and relies on PostgreSQL for persistence, Redis for caching and Elasticsearch, optionally, for full text search. 

Why run your own Mastodon?  

Social media giants like Twitter have captured a dizzying number of users and these users are at the mercy of whomever currently runs the company.  

For Twitter users, this means you could be banned without any warning or forced into paying for a subscription just to ensure your posts stay visible.  

For a company, you may be placed into uncomfortable associations with individuals or groups that could damage you and your brand.  

All users are at the mercy of the company shuttering completely and losing a valuable tool for outreach, not to mention the investment that went into creating the content in the first place.  

By using a self-hosted Mastodon server, companies and individuals can take back control of the platform for themselves.  

All the content posted on your Mastodon instance is stored locally with the server you own and run. You can post content with the confidence that the text, images and video posted will not be taken down, deleted or lost.  

In the event Mastodon instances need to be shut down, users can even be migrated to other servers, ensuring you maintain control of your online presence.  

Mastodon on Instaclustr

Mastodon is built upon a foundation of open-source software that facilitates its operation — the main technology being PostgreSQL — which is used to store all the user’s information and content. 

First up: we will create a PostgreSQL server on Instaclustr’s managed service platform. After that, we will configure a Mastodon server on Debian 11 to use it. 

Before we get started, we should have our Debian 11 server running as we will need its IP address. 

Creating your PostgreSQL server 

The first thing we need to do is create our PostgreSQL server using the Instaclustr console. 

We will be following these instructions to guide us through the process: 

  1. Sign up and login to your Instaclustr Console account 
  2. Follow the “create cluster wizard” to create a PostgreSQL server in the cloud provider and region of your choice 
  3. Wait for the new cluster to finish provisioning and enter the Running state 

Now that our PostgreSQL server is running, we need to open the firewall and allow our Debian server to connect. 

Using the Instaclustr Console again, we want to click on our PostgreSQL cluster and select Firewall Rules: 

On this page, we will enter our Debian 11 server’s IP address and select Save. 

PostgreSQL configuration 

Now that we have a running PostgreSQL cluster, we can create a new user that Mastodon will use and give it access to the mastodon database. 

On the Instaclustr Console, we can select the “Connection Info” option and we will see the command we can run to connect as the default icpostgresql user: 

This will give us a psql  prompt. 

From this prompt, we can now create a new user called mastodon_user and give it a password which we will use in a later step: 

With these steps completed, PostgreSQL is configured and ready to be used by the Mastodon server. 

Mastodon Setup

Now that we have our PostgreSQL server running, we can set up our Mastodon server. 

Mastodon servers have a number of pre-requisites that are out of scope for this article, such as obtaining a domain name, generating SSL certificates and setting up an SMTP server for email. In the interest of brevity, we will assume these steps have already been completed. 

Today, we are going to be using Docker on a Debian 11 instance in AWS to host our Mastodon service. 

We will be closely following the instructions from this excellent github gist, with some minor modifications to use our Instaclustr PostgreSQL cluster. 

Prepare Debian 

Login to your Debian server and start performing the following steps: 

  1. First, we need to install Docker, following these instructions 
  1. Then, if required, we can install git using apt: 

Now we can pull the Mastodon git repository and change to the latest stable version: 

Next, we need to create a temporary .env.production file. This file will be populated later but we just need it to exist right now for the compose command: 

Finally, we can build the docker images: 

Remove Postgres container from compose 

The included docker-compose file configures a local PostgreSQL docker container, but we want to use Instacustr’s managed service, so we are going to remove this dependency from the configuration before starting our services. 

Using vim, we will modify the docker compose file and remove references PostgreSQL configuration: 

Now our compose file is ready. 

Create our environment file 

We can now use docker-compose to run the Mastodon configuration wizard by executing the following command: 

We will answer the wizard prompts as follows: 

  • Domain name: <DOMAIN_NAME> 
  • Do you want to enable single user mode? No 
  • Are you using Docker to run Mastodon? Yes 
  • PostgreSQL host: 54.71.216.232 
  • PostgreSQL port: 5432 
  • Name of PostgreSQL database: mastodon 
  • Name of PostgreSQL user: mastodon_user 
  • Password of PostgreSQL user: <PASSWORD> 
  • Database configuration works! 🎆 
  • Do you want to store uploaded files on the cloud? No 
  • Do you want to send e-mails from localhost? Yes 
    • If you have an STMP service, answer No and follow the prompts 
  • E-mail address to send e-mails “from”: [email protected]<DOMAIN_NAME> 
  • Send a test e-mail with this configuration right now? No 
  • Save configuration? Yes 
    • Note: this will print the configuration in the terminal window, we need to copy the values into our empty .env.production file. 
  • Prepare the database now? Yes 
    • This will connect to PostgreSQL and configure the database 
  • Do you want to create an admin user straight away? No 

With the wizard complete, we can start all our containers with: 

If everything ran correctly, we should have: 

  • The .env.production file populated with our config 
  • The Mastodon PostgreSQL database initialised 
  • The docker containers up and running on our Debian server 

Nginx setup 

The last step is to configure nginx on our Debian server to serve the Mastodon website. 

Again, we will follow the instructions from the github gist which will configure our http & https websites and serve them. 

Mastodon is now online 

Now from our local computer, we can connect to our Mastodon server by entering the DOMAIN_NAME URL in the browser of your choice: 

The front page is pretty empty! This server has no users or accounts so there is nothing to show. 

Now we need to grow our Mastodon server by inviting people to use our instance. 

Existing users can migrate to your new server from their current server, which will keep all their content and followers intact. This is a great way to kick start a new Mastodon instance and start filling it with content. 

Finishing up 

And there we have it; we are now part of the Mastodon social network!  

We are hosting our own server with its own content and community. We have total control of how long the content is persisted, how we want to run the server and we can scale from smaller instances to larger ones as our network grows. 

Our Mastodon server is backed by Instaclustr’s PostgreSQL managed service, which includes monitoring metrics, back-up and restore, and cluster resizing.   

All our managed service offerings include application and operating system security patching, maintenance operations and security accreditations, and are all backed by 24/7 support with PostgreSQL experts. 

If you are interested in taking advantage of this service, or any of Instaclustr’s offerings, reach out and contact us. 

Get in touch to discuss a solution for your PostgreSQL database from Instaclustr’s support team.

Learn more about PostgreSQL