Configure Mastodon with Sonic + CDN

Mastodon logo

Mastodon is a free and open source software that allows you to run your own, self-hosted social network similar to Twitter. Mastodon became widely popular in 2022, following the announcement of Elon Musk purchase of Twitter. As your social network grows beyond your server's disk space, you might consider offloading user-uploaded media content to the cloud and adding a delivery layer to ensure images and video load fast for all your users. Thankfully, Mastodon offers a very easy way to do this and with PUSHR it takes just a few clicks to set up both cloud storage and CDN. 

Setting up your PUSHR storage zone

Let's start by setting up a push (storage) zone. When you create a push zone, PUSHR does two things in the background for you: 

• Creates a S3-compatible object storage space in your account, a storage bucket, generates access keys, and configures the storage space to be ready for use

• Creates a CDN configuration file and applies the configuration to our network edge to enable your CDN for you

Head over to your PUSHR dashboard and click to add a push zone:

Set up Sonic storage space for Mastodon

For a more detailed guide on how to set up a push zone, follow the steps here and remember to select S3-compatible storage type for Sonic. Once done, continue with the next step:

Sonic object storage + CDN via the Mastodon setup wizard

Mastodon comes with a setup wizard for new installations. When thw wizard is ran, among other things, it will ask if you want to store uploaded files to the cloud. Answer with y and select Minio as a storage provider:

Do you want to store user-uploaded files on the cloud? yes
Provider Minio

The wizard will then ask you to fill in your storage credentials. In your PUSHR dashboard, those can be found inside the Storage & Hostnames page when you click on the storage zone you created in the previous step:

Find you S3 endpoint URL, access key, secret key and bucket name

Copy and paste the S3 endpoint URL, access key, secret key and bucket name from your dashboard to the Mastodon wizard:

Minio endpoint URL: https://s3.de01.sonic.r-cdn.com
Minio bucket name: [your_bucket_name]
Minio access key: [your_access_key]
Minio secret key: [Your_secret_key]

Mastodon will now store all user-uploaded content to your Sonic storage space 👏 The last step left is to enable CDN for your Mastodon server so that PUSHR can cache and deliver the stored content at maximum speed to all your users. Copy the CDN Hostname which is displayed under the storage credentials in your dashboard, answer to yes: when the wizard asks to access files from your own domain, and paste the CDN hostname:

Do you want to access the uploaded files from your own domain? Yes
Domain for uploaded files: [your_cdn_hostname]

That's it. Your Mastodon-based network can now store unlimited amount of user-uploaded content and will serve it with low latency and high speed to everyone around the world ⚡

Adding Sonic object storage + CDN to a running Mastodon server

If your Mastodon service is in production, remember to sync exisitng user-uploaded content from your server to your Sonic object storage bucket before you apply the new configuration.

If your Mastodon server is already up and running, you can easily enable Sonic and PUSHR's CDN by manually editing the .env.production configuration file located in the /home/mastodon/live directory. Add the following lines to the file, and replace the secret key, access key and bucket name with the ones shown in your PUSHR dashboard:

S3_ENABLED=true
S3_PROTOCOL=https
S3_REGION=us-east-1
S3_ENDPOINT=https://s3.de01.sonic.r-cdn.com
S3_HOSTNAME=s3.de01.sonic.r-cdn.com
S3_BUCKET=2794
AWS_ACCESS_KEY_ID=1MNFV7JUJ55V1CA7WQXT0
AWS_SECRET_ACCESS_KEY=OFNKQlI1w0RUSEvDOEs1LK3DlkVYwzE5MEJZSU0z
S3_ALIAS_HOST=c756z2794.r-cdn.com

Restart Mastodon with the following commmands to enable the new configuration:

sudo systemctl restart mastodon-web
sudo systemctl restart mastodon-streaming
sudo systemctl restart mastodon-sidekiq

If everything went smoothly, your Mastodon instance just received super powers 💪 Visit your Mastodon website to confirm all images are showing up, and run a test upload to confirm proper operation. 

Mastodon with object storage and CDN  by PUSHR - setup completed