Skip to content

Instantly share code, notes, and snippets.

@killerbees19
Last active September 19, 2024 06:46
Show Gist options
  • Save killerbees19/9b0fb9358b1570d7ef17684aa7754259 to your computer and use it in GitHub Desktop.
Save killerbees19/9b0fb9358b1570d7ef17684aa7754259 to your computer and use it in GitHub Desktop.
Mastodon 3.5.x/4.1.x @ Debian (with OpenSearch & LibreTranslate)

MASTODON 3.5.X/4.1.x -- DEBIAN 11/12 -- LAN TEST INSTALLATION (NOT FOR PRODUCTION!)

# ln -s /usr/bin/yarnpkg /usr/local/bin/yarn
# apt install imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev \
              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 libgdbm-dev nginx redis-server redis-tools \
              postgresql postgresql-contrib libidn11-dev libicu-dev \
              libjemalloc-dev yarnpkg curl gnupg postfix bsd-mailx

# su -c psql postgres
> CREATE USER mastodon CREATEDB;
> \q

# useradd -m -s /bin/bash -u 7007 -U mastodon
# su -l mastodon
$ git clone https://github.com/rbenv/rbenv.git ~/.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 https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 3.0.6
$ rbenv global 3.0.6
$ gem install bundler --no-document
$ git clone https://github.com/mastodon/mastodon.git ~/live && cd live
$ git checkout "$(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)"
$ bundle config deployment 'true'
$ bundle config without 'development test'
$ bundle install -j"$(nproc)"
$ yarnpkg set version 1
$ yarnpkg install --pure-lockfile
$ RAILS_ENV=production bundle exec rake mastodon:setup
$ exit

# cp /home/mastodon/live/dist/nginx.conf /etc/nginx/sites-available/mastodon
# ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon

# editor /etc/nginx/sites-available/mastodon
> Replace example.com and disable HSTS header!
> ssl_certificate     /etc/ssl/certs/ssl-cert-snakeoil.pem;
> ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;

# service nginx reload
# cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/

# editor /etc/systemd/system/mastodon-*.service
> ExecStart=/usr/bin/bundle ...

# systemctl daemon-reload
# systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming

WIP! USE AT YOUR OWN RISK!

# apt install pipx cmake python3-dev
# useradd -m -s /bin/bash -u 7177 -U libretranslate
# su -l libretranslate

$ pipx install libretranslate
$ pipx ensurepath
$ exit

# editor /etc/systemd/system/libretranslate.service
# systemctl reload-daemon
# systemctl enable --now libretranslate

# editor ~mastodon/live/.env.production
> LIBRE_TRANSLATE_ENDPOINT=http://127.0.0.1:5000
> LIBRE_TRANSLATE_API_KEY=

# systemctl reload mastodon-web
# systemctl restart mastodon-sidekiq

WIP! USE AT YOUR OWN RISK!

# sysctl -w vm.max_map_count=262144
# echo 'vm.max_map_count=262144' >> /etc/sysctl.d/50-opensearch.conf
# useradd -m -s /bin/bash -u 7997 -U opensearch
# su -l opensearch
$ wget 'https://artifacts.opensearch.org/releases/core/opensearch/1.3.11/opensearch-min-1.3.11-linux-x64.tar.gz'
$ tar xfva opensearch-min-1.3.11-linux-x64.tar.gz
$ rm -v opensearch-min-1.3.11-linux-x64.tar.gz
$ ln -s opensearch-1.3.11 opensearch.latest
$ cp -avn opensearch.latest/config config
$ echo -e '-Xms256m\n-Xmx256m' >> config/jvm.options.d/memory.options
$ echo '-XX:HeapDumpPath=/home/opensearch/data' >> config/jvm.options.d/misc.options
$ mkdir data
$ chmod 0750 config data

$ editor config/opensearch.yml
> path.data: /home/opensearch/data

$ exit
# editor /etc/systemd/system/opensearch.service
# systemctl daemon-reload
# systemctl enable --now opensearch

# editor ~mastodon/live/.env.production
> ES_ENABLED=true
> ES_HOST=localhost
> ES_PORT=9200

# systemctl reload mastodon-web
# systemctl restart mastodon-sidekiq
# su -l -s /bin/sh -c 'RAILS_ENV=production bin/tootctl search deploy' mastodon

# ### OPTIONAL? Initially no success after index deployment...
# systemctl restart mastodon-sidekiq mastodon-streaming mastodon-web
#!/bin/bash
###
# !!! EXPERIMENTAL !!!
###
set -eu -o pipefail
filename="/tmp/$(hostname -s)-$(date --utc +%Y%m%d-%H%M%S).tar.xz"
export XZ_OPT=-9
if [[ ! -x /usr/local/sbin/postgres-backup.sh ]]
then
(
echo "ERROR: postgres-backup.sh not found! Please setup postgres-backup script:"
echo
echo " https://github.com/froonix/misc-stuff/blob/master/scripts/postgres-backup"
echo
) >&2
exit 1
fi
shopt -s nullglob
dumps=( /var/lib/postgresqlbackups/{.globals,*}/"$(date --utc +%Y%m%d)"-*.sql.* )
shopt -s failglob
if [[ -z "${dumps[*]}" ]]
then
(
echo "Notice: Executing postgres-backup.sh"
/usr/local/sbin/postgres-backup.sh
echo "Done!"
echo
) >&2
fi
dpkg="/tmp/dpkg.selections"
dpkg --get-selections > "$dpkg"
touch "$filename"
chgrp sudo "$filename"
chmod 0640 "$filename"
cd /
tar cfa "$filename" \
home/USERNAME \
home/opensearch/config \
home/mastodon/live/.env.production \
home/mastodon/live/public/system/{media_attachments,accounts} \
var/lib/postgresqlbackups/{.globals,*}/"$(date --utc +%Y%m%d)"-*.sql.* \
var/cache/debconf \
var/spool/cron \
"${dpkg:1}" \
usr/local \
root \
etc
rm -f "$dpkg"
echo "$filename"
[Unit]
Description=LibreTranslate
Documentation=https://github.com/LibreTranslate/LibreTranslate
Wants=network-online.target
After=network-online.target
[Service]
Type=exec
User=libretranslate
Group=libretranslate
WorkingDirectory=/home/libretranslate
ExecStart=/home/libretranslate/.local/bin/libretranslate --disable-web-ui
ReadWritePaths=/home/libretranslate/.local/share/
ProtectSystem=strict
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# Credits: elasticsearch.service
# Apache License 2.0
[Unit]
Description=OpenSearch
Documentation=https://opensearch.org/
Wants=network-online.target
After=network-online.target
[Service]
Type=exec
RuntimeDirectory=opensearch
PrivateTmp=true
Environment=OPENSEARCH_HOME=/home/opensearch/opensearch.latest
Environment=OPENSEARCH_PATH_CONF=/home/opensearch/config
#Environment=OPENSEARCH_JAVA_OPTS=""
WorkingDirectory=/home/opensearch/opensearch.latest
User=opensearch
Group=opensearch
ExecStart=/home/opensearch/opensearch.latest/bin/opensearch
StandardOutput=journal
StandardError=inherit
# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=65535
# Specifies the maximum number of processes
LimitNPROC=4096
# Specifies the maximum size of virtual memory
LimitAS=infinity
# Specifies the maximum file size
LimitFSIZE=infinity
# Disable timeout logic and wait until process is stopped
TimeoutStopSec=0
# SIGTERM signal is used to stop the Java process
KillSignal=SIGTERM
# Send the signal only to the JVM rather than its control group
KillMode=process
# Java process is never killed
SendSIGKILL=no
# When a JVM receives a SIGTERM signal it exits with code 143
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target

Debian

Bullseye (11) » Bookworm (12)

# sed -i s/bullseye/bookworm/ /etc/apt/sources.list
# apt update
# apt upgrade --without-new-pkgs
# apt full-upgrade
# shutdown -r now

Repair Mastodon (3.5.8)

# cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/
# apt purge ruby ruby-dev bundler
# apt --purge autoremove
# su -l mastodon

$ git clone https://github.com/rbenv/rbenv.git ~/.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 https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 3.0.6
$ rbenv global 3.0.6
$ gem install bundler --no-document
$ cd ~/live
$ bundle install -j"$(nproc)"
$ yarn install
$ exit

# systemctl daemon-reload
# systemctl restart mastodon-web mastodon-sidekiq mastodon-streaming

Update PostgreSQL: 13 -> 15

# systemctl stop mastodon-web mastodon-sidekiq mastodon-streaming
# pg_dropcluster --stop 15 main
# pg_upgradecluster 13 main
# pg_dropcluster 13 main
# apt purge postgresql-13 postgresql-client-13
# apt --purge autoremove
# systemctl restart mastodon-web mastodon-sidekiq mastodon-streaming

Final cleanup

# apt purge '~c'
# apt purge '~o'
# apt --purge autoremove
# apt autoclean

Mastodon (Debian 12)

3.5.8 -> 4.1.2

# systemctl stop mastodon-web mastodon-sidekiq mastodon-streaming
# su -l mastodon

$ cd ~/live
$ git fetch && git checkout v4.1.2
$ bundle install -j"$(nproc)"
$ yarn install
$ SKIP_POST_DEPLOYMENT_MIGRATIONS=true RAILS_ENV=production bundle exec rails db:migrate
$ NODE_OPTIONS=--openssl-legacy-provider RAILS_ENV=production bundle exec rails assets:precompile
$ RAILS_ENV=production bundle exec rails db:migrate
$ exit

# cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/
# systemctl daemon-reload
# systemctl restart mastodon-web mastodon-sidekiq mastodon-streaming
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment