网站搜索

使用 Let's Encrypt SSL 证书在 Ubuntu 20.04/18.04 上安装 Mastodon |


Mastodon 是一个免费开源的自托管社交网络服务器,已获得 AGPLv3 许可。 Mastodon 的功能与 Twitter 类似,允许您发布链接、文本、视频和图片,并在全球互联的微博社区中分享。在这篇文章中,我们将讨论如何在使用 Let’s Encrypt SSL 证书保护的 Ubuntu 20.04/18.04 Linux 上安装 Mastodon。

Mastodon 有许多必须安装的依赖项。它们包括 Ruby、PostgreSQL、Node.js、Yarn、Redis、Nginx 等。我们的步骤将讨论在 Ubuntu 20.04/18.04 LTS 上运行 Mastodon 所需的所有步骤。

乳齿象的主要特征

  • 无供应商锁定:与任何合规平台完全互操作
  • 按时间顺序排列的实时时间线更新:通过 WebSockets 查看您所关注的人员的更新实时显示在用户界面中
  • 安全和审核工具:私人帖子、锁定帐户、短语过滤、静音、屏蔽和各种其他功能,以及报告和审核系统。
  • 图像和短视频等媒体附件:上传和查看更新附加的图像和 WebM/MP4 视频。没有音轨的视频将被视为 GIF;普通视频是循环播放的——就像藤蔓一样!
  • OAuth2 和简单的 REST API:Mastodon 充当 OAuth2 提供商,因此第 3 方应用程序可以使用 REST 和 Streaming API,从而形成一个具有多种选择的丰富应用程序生态系统!

设置要求

在 Ubuntu 20.04/18.04 Linux 上设置 Mastodon 时需要以下软件。

  • PostgreSQL 9.5+
  • Redis
  • 红宝石 2.4+
  • Node.js 8+

我们假设您也已准备好以下内容。

  • 正在运行的 Ubuntu 20.04/18.04 计算机
  • Mastodon 服务器的域名(或子域),例如 example.com 或social.example.com
  • 用于通知的电子邮件传送服务或其他 SMTP 服务器。

第1步:更新系统

确保您的服务器已更新:

sudo apt -y update && sudo apt -y upgrade
sudo reboot

第 2 步:安装 Node.js 和 Yarn

安装 Node.js

curl -sL https://deb.nodesource.com/setup_8.x | sudo bash -
sudo apt-get install -y nodejs

通过检查版本确认安装成功:

$ nodejs --version
v8.10.0

安装纱线:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt -y install yarn

第三步:安装其他依赖包

在 Ubuntu 20.04/18.04 上运行 Mastodon 需要许多系统依赖包。让我们确保这些包安装在本地。

sudo apt install -y imagemagick ffmpeg libpq-dev libxml2-dev libxslt1-dev file 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 libgdbm5 libgdbm-dev nginx redis-server redis-tools certbot python-certbot-nginx yarn libidn11-dev libicu-dev libjemalloc-dev

第四步:安装红宝石

运行 Ruby 的首选方式是使用 rbenv,因为它允许您轻松管理多个版本。我们将创建一个 Mastodon 用户用于这些操作。

sudo adduser --disabled-login mastodon
sudo su - mastodon

然后安装rbenv和rbenv-build:

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 版本:

RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 2.6.5
rbenv global 2.6.5

ruby_2.6.0 附带的默认 gem 版本与最新的捆绑程序不兼容,因此我们需要更新 gem 并安装捆绑程序。

gem update --system
gem install bundler --no-document
exit

第5步:安装PostgreSQL数据库服务器

使用我们的 PostgreSQL 安装指南:

安装 PostgreSQL 数据库 Ubuntu

创建用户:

sudo -u postgres psql
CREATE USER mastodon CREATEDB;
\q

第 6 步:设置 Mastodon

是时候下载 Mastodon 代码了。首先从 root 或您的用户帐户切换到 mastodon 用户:

sudo apt -y install git
sudo su - mastodon

克隆乳齿象代码:

git clone https://github.com/tootsuite/mastodon.git live && cd live
git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)

安装最后的依赖项:

gem install bundler:1.17.3
bundle install -j$(getconf _NPROCESSORS_ONLN) --deployment --without development test
yarn install --pure-lockfile

运行交互式向导以生成配置:

RAILS_ENV=production bundle exec rake mastodon:setup

填写所需信息:

/home/mastodon/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/rubygems_integration.rb:200: warning: constant Gem::ConfigMap is deprecated
Your instance is identified by its domain name. Changing it afterward will break things.
Domain name: example.com

Single user mode disables registrations and redirects the landing page to your public profile.
Do you want to enable single user mode? yes

Are you using Docker to run Mastodon? no

PostgreSQL host: /var/run/postgresql
PostgreSQL port: 5432
Name of PostgreSQL database: mastodon_production
Name of PostgreSQL user: mastodon
Password of PostgreSQL user: 
Database configuration works! 🎆

Redis host: localhost
Redis port: 6379
Redis password: 
Redis configuration works! 🎆

Do you want to store uploaded files on the cloud? No

Do you want to send e-mails from localhost? yes
E-mail address to send e-mails "from": Mastodon <[email >
Send a test e-mail with this configuration right now? no

This configuration will be written to .env.production
Save configuration? Yes

出现提示时同意编译资产:

Done!

The final step is compiling CSS/JS assets.
This may take a while and consume a lot of RAM.
Compile the assets now? Yes
Running `RAILS_ENV=production rails assets:precompile` ...


/home/mastodon/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/rubygems_integration.rb:200: warning: constant Gem::ConfigMap is deprecated
/home/mastodon/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/rubygems_integration.rb:200: warning: constant Gem::ConfigMap is deprecated
/home/mastodon/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/rubygems_integration.rb:200: warning: constant Gem::ConfigMap is deprecated
yarn install v1.21.1
[1/6] Validating package.json...
[2/6] Resolving packages...
[3/6] Fetching packages...
info [email : The platform "linux" is incompatible with this module.
info "[email " is an optional dependency and failed compatibility check. Excluding it from installation.
info [email : The platform "linux" is incompatible with this module.
info "[email " is an optional dependency and failed compatibility check. Excluding it from installation.
[4/6] Linking dependencies...
warning " > [email " has incorrect peer dependency "react-redux@^3.0.0 || ^4.0.0 || ^5.0.0".
[5/6] Building fresh packages...
[6/6] Cleaning modules...
Done in 12.60s.
.....

同意创建管理员帐户

Done!

All done! You can now power on the Mastodon server 🐘

Do you want to create an admin user straight away? Yes
Username: admin
E-mail: [email 
You can login with the password: 1b417e401f44c3db5d30f2f2f2a2328b
You can change your password once you login.

升级纱线:

yarn upgrade

第 7 步:为 Mastodon 设置 nginx

我们将使用 Nginx 作为 Mastodon 应用程序的反向代理。检查 nginx 服务是否正在运行:

$ systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-01-07 08:25:31 UTC; 7h ago
     Docs: man:nginx(8)
 Main PID: 14626 (nginx)
    Tasks: 2 (limit: 2361)
   CGroup: /system.slice/nginx.service
           ├─14626 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─14629 nginx: worker process

现在从 Mastodon 目录复制 nginx 的配置模板:

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

编辑该文件以为您的应用程序设置正确的 DNS 名称:

sudo vim /etc/nginx/sites-available/mastodon.conf

完成后,激活配置:

sudo ln -s /etc/nginx/sites-available/mastodon.conf /etc/nginx/sites-enabled/mastodon.conf
sudo systemctl restart nginx

如果您运行了 ufw 防火墙,请允许服务端口:

sudo ufw allow 'Nginx Full'

使用 Let’s Encrypt SSL 证书

如果您想使用 Let's Encrypt 证书,请运行以下命令:

sudo certbot --nginx -d example.com

示例输出:

$ sudo certbot --nginx -d social.computingforgeeks.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for social.computingforgeeks.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/mastodon.conf

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/mastodon.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled
https://social.computingforgeeks.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=social.computingforgeeks.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/social.computingforgeeks.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/social.computingforgeeks.com/privkey.pem
   Your cert will expire on 2020-04-06. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

将 example.com 替换为 Mastodon 应用程序的实际域名。

第 8 步:设置 systemd 服务

从 Mastodon 目录复制 systemd 服务模板:

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

然后编辑文件以确保用户名和路径正确:

    /etc/systemd/system/mastodon-web.service
    /etc/systemd/system/mastodon-sidekiq.service
    /etc/systemd/system/mastodon-streaming.service

最后,启动并启用新的 systemd 服务:

sudo systemctl daemon-reload
for i in web sidekiq streaming; do sudo systemctl enable mastodon-$i && sudo systemctl restart mastodon-$i; done

检查所有服务的状态:

for i in web sidekiq streaming; do systemctl status mastodon-$i; done

第 9 步:访问 Mastodon 仪表板

现在在浏览器中访问您的域名以访问 Mastodon 仪表板。

单击登录按钮进行登录。我将使用之前生成的管理员用户名电子邮件地址和密码。

完成初始教程以完成设置。

现在,您应该会看到一个很棒的 Mastodon 仪表板。

万岁!您已在 Ubuntu 20.04/18.04 Linux 上成功安装 Mastodon。

参考:

  • 乳齿象文档

相关文章: