在本页面上
- 先决条件
- 第 1 步 - 配置防火墙
- 第 2 步 - 安装 PostgreSQL
- 第 3 步 - 安装 Nginx
- 第 4 步 - 安装 Pleroma
- 第 5 步 - 配置 Pleroma
- 第 6 步 - 使用 Let's Encrypt 安装 SSL
- 第 7 步 - 配置 Nginx
- 第 8 步 - 配置 Pleroma 创建管理员用户 更改设置 更新 Pleroma 备份 Pleroma 恢复 Pleroma
- 结论
- 创建管理员用户
- 更改设置
- 更新 Pleroma
- 备份 Pleroma
- 恢复 Pleroma
Pleroma 是一个开源的联邦式社交网络平台,与 Mastodon 和其他 ActivityPub 平台兼容。它是 Fediverse 的一部分,Fediverse 是一个由可以使用通用协议进行通信的实例组成的联邦网络。一个实例上的单个账户就可以与整个 Fediverse 网络进行通信。
本指南将向你展示如何通过在基于 Ubuntu 20.04 的服务器上安装 Pleroma 来创建你自己的 Pleroma 实例。
先决条件
- 一台运行 Ubuntu 20.04 的服务器。
- 一个非 root 的 sudo 用户。
- 确保所有内容都已更新。$ sudo apt update $ sudo apt upgrade
- 安装 Pleroma 之前你需要的几个软件包和依赖项。$ sudo apt install wget curl gnupg2 ca-certificates lsb-release gnupg zip libncurses5 libmagic-dev -y
一台运行 Ubuntu 20.04 的服务器。
一个非 root 的 sudo 用户。
确保所有内容都已更新。
$ sudo apt update
$ sudo apt upgrade
安装 Pleroma 之前你需要的几个软件包和依赖项。
$ sudo apt install wget curl gnupg2 ca-certificates lsb-release gnupg zip libncurses5 libmagic-dev -y
第 1 步 - 配置防火墙
第一步是配置防火墙。Ubuntu 默认带有 ufw(Uncomplicated Firewall,简单防火墙)。
检查防火墙是否正在运行。
$ sudo ufw status
你应该会得到以下输出。
Status: inactive
允许 SSH 端口,以便防火墙在启用时不会中断当前连接。
$ sudo ufw allow OpenSSH
同样允许 HTTP 和 HTTPS 端口。
$ sudo ufw allow 80
$ sudo ufw allow 443
启用防火墙
$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
再次检查防火墙的状态。
$ sudo ufw status
你应该会看到类似的输出。
Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere 80 ALLOW Anywhere 443 ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) 80 (v6) ALLOW Anywhere (v6) 443 (v6) ALLOW Anywhere (v6)
第 2 步 - 安装 PostgreSQL
将官方 PostgreSQL 仓库添加到 Ubuntu 源列表中。
$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
添加该仓库的 GPG 密钥。
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
更新系统软件包列表。
$ sudo apt update
第 3 步 - 安装 Nginx
Ubuntu 20.04 默认自带 Nginx 18 稳定版。
$ sudo apt install nginx
第 4 步 - 安装 Pleroma
本指南从 OTP 发布版安装 Pleroma。第一步是创建一个没有登录能力的 Pleroma 用户。它还会在 /opt/pleroma 为其创建主目录。
$ sudo adduser --system --shell /bin/false --home /opt/pleroma pleroma
切换到 Pleroma 用户。但首先,我们需要切换到 root 用户。
$ sudo su
$ su pleroma -s $SHELL -l
将 Pleroma 下载到一个临时位置。
$ curl "https://git.pleroma.social/api/v4/projects/2/jobs/artifacts/stable/download?job=amd64" -o /tmp/pleroma_amd64.zip
解压该归档文件。
$ unzip /tmp/pleroma_amd64.zip -d /tmp/
安装 Pleroma。
$ mv /tmp/release/* /opt/pleroma
删除临时文件。
$ rm -rf /tmp/pleroma_amd64.zip /tmp/release
切换到 root 用户。
$ exit
为上传文件和公共文件创建目录。
$ mkdir -p /var/lib/pleroma/{uploads,static}
为 Pleroma 配置创建目录。
$ mkdir -p /etc/pleroma
将 Pleroma 目录的所有权更改为 Pleroma 用户。
$ chown -R pleroma /var/lib/pleroma /etc/pleroma
第 5 步 - 配置 Pleroma
切换回 Pleroma 用户。
$ su pleroma -s /bin/bash -l
运行以下命令为 Pleroma 实例生成配置文件。
$ ./bin/pleroma_ctl instance gen --output /etc/pleroma/config.exs --output-psql /tmp/setup_db.psql
你将被问到几个关于设置 Pleroma 的问题。如果你收到一条提示找不到配置文件的警告,请忽略它。
pleroma@pleroma:~$ ./bin/pleroma_ctl instance gen --output /etc/pleroma/config.exs --output-psql /tmp/setup_db.psql !!! /etc/pleroma/config.exs not found! Please ensure it exists and that PLEROMA_CONFIG_PATH is unset or points to an existing file What domain will your instance use? (e.g pleroma.soykaf.com) [] example.com What is the name of your instance? (e.g. The Corndog Emporium) [nspeaks.com] example Pleroma What is your admin email address? [] [email protected] What email address do you want to use for sending email notifications? [ [email protected] ] <Press Enter> Do you want search engines to index your site? (y/n) [y] y Do you want to store the configuration in the database (allows controlling it from admin-fe)? (y/n) [n] n What is the hostname of your database? [localhost] localhost What is the name of your database? [pleroma] pleroma What is the user used to connect to your database? [pleroma] pleroma What is the password used to connect to your database? [autogenerated] yourpassword Would you like to use RUM indices? [n] n What port will the app listen to (leave it if you are using the default setup with nginx)? [4000] <Press Enter> What ip will the app listen to (leave it if you are using the default setup with nginx)? [127.0.0.1] <Press Enter> What directory should media uploads go in (when using the local uploader)? [/var/lib/pleroma/uploads] <Press Enter> What directory should custom public files be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)? [/var/lib/pleroma/static] <Press Enter> Do you want to strip location (GPS) data from uploaded images? This requires exiftool, it was detected as not installed, please install it if you answer yes. (y/n) [n] n Do you want to anonymize the filenames of uploads? (y/n) [n] n Do you want to deduplicate uploaded files? (y/n) [n] y Writing config to /etc/pleroma/config.exs. Writing the postgres script to /tmp/setup_db.psql. Writing /var/lib/pleroma/static/robots.txt. All files successfully written! Refer to the installation instructions for your platform for next steps.
你可以根据自己的需求选择一组不同的选项。为你的数据库选择一个强密码。如果你想通过管理面板配置你的实例,请在关于将配置存储在数据库中的问题上选择 y。
切换到安装 PostgreSQL 时创建的默认 PostgreSQL 用户。
$ exit
$ su postgres -s /bin/bash -l
使用 Pleroma 提供的 SQL 文件创建数据库。
$ psql -f /tmp/setup_db.psql
切换回 Pleroma 用户。
$ exit
$ su pleroma -s /bin/bash -l
初始化我们刚刚创建的数据库。
$ ./bin/pleroma_ctl migrate
退出到 Root 用户。
$ exit
第 6 步 - 使用 Let's Encrypt 安装 SSL
要使用 Let's Encrypt 安装 SSL 证书,我们需要下载 Certbot 工具。
要安装 Certbot,我们将使用 Snapd 软件包安装程序。Certbot 的官方仓库已被弃用,而 Ubuntu 的 Certbot 软件包已有一年多历史。Snapd 始终提供 Certbot 的最新稳定版本,你应该使用它。幸运的是,Ubuntu 20.04 预装了 Snapd。
确保你的 Snapd 版本是最新的。
$ snap install core
$ snap refresh core
移除任何旧版本的 Certbot。
$ apt remove certbot
安装 Certbot。
$ snap install --classic certbot
使用以下命令,通过创建指向 /usr/bin 目录的符号链接来确保 Certbot 命令可以运行。
$ ln -s /snap/bin/certbot /usr/bin/certbot
停止 Nginx 服务。
$ systemctl stop nginx
生成 SSL 证书。
$ certbot certonly --standalone --preferred-challenges http -d example.com
上述命令会将证书下载到服务器上你的 /etc/letsencrypt/live/example.com 目录。
为 Let's Encrypt 自动续期创建一个 challenge Web 根目录。
$ mkdir -p /var/lib/letsencrypt
创建一个 Cron Job 来续期 SSL。它将每天运行以检查证书并在需要时进行续期。为此,首先创建文件 /etc/cron.daily/certbot-renew 并打开它进行编辑。
$ nano /etc/cron.daily/certbot-renew
粘贴以下代码。
#!/bin/sh certbot renew --cert-name example.com --webroot -w /var/lib/letsencrypt/ --post-hook "systemctl reload nginx"
按 Ctrl + X 并在提示时输入 Y 来保存文件。
更改该任务文件的权限以使其可执行。
$ chmod +x /etc/cron.daily/certbot-renew
第 7 步 - 配置 Nginx
Pleroma 自带一个默认的 Nginx 配置文件。将其移动到 /etc/nginx/sites-available 目录来安装它。
$ mv /opt/pleroma/installation/pleroma.nginx /etc/nginx/sites-available/pleroma.conf
将所有出现的 example.tld 替换为你的域名。
$ sed -i 's/example\.tld/example.com/g' /etc/nginx/sites-available/pleroma.conf
打开配置文件进行编辑。
$ nano /etc/nginx/sites-available/pleroma.conf
取消注释 location ~ /\.well-known/acme-challenge 块。你的配置文件的 server 块应如下所示。
server {
server_name example.com;
listen 80;
listen [::]:80;
location ~ /\.well-known/acme-challenge {
root /var/lib/letsencrypt/;
}
location / {
return 301 https://$server_name$request_uri;
}
}
按 Ctrl + X 并在提示时输入 Y 来保存文件。
通过创建符号链接来启用 Pleroma 的 Nginx 配置。
$ ln -s /etc/nginx/sites-available/pleroma.conf /etc/nginx/sites-enabled/pleroma.conf
启用 Nginx 服务器以使其在启动时运行。
$ systemctl enable nginx.
启动 Nginx 服务器。
$ systemctl start nginx
安装发行版中提供的 Pleroma systemd 服务单元文件。
$ mv /opt/pleroma/installation/pleroma.service /etc/systemd/system/pleroma.service
启用并启动 Pleroma 服务。
$ systemctl enable pleroma
$ systemctl start pleroma
Pleroma 站点可能需要大约 30 秒才能可用。现在,你可以在网页浏览器中打开 https://example.com 来访问 Pleroma。它应该看起来像下面这样。

第 8 步 - 配置 Pleroma
创建管理员用户
你可以通过命令行创建管理员用户。首先切换到 Pleroma 用户。
$ su pleroma -s /bin/bash -l
创建一个管理员用户。将 example 替换为你的用户名,将 [email protected] 替换为你的电子邮件地址,将 password123 替换为一个强密码。
$ ./bin/pleroma_ctl user new example
[email protected]
--password password123 --admin
完成后切换回 root 用户。
$ exit
更改设置
如果你在将配置存储在数据库中的问题选择了 no,这意味着你无法从 Pleroma 的管理面板更改设置。要更改设置,你需要修改 /etc/pleroma/config.exs 文件。
$ nano /etc/pleroma/config.exs
编辑完文件后,你还需要重启 Pleroma 服务。你可能需要等待一段时间,服务才会恢复。
$ systemctl restart pleroma
更新 Pleroma
要更新 Pleroma,第一步是下载新版本。运行以下命令下载 Pleroma 的新版本。
$ su pleroma -s $SHELL -lc "./bin/pleroma_ctl update"
停止 Pleroma 实例。
$ systemctl stop pleroma
下一步是迁移数据库。
$ su pleroma -s $SHELL -lc "./bin/pleroma_ctl migrate"
启动 Pleroma 实例。
$ systemctl start pleroma
备份 Pleroma
停止 Pleroma 服务。
$ systemctl stop pleroma
切换到 Pleroma 的目录。
$ cd /opt/pleroma
运行以下命令备份数据库。
$ sudo -Hu postgres pg_dump -d <pleroma_db> --format=custom -f </path/to/backup_location/pleroma.pgdump>
将文件 pleroma.pgdump、config/prod.secret.exs、config/setup_db.sql 和 uploads 文件夹复制到你的备份目标位置。
再次启动 Pleroma 服务。
$ systemctl start pleroma
恢复 Pleroma
要恢复 Pleroma,你需要重新安装 Pleroma,并确保 Pleroma 服务未在运行。
然后将备份的文件复制回其原始位置。
使用以下命令删除现有的数据库和用户。
$ sudo -Hu postgres psql -c 'DROP DATABASE <pleroma_db>;';` `sudo -Hu postgres psql -c 'DROP USER <pleroma_db>;'
使用备份的 setup_db.sql 文件恢复数据库架构和 Pleroma Postgres 角色。
$ sudo -Hu postgres psql -f config/setup_db.psql
接下来,恢复 Pleroma 实例的数据。
$ sudo -Hu postgres pg_restore -d <pleroma_db> -v -1 </path/to/backup_location/pleroma.pgdump>
如果你要升级到更新版本,并且还有任何待执行的迁移,请迁移数据库。
$ su pleroma -s $SHELL -lc "./bin/pleroma_ctl migrate"
重启 Pleroma 服务。
$ systemctl restart pleroma
生成统计信息,以便 Postgres 能够正确地规划查询。
$ sudo -Hu postgres vacuumdb --all --analyze-in-stages
结论
本教程关于在由 Ubuntu 20.04 驱动的服务器上安装 Pleroma 社交网络平台的内容到此结束。如果你有任何问题或反馈,请在下面的评论中发布。