Mastodon is a free and open source social network platform very similar to the Twitter. Anyone can run Mastodon and participate in running a social network seamlessly. It is written in Ruby and JavaScript.
In this tutorial, we will install Mastodon in Ubuntu 16.04 server.
Requirements
- A server running Ubuntu 16.04.
- A normal user with sudo privileges setup on your server.
Install Required Dependencies
First, it is recommmended to update your system with the latest version. You can do this by running the following command:
sudo apt-get update -y
sudo apt-get upgrade -y
Once your system is updated, restart your system and login with sudo user.
Next, you will need to install some required dependencies to your server. You can install all of them by running the following command:
sudo curl -sL https://deb.nodesource.com/setup_4.x | sudo bash -
sudo apt-get install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev nodejs -y
sudo npm install -g yarn
Next, you will also need to install redis server to your system. You can install it by just running the following command:
sudo apt-get install redis-server redis-tools -y
Once all the components are installed, you can proceed to install Postgresql server.
Install and Configure PostgreSQL
Mastodon uses a PostgreSQL database, so you will need to install PostgreSQL server and the ident daemon. You can install them with the following command:
sudo apt-get install postgresql pidentd -y
Once the installation is completed, start the ident daemon with the following command:
sudo systemctl start pidentd
Next, login to postgre user and create a user for Mastodon:
sudo su - postgres
psql
CREATE USER mastodon CREATEDB;
exit
Next, you will need to make users be able to login without a password. You can do this by running the following command:
sudo sed -i '/^local.*postgres.*peer$/a host all all 127.0.0.1/32 ident' /etc/postgresql/9.?/main/pg_hba.conf
Once you are done, restart PostgreSQL for the changes to take effect.
sudo systemctl postgresql restart
Install Ruby
Mastodon is a Ruby based application. So you will need to install Ruby and all required dependencies to your server.
You can install all of them by running the following command:
sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libncurses5-dev libffi-dev libgdbm3 libreadline6-dev zlib1g-dev libgdbm-dev -y
Once all the packages are installed, create a user called mastodon without password:
sudo adduser --disabled-password --disabled-login mastodon
Next, login with mastodon user and install rbenv and rbenv-build with the following command:
su - mastodon
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
Next, logout from the mastodon user and login again to apply the bash modification:
exit
su - mastodon
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
Next, install ruby 2.4.1 for mastodon with the following command:
rbenv install 2.4.1
rbenv global 2.4.1
Once the installation is complete, you can check the ruby version with the following command:
ruby -v
Install Mastodon
Now, all the dependencies required to setup Mastodon are installed. So download the latest version of the Mastodon from the Git repository.
To do so, run the following command:
git clone https://github.com/tootsuite/mastodon.git live
cd live
git checkout $(git tag | tail -n 1)
Next, install bundler to manage the dependencies and disable the gem documentation :
echo "gem: --no-document" > ~/.gemrc
gem install bundler --no-ri
Next, install Mastodon with the following command:
bundle install --deployment --without development test
yarn install
Once installation is completed, create a configuration file for Mastodon:
cp .env.production.sample .env.production
nano .env.production
Add the following contents:
REDIS_HOST=localhost
REDIS_PORT=6379
DB_HOST=/var/run/postgresql
DB_USER=mastodon
DB_NAME=mastodon_production
DB_PASS=
DB_PORT=5432
# Federation
LOCAL_DOMAIN=yourdomain.com
LOCAL_HTTPS=true
Save and close the file when you are finished.
Create the Mastodon Systemd File
Next, you will need to create a Mastodon systemd web service, background service and API service file for Mastodon.
First, create a web service file with the following command:
nano /etc/systemd/system/mastodon-web.service
Add the following lines:
[Unit]
Description=mastodon-web
After=network.target
[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/live
Environment="RAILS_ENV=production"
Environment="PORT=3000"
ExecStart=/home/mastodon/.rbenv/shims/bundle exec puma -C config/puma.rb
TimeoutSec=15
Restart=always
[Install]
WantedBy=multi-user.target
Next, create a Backgroud service file:
nano /etc/systemd/system/mastodon-sidekiq.service
Add the following lines:
[Unit]
Description=mastodon-sidekiq
After=network.target
[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/live
Environment="RAILS_ENV=production"
Environment="DB_POOL=5"
ExecStart=/home/mastodon/.rbenv/shims/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q push
TimeoutSec=15
Restart=always
[Install]
WantedBy=multi-user.target
Next, create an API service file:
nano /etc/systemd/system/mastodon-streaming.service
Add the following lines:
[Unit]
Description=mastodon-streaming
After=network.target
[Service]
Type=simple
User=mastodon
WorkingDirectory=/home/mastodon/live
Environment="NODE_ENV=production"
Environment="PORT=4000"
ExecStart=/usr/bin/npm run start
TimeoutSec=15
Restart=always
[Install]
WantedBy=multi-user.target
Save and close the file, then enable all this service file with the following command:
sudo systemctl enable /etc/systemd/system/mastodon-*.service
sudo systemctl enable /etc/systemd/system//etc/systemd/system//etc/systemd/system/mastodon-web.service
sudo systemctl enable /etc/systemd/system//etc/systemd/system/mastodon-streaming.service
Next, start Mastodon instance with the following command:
sudo systemctl start mastodon-web.service
sudo systemctl start mastodon-sidekiq.service
sudo systemctl start mastodon-streaming.service
Configure Nginx for Mastodon
Next, you will need to setup a reverse-proxy using Nginx to to access Mastodon with your domain name.
First, install Nginx with the following command:
sudo apt-get install nginx -y
Next, create Nginx virtual server block for Mastodon.
sudo nano /etc/nginx/sites-enabled/mastodon.conf
Add the following lines:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
listen [::]:80;
server_name www.yourdomain.com yourdomain.com;
return 301 https://votredomaine.com$request_uri;
access_log /dev/null;
error_log /dev/null;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.yourdomain.com yourdomain.com;
access_log /var/log/nginx/yourdomain.com-access.log;
error_log /var/log/nginx/yourdomain.com-error.log;
ssl_certificate /etc/letsencrypt/live/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/privkey.pem;
ssl_protocols TLSv1.2;
ssl_ciphers EECDH+AESGCM:EECDH+AES;
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=15552000; preload";
keepalive_timeout 70;
sendfile on;
client_max_body_size 0;
gzip off;
root /home/mastodon/live/public;
location / {
try_files $uri @proxy;
}
location @proxy {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_pass_header Server;
proxy_pass http://127.0.0.1:3000;
proxy_buffering off;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
tcp_nodelay on;
}
location /api/v1/streaming {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_pass http://127.0.0.1:4000;
proxy_buffering off;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
tcp_nodelay on;
}
error_page 500 501 502 503 504 /500.html;
}
Save and close the file, then restart Nginx with the following command:
systemctl restart nginx
Finally, open your web browser and type the URL http//yourdomain.com to access your Mastodon install.