Installing a Mastodon Server on Debian 10
- Ubuntu 20.04
- CentOS Stream 8
- Deprecated guides:
- Ubuntu 16.04
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Mastodon is an open source and self-hosted microblogging platform. It’s a social media platform similar to Twitter, allowing users to follow other users and post text, pictures, and video content. Unlike Twitter, Mastodon is decentralized, meaning that its content is not maintained by a central authority.
What sets the Mastodon platform apart is its federated approach to social networking. Each Mastodon server operates independently — anyone can host a server and build their own community. But users from different servers can still follow each other, share content, and communicate.
Mastodon participates in the Fediverse, a collection of social networks and other websites that communicate using the ActivityPub protocol. That allows different Mastodon servers to communicate, and also allows other platforms in the Fediverse to communicate with Mastodon.
Mastodon servers range in size from small private instances to massive public instances, and typically center on special interests or shared principles. The biggest is Mastodon server is Mastodon.social, a general-interest server created by the developers of the Mastodon platform. It has over 540,000 users and boasts a strong Code of Conduct.
Before You Begin
Familiarize yourself with our Getting Started with Linode guide and complete the steps for setting your Linode’s hostname and timezone.
This guide uses
sudo
wherever possible. Complete the sections of our How to Secure Your Server guide to create a standard user account, harden SSH access, and remove unnecessary network services.Complete the steps in the Add DNS Records section to register a domain name to point to your Mastodon instance.
Install and configure UFW for managing your machine’s firewall rules. Refer to the How to Configure a Firewall with UFW guide.
Prepare an SMTP server for Mastodon to send email notifications to users when they register for the site, get a follower, receive a message, and for other Mastodon activity.
You can create your own SMTP server — and even host it on the same machine as your Mastodon server — by following the Email with Postfix, Dovecot, and MySQL guide.
Alternatively, you can use a third-party SMTP service. This guide provides instructions for using Mailgun as your SMTP provider.
Update your system.
sudo apt update && sudo apt upgrade
Replace occurrences of
example.com
in this guide with the domain name you are using for your Mastodon instance.
NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed withsudo
. If you’re not familiar with thesudo
command, see the Users and Groups guide.
Install Docker and Docker Compose
Mastodon can be installed using its included Docker Compose file. Docker Compose installs and runs all of the requisites for the Mastodon environment in Docker containers. If you have not used Docker before, it is recommended that you review the following guides:
Install Docker
To install Docker CE (Community Edition), follow the instructions within one of the guides below:
For complete instructions on even more Linux distributions, reference the Install Docker Engine section of Docker’s official documentation.
Install Docker Compose
Download the latest version of Docker Compose. Check the releases page and replace
1.25.4
in the command below with the version tagged as Latest release: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
Set file permissions:
sudo chmod +x /usr/local/bin/docker-compose
Download Mastodon
Clone the Mastodon Git repository into the home directory, and change into the resulting Mastodon directory.
cd ~/ git clone https://github.com/mastodon/mastodon.git cd mastodon
Unless otherwise stated, the remainder of the commands related to Docker Compose should be run in this directory.
Configure Docker Compose
Using your preferred text editor, open the
docker-compose.yml
file.Comment out the
build
lines (adding#
in front of each), and append a release number to the end of eachimage: tootsuite/mastodon
line as here:tootsuite/mastodon:v3.3.0
.Although you can use
latest
as the release, it is recommended that you select a specific release number. The Mastodon GitHub page provides a chronological list of Mastodon releases.In the
db
section, add the following beneath theimage
line; replacepassword
with a password you would like to use for the PostgreSQL database that operates on the Mastodon backend.environment: POSTGRES_PASSWORD: password POSTGRES_DB: mastodon_production POSTGRES_USER: mastodon
The resulting
docker-compose.yml
file should look something like the example Docker file.Copy the
.env.production.sample
file to create a new environment configuration file.cp .env.production.sample .env.production
Use the following commands to generate a
SECRET_KEY_BASE
andOTP_SECRET
. Copy the output, and paste it into theSECRET_KEY_BASE
andOTP_SECRET
lines in the.env.production
file.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
Generate the
VAPID_PRIVATE_KEY
andVAPID_PUBLIC_KEY
using the following command. Copy the output, and paste it into theVAPID_PRIVATE_KEY
andVAPID_PUBLIC_KEY
lines in the.env.production
file.docker-compose run --rm web bundle exec rake mastodon:webpush:generate_vapid_key
$ 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=
Fill out the remainder of the
.env.production
file’s fields.LOCAL_DOMAIN
: Enter your Mastodon server’s domain name.DB_PASS
: Enter the password you set for the database in thedocker-compose.yml
file.Enter
mastodon_db_1
forDB_HOST
andmastodon_redis_1
forREDIS_HOST
. In both of these values,mastodon
corresponds to the name of the Mastodon base folder.Fill out the
SMTP
fields with the information from your SMTP provider. If you set up your own SMTP server, use its domain name forSMTP_SERVER
and add the following lines:SMTP_AUTH_METHOD=plain SMTP_OPENSSL_VERIFY_MODE=none
Comment out the sections denoted as “optional” by adding a
#
before each line in the section.
The resulting
.env.production
file should resemble example environment file.
Complete the Docker Compose Setup
Build the Docker Compose environment.
docker-compose build
Give ownership of the Mastodon
public
directory to user991
. This is the default user ID for Mastodon, and this command ensures that it has the necessary permissions.sudo chown -R 991:991 public
Run Mastodon’s Docker Compose setup script. You are prompted to enter information about the Docker Compose services and the Mastodon instance.
docker-compose run --rm web bundle exec rake mastodon:setup
Many prompts repeat fields you completed in the
.env.production
file. Make sure to enter the same information here as you entered in the file.When prompted to create a Mastodon administrator user account, choose to do so (
Y
). Enter the username, password, and email address you would like to use to access the account.For any other prompts, enter the default values by pressing Enter.
Initiate the Docker Compose Services
Start the Docker Compose services. The following command assumes that you are in the base Mastodon directory (
~/mastodon
in this guide):docker-compose up -d
Unless manually stopped, the Docker Compose services begin running automatically at system start up. Run the following command to manually stop the Docker Compose services:
docker-compose down
Setup an HTTP/HTTPS Proxy
Allow HTTP and HTTPS connections on the system’s firewall:
sudo ufw allow http sudo ufw allow https sudo ufw reload
Install NGINX, which proxies requests to your Mastodon server.
sudo apt install nginx
Copy the
nginx.conf
file included with the Mastodon installation to thesites-available
NGINX folder; use your Mastodon domain name instead ofexample.com
in the file name.sudo cp ~/mastodon/dist/nginx.conf /etc/nginx/sites-available/example.com.conf
Open the
example.com.conf
file with your preferred text editor, and replace all instances ofexample.com
with the domain name for your Mastodon site.Create a symbolic link of this file in the
sites-enabled
NGINX folder.cd /etc/nginx/sites-enabled sudo ln -s ../sites-available/example.com.conf
Get an SSL/TLS Certificate
Mastodon is served over HTTPS, so you need an SSL/TLS certificate. This guide uses Certbot to request and download a free certificate from Let’s Encrypt.
Install the Snap app store. Snap provides application bundles that work across major Linux distributions.
sudo apt install snapd
Update and refresh Snap.
sudo snap install core && sudo snap refresh core
Ensure that any existing Certbot installation is removed:
sudo apt remove certbot
Install Certbot.
sudo snap install --classic certbot
Create a symbolic link for Certbot.
sudo ln -s /snap/bin/certbot /usr/bin/certbot
Download a certificate for your site.
sudo certbot certonly --nginx
Certbot prompts you to select from the NGINX sites configured on your machine. Select the one with the domain name you set up for your Mastodon instance.
Certbot includes a chron job that automatically renews your certificate before it expires. You can test the automatic renewal with the following command:
sudo certbot renew --dry-run
Open the
/etc/nginx/sites-available/example.com.conf
file again, and un-comment thessl_certificate
andssl_certificate_key
lines.Restart the NGINX server.
sudo systemctl restart nginx
Using Mastodon
In a web browser, navigate to your Mastodon site’s domain. You should see the Mastodon login page, where you can login as the admin user you created earlier or create a new user.
You can navigate to your instance’s administration page by navigating to
example.com/admin/settings/edit
. The administration page allows you to alter the look, feel, and behavior of your instance.If your instance is running but having issues, you can troubleshoot them from the Sidekiq dashboard. Either select Sidekiq from the administration menu or navigate to
example.com/sidekiq
to see the dashboard.
To learn more about Mastodon, check out the official Mastodon blog with news and articles related to Mastodon. You can engage with the Mastodon administrator community on Mastodon’s discussion forum, where you can peruse conversations about technical issues and community governance.
When you are ready to make your instance known to the world, you can add it to the list over at Instances.social by filling out the admin form.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on