Mastodon Glitch
I am running a Mastodon Glitch Edition instance. Here is a short description on how to install this fediverse software in a docker compose environment with Caddy as a reverse proxy.
It is important to emphasize, that there are many other great fediverse softwares, like Sharkey, that are worth to be investigated.
Install Mastodon Glitch Edition
Note
This description is about how I installed Mastodon Glitch Edition. Please also have a look at the Mastodon Documentation and the Mastodon Glitch Documentation.
A big disadvantage of Mastodon compared to other fediverse software is its more complex setup. Nevertheless I hope I can show you a way to try this adventure.
Docker Compose Project Folder
Create a new folder for your docker compose project and switch to this folder.
Compose File
Create the file compose.yml and copy the contents from Glitch’s
git repository: docker-compose.yml.
You can take the file as is, I did two changes for each of the services listed in the file:
I changed
restart: alwaysintorestart: unless-stopped.I gave all services a name to have a better overview:
container_name: mastodon_dbfor thedbservice.container_name: mastodon_redisfor theredisservice.container_name: mastodon_webfor thewebservice.container_name: mastodon_streamingfor thestreamingservice.container_name: mastodon_sidekiqfor thesidekiqservice.container_name: mastodon_esfor theesservice.
As an example how this looks like for db:
...
db:
restart: unless-stopped
image: postgres:14-alpine
container_name: mastodon_db
shm_size: 256mb
networks:
- internal_network
healthcheck:
test: ['CMD', 'pg_isready', '-U', 'postgres']
volumes:
- ./postgres14:/var/lib/postgresql/data
environment:
- 'POSTGRES_HOST_AUTH_METHOD=trust'
...If you want to enable elasticsearch then remove all # from the es service.
Mastodon Configuration File
Create the file .env.production and copy the contents from Glitch’s
git repository: .env.production.sample.
Following changes need to be done in .env.production:
LOCAL_DOMAIN=mastodon.example.com- Replace the domain with your domain for mastodon.REDIS_HOST=redis- In docker compose projects the service is to be used.DB_HOST=db- In docker compose projects the service is to be used.DB_PASS=mastodon- You have to add a password, it can be simple since the database is not accessible outside the docker compose project.SECRET_KEY_BASE=...- Execute:docker compose run --rm web bundle exec rails secretand copy the key here.ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY=...- Execute:docker compose run --rm web bundle exec rails db:encryption:initand copy the three lines here.ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT=...- See aboveACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY=...- See above.VAPID_PRIVATE_KEY=...- Execute:docker compose run --rm web bundle exec rails mastodon:webpush:generate_vapid_keyand copy the lines here.VAPID_PUBLIC_KEY=...- See above.SMTP_SERVER=<your_smtp_server>- Replace with your SMTP data.SMTP_PORT=587- See above.SMTP_LOGIN=...- See above.SMTP_PASSWORD=...- See above.SMTP_FROM_ADDRESS=...- See above.
If you want to enable elasticsearch, then uncomment ES_ENABLED=true, ES_HOST=es, and
ES_PORT=9200. It is important to change the host to es which is the service name in
the docker compose project.
Prepare the Database
The database preparation needs to be done before the docker stack is being started.
Create the mastodon role in the database by starting the Postgres SQL console:
docker compose exec db psql -U postgresThen execute the SQL command:
CREATE USER mastodon WITH CREATEDB PASSWORD 'mastodon';
\qNow setup the database:
docker compose run --rm web rails db:setupDo database migration even if the database is new:
docker compose run --rm web rails db:migrateChange Owner of the Public Folder
The mastodon user in the web container has the user ID and group ID 991.
The public folder needs to have exactly the ownership of the mastodon user:
sudo chown -R 991:991 publicChange Owner of the Elasticsearch Folder
Only needed if you enabled elasticsearch.
The es container has needs the user ID 1000 and group ID 0.
The elasticsearch folder needs to have exactly this ownership:
sudo chown -R 1000:0 elasticsearchStop and Start the Docker Stack
docker compose down
docker compose up -dCreate the Admin/Owner User
To be able to login to your instance, at least the Owner (which also gets the Admin role), needs to be created:
docker compose run --rm web tootctl accounts create USERNAME --email EMAIL --password PASSWORD --confirmed --role AdminUSERNAME and EMAIL needs to be adapted. The password is created by the tool. You need to copy and remember it to login.
Before You Continue with Caddy
Make sure that the docker compose project is running without errors. You can check by looking into the logs with:
docker compose logs -fIf you need to start over, you can always delete the created local folders, but you should do this before your Mastodon instance goes online:
- Stop the docker compose project:
docker compose down. - Delete the contents of the folders
elasticsearch,postgres14,public, andredis. - Do stuff that needs to be fixed.
- Start the docker compose project:
docker compose up -d.
Caddy Setup
Note
For a detailed documentation pleaser refer to the Caddy website.
Caution
In the Caddyfile always use TABs to indent statements. Spaces will cause format errors.
The section in the Caddyfile should look like this.
To be honest, I do not understand all the entries, yet.
But it worked for me:
<your_mastodon_url> {
@local {
file
not path /
}
@local_media {
path_regexp /system/(.*)
}
@streaming {
path /api/v1/streaming/*
}
@cache_control {
path_regexp ^/(emoji|packs|/system/accounts/avatars|/system/media_attachments/files)
}
root * <full_path_to_docker_compose_folder>/public
log {
output file /var/log/caddy/mastodon.log
}
encode zstd gzip
handle_errors {
rewrite 500.html
file_server
}
header {
Strict-Transport-Security "max-age=31536000"
}
header /sw.js Cache-Control "public, max-age=0"
header @cache_control Cache-Control "public, max-age=31536000, immutable"
handle @local {
file_server
}
## If you've been migrated media from local to object storage, this navigate old URL to new one.
# redir @local_media https://yourobjectstorage.example.com/{http.regexp.1} permanent
reverse_proxy @streaming {
to http://localhost:4000
transport http {
keepalive 5s
keepalive_idle_conns 10
}
}
reverse_proxy {
to http://localhost:3000
header_up X-Forwarded-Port 443
header_up X-Forwarded-Proto https
transport http {
keepalive 5s
keepalive_idle_conns 10
}
}
}You need to adapt following placeholders:
<your_mastodon_url>: Your Mastodon web address (e.g.,mastodon.example.com)<full_path_to_docker_compose_folder>: The full path to your docker compose project directory.
With following commands Caddy will read the new configuration and you can check for the status:
cd /etc/caddy
sudo caddy reload
sudo service caddy statusCaddy will provide the Mastodon website with the web address you specified. An SSL certificate will be assigned and updated automatically.
Fine Tuning the Database
If you open PgHero from the settings, you might see a warning: “Query stats must be enabled for slow queries”. Clicking on the “Enable”-button likely does not help, but gives you an error: “The database user does not have permission to enable query stats comes up.” If this is the case, then follow these steps:
Edit the Postgresql Configuration File
Open the postgresql.conf file in an editor with root rights:
sudo nano postgres14/postgresql.confSearch for the line with #shared_preload_libraries = '' # (change requires restart) and
insert below this line these two lines:
shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.track = allIf there are already other libraries, then add this one separated by a comma.
Then restart the docker compose project.
Enable the Database for Stats
Open a shell in the db container:
docker compose exec -it -u70 db /bin/shOpen a SQL console:
psql -U postgresEnter this command to see if the library pg_stat_statementsis in the list:
SHOW shared_preload_libraries;Now close the SQL console and open a new one with the Mastodon database:
psql -U postgres -d mastodon_productionThen create the extension of this database and allow the user mastodon to access it:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
GRANT pg_read_all_stats TO mastodon;Finally close the SQL console again and open now one for the user mastodon:
psql -U mastodon -d mastodon_productionCheck if the use has access by executing this SQL command:
SELECT * FROM pg_stat_statements LIMIT 5;The warning in PgHero should now have been disappeared.