如何在 Ubuntu 20.04 上安装 Mastodon

介绍

Mastodon是一个微博社交网络应用程序,使用一个分散的模型,也称为 federation. Mastodon 是开源的,可以由任何人在任何服务器上部署,每个服务器都是自己的 instance. 每个实例可以有自己的策略和调控。 在所有 Mastodon 服务器的联盟中,每个 Mastodon 实例可以了解其他实例。

在本教程中,您将安装最新版本的Mastodon,该版本是 4.0.2 在此写作时。您将配置官方Mastodon安装存储库,并设置环境来安装所有其他依赖。 使用互动设置,您将设置您的自定义Mastodon实例存储,电子邮件,资产和您的管理员帐户。 最后,您将通过SSL/TLS证书通过Let's Encrypt保护您的实例。

<$>[note] 注: 本教程详细介绍了Mastodon背后的团队推荐的裸体金属服务器上的安装方法。

您可以按照我们的教程在 如何安装Mastodon with DigitalOcean Marketplace 1-Click开始。

美元

前提条件

要跟随这个教程,你需要:

您可以通过遵循我们的 初始服务器设置与Ubuntu 20.04教程来设置此设置。

一旦你已经设置了一切,你已经准备好开始第一步。

步骤 1 — 创建一个mastodon系统用户和PostgreSQL角色

Mastodon的源代码假定您的服务器将使用一个特定的系统用户命名为mastodon进行管理,这目前被硬编码到Mastodon的许多部分。

本教程假定您在前提条件下在初始服务器设置中创建了一个用户,但下列命令也可以作为 root 运行,在命令之前忽略了sudo

1sudo adduser mastodon

接下来,将你的mastodon用户添加到sudo组中,以授予它管理权限:

1sudo usermod -aG sudo mastodon

随着您的系统级mastodon用户创建,然后在您的数据库中创建一个匹配的角色。 PostgreSQL 使用 peer authentication,将创建的角色与匹配的系统用户联系起来。

1sudo -u postgres createuser --interactive

互动旗帜将显示以下提示,您可以选择您新角色的名称mastodon

1[secondary_label Output]
2Enter name of role to add: mastodon
3Shall the new role be a superuser? (y/n) y

现在你可以登录你的新创建的mastodon用户:

1su - mastodon

在本教程的剩余时间里,您将作为这个mastodon用户运行所有命令。

步骤2 — 安装依赖物和克隆马斯托登

首先,你需要安装它的依赖性. Mastodon 是一个在 Linux 环境中运行的 Ruby 和 Node.js 的项目,因此需要三个包管理器: Linux 系统级别的 [APT](https://en.wikipedia.org/wiki/APT_(software), Ruby 宝石的 Bundler和 Node.js 包的 Yarn

  • 安装 APT 依赖.
  • 从源头克隆 Mastodon.
  • 安装 Ruby 和 Mastodon 的 Ruby 依赖.
  • 安装 Node.js 依赖。

第一步是通过apt安装依赖,其中包括: Redis作为密钥商店数据库, Certbot用于处理TLS/SSL加密,以及用于构建,编译和图像处理的不同库。

1sudo apt update

然后安装apt install的包:

1sudo apt install \
2  imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev \
3  libprotobuf-dev protobuf-compiler pkg-config \
4  redis-server redis-tools \
5  certbot python3-certbot-nginx libidn11-dev libicu-dev libjemalloc-dev

克隆马斯托顿

现在您的服务器已经准备好从其官方存储库克隆 Mastodon 源. 如前所述,本教程将使用最新版本的 Mastodon,该版本是 v4.0.2 在此写作时,最新的功能,安全性和错误修复。

首先,将 Mastodon 存储库克隆到您的服务器中,以一个名为live的新目录:

1git clone https://github.com/mastodon/mastodon.git live

这个目录命名公约在整个Mastodon的文档中使用,并建议保持一致性。接下来,进入您创建的目录,请注意,这将是您的项目目录,用于本教程的其余部分:

1cd live

现在你可以使用git来检查最新版本 v4.0.2:

1git checkout v4.0.2

安装 Ruby 和 Mastodon 的 Ruby 依赖

有了Mastodon的源代码,你现在可以继续安装Ruby和Mastodon的Ruby依赖。你将安装rbenv,你将使用的版本管理工具来安装Ruby编程语言.因为rbenv安装在用户级别而不是系统级别,此步骤必须在你创建并切换到你的mastodon用户后在这里完成。你将安装Ruby版本 3.0.4 按照我们的教程(How To Install Ruby on Rails with rbenv on Ubuntu 20.04)(https://andsky.com/tech/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-ubuntu-20-04)的步骤。虽然版本 ** 3.0.4** 在 ** Step 2** 中不会是一个明确列出的选项,它仍然会工作,如果你在安装命令中指定这个版本。 请记住,虽然本教程是用于Ruby on Rails,你只会使用以Ruby

Bundler 安装了 Mastodon 所需的所有 Ruby 宝石,但首先你需要配置它以进行生产的安装。

使用Bundler的bundle config命令与其配置系统进行交互,用它将部署设置为:

1bundle config deployment 'true'

接下来,使用没有来确保开发和测试的依赖性不会安装:

1bundle config without 'development test'

您现在可以使用安装来安装 Ruby 依赖性,这可能需要一些时间,但这是正常的行为:

1bundle install
1[secondary_label Output]
2Fetching gem metadata from https://rubygems.org/.........
3Fetching https://github.com/ClearlyClaire/webpush.git
4Fetching rake 13.0.6
5Installing rake 13.0.6
6Fetching concurrent-ruby 1.1.10
7Installing concurrent-ruby 1.1.10
8. . .

一旦完成,最终会有大量的输出,这可能看起来像需要采取行动,但是,您可以安全地继续而不需要额外的行动。

安装 Mastodon 的 JavaScript 依赖

一旦Mastodon的Ruby依赖完成安装,您可以转到JavaScript依赖。Mastodon依赖 yarn来管理和安装JavaScript包,而不是npm。

若要访问yarn,请启用 Node.js 包含的corepack:

1sudo corepack enable

Yarn的开发历史在其版本 1.0,也被称为经典和版本 2.0之间有所不同。

1yarn set version classic

现在您可以使用yarn install安装Mastodon的JavaScript依赖程序,额外的旗帜确保不会生成yarn.lock文件:

1yarn install --pure-lockfile
 1[secondary_label Output]
 2yarn install v1.22.19
 3[1/6] Validating package.json...
 4[2/6] Resolving packages...
 5[3/6] Fetching packages...
 6[4/6] Linking dependencies...
 7warning Workspaces can only be enabled in private projects.
 8[5/6] Building fresh packages...
 9[6/6] Cleaning modules...
10Done in 50.84s.

您现在已经准备好为您的服务器创建一个自定义 Mastodon 配置文件。

步骤 3 — 使用互动设置生成 Mastodon 配置文件

Mastodon 使用.env 文件的变体作为其配置文件. 有一个样本可在 ~/live/.env.production.sample 查看,但而不是复制该文件,您将使用交互式设置流程. 完成后,将生成一个自定义文件在 ~/live/.env.production. 这个交互式设置将涵盖基本的服务器信息,建立 PostgreSQL 和 Redis 连接,设置云存储和电子邮件处理,编译 CSS 和 JavaScript 资产,并在 Mastodon 中创建管理员帐户。

在开始多步设置过程中,请确保您的防火墙允许通过相应的端口 SMTP 流量. 安全的 TLS 连接的默认 SMTP 端口是端口 587,这将在电子邮件设置部分稍后解释。

1sudo ufw allow 587

要开始交互式设置,请使用bundle exec rake执行Mastodon的设置,同时为生产设置环境变量:

1RAILS_ENV=production bundle exec rake mastodon:setup

请注意,在事实发生后很难更改,所以双重检查您的输入准确性:

1[secondary_label Output]
2Your instance is identified by its domain name. Changing it afterward will break things.
3Domain name: your_domain

Mastodon 有一个单用户模式,注册被禁用. 对于旨在创建与他人共享空间的服务器,您不应该启用单用户模式:

1[secondary_label Output]
2Single user mode disables registrations and redirects the landing page to your public profile.
3Do you want to enable single user mode? No

回答 no 关于使用 Docker 的提示,因为本教程涵盖了在纯金属服务器上的常规安装:

1[secondary_label Output]
2Are you using Docker to run Mastodon? no

配置数据库

您的数据库不会使用密码进行身份验证,而是使用同行身份验证,我们将在稍后讨论。

1[secondary_label Output]
2PostgreSQL host: /var/run/postgresql
3PostgreSQL port: 5432
4Name of PostgreSQL database: mastodon_production
5Name of PostgreSQL user: mastodon
6Password of PostgreSQL user:
7Database configuration works!

Redis 设置也可以保持其默认值,并且在密码字段左边是空的。您将不会设置密码,因为这个数据库仅在本地服务器上可访问。

1[secondary_label Output]
2Redis host: localhost
3Redis port: 6379
4Redis password:
5Redis configuration works!

两个数据库的连接将被测试为工作。

配置存储

接下来,您将被问及如何将文件上传到云存储中。Mastodon存储可以使用本地存储或通过多个提供商进行云存储设置。如果您想使用本地存储,或者如果您没有云存储即时解决方案,请回答 No

然而,这不是作为长期存储解决方案的可持续性,并且用于生产目的建议使用物体存储解决方案。 Mastodon 支持多种流行的物体存储解决方案,包括 DigitalOcean Spaces 和其他 S3 兼容的解决方案,如 MinIO. MinIO 是开源的,您可以遵循我们关于 如何设置自己的 MinIO 物体存储服务器的教程。 为本教程的目的,您可以选择 Yes ,并使用以下示例用于 DigitalOcean Spaces。

1Do you want to store uploaded files on the cloud? yes
2Provider DigitalOcean Spaces
3Space name: your_spaces_name
4Space region: your_spaces_region
5Space endpoint: your_spaces_endpoint
6Space access key: your_spaces_access_key
7Space secret key: your_spaces_secret_key
8Do you want to access the uploaded files from your own domain? No

您还可以使用域上的链接访问上传到您的 Mastodon 实例的文件,而不是使用直接链接到您的云存储. 如果您想要,您可以选择此选项,但本教程将选择 No

配置外出电子邮件

Mastodon 需要访问 SMTP 电子邮件服务器作为发送电子邮件的方法。建议使用外部电子邮件服务提供商而不是运行自己的服务器。 Mastodon 在其示例中使用 Mailgun。 维护安全可靠的电子邮件服务器是一个艰难的任务,因此建议您依赖第三方电子邮件服务提供商。 但是,您应该知道大多数第三方电子邮件服务提供商根据数量收费,并且可能或可能没有在特定使用门槛以下的免费级别。

<$>[注] 注: 虽然本教程可以没有立即的SMTP解决方案完成,但如果所有凭证字段都是空的或默认的,那么这样做会妨碍Mastodon的全部功能,并且不建议使用。您可以选择与电子邮件提供商建立帐户,并在稍后将凭证添加到您的配置文件中。

1[secondary_label Output]
2Do you want to send e-mails from localhost? No

对于所有电子邮件提供商,您必须创建和配置一个帐户。这取决于提供商。最有可能在注册后,您需要与提供商验证您的域,以便在涉及添加 DNS 记录到您的域的过程中允许发送电子邮件。

本教程使用 Mailgun 作为一个例子,虽然任何选择的电子邮件服务提供商都会起作用. 这些选择也可以作为其他电子邮件服务提供商的指导方针。 Mailgun 特别是 自动为每个添加的域名生成 SMTP 凭证)。

1[secondary_label Output]
2SMTP server: smtp.mailgun.org
3SMTP port: 587
4SMTP username: your_smtp_username
5SMTP password: your_smtp_password
6SMTP authentication: plain
7SMTP OpenSSL verify mode: peer
8Enable STARTTLS: auto
9E-mail address to send e-mails "from": Mastodon <notifications@your_domain>

虽然平行的默认值似乎不适用于SMTP身份验证,但电子邮件服务提供商支持的SMTP身份验证(https://en.wikipedia.org/wiki/SMTP_Authentication)的常见选项包括平行登录,这两者都是Base64编码的。Base64编码本身并不是一个足够的安全措施。相反,真正重要的是通过安全的TLS/SSL连接向电子邮件提供商提交请求。在端口587上的SMTP连接是与电子邮件提供商的TLS连接的默认值。

发送测试电子邮件以确保您的 SMTP 凭证工作,然后指定您可以访问的收件人电子邮件,例如您的个人电子邮件:

1[secondary_label Output]
2Send a test e-mail with this configuration right now? yes

这个测试电子邮件可能需要一分钟的时间来到达,取决于您的电子邮件服务. 再次,您可以稍后返回以更改您生成的 Mastodon 配置文件中的 SMTP 凭证。

完成您的配置

接下来,您将被要求保存您的配置,然后Mastodon将使用它来创建您的数据库:

 1[secondary_label Output]
 2This configuration will be written to .env.production
 3Save configuration? Yes
 4
 5Now that configuration is saved, the database schema must be loaded.
 6If the database already exists, this will erase its contents.
 7Prepare the database now? Yes
 8Running `RAILS_ENV=production rails db:setup` ...
 9
10Created database 'mastodon_production'
11Done!

之后,您将被要求编译 Mastodon 的 CSS 和 JavaScript 资产. 请注意有关时间和资源消耗的警告:

 1[secondary_label Output]
 2The final step is compiling CSS/JS assets.
 3This may take a while and consume a lot of RAM.
 4Compile the assets now? Yes
 5Running `RAILS_ENV=production rails assets:precompile` ...
 6
 7yarn install v1.22.19
 8[1/6] Validating package.json...
 9[2/6] Resolving packages...
10[3/6] Fetching packages...
11[4/6] Linking dependencies...
12. . .
13
14All done! You can now power on the Mastodon server

您的 Mastodon 服务器已经准备好了,但尚未运行. 另一个提示是创建管理员帐户来登录您的 Mastodon 服务器:

1[secondary_label Output]
2Do you want to create an admin user straight away? Yes
3Username: mastodon
4E-mail: your_email
5You can login with the password: your_generated_password
6You can change your password once you login.

请注意,您现在没有选择为您的管理员帐户选择密码的选项,您将得到一个独特的,生成的密码,您应该在其他地方记住。

打开生成的 Mastodon 配置文件,在nano或您喜爱的文本编辑器中验证其内容:

1nano ~/live/.env.production

内容将类似于如下:

 1[label ~/live/.env.production]
 2# Generated with mastodon:setup on 2022-11-22 18:22:25 UTC
 3
 4# Some variables in this file will be interpreted differently whether you are
 5# using docker-compose or not.
 6
 7LOCAL_DOMAIN=your_domain
 8SINGLE_USER_MODE=false
 9SECRET_KEY_BASE=your_generated_key_base
10OTP_SECRET=your_generated_secret
11VAPID_PRIVATE_KEY=your_generated_private_key
12VAPID_PUBLIC_KEY=your_generated_public_key
13DB_HOST=/var/run/postgresql
14DB_PORT=5432
15DB_NAME=mastodon_production
16DB_USER=mastodon
17DB_PASS=
18REDIS_HOST=localhost
19REDIS_PORT=6379
20REDIS_PASSWORD=
21S3_ENABLED=true
22S3_PROTOCOL=https
23S3_BUCKET=your_spaces_name
24S3_REGION=your_spaces_region
25S3_HOSTNAME=your_spaces_endpoint
26S3_ENDPOINT=https://your_spaces_endpoint
27AWS_ACCESS_KEY_ID=your_spaces_access_key
28AWS_SECRET_ACCESS_KEY=your_spaces_secret_key
29SMTP_SERVER=smtp.mailgun.org
30SMTP_PORT=587
31SMTP_LOGIN=your_smtp_username
32SMTP_PASSWORD=your_smtp_password
33SMTP_AUTH_METHOD=plain
34SMTP_OPENSSL_VERIFY_MODE=peer
35SMTP_ENABLE_STARTTLS=auto
36SMTP_FROM_ADDRESS='Mastodon <[email protected]>'

您可以在交互式设置过程后手动编辑此文件. 完成检查后,保存并关闭该文件. 如果您使用nano,您可以使用CTRL + X,然后在提示时,Y,然后Enter

步骤 4 — 使用 Certbot 和 Let's Encrypt 配置 Nginx for Mastodon

您的 Mastodon 應用程式伺服器目前僅可在本地使用。 要透過互聯網允許存取,您需要使用反向代理機曝光它。 Nginx 被建議作為反向代理機服務您的 Mastodon 應用程式。 要了解更多關於反向代理的話題,您可以參閱我們的教程 如何設定 Nginx 作為反向代理或我們的通用 介紹代理

Mastodon 存储器配备了默认的 Nginx 配置文件,您可以使用,但您需要做出一些更改以匹配您的域,并使用 Certbot 和 Let's Encrypt 保护它. 虽然 Mastodon 提供的 Nginx 文件完全挤满了必要的块和指令,以便全面部署,其默认的 SSL/TLS 设置防止与 Certbot 无缝交互。 此步骤将通过两轮编辑 Nginx 配置文件来绕过此限制,在使用 Certbot 之前和之后。

首先,调整防火墙设置以允许HTTP和HTTPS流量到您的Nginx服务器:

1sudo ufw allow 'Nginx Full'

这对于用户访问 Mastodon 网站是必要的,但 Certbot 也需要对您的证书进行必要的验证。接下来,从 Mastodon 存储库复制提供的 Nginx 配置:

1sudo cp /home/mastodon/live/dist/nginx.conf /etc/nginx/sites-available/mastodon

Nginx 使用一个常见的做法,称为 symbolic links,或 symlinks,来跟踪您的服务器块中哪些是启用的。创建一个 symlink 就像在磁盘上创建一个快捷键,这样您可以稍后从网站启用目录中删除快捷键,同时将服务器块保留在网站可用中,如果你想启用它。

1sudo ln -s /etc/nginx/sites-available/mastodon /etc/nginx/sites-enabled/mastodon

现在 Nginx 知道这个配置文件中的设置应该被用来服务您的 Mastodon 网站. 但是,您还没有对您的 Nginx 配置做出必要的更改。

1sudo nano /etc/nginx/sites-enabled/mastodon

您需要确保server_name指向您的域名,并指向您的域名的www.版本。有两个实例的server_name,其中一个在服务器块上倾听在端口80的HTTP,另一个在区块上倾听在端口443的HTTPS。

此外,您需要在第二个服务器区块中评论倾听指令,并在行开始时添加一个# 符号。随着您的SSL证书尚未设置,对于这个倾听指令的ssl参数会导致Nginx将此配置检测为无效,这意味着Certbot不会为您创建证书。

 1[label /etc/nginx/sites-enabled/mastodon]
 2. . .
 3server {
 4  listen 80;
 5  listen [::]:80;
 6  server_name your_domain www.your_domain;
 7  root /home/mastodon/live/public;
 8  location /.well-known/acme-challenge/ { allow all; }
 9  location / { return 301 https://$host$request_uri; }
10}
11
12server {
13   # listen 443 ssl http2;
14   # listen [::]:443 ssl http2;
15  server_name your_domain www.your_domain;
16. . .

完成后,保存并关闭文件. 现在您已经准备好创建安全的 SSL/TLS 访问证书. 因为您正在使用 Mastodon 的默认 Nginx 配置文件,该文件已预定位,您的 Certbot 命令被称为certonly。 这允许您手动配置配置配置文件。 否则,Certbot 可能会对您的 Nginx 环境做出不兼容 Mastodon 的假设:

1sudo certbot certonly --nginx -d your_domain -d www.your_domain
1[secondary_label Output]
2. . .
3IMPORTANT NOTES:
4 - Congratulations! Your certificate and chain have been saved at:
5   /etc/letsencrypt/live/your_domain/fullchain.pem
6   Your key file has been saved at:
7   /etc/letsencrypt/live/your_domain/privkey.pem
8. . .

请注意您生成的证书和关键文件的路径,您需要将其添加到配置文件中。

1sudo nano /etc/nginx/sites-enabled/mastodon

回想一下你之前对端口443发表评论的倾听指令。你现在可以安全地放弃评论。以前,这些行会因为没有证书而导致错误。

此外,请在同一个区块中解除ssl_certificatessl_certificate_key指令,确保它匹配了由Certbot先前生成的证书的文件路径:

 1[label /etc/nginx/sites-enabled/mastodon]
 2. . .
 3server {
 4   listen 443 ssl http2;
 5   listen [::]:443 ssl http2;
 6  server_name your_domain www.your_domain;
 7. . . 
 8  # Uncomment these lines once you acquire a certificate:
 9   ssl_certificate 	/etc/letsencrypt/live/your_domain/fullchain.pem;
10   ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem;
11. . .

保存您的文件并离开文本编辑器 请通过以下命令检查您的 Nginx 配置文件是否有效:

1sudo nginx -t

最后,重新启动 Nginx 以应用这些更改:

1sudo systemctl reload nginx

您的 Mastodon 服务器现在由 Nginx 提供服务,但您仍然需要在检查您的网站之前设置一些服务。

步骤5 - 设置Mastodon服务

Mastodon 由三个系统服务组成,以维持功能和运行时间:mastodon-web、mastodon-sidekiq 和mastodon-streaming. 这允许 Mastodon 始终重新启动并保持网络应用程序和不同步任务安排。

首先,复制来自Mastodon源的服务:

1sudo cp /home/mastodon/live/dist/mastodon-*.service /etc/systemd/system/

这些文件假设一切都安装在默认情况下,例如使用mastodon作为您的用户. 如果您使用了任何与本教程的默认情况或建议不同的东西,您可以在您偏好的文本编辑器中打开这些文件,并检查需要更改的任何用户名或路径:

1sudo nano /etc/systemd/system/mastodon-*.service

如果您遵循本教程中的步骤,您将无需更改任何东西. 检查过您的文件后,关闭它们并继续激活这些服务. 首先,您需要拨打daemon-reload来识别这些:

1sudo systemctl daemon-reload

然后您可以启用您新承认的服务:

1sudo systemctl enable --now mastodon-web mastodon-sidekiq mastodon-streaming
1Created symlink /etc/systemd/system/multi-user.target.wants/mastodon-web.service → /etc/systemd/system/mastodon-web.service.
2Created symlink /etc/systemd/system/multi-user.target.wants/mastodon-sidekiq.service → /etc/systemd/system/mastodon-sidekiq.service.
3Created symlink /etc/systemd/system/multi-user.target.wants/mastodon-streaming.service → /etc/systemd/system/mastodon-streaming.service.

您的 Mastodon 安装已经完成. 您可以在浏览器中打开 Mastodon 通过访问 https://your_domain. 请注意,您在浏览器中第一次尝试打开 Mastodon 会触发一些后端初始化,这些初始化可能不会通过时间页面加载完成,从而导致一个错误,如 我们很抱歉,但在我们的结尾上发生了错误 。 在这种情况下,等待几秒钟,然后保持‘Shift’时刷新浏览器,以执行硬更新。 这将解决问题,并且只发生在 Mastodon 第一次加载时:

Mastodon Homepage

您可以使用管理员用户名和先前生成的密码登录并开始。

结论

在本教程中,你设置了自己的Mastodon服务器,包括设置数据库和Web服务器的反向代理,以及所有Mastodon特定的要求和依赖。在Mastodon中,你设置了电子邮件,存储和资产,然后创建了你的第一个管理员帐户。最后,你使用Certbot和Let's Encrypt来保护你的SSL/TLS设置。

要了解如何使用您的新的 Mastodon 实例并探索其功能,您可以查看我们 如何使用 DigitalOcean Marketplace 1-Click 安装 Mastodon教程中的 步骤 4 到 6

Published At
Categories with 技术
comments powered by Disqus