What is Mastodon?

Mastodon is an open-source, free software allowing users to establish their social networking sites. It offers microblogging services comparable to those of Twitter’s services, which are provided by a sizable number of autonomous nodes or instances, each of which has its own set of rules for conduct, service terms, privacy policy, privacy settings, and moderation procedures.

Each user is a part of a particular Mastodon server, which functions as a federated social network and enables communication between users in various cases. By doing this, users will be free to choose a node whose standards they value while still having access to a broader social network.

setup a server

How to Setup Mastodon Server

To set up your own mastodon server, you will need a web server, a new domain name, an SSH connection, and a Mailgun account to manage sign-ups to your Mastodon server. There are three main elements needed to set up a Mastodon server, these include:

  • Docker – Useful virtualization software
  • Mastodon – The social network itself
  • Nginx (pronounced EngineX) – Multipurpose web server software used as a reverse proxy

Before you start setting up your Mastodon server, follow the steps below

Step 1: Create a Servermania Cloud account, and fill out the form for names, email, and passwords. Next should take you to fill out the billing method form. Now you can move to the project selector, select a managed hosting provider, make a restricted user account, and secure SSH access. You can add custom cloud server settings on the Site Settings page.

Step 2: Registering a domain name to point to your Mastodon instance requires completing the Add DNS Records section steps.

Step 3: Create an SMTP server so that Mastodon may send email notifications to users when they sign up, become a follower, get a message, and for other Mastodon activities. You can create your SMTP server and host it on the same machine as your Mastodon server. You might also use an SMTP service from a third party.

Installing Docker and Docker Compose

Mastodon can be installed using its included Docker Compose file. Each requirement for the Mastodon environment is installed and operated in a Docker container by Docker Compose. 

Install Docker Compose

Step 1: Download Docker Compose’s most recent version. The version marked as the latest release should be used in place of version 1.25.4 in the command below.

sudo curl -L https://github.com/docker/compose/releases/download/1.25.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

Step 2: Set file permissions

sudo chmod +x /usr/local/bin/docker-compose

Download Mastodon

Step 1: To create the Mastodon directory, replicate the Mastodon Git repository into the home directory.

cd ~/
git clone https://github.com/mastodon/mastodon.git
cd mastodon

Step 2: All commands related to Docker Compose should be executed in this directory unless otherwise specified.

Configure Docker Compose

Step 1: The docker-compose.yml file should be in your preferred text editor.

Step 2: Add # in front of the build lines to comment on it and a release number to the end of each image: tootsuite/mastodon line as here: tootsuite/mastodon:v3.3.0.

You should choose a specific release number, even if you can use the most recent release as the release. A list of all Mastodon releases is available on its GitHub website.

Step 3: Add the following from the image below in the database section. Now replace the password with the PostgreSQL database password you want to use for the Mastodon backend.

environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: mastodon_production
POSTGRES_USER: mastodon

The complete docker-compose.yml file must look like the example Docker file.

Step 4: Make a copy of the .env.production.sample file found in the current mastodon directory to start a new environment configuration file.

cp .env.production.sample .env.production

Step 5: Create an OTP SECRET and SECRET KEY BASE using the commands below. The output should be copied and pasted into the.env.production file’s SECRET KEY BASE and OTP SECRET lines, respectively.

echo SECRET_KEY_BASE=$(docker-compose run --rm web bundle exec rake secret)
sed -i -e "s/SECRET_KEY_BASE=/&${SECRET_KEY_BASE}/" .env.production
echo OTP_SECRET=$(docker-compose run --rm web bundle exec rake secret
sed -i -e "s/OTP_SECRET=/&${OTP_SECRET}/" .env.production
$ echo SECRET_KEY_BASE=$(docker-compose run --rm web bundle exec rake secret)
Creating mastodon_web_run ... done SECRET_KEY_BASE=8bc28644a18cc8f8e30ba30087b71e29ed0b53fcdfc6
$ echo OTP_SECRET=$(docker-compose run --rm web bundle exec rake secret)
Creating mastodon_web_run ... done
OTP_SECRET=28424e7560ad65d3af38e6d35f9ee7c7a3dfc8475ce2120ff7

Step 6: Create the VAPID PRIVATE KEY and VAPID PUBLIC KEY using the following command, copy the result once again, and then paste it into the respective VAPID PRIVATE KEY and VAPID PUBLIC KEY lines in the.env.production file.

$ docker-compose run --rm web bundle exec rake mastodon:webpush:generate_vapid_key
Creating mastodon_web_run ... done
VAPID_PRIVATE_KEY=yzqlIVTpiNLtWXMHUTRuQIZNCv4hT0BkoMcsMU5-dz8=
VAPID_PUBLIC_KEY=BAy1_TgcWYBei7pXcX0MX-z0x-Cc85Fl9p-FhpDE4_BWT=

Step 7: Complete the remaining fields in the.env.production file as follows:

  • LOCAL_DOMAIN: Fill in your Mastodon server’s domain name.
  • DB_PASS: Fill in your preferred password for the database in the docker-compose.yml file.
  • Insert mastodon_db_1 for DB_HOST and mastodon_redis_1 for REDIS_HOST fields, respectively. Mastodon is the name of the base folder for Mastodon in both of these values.
  • Your SMTP provider’s data should be used to complete the SMTP fields. The following lines need to be added if you configure your SMTP server using its domain name for SMTP SERVER:
SMTP_AUTH_METHOD=plain
SMTP_OPENSSL_VERIFY_MODE=none

Add a # before each line in the sections marked as “optional” to comment them out.

Step 8: The .env.production file that is generated should look like the sample environment file.

Complete the Docker Compose Setup

Step 1: Create the Docker Compose environment.

docker-compose build

Step 2: Grant user 991 control over the Mastodon public directory. This command guarantees that the default user ID for Mastodon has the required rights.

sudo chown -R 991:991 public

Step 3: Start the Docker Compose setup procedure for Mastodon. You are requested to enter details for the Mastodon instance and the Docker Compose services.

docker-compose run --rm web bundle exec rake mastodon:setup
  • Many of the questions replicate the fields you filled out in the .env.production file. Ensure that the data you enter here matches what you entered in the file.
  • Select “Yes” (Y) to the request to register as a Mastodon admin user. The login information you need to access the account is your username, password, and email address.
  • When prompted with any other values, hit Enter to enter the defaults.

Initiate the Docker Compose Services

Step 1: Initiate the Docker Compose services.

docker-compose up -d

Step 2: The Docker Compose services start up automatically on a system startup and continue operating until manually terminated. To terminate the Docker Compose services yourself, issue the following command.

docker-compose down

Setup an HTTP/HTTPS Proxy

Step 1: Enable HTTP and HTTPS connection on the system’s firewall.

sudo ufw allow http
sudo ufw allow https
sudo ufw reload

Step 2: Install NGINX so requests are redirected to your Mastodon server.

sudo apt install nginx

Step 3: Place the nginx.conf file from the Mastodon installation in the sites-available NGINX folder, replacing example.com with the domain name of your Mastodon account:

sudo cp ~/mastodon/dist/nginx.conf /etc/nginx/sites-available/example.com.conf

Step 4: Replace all entries of example.com with the URL for your Mastodon site by opening the example.com.conf file in your preferred text editor.  This domain name must be the same as you entered when configuring Docker Compose for Mastodon.

Step 5: Make a symbolic link to this file in the NGINX folder for sites.

cd /etc/nginx/sites-enabled
sudo ln -s ../sites-available/example.com.conf

Get an SSL/TLS Certificate

You require an SSL/TLS certificate because Mastodon is hosted over HTTPS. Therefore, follow the processes below using Certbot and Let’s Encrypt.

Step 1: Update the Snap app store. All Ubuntu releases since 16.04 have used Snap by default, which offers application bundles compatible with the majority of Linux distributions:

sudo snap install core && sudo snap refresh core

Step 2: Ensure sure to delete any previous Certbot installations.

sudo apt remove certbot

Step 3: Install Certbot.

sudo snap install --classic certbot

Step 4: Download a certificate for your site.

sudo certbot certonly --nginx

You are given the option to choose from the NGINX sites set up on your computer by Certbot. Choose the instance with the domain name you configured for your Mastodon instance.

See also: How to Install and Configure Nginx on Ubuntu 20.04

Step 5: A chron job that is part of Certbot will automatically renew your certificate before it expires. The following command will allow you to test the automatic renewal.

sudo certbot renew --dry-run

Step 6: Open the /etc/nginx/sites-available/example.com.conf file again, un-comment the ssl_certificate and ssl_certificate_key lines, and restart the NGINX server.

sudo systemctl restart nginx

Using Mastodon

Step 1: Open a web browser and go to the domain of your Mastodon site. You should now be able to access the Mastodon login page and sign in using the admin user you previously created or by creating a new user.

Step 2: The administrative page for your instance can be reached by going to example.com/admin/settings/edit. You can modify your instance’s appearance, impression, and conduct using the administrative page.

Step 3: You can resolve your instance’s problems while running the Sidekiq dashboard. View the dashboard, choose Sidekiq from the administrative menu or go to example.com/sidekiq.

When you are prepared to share your instance with the world, you can complete the admin form on instances social and add it to the list there.

Conclusion 

Twitter has drawn criticism for its tardy response to harassment and the rise of accounts that support and promote religious extremism. As a result, attention has been drawn toward using Mastodon as an alternative to Twitter. After following the above processes in the guide, you can explore the settings and interaction options now that your own server/instance is operational.

For more information on Mastodon, you can visit the Mastodon blog,  interact with the administrative Mastodon community and discuss technical difficulties and community management on the Mastodon discussion forum.

If you are looking for a server hosting company to help you with media streaming and hosting your Mastodon server, we can help. ServerMania is a server hosting company that offers dedicated, hybrid, and cloud servers that are reliable, secure, scalable, and perfectly tailored to your needs. Visit ServerMania today to get started.