本页内容
- 先决条件
- 步骤 1 - 配置防火墙
- 步骤 2 - 安装 Docker 和 Docker Compose
- 步骤 3 - 安装前准备
- 步骤 4 - 安装 Mastodon 创建目录并设置所有权 创建环境和 docker compose 文件 创建应用密钥 Mastodon 环境文件 准备 Mastodon
- 步骤 5 - 安装 Nginx
- 步骤 6 - 安装 SSL
- 步骤 7 - 配置 Nginx
- 步骤 8 - 启动 Mastodon Tootctl CLI 工具 Mastodon 服务文件 初始化搜索 附加辅助服务 访问 Mastodon
- 步骤 9 - Mastodon 维护
- 步骤 10 - 备份 Mastodon
- 步骤 11 - 升级 Mastodon
- 结论
- 创建目录并设置所有权
- 创建环境和 docker compose 文件
- 创建应用密钥
- Mastodon 环境文件
- 准备 Mastodon
- Tootctl CLI 工具
- Mastodon 服务文件
- 初始化搜索
- 附加辅助服务
- 访问 Mastodon
Mastodon 是一个免费、去中心化的开源社交网络。它作为 Twitter 的替代品而生。和 Twitter 一样,用户可以互相关注,并发布消息、图片和视频。但与 Twitter 不同的是,其内容没有中央存储或权威机构。
相反,Mastodon 运行在上千台不同的服务器上,每台都由社区的不同成员运行。在一个服务器上注册的用户可以轻松连接到其他网络上的用户,并在不同实例之间互相关注。
任何人都可以安装自己的 Mastodon 服务器实例。本教程将教你如何在运行 Rocky Linux 9 的服务器上使用 Docker 搭建自己的 Mastodon 实例。Docker 将所有所需的软件包和服务封装在容器中,使 Mastodon 的安装变得轻松。
先决条件
- 运行 Rocky Linux 9 的服务器,至少配备 2 个 CPU 核心和 2GB 内存。你需要根据需求升级服务器。
- 具有 sudo 权限的非 root 用户。
- 指向你服务器的完全限定域名(FQDN)。在本教程中,我们将使用 mastodon.example.com 作为域名。
- Mastodon 会向用户发送邮件通知。我们建议你使用第三方事务性邮件服务,如 Mailgun、Sendgrid、Amazon SES 或 Sparkpost。本指南中的说明将使用 Amazon SES。
- 确保一切都是最新的。$ sudo dnf update
- 安装基本的实用工具包。其中部分可能已安装。$ sudo dnf install wget curl nano unzip yum-utils -y
运行 Rocky Linux 9 的服务器,至少配备 2 个 CPU 核心和 2GB 内存。你需要根据需求升级服务器。
具有 sudo 权限的非 root 用户。
指向你服务器的完全限定域名(FQDN)。在本教程中,我们将使用 mastodon.example.com 作为域名。
Mastodon 会向用户发送邮件通知。我们建议你使用第三方事务性邮件服务,如 Mailgun、Sendgrid、Amazon SES 或 Sparkpost。本指南中的说明将使用 Amazon SES。
确保一切都是最新的。
$ sudo dnf update
安装基本的实用工具包。其中部分可能已安装。
$ sudo dnf install wget curl nano unzip yum-utils -y
步骤 1 - 配置防火墙
第一步是配置防火墙。Rocky Linux 使用 Firewalld 防火墙。检查防火墙的状态。
$ sudo firewall-cmd --state
running
防火墙以不同的区域(zone)运作,public 区域是我们将使用的默认区域。列出防火墙上所有处于活动状态的服务和端口。
$ sudo firewall-cmd --permanent --list-services
它应该显示如下输出。
cockpit dhcpv6-client ssh
Wiki.js 需要 HTTP 和 HTTPS 端口才能运行。开放这些端口。
$ sudo firewall-cmd --permanent --add-service=http
$ sudo firewall-cmd --permanent --add-service=https
添加 masquerade,因为该应用将与其他实例通信。
$ sudo firewall-cmd --permanent --add-masquerade
重新加载防火墙以应用更改。
$ sudo firewall-cmd --reload
步骤 2 - 安装 Docker 和 Docker Compose
Rocky Linux 自带的 Docker 版本较旧。要安装最新版本,首先安装官方的 Docker 仓库。
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
安装最新版本的 Docker。
$ sudo dnf install docker-ce docker-ce-cli containerd.io
在安装 Docker 时你可能会遇到以下错误。
ror:
Problem: problem with installed package buildah-1:1.26.2-1.el9_0.x86_64
- package buildah-1:1.26.2-1.el9_0.x86_64 requires runc >= 1.0.0-26, but none of the providers can be installed
- package containerd.io-1.6.9-3.1.el9.x86_64 conflicts with runc provided by runc-4:1.1.3-2.el9_0.x86_64
- package containerd.io-1.6.9-3.1.el9.x86_64 obsoletes runc provided by runc-4:1.1.3-2.el9_0.x86_64
- cannot install the best candidate for the job
如果出现上述错误,请使用以下命令。
$ sudo dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin --allowerasing
启用并运行 Docker 守护进程。
$ sudo systemctl enable docker --now
验证它是否正在运行。
? docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-11-12 00:19:44 UTC; 6s ago
TriggeredBy: ? docker.socket
Docs: https://docs.docker.com
Main PID: 99263 (dockerd)
Tasks: 8
Memory: 28.1M
CPU: 210ms
CGroup: /system.slice/docker.service
??99263 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
默认情况下,Docker 需要 root 权限。如果你想在每次运行 docker 命令时避免使用 sudo,可将你的用户名加入 docker 组。
$ sudo usermod -aG docker $(whoami)
你需要退出服务器并以同一用户重新登录以使此更改生效,或者使用以下命令。
$ su - ${USER}
确认你的用户已加入 Docker 组。
$ groups
navjot wheel docker
步骤 3 - 安装前准备
对于 Elasticsearch 而言,mmap 计数的默认限制非常低。运行以下命令检查默认值。
$ sysctl vm.max_map_count
你将看到如下输出。
vm.max_map_count = 65530
使用以下命令提高该值。
$ echo "vm.max_map_count=262144" | sudo tee /etc/sysctl.d/90-max_map_count.conf
$ sudo sysctl --load /etc/sysctl.d/90-max_map_count.conf
配置 SELinux 以允许网络连接。
$ sudo setsebool -P httpd_can_network_connect 1
步骤 4 - 安装 Mastodon
创建目录并设置所有权
为 Mastodon 及相关服务创建目录。
$ sudo mkdir -p /opt/mastodon/database/{postgresql,pgbackups,redis,elasticsearch}
$ sudo mkdir -p /opt/mastodon/web/{public,system}
$ sudo mkdir -p /opt/mastodon/branding
为 Elasticsearch、web 和 backup 目录设置正确的所有权。
$ sudo chown 991:991 /opt/mastodon/web/{public,system}
$ sudo chown 1000 /opt/mastodon/database/elasticsearch
$ sudo chown 70:70 /opt/mastodon/database/pgbackups
切换到 Mastodon 目录。
$ cd /opt/mastodon
创建环境和 docker compose 文件
为应用和数据库创建环境文件。
$ sudo touch application.env database.env
创建并打开 Docker compose 文件进行编辑。
$ sudo nano docker-compose.yml
将以下代码粘贴进去。
version: '3'
services:
postgresql:
image: postgres:15-alpine
env_file: database.env
restart: always
shm_size: 512mb
healthcheck:
test: ['CMD', 'pg_isready', '-U', 'postgres']
volumes:
- postgresql:/var/lib/postgresql/data
- pgbackups:/backups
networks:
- internal_network
redis:
image: redis:7-alpine
restart: always
healthcheck:
test: ['CMD', 'redis-cli', 'ping']
volumes:
- redis:/data
networks:
- internal_network
redis-volatile:
image: redis:7-alpine
restart: always
healthcheck:
test: ['CMD', 'redis-cli', 'ping']
networks:
- internal_network
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.17.7
restart: always
env_file: database.env
environment:
- cluster.name=elasticsearch-mastodon
- discovery.type=single-node
- bootstrap.memory_lock=true
- xpack.security.enabled=true
- ingest.geoip.downloader.enabled=false
ulimits:
memlock:
soft: -1
hard: -1
healthcheck:
test: ["CMD-SHELL", "nc -z elasticsearch 9200"]
volumes:
- elasticsearch:/usr/share/elasticsearch/data
networks:
- internal_network
website:
image: tootsuite/mastodon:v4.0.2
env_file:
- application.env
- database.env
command: bash -c "bundle exec rails s -p 3000"
restart: always
depends_on:
- postgresql
- redis
- redis-volatile
- elasticsearch
ports:
- '127.0.0.1:3000:3000'
networks:
- internal_network
- external_network
healthcheck:
test: ['CMD-SHELL', 'wget -q --spider --proxy=off localhost:3000/health || exit 1']
volumes:
- uploads:/mastodon/public/system
shell:
image: tootsuite/mastodon:v4.0.2
env_file:
- application.env
- database.env
command: /bin/bash
restart: "no"
networks:
- internal_network
- external_network
volumes:
- uploads:/mastodon/public/system
- static:/static
streaming:
image: tootsuite/mastodon:v4.0.2
env_file:
- application.env
- database.env
command: node ./streaming
restart: always
depends_on:
- postgresql
- redis
- redis-volatile
- elasticsearch
ports:
- '127.0.0.1:4000:4000'
networks:
- internal_network
- external_network
healthcheck:
test: ['CMD-SHELL', 'wget -q --spider --proxy=off localhost:4000/api/v1/streaming/health || exit 1']
sidekiq:
image: tootsuite/mastodon:v4.0.2
env_file:
- application.env
- database.env
command: bundle exec sidekiq
restart: always
depends_on:
- postgresql
- redis
- redis-volatile
- website
networks:
- internal_network
- external_network
healthcheck:
test: ['CMD-SHELL', "ps aux | grep '[s]idekiq\ 6' || false"]
volumes:
- uploads:/mastodon/public/system
networks:
external_network:
internal_network:
#internal:true
volumes:
postgresql:
driver_opts:
type: none
device: /opt/mastodon/database/postgresql
o: bind
pgbackups:
driver_opts:
type: none
device: /opt/mastodon/database/pgbackups
o: bind
redis:
driver_opts:
type: none
device: /opt/mastodon/database/redis
o: bind
elasticsearch:
driver_opts:
type: none
device: /opt/mastodon/database/elasticsearch
o: bind
uploads:
driver_opts:
type: none
device: /opt/mastodon/web/system
o: bind
static:
driver_opts:
type: none
device: /opt/mastodon/web/public
o: bind
按 Ctrl + X 保存文件,并在提示时输入 Y。
在撰写本教程时,Mastodon 的最新可用版本是 v4.0.2。请查看 Mastodon 的 GitHub Releases 页面,并相应调整 Docker compose 文件中的版本。我们同时使用了最新版本的 PostgreSQL 和 Redis。你可以根据需求进行调整。我们目前使用的是 Elasticsearch 7.x。Docker Hub 页面上没有可跟踪的 Elasticsearch 主要版本,因此你需要手动持续更新它,以获取与 Java 相关的安全更新。
internal: true 无法与 firewalld 配合使用,这就是为什么它在上面的文件中被注释掉了。如果这一问题得到修复,你可以重新添加该额外限制。
创建应用密钥
下一步是创建应用密钥值。
运行以下命令两次来生成 SECRET_KEY_BASE 和 OTP_SECRET 值。第一次会耗费一些时间,因为它会拉取镜像。
$ docker compose run --rm shell bundle exec rake secret
你也可以使用 openssl 工具来完成同样的操作。
$ openssl rand -hex 64
使用以下命令生成 VAPID_PRIVATE_KEY 和 VAPID_PUBLIC_KEY 值。
$ docker compose run --rm shell bundle exec rake mastodon:webpush:generate_vapid_key
你将得到类似的输出。
VAPID_PRIVATE_KEY=u2qsCs5JdmdmMLnUuU0sgmFGvZedteJz-lFB_xF4_ac=
VAPID_PUBLIC_KEY=BJXjE2hIXvFpo6dnHqyf1i-2PcP-cBoL95UCmhhxwlAgtFw_vnrYp4GBneR7_cmI9LZUYjHFh-TBAPSb9WTqH9A=
使用 openssl 工具生成 PostgreSQL 和 Elasticsearch 密码。
$ openssl rand -hex 15
Mastodon 环境文件
打开 application.env 文件进行编辑。
$ sudo nano application.env
将以下几行粘贴进去。
# environment
RAILS_ENV=production
NODE_ENV=production
# domain
LOCAL_DOMAIN=mastodon.example.com
# redirect to the first profile
SINGLE_USER_MODE=false
# do not serve static files
RAILS_SERVE_STATIC_FILES=false
# concurrency
WEB_CONCURRENCY=2
MAX_THREADS=5
# pgbouncer
#PREPARED_STATEMENTS=false
# locale
DEFAULT_LOCALE=en
# email, not used
SMTP_SERVER=email-smtp.us-west-2.amazonaws.com
SMTP_PORT=587
SMTP_LOGIN=AKIA3FIG4NVFNSC3AHXE
SMTP_PASSWORD=BHM4MVOjBmnGhSJ9lH3PAXKJ/9AiLWcUghG/kEN2kkFo
[email protected]
# secrets
SECRET_KEY_BASE=c09fa403575e0b431e54a2e228f20cd5a5fdfdbba0da80598959753b829a4e3c0266eedbac7e3cdf9f3345db36c56302c0e1bc5bfc8c5d516be59a2c41de7e37
OTP_SECRET=febb7dbb0d3308094083733fc923a430e52ccec767d48d7d2e0c577bfcb6863dbdfc920b1004b1f8c2967b9866bd7a0b4a15460f9fc7687aa4a42acf54e5a3d4
# Changing VAPID keys will break push notifications
VAPID_PRIVATE_KEY=13RgrfOY2tkwuUycylDPOkoHennkJ0ZAPV_fUwDy7-g=
VAPID_PUBLIC_KEY=BDAQuGwPbh1kbCV904adYXHvz9lLRaJHkiQkihRDPyBn3QmkAYbR21WHYoP8TkyG6dylG6IXpEVfLwdoW7fJVns=
按 Ctrl + X 保存文件,并在提示时输入 Y。
打开 database.env 文件进行编辑。
$ sudo nano database.env
将以下几行粘贴进去。
# postgresql configuration
POSTGRES_USER=mastodon
POSTGRES_DB=mastodon
POSTGRES_PASSWORD=15ff12dcb93aa60680d2aadb4032ee
PGPASSWORD=15ff12dcb93aa60680d2aadb4032ee
PGPORT=5432
PGHOST=postgresql
PGUSER=mastodon
# pgbouncer configuration
#POOL_MODE=transaction
#ADMIN_USERS=postgres,mastodon
#DATABASE_URL="postgres://mastodon:15ff12dcb93aa60680d2aadb4032ee@postgresql:5432/mastodon"
# elasticsearch
ES_JAVA_OPTS=-Xms512m -Xmx512m
ELASTIC_PASSWORD=13382e99f6b2d4dc7f3d66e4b9872d
# mastodon database configuration
#DB_HOST=pgbouncer
DB_HOST=postgresql
DB_USER=mastodon
DB_NAME=mastodon
DB_PASS=15ff12dcb93aa60680d2aadb4032ee
DB_PORT=5432
REDIS_HOST=redis
REDIS_PORT=6379
CACHE_REDIS_HOST=redis-volatile
CACHE_REDIS_PORT=6379
ES_ENABLED=true
ES_HOST=elasticsearch
ES_PORT=9200
ES_USER=elastic
ES_PASS=13382e99f6b2d4dc7f3d66e4b9872d
按 Ctrl + X 保存文件,并在提示时输入 Y。
准备 Mastodon
准备好将由 Nginx 提供服务的静态文件。
$ docker compose run --rm shell bash -c "cp -r /opt/mastodon/public/* /static/"
启动数据层。
$ docker compose up -d postgresql redis redis-volatile
检查容器的状态。
$ watch docker compose ps
等待变为 running (healthy),然后按 Ctrl + C,并使用以下命令初始化数据库。
$ docker compose run --rm shell bundle exec rake db:setup
步骤 5 - 安装 Nginx
Rocky Linux 自带的 Nginx 版本较旧。你需要下载官方的 Nginx 仓库来安装最新版本。
创建并打开 /etc/yum.repos.d/nginx.repo 文件以创建官方的 Nginx 仓库。
$ sudo nano /etc/yum.repos.d/nginx.repo
将以下代码粘贴进去。
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
按 Ctrl + X 保存文件,并在提示时输入 Y。
安装 Nginx 服务器。
$ sudo dnf install nginx
验证安装。
$ nginx -v
nginx version: nginx/1.22.1
启用并启动 Nginx 服务器。
$ sudo systemctl enable nginx --now
检查服务器的状态。
$ sudo systemctl status nginx
? nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2022-11-13 13:49:55 UTC; 1s ago
Docs: http://nginx.org/en/docs/
Process: 230797 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 230798 (nginx)
Tasks: 3 (limit: 12355)
Memory: 2.8M
CPU: 13ms
CGroup: /system.slice/nginx.service
??230798 "nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf"
??230799 "nginx: worker process"
??230800 "nginx: worker process"
步骤 6 - 安装 SSL
Certbot 工具使用 Let's Encrypt API 生成 SSL 证书。它需要 EPEL 仓库才能工作。
$ sudo dnf install epel-release
我们将使用 Snapd 安装 Certbot。安装 Snapd。
$ sudo dnf install snapd
启用并启动 Snap 服务。
$ sudo systemctl enable snapd --now
安装 Snap core 软件包。
$ sudo snap install core
$ sudo snap refresh core
创建 Snapd 工作所需的必要链接。
$ sudo ln -s /var/lib/snapd/snap /snap
$ echo 'export PATH=$PATH:/var/lib/snapd/snap/bin' | sudo tee -a /etc/profile.d/snapd.sh
执行以下命令安装 Certbot。
$ sudo snap install --classic certbot
通过创建指向其可执行文件的符号链接来启用 Certbot。
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot
生成 SSL 证书。
$ sudo certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m
[email protected]
-d mastodon.example.com
上述命令会将证书下载到你服务器上的 /etc/letsencrypt/live/mastodon.example.com 目录。
生成 Diffie-Hellman 组证书。
$ sudo openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096
要检查 SSL 续期是否正常工作,请对该过程进行一次演练(dry run)。
$ sudo certbot renew --dry-run
如果你没有看到任何错误,就说明一切就绪。你的证书将自动续期。
步骤 7 - 配置 Nginx
打开文件 /etc/nginx/nginx.conf 进行编辑。
$ sudo nano /etc/nginx/nginx.conf
在 include /etc/nginx/conf.d/*.conf; 这一行之前添加以下行。
server_names_hash_bucket_size 64;
按 Ctrl + X 保存文件,并在提示时输入 Y。
创建并打开文件 /etc/nginx/conf.d/mastodon.conf 进行编辑。
$ sudo nano /etc/nginx/conf.d/mastodon.conf
将以下代码粘贴进去。
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream backend {
server 127.0.0.1:3000 fail_timeout=0;
}
upstream streaming {
server 127.0.0.1:4000 fail_timeout=0;
}
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=CACHE:10m inactive=7d max_size=1g;
server {
listen 80 default_server;
server_name mastodon.example.com;
location / { return 301 https://$host$request_uri; }
}
server {
listen 443 ssl http2;
server_name mastodon.example.com;
access_log /var/log/nginx/mastodon.access.log;
error_log /var/log/nginx/mastodon.error.log;
http2_push_preload on; # Enable HTTP/2 Server Push
ssl_certificate /etc/letsencrypt/live/mastodon.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mastodon.example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/mastodon.example.com/chain.pem;
ssl_session_timeout 1d;
# Enable TLS versions (TLSv1.3 is required upcoming HTTP/3 QUIC).
ssl_protocols TLSv1.2 TLSv1.3;
# Enable TLSv1.3's 0-RTT. Use $ssl_early_data when reverse proxying to
# prevent replay attacks.
#
# @see: https://nginx.org/en/docs/http/ngx_http_ssl_module.html#ssl_early_data
ssl_early_data on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
keepalive_timeout 70;
sendfile on;
client_max_body_size 80m;
# OCSP Stapling ---
# fetch OCSP records from URL in ssl_certificate and cache them
ssl_stapling on;
ssl_stapling_verify on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
add_header X-Early-Data $tls1_3_early_data;
root /opt/mastodon/web/public;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml image/x-icon;
add_header Strict-Transport-Security "max-age=31536000" always;
location / {
try_files $uri @proxy;
}
location ~ ^/(system/accounts/avatars|system/media_attachments/files) {
add_header Cache-Control "public, max-age=31536000, immutable";
add_header Strict-Transport-Security "max-age=31536000" always;
root /opt/mastodon/;
try_files $uri @proxy;
}
location ~ ^/(emoji|packs) {
add_header Cache-Control "public, max-age=31536000, immutable";
add_header Strict-Transport-Security "max-age=31536000" always;
try_files $uri @proxy;
}
location /sw.js {
add_header Cache-Control "public, max-age=0";
add_header Strict-Transport-Security "max-age=31536000" always;
try_files $uri @proxy;
}
location @proxy {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Proxy "";
proxy_pass_header Server;
proxy_pass http://backend;
proxy_buffering on;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_cache CACHE;
proxy_cache_valid 200 7d;
proxy_cache_valid 410 24h;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
add_header X-Cached $upstream_cache_status;
add_header Strict-Transport-Security "max-age=31536000" always;
tcp_nodelay on;
}
location /api/v1/streaming {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Proxy "";
proxy_pass http://streaming;
proxy_buffering off;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
tcp_nodelay on;
}
error_page 500 501 502 503 504 /500.html;
}
# This block is useful for debugging TLS v1.3. Please feel free to remove this
# and use the `$ssl_early_data` variable exposed by NGINX directly should you
# wish to do so.
map $ssl_early_data $tls1_3_early_data {
"~." $ssl_early_data;
default "";
}
完成后,按 Ctrl + X 保存文件,并在提示时输入 Y。
验证 Nginx 配置文件的语法。
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重启 Nginx 服务器。
$ sudo systemctl restart nginx
如果你遇到以下错误,很可能是由 SELinux 限制引起的。
nginx: [emerg] open() "/var/run/nginx.pid" failed (13: Permission denied)
要修复该错误,请运行以下命令。
$ sudo ausearch -c 'nginx' --raw | audit2allow -M my-nginx
$ sudo semodule -X 300 -i my-nginx.pp
再次启动 Nginx 服务。
$ sudo systemctl start nginx
步骤 8 - 启动 Mastodon
Tootctl CLI 工具
Tootctl CLI 工具用于在 Mastodon 上执行管理任务。我们需要让它在宿主机的 shell 中可用。
创建文件 /usr/local/bin/tootctl 并打开进行编辑。
$ sudo nano /usr/local/bin/tootctl
将以下代码粘贴进去。
#!/bin/bash
docker compose -f /opt/mastodon/docker-compose.yml run --rm shell tootctl "$@"
按 Ctrl + X 保存文件,并在提示时输入 Y。
赋予该文件可执行权限。
$ sudo chmod +x /usr/local/bin/tootctl
Mastodon 服务文件
你可以使用 Docker compose 命令启动 Mastodon 容器,但通过 systemd 单元文件来操作会更简单。
创建并打开 Mastodon 服务文件进行编辑。
$ sudo nano /etc/systemd/system/mastodon.service
将以下代码粘贴进去。
[Unit]
Description=Mastodon service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/opt/mastodon
ExecStart=/usr/bin/docker compose -f /opt/mastodon/docker-compose.yml up -d
ExecStop=/usr/bin/docker compose -f /opt/mastodon/docker-compose.yml down
[Install]
WantedBy=multi-user.target
按 Ctrl + X 保存文件,并在提示时输入 Y。
重新加载 systemd 守护进程以启用该服务文件。
$ sudo systemctl daemon-reload
启用并启动 Mastodon 服务。
$ sudo systemctl enable --now mastodon.service
检查 Docker 容器的状态。
$ watch docker compose -f /opt/mastodon/docker-compose.yml ps
一旦容器的状态变为 running (healthy),按 Ctrl + C 退出该界面。
为 Mastodon 创建管理员用户,并记下提供的密码。
$ tootctl accounts create navjot --email
[email protected]
--confirmed --role admin
OK
New password: 1338afbe1b4e06e823b6625da80cb537
如果你想关闭用户注册,请使用以下命令。
$ tootctl settings registrations close
要重新开放注册,请执行以下命令。
$ tootctl settings registrations open
初始化搜索
在创建并填充 Elasticsearch 索引之前,你需要先发布一条 toot。发布 toot 后,执行以下命令。
$ tootctl search deploy
你可能会遇到以下错误。
/opt/mastodon/vendor/bundle/ruby/3.0.0/gems/ruby-progressbar-1.11.0/lib/ruby-progressbar/progress.rb:76:in `total=': You can't set the item's total value to less than the current progress. (ProgressBar::InvalidProgressError)
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/ruby-progressbar-1.11.0/lib/ruby-progressbar/base.rb:178:in `block in update_progress'
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/ruby-progressbar-1.11.0/lib/ruby-progressbar/output.rb:43:in `with_refresh'
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/ruby-progressbar-1.11.0/lib/ruby-progressbar/base.rb:177:in `update_progress'
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/ruby-progressbar-1.11.0/lib/ruby-progressbar/base.rb:101:in `total='
from /opt/mastodon/lib/mastodon/search_cli.rb:67:in `deploy'
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/thor-1.2.1/lib/thor/invocation.rb:116:in `invoke'
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/thor-1.2.1/lib/thor.rb:243:in `block in subcommand'
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
from /opt/mastodon/vendor/bundle/ruby/3.0.0/gems/thor-1.2.1/lib/thor/base.rb:485:in `start'
from /opt/mastodon/bin/tootctl:8:in `<main>'
在这种情况下,进入 website 容器的 shell。
$ docker exec -it mastodon-web-1 /bin/bash
运行以下命令。
$ sed -E 's/indices.sum.+/2000/g' -i lib/mastodon/search_cli.rb
退出容器 shell。
$ exit
再次运行 Elasticsearch 部署命令。
$ tootctl search deploy
附加辅助服务
让我们创建另一个用于删除已下载媒体文件的服务。
创建并打开 Mastodon 媒体删除服务进行编辑。
$ sudo nano /etc/systemd/system/mastodon-media-remove.service
将以下代码粘贴进去。
[Unit]
Description=Mastodon - media remove service
Wants=mastodon-media-remove.timer
[Service]
Type=oneshot
StandardError=null
StandardOutput=null
WorkingDirectory=/opt/mastodon
ExecStart=/usr/bin/docker compose -f /opt/mastodon/docker-compose.yml run --rm shell tootctl media remove
[Install]
WantedBy=multi-user.target
按 Ctrl + X 保存文件,并在提示时输入 Y。
如果你想安排媒体删除计划,可以为其设置一个 timer 服务。
$ sudo nano /etc/systemd/system/mastodon-media-remove.timer
粘贴以下代码。
[Unit]
Description=Schedule a media remove every week
[Timer]
Persistent=true
OnCalendar=Sat *-*-* 00:00:00
Unit=mastodon-media-remove.service
[Install]
WantedBy=timers.target
按 Ctrl + X 保存文件,并在提示时输入 Y。
你可以设置另一个服务来删除使用 OpenGraph 标签生成的富预览卡片。
$ sudo nano /etc/systemd/system/mastodon-preview_cards-remove.service
粘贴以下代码。
[Unit]
Description=Mastodon - preview cards remove service
Wants=mastodon-preview_cards-remove.timer
[Service]
Type=oneshot
StandardError=null
StandardOutput=null
WorkingDirectory=/opt/mastodon
ExecStart=/usr/bin/docker compose -f /opt/mastodon/docker-compose.yml run --rm shell tootctl preview_cards remove
[Install]
WantedBy=multi-user.target
按 Ctrl + X 保存文件,并在提示时输入 Y。
设置相应的 timer 服务。
$ sudo nano /etc/systemd/system/mastodon-preview_cards-remove.timer
粘贴以下代码。
[Unit]
Description=Schedule a preview cards remove every week
[Timer]
Persistent=true
OnCalendar=Sat *-*-* 00:00:00
Unit=mastodon-preview_cards-remove.service
[Install]
WantedBy=timers.target
按 Ctrl + X 保存文件,并在提示时输入 Y。
重新加载 systemd 守护进程。
$ sudo systemctl daemon-reload
启用并启动这些 timer。
$ sudo systemctl enable --now mastodon-preview_cards-remove.timer
$ sudo systemctl enable --now mastodon-media-remove.timer
列出所有 timer,以查看 Mastodon 服务的计划安排。
$ systemctl list-timers
访问 Mastodon
访问 URL https://mastodon.example.com 来进入你的实例,你将看到一个类似的页面。

在上面的截图中,你可以看到有 2 个用户,其中 1 个(我)被设为管理员。通常情况并非如此。即使你创建了管理员账户,它在首次运行时也不会显示在主页上。为此,请登录你的实例,你将被带到以下页面。

点击右侧边栏中的 Preferences 选项以进入设置。在那里,点击左侧菜单中的 Administration 选项,进入 Mastodon 的管理面板。

点击左侧边栏中的 Site settings 选项。

在此处填写你的联系用户名和商业邮箱,这些信息现在将显示在你服务器的主页上。同时填写其他各种信息,包括服务器描述、logo 和服务器规则,以自定义你的 Mastodon 实例。
步骤 9 - Mastodon 维护
要查看你的 Mastodon 实例的性能和日志,请前往 https://mastodon.example.com/sidekiq/ 。
在这里你可以查看与你的 Mastodon 实例相关的各种进程和计划任务的列表。你还可以在 Dead 或 Retries 部分查看失败的任务。它还会显示你实例的内存使用情况。

你可以从 https://mastodon.example.com/pghero/ 查看你实例数据库的健康状况。

你可以对数据库进行维护、运行 SQL 查询,并删除未使用的索引。要启用查询统计,请点击上述页面上的 Enable 按钮,你将得到以下信息。

切换到 root 用户。
$ sudo -i su
切换到 /opt/mastodon/database/postgresql 目录。
$ cd /opt/mastodon/database/postgresql
打开 postgresql.conf 文件。
$ nano postgresql.conf
找到 #shared_preload_libraries = '' # (change requires restart) 这一行,并将其替换为以下内容。
shared_preload_libraries = 'pg_stat_statements'
在文件末尾添加以下行。
pg_stat_statements.track = all
按 Ctrl + X 保存文件,并在提示时输入 Y。
重启 Mastodon 容器。
$ systemctl restart mastodon.service
退出 root shell。
$ exit
如果你查看数据库健康页面,现在可以看到是否存在任何慢查询。

注意:你也可以从 Preferences 菜单启动 PgHero 和 Sidekiq 的 URL。
如果你的站点由于某种原因无法加载,你可以查看 Docker 生成的日志。
$ docker logs <container-name>
步骤 10 - 备份 Mastodon
我们将使用一个名为 Restic 的第三方工具来备份 Mastodon。使用 Restic 备份的第一步是将所有文件和目录添加到仓库列表中。
创建并打开仓库列表文件进行编辑。
$ sudo nano /opt/mastodon/backup-files
将以下几行粘贴进去。
/etc/nginx
/etc/letsencrypt
/etc/systemd/system
/root
/opt/mastodon/database/pgbackups
/opt/mastodon/*.env
/opt/mastodon/docker-compose.yml
/opt/mastodon/branding
/opt/mastodon/database/redis
/opt/mastodon/web/system
/opt/mastodon/backup-files
/opt/mastodon/mastodon-backup
按 Ctrl + X 保存文件,并在提示时输入 Y。
安装 Restic。
$ sudo dnf install restic
创建一个备份仓库并进行初始备份。我们将数据备份到 S3 服务。
$ restic -r s3:https://$SERVER:$PORT/mybucket init
$ restic -r s3:https://$SERVER:$PORT/mybucket backup $(cat /opt/mastodon/backup-files) --exclude /opt/mastodon/database/postgresql
创建 Mastodon 备份服务 timer 并打开进行编辑。
$ sudo nano /etc/systemd/system/mastodon-backup.timer
将以下代码粘贴进去。
[Unit]
Description=Schedule a mastodon backup every hour
[Timer]
Persistent=true
OnCalendar=*:00:00
Unit=mastodon-backup.service
[Install]
WantedBy=timers.target
按 Ctrl + X 保存文件,并在提示时输入 Y。
创建 Mastodon 备份服务文件并打开进行编辑。
$ sudo nano /etc/systemd/system/mastodon-backup.service
将以下代码粘贴进去。
[Unit]
Description=Mastodon - backup service
# Without this, they can run at the same time and race to docker compose,
# double-creating networks and failing due to ambiguous network definition
# requiring `docker network prune` and restarting
After=mastodon.service
[Service]
Type=oneshot
StandardError=file:/var/log/mastodon-backup.err
StandardOutput=file:/var/log/mastodon-backup.log
WorkingDirectory=/opt/mastodon
ExecStart=/bin/bash /opt/mastodon/mastodon-backup
[Install]
WantedBy=multi-user.target
按 Ctrl + X 保存文件,并在提示时输入 Y。
接下来,创建并打开 /opt/mastodon/mastodon-backup 文件进行编辑。其中包含实际的备份命令。
$ sudo nano /opt/mastodon/mastodon-backup
将以下代码粘贴进去。
#!/bin/bash
set -e
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
SERVER=
PORT=
RESTIC_PASSWORD_FILE=/root/restic-pasword
docker compose -f /opt/mastodon/docker-compose.yml run --rm postgresql sh -c "pg_dump -Fp mastodon | gzip > /backups/dump.sql.gz"
restic -r s3:https://$SERVER:$PORT/mybucket --cache-dir=/root backup $(cat /opt/mastodon/backup-files) --exclude /opt/mastodon/database/postgresql
restic -r s3:https://$SERVER:$PORT/mybucket --cache-dir=/root forget --prune --keep-hourly 24 --keep-daily 7 --keep-monthly 3
按 Ctrl + X 保存文件,并在提示时输入 Y。
赋予备份脚本可执行权限。
$ sudo chmod +x /opt/mastodon/mastodon-backup
重新加载服务守护进程,并启动备份服务和 timer。
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now mastodon-backup.service
$ sudo systemctl enable --now mastodon-backup.timer
使用以下命令确认每小时的备份正在执行并可访问。
$ restic -r s3:https://$SERVER:$PORT/mybucket snapshots
$ restic -r s3:https://$SERVER:$PORT/mybucket mount /mnt
步骤 11 - 升级 Mastodon
升级 Mastodon 需要几个步骤。首先,切换到该目录。
$ cd /opt/mastodon
拉取 Mastodon 的最新容器镜像。
$ docker compose pull
如果需要,可在 docker-compose.yml 中进行任何更改。
执行所有数据库迁移。
$ docker compose run --rm shell bundle exec rake db:migrate
更新你的静态文件副本。
$ docker compose run --rm shell bash -c "cp -r /opt/mastodon/public/* /static/"
重启 Mastodon 容器。
$ sudo systemctl restart mastodon.service
上述说明是通用的更新说明。请务必查看 Mastodon 的 GitHub releases 页面,查找版本之间任何特定的更新任务和命令,以确保一切顺利进行。
结论
至此,我们关于在 Rocky Linux 9 服务器上使用 Docker 安装 Mastodon 社交网络的教程就结束了。如果你有任何问题,请在下方评论中留言。