How to Raise a Mastodon Instance (Similar to Twitter)
It’s been two months since I started migrating from Twitter to Mastodon (further in words). Mastodon is a complete analogue of twitter, only you can raise your server and all servers are interconnected, so you can communicate with the entire network. Content, especially English, in the mastodon sea, journalists, NASA, all major news publications are already there and among my subscriptions. If you do not want to fuss, then you can create an account on any server where registration is open. There are web clients and applications for android and ios. All this is called a federated network. You don’t like the server moderator, start your own or migrate to another one. I’m less and less on Twitter now. But it was not interesting to simply create an account in Mastodon, so I raised an instance for myself (as Mastodon servers are usually called), which I will write about.
I especially want to note the adequacy and interestingness of the inhabitants. So far, this network is not cancer-stricken like twitter. So there are few shitposting and inadequate ones. And just like on Twitter there is not one Elon, our everything, Musk, who is able to ban anyone at the request of the left heel. Not only that, since you are the server admin, you can ban entire fediverse servers, so that any nastiness, from your point of view, quickly remains isolated and can communicate quite calmly among themselves on their servers and between their servers within their fediverse segment. There are many instances where they are allowed only by invitation or not allowed at all. At the same time, they can read everyone, everyone can read them, but they can ban anyone they don’t want to see. In general, everything is honest and calm, everyone lives with whom they want and how they want, and can communicate. Hundreds of instances, maybe thousands. With millions of users already.
So, server setup. Bought a domain name anyqn.com. Then I bought myself a VPS on DigitalOcean. Its charm is that there is a ready-made image for deployment, with a server. Mastodon droplet https://marketplace.digitalocean.com/apps/mastodon. A little about her is written here https://blog.joinmastodon.org/2019/04/mastodon-now-available-on-digitalocean/
I registered DNS so that it would output to the droplet with the installed mastodon. It is not difficult to do and is described here. https://docs.digitalocean.com/tutorials/dns-registrars/ and here https://docs.digitalocean.com/products/networking/dns/how-to/add-subdomain/ consists in the fact that you need to buy a domain name and then register with the provider as DNS servers for this domain name the DigitalOcean DNS server. After that, all requests to the domain will be processed by these servers, and already in the DigitalOcean control panel, you can make subdomains lead to a specific droplet (VPS server) that you launched with a mastodon on board. In my case, the mastodon is raised to mtdn.anyqn.com and if you go in, the normal web interface will open. I did not want to turn the root domain into a mastodon host, since I still need to raise services. It would be nice to make a page at the root, but the hands will not reach in any way.
When installing a ready-made mastodon assembly on a VPS, some questions will be asked so that the application can work later. Among them, it is important that just the correct hostname leading to the droplet be given, since the nginx config for this name will be generated. The second important thing is the name of the smtp server that will be used by the application to send emails with account confirmations during registration. In order not to pay for the mailing service, I just made a box in gmail.com and generated smtp login and password for it. Since I do not expect millions of users on my home server. It’s roughly described here. https://support.google.com/mail/answer/185833?hl=en and here https://support.google.com/a/answer/176600?hl=en, you can also easily find many articles on how to configure smtp mail access to send emails. You can use protonmail where mailings can be made fatter. As a result, in the config (default setting) /home/mastodon/live/.env.production, properties will appear on the server
SMTP_SERVER=smtp.gmail.com SMTP_PORT=587 SMTP_LOGIN=adm@gmail.com SMTP_PASSWORD= SMTP_AUTH_METHOD=plain SMTP_OPENSSL_VERIFY_MODE=client_once SMTP_FROM_ADDRESS=Mastodon notifications@mtdn.anyqn.com
After I set up mastodon, I needed a little server setup, not mandatory but useful. The first thing I did was install https://jemalloc.net/ this is an implementation of a memory allocation library that can fragment it less, read if you want what it is and why. There is an analogue from Google if necessary, but it seemed very complicated to me, I did not bother. In addition, I wrote LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 in /etc/environment so that this particular library would be used when starting applications. I didn’t particularly understand the benefits of this case, so if you have any recommendations for fine-tuning or any other, they will be grateful, write in the comments.
The second thing I did was install the liquorix kernel.
curl’https://liquorix.net/install-liquorix.sh‘ | sudo bash
this was done because, firstly, it is fresh, and secondly, unlike the native one for the droplet, there is a zram module. After installing the kernel and rebooting the droplet
systemctl poweroff -i (so that everything closes decently) I installed zram-config
apt-get install zram-config
by default, this will cause half of the RAM to be allocated (not immediately, but as needed) for compressed memory pages. This will expand the RAM on the server by about a third, and I just had to do this in order to launch my service and not pay extra. In addition, I made an additional swap file on the disk, described here https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-22-04 With a lower priority than compressed in memory. So that if something happens, OOMKiller does not come nailing everything, it’s better to let it slow down, there is still no load. As a result, everything healed with my services and it became 10 bucks cheaper.
Ideally, I would also enable ksm, but unfortunately it only works if applications explicitly mark memory as suitable for page deduplication, and only virtual machines do this. I did not find a way to start it forcibly for normal processes, at least I did not find a simple and convenient one, and not abandoned ten years ago by the author. Maybe there are some other things that would somehow optimize memory consumption, if you have any tips, write in the comments. The most valuable thing is memory.
Then it is useful to set up cron rules for clearing the cache, Mastodon itself cannot do this, unfortunately. Otherwise, the relay will quickly fill up the disk of the instance, there are gigabytes per day. We type the command su – mastodon, then crontab -e and write to the file
“`
SHELL=/bin/bash
PATH=/home/mastodon/.rbenv/shims:/home/mastodon/.rbenv/bin:/usr/local/bin:/usr/bin:/bin
0 3 * * * RAILS_ENV=production /home/mastodon/live/bin/tootctl media remove –days 1
0 4 * * * RAILS_ENV=production /home/mastodon/live/bin/tootctl preview_cards remove –days 1
0 5 * * * RAILS_ENV=production /home/mastodon/live/bin/tootctl statuses remove –days 1
“`
The next important step in setting up the server is to go to the web interface, to the admin panel and set the repeater itself in the “administration”->”repeaters” section. This is such a special server that collects posts from hundreds of Mastodon instances and sends them between them. These servers are not particularly needed for the operation of the fedivers. You need this to be on your server until you have subscriptions and many users, there was content. If you have subscribed to someone a mastodon and will start collecting the feed of the server you subscribed to, but on an empty server where you have one repeater, it will collect the feeds and send them to you, and among the feeds you will find someone to subscribe to and somehow everything will grow.
I would especially like to note the principle by which you see communication in Mastodon. There is a thread in which someone answers. If the relay does not take posts for your instance, you are not subscribed to people and they do not mention you, then you will not see their communication in the thread at all. This is what upset me about Mastodon, as it is more profitable to live on large instances than to have your own small one without a repeater.
I had to google mastodon repeaters. But then I realized that you can find a site with statistics on Fediverse, for example this one https://the-federation.info/platform/44 then find large mastodon instances on it and just go to the relay (relay, synonym) urls. There they are on the list. This is generally an interesting site, I recommend it for study, a list of projects based on ActivityPub, nodes, relays, etc. For example, a relay https://relay.mastodon.kr/inbox
It should be noted here that you need to attach an inbox to the url for a specific mastodon relay, this is the entry point for the mastodon instance in order to collect the tape. Based on the ActivityPub protocol, there is more than one server implementation and others use different entry points.
Unfortunately, the repeaters are not open, so I had to go to them and look in the descriptions of who holds them, and then contact them through the mastoday and ask them to allow my instance to use the relay. This is understandable since there is a lot of traffic and the relay is not rubber. They wrote to me in the first relay that there are no free resources yet, but a week later they wrote that they had opened access and I added the relay to my instance in the settings. The feed became much richer, but by that time I had already googled lists of interesting users and subscribed to them myself, so the feed came to life. An example list can be found here https://www.reddit.com/r/Mastodon/comments/yugh2o/some_useful_mastodon_lists/?utm_source=share&utm_medium=web2x&context=3 or here https://getstarted.social/
When I first came to Twitter, it was also unclear who to subscribe to here and in general what was happening. In Mastodon, this feeling is alleviated by the fact that users try to collect curious accounts in a list and publish them. So it’s easier.
After it became clear with this, I just began to post something regularly, they began to answer me from other servers, subscriptions arose, life somehow went on. Some of the people I know on Twitter moved to Mastodon, though without raising their server. But I didn’t want to lose communication on Twitter, so I wrote myself a sync between Twitter and Mastodon. He works by posting tweets if I post something on mastodon and replies if they appear on twitter, but he already posts them on mastodon from twitter. Unfortunately, Twitter has blocked my application and is still silent why, so in a not quite finished form, I published what is here https://github.com/3DRaven/mastodoit
The application itself consists of a client and a server. The server is only needed to hide the clientssecret that twitter gave me to access the api. The server does not remember anything important, you can see it in the source code. No tokens are stored on it. All tokens are stored on the client, just like most of the connections are made from the client. Unfortunately, it was not possible to make a completely client-side open solution, since in order to authenticate a request, you need to have both an application token and a user token. But you can raise your server just by registering as a developer on twitter and getting the keys.
In general, in the end, everything worked out, I did not find any problems, bugs or other troubles.