Skip to content

Instantly share code, notes, and snippets.

@xatier
Last active November 1, 2017 08:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xatier/60894616c88695d2e5b7c9ddcb64305c to your computer and use it in GitHub Desktop.
Save xatier/60894616c88695d2e5b7c9ddcb64305c to your computer and use it in GitHub Desktop.

Requirements

  • hareware: GCE g1-small instance (1 vCPU, 1.7 GB memory), about $15/mo

  • software (Archlinux):

sudo pacman -Sy nginx certbot docker docker-compose

# optional tools
sudo pacman -Sy glances htop w3m

Swap

It is suggested to add some swap space for Mastodon

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
  • add to swap /etc/fstab
/swapfile none swap defaults 0 0

(Oprional) adjust swappiness to a higher value

sudo sysctl vm.swappiness=85
  • /etc/sysctl.d/99-sysctl.conf
vm.swappiness=85

Change docker path

It is suggested to move docker storage to another path (dockerd -g $PATH) instead of /var/lib/docker by default. This can prevent the server from accidentally running out of disk space.

  • /etc/systemd/system/docker.service.d/docker-storage.conf
[Service]
ExecStart= 
ExecStart=/usr/bin/dockerd -g /path/to/new/location/docker -H fd://
sudo systemctl (re)start docker

Mail service

Apply a mailgun account, setup DNS records with mailgun (MX, SPF, DKIM, CNAME).

Installation

  • clone repo and copy the env file.
git clone git@github.com:tootsuite/mastodon.git
cd mastodon

cp .env.production.sample .env.production
  • vim .env.production
LOCAL_DOMAIN=example.com
LOCAL_HTTPS=true

SMTP_SERVER=smtp.mailgun.org
SMTP_PORT=2525
SMTP_LOGIN=postmaster@example.com
SMTP_PASSWORD=<<API key>>
SMTP_FROM_ADDRESS=noreply@example.com>
  • build docker image (slow!)
docker-compose build
  • run rake secret on web 3 times for PAPERCLIP_SECRET, SECRET_KEY_BASE, and OTP_SECRET.
docker-compose run --rm web rake secret
  • create database
docker-compose run --rm web rails db:migrate
  • precompile the assets (slow!)
docker-compose run --rm web rails assets:precompile
  • run / stop service (Mastodon is running on http://localhost:3000)

    • hint: docker-compose down will STOP and REMOVE containers.
docker-compose up -d
docker-compose stop

Deploy

  • create https certificates: certbot certonly --manual

  • nginx settings

  • become admin

docker-compose run --rm web rake mastodon:make_admin USERNAME=xatierlike

or, via an interactive shell:

docker exec -it mastodon_web_1 /bin/sh
RAILS_ENV=production bundle exec rails mastodon:make_admin USERNAME=xatierlike

Update with git master

(Risky!)

git pull
docker-compose build
docker-compose run --rm web rails assets:precompile

docker-compose stop
docker-compose run --rm web rails db:migrate
docker-compose up -d

Cleanup docker images

docker rmi $(docker images -f "dangling=true" -q)
docker rm -v $(docker ps -a -q -f status=exited)

Refresh tasks

  • please refere the details defined in lib/tasks/mastodon.rake
#!/bin/bash -x                                                                  
                                                                                
set -ueo pipefail                                                               
                                                                                
docker-compose run --rm web rake mastodon:daily                                 
docker-compose run --rm web rake mastodon:media:remove_remote

Patches

My persoanl patches to the site

  • show NSFW iamges by default
diff --git a/app/assets/javascripts/components/components/media_gallery.jsx b/app/assets/javascripts/components/components/media_gallery.jsx
index ebc6e709..f54f216b 100644
--- a/app/assets/javascripts/components/components/media_gallery.jsx
+++ b/app/assets/javascripts/components/components/media_gallery.jsx
@@ -194,7 +194,7 @@ class MediaGallery extends React.PureComponent {
   constructor (props, context) {
     super(props, context);
     this.state = {
-      visible: !props.sensitive
+      visible: 1
     };
     this.handleOpen = this.handleOpen.bind(this);
     this.handleClick = this.handleClick.bind(this);   
  • set remote media cache to 1 day only
diff --git a/lib/tasks/mastodon.rake b/lib/tasks/mastodon.rake
index 7dd7b5cd..fd5ccfd6 100644
--- a/lib/tasks/mastodon.rake
+++ b/lib/tasks/mastodon.rake
@@ -44,9 +44,9 @@ namespace :mastodon do
       MediaAttachment.where(account: Account.silenced).find_each(&:destroy)
     end

-    desc 'Remove cached remote media attachments that are older than a week'
+    desc 'Remove cached remote media attachments that are older than a day'
     task remove_remote: :environment do
-      MediaAttachment.where.not(remote_url: '').where('created_at < ?', 1.week.ago).find_each do |media|
+      MediaAttachment.where.not(remote_url: '').where('created_at < ?', 1.day.ago).find_each do |media|
         media.file.destroy
       end
     end

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment