Install Mastodon on Ubuntu 18.04 with Apache 2.4

Mastodon is a social network like Facebook and Twitter, but is non commercial, open source and does not track the users.

Here are installation instructions how to install a Mastodon instance on Ubuntu 18.04 with ISPConfig and Apache 2.4. It is written in Ruby and needs PostgrSQL, Redis and a running mailserver.

Create Domain

Log in to ISPConfig as admin an create a site with SSL.

Install Packages

ssh as root to the server and install necessary packages, Ruby, Redis, PostgrSQL

apt update && apt -y upgrade
apt install -y curl
curl -sL | bash -
curl -sS | apt-key add -
echo "deb stable main" | tee /etc/apt/sources.list.d/yarn.list
apt install -y imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file git-core g++ libprotobuf-dev protobuf-compiler pkg-config nodejs gcc autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev     zlib1g-dev libncurses5-dev libffi-dev libgdbm5 libgdbm-dev redis-server redis-tools postgresql postgresql-contrib certbot yarn libidn11-dev libicu-dev libjemalloc-dev
adduser --disabled-login mastodon
su - mastodon
git clone ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec bash
git clone ~/.rbenv/plugins/ruby-build
RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 2.6.1
rbenv global 2.6.1
gem update --system
gem install bundler --no-document

Setup PostgreSQL

sudo -u postgres psql
\password mastodon

Setup Mastodon

su - mastodon
git clone live && cd live
git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)
bundle install \
  -j$(getconf _NPROCESSORS_ONLN) \
  --deployment --without development test
yarn install --pure-lockfile

Run the interactive setup wizard

RAILS_ENV=production bundle exec rake mastodon:setup

Setup Apache

a2enmod ssl proxy proxy_wstunnel proxy_http headers 
vi /etc/apache2/sites-available/domain.xx.vhost
   ServerAdmin [email protected]
   ServerName domain.xx
   ServerAlias www.domain.xx
   Redirect Permanent / https://www.domain.xx/

   ServerAdmin [email protected]
   ServerName domain.xx
   ServerAlias www.domain.xx
   DocumentRoot /home/mastodon/live/public/

   Header always set Referrer-Policy "strict-origin-when-cross-origin"
   Header always set Strict-Transport-Security "max-age=31536000"

   SSLEngine on
   SSLProtocol -all +TLSv1.2
   SSLHonorCipherOrder on
   SSLCompression off
   SSLSessionTickets off
   SSLStaplingResponderTimeout 5
   SSLStaplingReturnResponderErrors off
   SSLUseStapling off

   SSLCertificateFile   /var/www/clients/client1/web1/ssl/domain.xx-le.crt
   SSLCertificateKeyFile /var/www/clients/client1/web1/ssl/domain.xx-le.key

   <LocationMatch "^/(assets|avatars|emoji|headers|packs|sounds|system)>
      Header always set Cache-Control "public, max-age=31536000, immutable"
      Require all granted

   ProxyPreserveHost On
   RequestHeader set X-Forwarded-Proto "https"

   ProxyPass /500.html !
   ProxyPass /sw.js !
   ProxyPass /robots.txt !
   ProxyPass /manifest.json !
   ProxyPass /browserconfig.xml !
   ProxyPass /mask-icon.svg !
   ProxyPassMatch ^(/.*\.(png|ico)$) !
   ProxyPassMatch ^/(assets|avatars|emoji|headers|packs|sounds|system|.well-known/acme-challenge) !

   ProxyPass /api/v1/streaming/ ws://localhost:4000/
   ProxyPassReverse /api/v1/streaming/ ws://localhost:4000/
   ProxyPass / http://localhost:3000/
   ProxyPassReverse / http://localhost:3000/

   ErrorDocument 500 /500.html
   ErrorDocument 501 /500.html
   ErrorDocument 502 /500.html
   ErrorDocument 503 /500.html
   ErrorDocument 504 /500.html
service apache2 restart
cp /etc/apache2/sites-available/domain.xx.vhost /etc/apache2/sites.available/domain.xx.vhost.mastodon

Setup systemd services

cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/
systemctl start mastodon-web mastodon-sidekiq mastodon-streaming
systemctl enable mastodon-*


Installation is completed and Mastodon is running. Login to your new Mastodon instance with the browser.

See also