Ghost with Self-hosted ActivityPub

@michael@westergaard.site

Ghost is a better publication platform than WordPress. It is more modern and its owner is not insane. It supports ActivityPub, the protocol underlying the Fediverse, but using a separate component. Ghost provides that component, but you have 3 ugly choices: 1) use Ghost's provided ActivityPub server, 2) self-host the ActivityPub component in synchronous mode, or 3) host your Ghost site on the ad-company, Google.

1 and 3 means you're not really self-hosting Ghost, and 2 means you don't really have ActivityPub.

The Problem

The problem of the ActivityPub is two-fold:

  1. it relies on Google PubSub, and
  2. it needs persistent storage.

Number two is the least bad: it means you will need to do hosting in a manner that allows persistent storage. That makes hosting under Kubernetes less convenient.

Number one is the big problem: Google PubSub is not a standard, and while ActivityPub can be hosted without, that means it works in synchronous mode: publication takes longer as it must wait for all followers, and if a follower is off-line, they just don't get a notification. It is, in other words, useless.

The Solution

To get around that, I made a fork of Ghost's ActivityPub at https://git.westergaard.cloud/klafbang/ActivityPub. In this fork, I solved the two issues. By "I," I mean I asked the slop machine to implement it for me. If that is a problem for you, feel free to move on.

I replaced the need for persistent storage by an alternative implementation using S3-compatible storage with a CDN. This is supported by most cloud providers. Using this, all media is uploaded to an S3 bucket and shared via your cloud provider's CDN.

I also replaced the need for Google PubSub by a MySQL backed database. The API underlying Ghost ActivityPub is Fedify, which already supports this as an alternative to PubSub. Ghost's own code also needs a queue, which has an alternative implementation using MySQL as well. MySQL was chosen as that is what Ghost uses already.

With these two changes, it is possible to host ActivityPub for Ghost in a normal Kubernetes cluster without any tricks.

The How

To deploy, I use Helm with the Helmforge Generic chart. This is my configuration:

fullnameOverride: activitypub

replicaCount: 1

workload:
  enabled: true
  type: Deployment

image:
  repository: git.westergaard.cloud/klafbang/activitypub
  tag: 1.2.14-michael
  pullPolicy: IfNotPresent

# Add a registry pull secret here only if the Forgejo package is private.
imagePullSecrets: []

containers:
  - name: activitypub
    ports:
      - name: http
        containerPort: 8080
        protocol: TCP

# Global environment is applied to the application container.
env:
  - name: NODE_ENV
    value: production
  - name: PORT
    value: "8080"

  # Downstream fork: durable MySQL-backed queues.
  - name: MQ_TYPE
    value: mysql

  # Downstream fork: native S3-compatible storage.
  - name: STORAGE_TYPE
    value: s3
  - name: S3_BUCKET
    value: bucket-name
  - name: S3_REGION
    value: nl-ams
  - name: S3_ENDPOINT
    value: https://s3.nl-ams.scw.cloud
  # Public URL returned by the storage adapter. Point this at the CDN,
  # not at the S3 API endpoint.
  - name: S3_PUBLIC_URL
    value: https://media.example.com

# Secrets are deliberately external to this chart release.
# See secrets.example.yaml for the required keys.
envFrom:
  - secretRef:
      name: activitypub-db
  - secretRef:
      name: activitypub-s3

startupProbe:
  httpGet:
    path: /ping
    port: http
  periodSeconds: 2
  timeoutSeconds: 2
  failureThreshold: 30

readinessProbe:
  httpGet:
    path: /ping
    port: http
  periodSeconds: 10
  timeoutSeconds: 2
  failureThreshold: 3

livenessProbe:
  httpGet:
    path: /ping
    port: http
  periodSeconds: 30
  timeoutSeconds: 2
  failureThreshold: 3

resources:
  requests:
    cpu: 50m
    memory: 512Mi
  limits:
    # Deliberately no CPU limit: allow burst CPU without CFS throttling.
    memory: 768Mi

service:
  enabled: true
  type: ClusterIP
  port: 8080
  targetPort: 8080

serviceAccount:
  create: true
  automountServiceAccountToken: false

terminationGracePeriodSeconds: 30

ingress:
  enabled: false

gatewayApi:
  enabled: false

hpa:
  enabled: false

pdb:
  enabled: false

persistence:
  volumes: []
  mounts: []
  storage: []
  persistentVolumeClaims: []
  persistentVolumes: []

extraManifests:
  - apiVersion: batch/v1
    kind: Job
    metadata:
      name: activitypub-migrate
      annotations:
        helm.sh/hook: pre-install,pre-upgrade
        helm.sh/hook-weight: "-10"
        helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded
    spec:
      backoffLimit: 1
      activeDeadlineSeconds: 300
      template:
        spec:
          restartPolicy: Never
          automountServiceAccountToken: false
          containers:
            - name: migrate
              image: git.westergaard.cloud/klafbang/activitypub-migrations:1.2.14-michael
              imagePullPolicy: IfNotPresent
              resources:
                requests:
                  cpu: 25m
                  memory: 128Mi
                limits:
                  memory: 192Mi
              env:
                - name: DB_HOST
                  valueFrom:
                    secretKeyRef:
                      name: activitypub-db
                      key: MYSQL_HOST
                - name: DB_PORT
                  valueFrom:
                    secretKeyRef:
                      name: activitypub-db
                      key: MYSQL_PORT
                - name: DB_USER
                  valueFrom:
                    secretKeyRef:
                      name: activitypub-db
                      key: MYSQL_USER
                - name: DB_PASS
                  valueFrom:
                    secretKeyRef:
                      name: activitypub-db
                      key: MYSQL_PASSWORD
                - name: DB_NAME
                  valueFrom:
                    secretKeyRef:
                      name: activitypub-db
                      key: MYSQL_DATABASE

I have two secrets for S3 and MySQL:

apiVersion: v1
kind: Secret
metadata:
  name: activitypub-db
  namespace: ghost
type: Opaque
stringData:
  MYSQL_HOST: mysql.example.com
  MYSQL_PORT: "3306"
  MYSQL_USER: activitypub
  MYSQL_PASSWORD: "password"
  MYSQL_DATABASE: activitypub
---
apiVersion: v1
kind: Secret
metadata:
  name: activitypub-s3
  namespace: ghost
type: Opaque
stringData:
  S3_ACCESS_KEY_ID: ACCESS
  S3_SECRET_ACCESS_KEY: SECRET

Install using

kubectl create ns ghost
kubectl apply -f ghost-activitypub.yaml
helm upgrade --install activitypub --namespace ghost helmforge/generic  -f ghost-activitypub-values.yaml

You will need an ingress or gateway to expose the web-service. I do not use either, so you are on your own here, but that should be standard if you are even interested in self-hosting in Kubernetes.

Finally, you need to reverse proxy the ActivityPub URLs in your site nginx to the new ActivityPub service:

	location ~ ^/(?:\.ghost/activitypub(?:/|$)|\.well-known/(?:webfinger|nodeinfo)$) {
		proxy_set_header Host ap.example.com;
		proxy_set_header X-Forwarded-Host example.com;
		proxy_set_header X-Forwarded-Proto https;
		proxy_set_header X-Forwarded-For $remote_addr;
		proxy_set_header X-Real-IP $remote_addr;

		proxy_ssl_server_name on;
		proxy_http_version 1.1;
		proxy_pass https://ap.example.com;
	}

Put this in the server section of your existing site.

After this, simply enable Social Web under Network in the Ghost admin and restart Ghost. You should now have self-hosted AP and can configure it + follow people.