Ubuntu 16.04: Run Mastodon instance

Table of Contents

1 Limitation

  • This article reffers Production-guide.md and Alternatives.md in Mastodon documentation.
  • This Mastodon instance is for checking Mastodon usage and API. This is not production level.
  • The Mastodon, Redis, PostgreSQL, Apache and Postfix will be installed in the same machine.
  • Only <user>@localhost can be used for account (This Postfix setting only sends to localhost). If you need multiple account, you need to create multiple local user in machine for creating multiple mail address.
  • You need to login to machine for checking confirmation mail.
  • The Apache setting does not use RewriteRule and subdomain. This will prevent other web service which uses Apache.

2 Run Mastodon instance

The following script will run Mastodon instance. The LOCAL_DOMAIN variable can be used as IP address and FQDN. This will be used in confirmation mail.

$ LOCAL_DOMAIN= ./run-this-script.sh #!/bin/shset -e[ -z “${LOCAL_DOMAIN}” ] && LOCAL_DOMAIN=ubuntu-1604-mastodon.hiroom2.com[ -z “${MASTODON_DIR}” ] && MASTODON_DIR=/var/lib/mastodonMASTODON_TAG=v1.3.3mastodon_install_depend(){ sudo apt install -y imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file git curl ruby-bundler ruby-dev sudo gem install bundler curl -sL https://deb.nodesource.com/setup_6.x | sudo bash – sudo apt install -y nodejs sudo npm install -g yarn}mastodon_install_redis(){ sudo apt install -y redis-server redis-tools sudo systemctl enable redis-server sudo systemctl start redis-server}mastodon_install_postgres(){ sudo apt install -y postgresql postgresql-contrib cat <<EOF | sudo -u postgres psqlCREATE USER mastodon CREATEDB;qEOF sudo sed -e ‘s;^local.*postgres.*peer$;host all all ident;g’ -i /etc/postgresql/9.?/main/pg_hba.conf sudo apt install -y pidentd sudo systemctl enable pidentd sudo systemctl start pidentd sudo systemctl restart postgresql}mastodon_install_mastodon(){ # Not use -m for disable creating skel like .bashrc. sudo useradd mastodon -M -d ${MASTODON_DIR} git clone https://github.com/tootsuite/mastodon.git cd mastodon git checkout ${MASTODON_TAG} -b ${MASTODON_TAG} cd .. sudo mkdir -p “$(dirname ${MASTODON_DIR})” sudo mv mastodon ${MASTODON_DIR} sudo chown -R mastodon:mastodon ${MASTODON_DIR} sudo su – mastodon -c “cd ${MASTODON_DIR}# The ruby version checker may not work correctly.sed -e “s/^ruby/#ruby/g” -i Gemfilebundle install –deployment –without development testyarn install –pure-lockfilePAPERCLIP_SECRET=$(rake secret)SECRET_KEY_BASE=$(rake secret)OTP_SECRET=$(rake secret)sed -e “s/^REDIS_HOST=.*/REDIS_HOST=localhost/g” -e “s/^DB_HOST=.*/DB_HOST=localhost/g” -e “s/^DB_USER=.*/DB_USER=mastodon/g” -e “s/^DB_NAME=.*/DB_NAME=mastodon/g” -e “s/^LOCAL_DOMAIN=.*/LOCAL_DOMAIN=${LOCAL_DOMAIN}/g” -e “s/^LOCAL_HTTPS=.*/LOCAL_HTTPS=true/g” -e “s/^PAPERCLIP_SECRET=.*/PAPERCLIP_SECRET=${PAPERCLIP_SECRET}/g” -e “s/^SECRET_KEY_BASE=.*/SECRET_KEY_BASE=${SECRET_KEY_BASE}/g” -e “s/^OTP_SECRET=.*/OTP_SECRET=${OTP_SECRET}/g” -e “s/^SMTP_SERVER=.*/SMTP_SERVER=localhost/g” -e “s/^SMTP_PORT=.*/SMTP_PORT=25/g” -e “s/^SMTP_LOGIN=/#SMTP_LOGIN=/g” -e “s/^SMTP_PASSWORD=/#SMTP_PASSWORD=/g” -e “s/^SMTP_FROM_ADDRESS=.*/SMTP_FROM_ADDRESS=noreply@localhost/g” -e “s/^#SMTP_AUTH_METHOD=.*/SMTP_AUTH_METHOD=none/g” -e “s/^# DEFAULT_LOCALE=.*/DEFAULT_LOCALE=en/g” .env.production.sample > .env.productionRAILS_ENV=production bundle exec rails db:setupRAILS_ENV=production bundle exec rails assets:precompile” cat <<EOF | sudo tee /lib/systemd/system/mastodon-web.service[Unit]Description=mastodon-webAfter=network.target[Service]Type=simpleUser=mastodonWorkingDirectory=${MASTODON_DIR}Environment=”RAILS_ENV=production”Environment=”PORT=3000″ExecStart=/usr/local/bin/bundle exec puma -C config/puma.rbTimeoutSec=15Restart=always[Install]WantedBy=multi-user.targetEOF cat <<EOF | sudo tee /lib/systemd/system/mastodon-sidekiq.service[Unit]Description=mastodon-sidekiqAfter=network.target[Service]Type=simpleUser=mastodonWorkingDirectory=${MASTODON_DIR}Environment=”RAILS_ENV=production”Environment=”DB_POOL=5″ExecStart=/usr/local/bin/bundle exec sidekiq -c 5 -q default -q mailers -q pull -q pushTimeoutSec=15Restart=always[Install]WantedBy=multi-user.targetEOF cat <<EOF | sudo tee /lib/systemd/system/mastodon-streaming.service[Unit]Description=mastodon-streamingAfter=network.target[Service]Type=simpleUser=mastodonWorkingDirectory=${MASTODON_DIR}Environment=”NODE_ENV=production”Environment=”PORT=4000″ExecStart=/usr/bin/npm run startTimeoutSec=15Restart=always[Install]WantedBy=multi-user.targetEOF sudo systemctl enable mastodon-web mastodon-sidekiq mastodon-streaming sudo systemctl start mastodon-web mastodon-sidekiq mastodon-streaming}mastodon_install_apache(){ sudo apt install -y apache2 cat <<EOF | sudo tee /etc/apache2/sites-available/mastodon.conf<VirtualHost _default_:443> SSLEngine on SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key <Location /assets> Header always set Cache-Control “public, max-age=31536000, immutable” </Location> ProxyPreserveHost On RequestHeader set X-Forwarded-Proto “https” ProxyPass /500.html ! ProxyPass /oops.png ! ProxyPass /api/v1/streaming/ ws://localhost:4000/ ProxyPassReverse /api/v1/streaming/ ws://localhost:4000/ ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/</VirtualHost>EOF for mod in ssl proxy proxy_http headers; do sudo a2enmod ${mod} done sudo a2ensite mastodon sudo systemctl enable apache2 sudo systemctl restart apache2}mastodon_install_postfix(){ cat <<EOF | sudo debconf-set-selectionspostfix postfix/main_mailer_type select No configurationEOF sudo apt install -y postfix cat <<EOF | sudo tee /etc/postfix/main.cfcompatibility_level = 2command_directory = /usr/sbindaemon_directory = /usr/lib/postfix/sbindata_directory = /var/lib/postfixmail_owner = postfixmyhostname = localhostinet_interfaces = allmydestination = localhostlocal_recipient_maps = unix:passwd.byname $alias_mapsunknown_local_recipient_reject_code = 550mynetworks_style = subnetmynetworks = = hash:/etc/aliasesalias_database = hash:/etc/aliasessmtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd $daemon_directory/$process_name $process_id & sleep 5sendmail_path = /usr/sbin/postfixnewaliases_path = /usr/bin/newaliasesmailq_path = /usr/bin/mailqsetgid_group = postdropinet_protocols = ipv4EOF sudo newaliases sudo systemctl enable postfix sudo systemctl restart postfix}mastodon_main(){ mastodon_install_depend mastodon_install_redis mastodon_install_postgres mastodon_i
nstall_mastodon mastodon_install_apache mastodon_install_postfix}mastodon_main “$@”

3 Access to Mastodon instance

Access to Mastodon instance with the following URL.


Because the chrome does not have this page’s certification, the crome warns the following and cannot to access to this page. You need to click “ADVANCED” and “Proceed to <server> (unsafe)”. The other browser will needs the similar way.


Mastodon top page is displayed. Add user with <user>@localhost.


Confirmation mail caution is displayed.


Login to machine’s user and run mutt. The confirmation mail is received. Click page link for activating account.

$ sudo apt install -y mutt$ mutt


Mail address is validated. Input mail address and password.


Mastodon login is done.


TOOT can be done.


Android | Linux | SDL - Narrow Escape