Sick of Twitter? Fed up with Facebook? Online abuse and NSFW content getting you down? Just want to exchange ideas away from the glare of people who oppose you?
Mastodon is the answer. And thanks to its open source nature, it is possible to set up your own Mastodon instance, enabling conversation on any topic and giving your users some safety and privacy from trolls of all kinds.
If you’re not familiar with Mastodon, here are the basics. Toots are like tweets, and can be composed of up to 500 characters (as opposed to Twitter’s 140 character limit). Check our recent look at Mastodon to see how it all works.
What You Will Need
The following is required to create your own instance of Mastodon:
- A web server running Ubuntu Server 16.04. You can find inexpensive server accounts for under $10 a month at Vultr.com, although other solutions are available.
- An unused domain name pointing to the server. This means that the DNS records at the web domain’s host are set to refer visitors to the IP address of the Ubuntu Server. How this is done differs between providers, so check your corresponding documentation.
- A Mailgun account to manage sign-ups to your Mastodon instance. This is available for free, but your credit card details are required to send the first 10,000 emails. Head to the Mailgun signup page to create an account, and make sure you follow the domain verification instructions to ensure your email new/unused domain is listed as active.
Setting up Mastodon requires three main elements:
- Docker — useful virtualization software
- Mastodon — the social network itself
- Nginx (pronounced EngineX) — multipurpose web server software, employed in this case as a reverse proxy
(A reverse proxy allows a server to retrieve resources or data from one or more other servers on behalf of a client. In many cases, this is a security measure to protect the server.)
Establish the SSH Connection and Install Docker
You’ll need to connect to your server over SSH and sign in with the usual admin credentials. Once this is done, create a user called “mastodon,” assign root privileges, and switch to the new user:
adduser mastodon usermod -aG sudo mastodon su - mastodon
Next, update the package database, and install the management tools:
sudo apt-get update sudo apt-get install apt-transport-https software-properties-common
The Docker repo needs a GPG key, which you can acquire with
sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
Next, add the Docker repository to sources, and update once more.
sudo apt-add-repository 'deb https://apt.dockerproject.org/repo ubuntu-xenial main' sudo apt-get update
To ensure this works correctly, Docker needs to be installed its own repository, rather than the version in the official Ubuntu 16.04 repo. This policy will ensure that happens:
sudo apt-cache policy docker-engine
You can now install Docker:
sudo apt-get install -y docker-engine
Wait while this completes. Installation includes a daemon that runs Docker when the server boots. Check the status with
sudo systemctl status docker
You’ll see a few lines of text — look for “active (running)” to confirm everything is as it should be.
Configure with Docker Compose
Creating a new user group will save time inputting the sudo command each time you run a Docker instruction in this configuration. Do this with
sudo usermod -aG docker $(whoami)
Finalize this by exiting the SSH setting (exit), then log back in.
Docker Compose is provided to manage the running of multi-container Docker applications. At github.com/docker/compose/releases you’ll find the latest release version. Make a note of the number, as you’ll need to input it in the next command, substituting it for “x.x.x”:
sudo curl -o /usr/local/bin/docker-compose -L "https://github.com/docker/compose/releases/download/x.x.x/docker-compose-$(uname -s)-$(uname -m)"
Make it executable:
sudo chmod +x /usr/local/bin/docker-compose
You can check that Docker Compose is installed with
The version number will then be displayed, , and it should match the version you downloaded — look out for the numbers you substituted for confirmation.
All the preparation is done. You’re now ready to install Mastodon itself. Switch to the mastodon sub-directory:
Next, clone mastodon.git into the directory, then open the location and make a copy of the .env.production.sample file.
git clone https://github.com/tootsuite/mastodon.git cd mastodon cp .env.production.sample .env.production
You can use that file in a moment. First, build the Docker image.
This will take a while. Once it has completed, you can use this command to create three secret keys:
docker-compose run --rm web rake secret
Run this three times, copying and pasting each unique key into a text editor. With all three ready, run the .env.production file in nano.
sudo nano .env.production
Look for the following fields: PAPERCLIP_SECRET, SECRET_KEY_BASE and OTP_SECRET. Each of these should have one of the secret keys inserted against them.
Add Your Mailgun Settings
It’s time to add the details for your Mailgun email account. Go to https://app.mailgun.com/app/domains and click the domain. Find the Default SMTP Login and Default Password, and copy the details into the .env.production file as entries for SMTP_LOGIN and SMTP_PASSWORD.
Next, look for SMTP_FROM_ADDRESS and ensure the entry for this has a name beginning “notifications”, such as firstname.lastname@example.org. Finally, look for LOCAL_DOMAIN and add the domain name you’re using for this project.
When done, hit CTRL+X to save and close, then rebuild Docker.
The following commands will ensure data migration and asset precompiling works as intended.
docker-compose run --rm web rails db:migrate docker-compose run --rm web rails assets:precompile
These commands will take a moment to complete. Once you’re done, enable the container:
docker-compose up -d
Nginx is used in this setup as a reverse proxy.
Install it with:
sudo apt-get install nginx
Nginx has a default profile, which you’ll need to delete:
sudo rm /etc/nginx/sites-available/default sudo rm /etc/nginx/sites-enabled/default
Create a new profile with
sudo touch /etc/nginx/sites-available/mastodon
Next, you’ll need to create a symbolic link for the profile to be accessed:
sudo ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon
You then have a configuration file to copy into the text editor. Visit this Github page and copy the contents, then paste into
sudo nano /etc/nginx/sites-available/mastodon
With the file open, find every instance of example.com and replace it with the domain name you’re using. Make sure you omit the “www”.
Hit CTRL+X to save and exit.
Get an SSL Certificate and Run Mastodon!
You’re now within sight of launching your Mastodon instance and creating your own social network. But for it to be trusted, you’ll need an SSL certificate.
Begin by installing the certbot PPA, and then certbot itself:
sudo add-apt-repository ppa:certbot/certbot sudo apt-get update sudo apt-get install certbot
Next, stop Nginx so that the SSL certificates can be generated.
sudo systemctl stop nginx.service
Follow this with the below command, substituting example.com with your own domain name.
sudo letsencrypt certonly --standalone -d example.com
Follow the prompts to complete the process. When you’re done, return to the mastodon directory:
The next step is to stop Docker temporarily:
We’re nearly done — just run these commands in turn:
docker-compose build docker-compose run --rm web rails assets:precompile docker-compose run --rm web rails db:migrate docker-compose up -d
Wait while these commands complete, then bring Nginx back up:
sudo systemctl restart nginx.service
Now, if everything has run okay, you should be able to open your browser window and head to the new Mastodon instance to see it working!
Any problems here, sadly, will mean repeating what you’ve done so far. If the .env.production is all correct, and Mailgun is working, then simply continue the steps following the last save of .env.production.Note that if your domain isn’t correctly configured with your domain registrar, and with Mailgun, then you won’t be able to run Mastodon until these problems are resolved.
Note that if your domain isn’t correctly configured with your domain registrar, and with Mailgun, then you won’t be able to run Mastodon until these problems are resolved.
You’re Up and Running: Automate Tasks and Administer Mastodon
We’re tantalizingly close to the end now. Just a few more minutes and you’ll be able to publicize your Mastodon instance.
Certain tasks need to be automated. Go to the mastodon directory, and create a new directory for the cron jobs — instructions that run at a predetermined time.
cd /home/mastodon nano mastodon_cron
In the text file, add:
cd /home/mastodon/mastodon docker-compose run --rm web rake mastodon:media:clear docker-compose run --rm web rake mastodon:push:refresh docker-compose run --rm web rake mastodon:push:clear docker-compose run --rm web rake mastodon:feeds:clear
Exit with CTRL + Y, then input:
sudo chmod +x mastodon_cron && sudo crontab -e
The crontab file — a sort of controlling index of all scheduled (cron) scripts – will open. Input this line at the end:
0 0 * * * /home/mastodon/mastodon_cron > /home/mastodon/mastodon_log
Press CTRL + X to save and exit.
Refresh the SSL
Although the SSL certificate is set up, it will expire after 90 days. The solution is a cron job that auto-renews the certificate. Input
sudo crontab -e
…then scroll to the end of the file and add these to lines:
0 1 * * 1 /usr/bin/letsencrypt renew >> /home/mastodon/letsencrypt.log 5 1 * * 1 /bin/systemctl reload nginx
Again, press CTRL + X to save and exit; the instruction will renew a certificate over 60 days old, at 1am on a Monday. It will then reload Nginx.
Everything is now ready. Anyone can head to your Mastodon instance to sign up and start sending their toots. This includes you — but of course, you’ll need an account with elevated permissions. Begin by creating your account and clicking the link in the confirmation email.
Next, return to the mastodon subdirectory:
Use this command to elevate your username to admin level:
docker-compose run --rm web rails mastodon:make_admin USERNAME=yourusername
Head back to Mastodon in the browser window, and access your account Preferences. Find the Administration link to view user accounts, and manage the Mastodon Site Settings, which includes things like the title and description, and the purpose/topic of the instance.
Your Own Social Network, Discussing Your Topics!
Twitter has come under fire in recent months for its slowness in dealing with cyberbullying, and the proliferation of accounts promoting and glorifying Islamic extremism. Facebook, meanwhile, continues to be a privacy nightmare.
To deal with this, you could join Mastodon. We think you should, as you’ll at least see how things can be done differently. And if you like it, why not install your own instance?
Have you tried Mastodon? Did you set up your own instance? Tell us how it went.