在 Debian 10 / Ubuntu 20.04 上安装 Pleroma

blue
最后编辑于 May 2022 Pleroma

博文链接: https://suicablog.cobaltkiss.blue/posts/pleroma-installation-on-linux-using-otp-releases/

Pleroma 官方 OTP 方式安装教程: https://docs-develop.pleroma.social/backend/installation/otp_en/

(因话题字数受限删除了「正式安装」前的内容,完整文章请点击上方博文链接查看)

推荐配置

尽管 Pleroma 内存需求远低于 Mastodon 和 Misskey,但购入 VPS 请最低保证 1 GB 的内存。这个配置对于用户较少的小型实例是足够的,再小实例就容易变得不稳定。如果预计会在未来接纳更多用户,请根据您需要购买更合适的套餐。

正式安装

一、前置安装

1、安装依赖

apt install curl unzip libncurses5 postgresql postgresql-contrib nginx certbot libmagic-dev

如果安装途中出现错误,则执行以下命令后再次尝试安装:

apt-get update

2、安装可选安装包(建议全部安装)

apt install imagemagick ffmpeg libimage-exiftool-perl

3、Postgresql 性能调优(可选)
为了优化性能,您可以使用 pgTune 生成一个合适的配置文件。编辑 /etc/postgresql/11.9/main/postgresql.conf 中的相应数值并使用以下命令重启 PostgreSQL 。

systemctl restart postgresql

(11.9 为我系统 Postgresql 的版本,您的版本不同的话会显示为其他序号)

二、安装 Pleroma

1、创建一个 Pleroma 用户

adduser --system --shell /bin/false --home /opt/pleroma pleroma

2、检测并设置环境变量 FLAVOUR

arch="$(uname -m)";if [ "$arch" = "x86_64" ];then arch="amd64";elif [ "$arch" = "armv7l" ];then arch="arm";elif [ "$arch" = "aarch64" ];then arch="arm64";else echo "Unsupported arch: $arch">&2;fi;if getconf GNU_LIBC_VERSION>/dev/null;then libc_postfix="";elif [ "$(ldd 2>&1|head -c 9)" = "musl libc" ];then libc_postfix="-musl";elif [ "$(find /lib/libc.musl*|wc -l)" ];then libc_postfix="-musl";else echo "Unsupported libc">&2;fi;echo "$arch$libc_postfix"

假设输出结果是 "amd64"(请替换为您自己的输出结果),输入 :

export FLAVOUR="amd64"

3、下载安装 Pleroma 所需的文件到 /tmp 文件夹并解压

su pleroma -s $SHELL -lc "
curl 'https://git.pleroma.social/api/v4/projects/2/jobs/artifacts/stable/download?job=$FLAVOUR' -o /tmp/pleroma.zip
unzip /tmp/pleroma.zip -d /tmp/
"

4、移动文件到 /opt/pleroma 并删除临时文件夹

su pleroma -s $SHELL -lc "
mv /tmp/release/* /opt/pleroma
rmdir /tmp/release
rm /tmp/pleroma.zip
"

5、创建 /uploads 文件夹并设置适当的权限

mkdir -p /var/lib/pleroma/uploads
chown -R pleroma /var/lib/pleroma

6、创建 static 文件(用于存放表情等文件)并设置其所有者为用户 pleroma

mkdir -p /var/lib/pleroma/static
chown -R pleroma /var/lib/pleroma

7、创建存放配置文件的文件夹并设置其所有者为用户 pleorma

mkdir -p /etc/pleroma
chown -R pleroma /etc/pleroma

8、生成 config 配置文件

su pleroma -s $SHELL -lc "./bin/pleroma_ctl instance gen --output /etc/pleroma/config.exs --output-psql /tmp/setup_db.psql"

9、若 config 文件生成成功,则出现若干问题需要回答:

What domain will your instance use? (e.g pleroma.soykaf.com) []
您的网站将使用什么网址?

What is the name of your instance? (e.g. Pleroma/Soykaf) []
您的网站叫什么名字?

What is your admin email address? []
网站管理员的邮箱地址是什么?

What email address do you want to use for sending email notifications?
[]
您想要用什么邮件地址来发送邮件通知?(这里先输入邮箱,之后若要向用户发送邮件需要额外配置)

Do you want search engines to index your site? (y/n) [n]
您希望搜索引擎收录您的网站吗?(建议不收录)

Do you want to store the configuration in the database (allows controlling it from admin-fe)? (y/n) [n] y
您希望将配置信息存入数据库吗(允许从管理后台进行控制)?(建议存入,否则 admin-fe 将无法使用)

Do you want to strip location (GPS) data from uploaded images? (y/n) [y] n
您希望从上传的图片中消除 GPS 信息吗?(建议消除)

Do you want to anonymize the filenames of uploads? (y/n) [n] y
您希望将上传文件的文件名匿名化吗?(建议匿名化)

Do you want to deduplicate uploaded files? (y/n) [n] y
您希望去除重复的已上传文件吗?(建议去除)

除以上问题外,其余选项均按 enter 选择默认配置即可

如果成功则有如下显示:

All files successfully written! Refer to the installation instructions for your platform for next steps. Please transfer your config to the database after running database migrations. Refer to "Transfering the config to/from the database" section of the docs for more information.

10、创建 postgres 数据库

su postgres -s $SHELL -lc "psql -f /tmp/setup_db.psql"

11、更新数据库 schema

su pleroma -s $SHELL -lc "./bin/pleroma_ctl migrate"

12、启动实例以验证一切都按预期工作

su pleroma -s $SHELL -lc "./bin/pleroma daemon"

13、等待大约 60 秒并检测实例运行状况,如果它正确地显示了您的网址、网站名称和电子邮件地址,则说明实例已经安装成功(update: 经建议,由 20 秒延长为 60 秒)

sleep 60 && curl http://localhost:4000/api/v1/instance

注:如果出现错误 "curl: (7) Failed to connect to localhost port 4000: Connection refused" ,请等待一会儿再此尝试,错误可能是 Pleroma 初次加载时间较慢导致的。

14、最后,暂时先暂停实例的运行

su pleroma -s $SHELL -lc "./bin/pleroma stop"

三、设置 nginx 并获取 Let’s Encrypt SSL 证书

1、获取一个 Let’s Encrypt 证书(注意修改域名)

certbot certonly --standalone --preferred-challenges http -d <yourinstance.tld>

2、输入您的邮箱地址(用于紧急重建和安全通知)并按照指示选择 A(agree)和 Y(yes)

*之前 Let’s Encrypt 曾撤销了一批有问题的证书,撤销前曾通过邮件进行通知,请务必留下一个有效的联系邮件。**

3、显示以下结果证明证书获取成功(否则尝试再次执行第一个指令):

Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/<实例域名>/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/<实例域名>/privkey.pem Your cert will expire on <到期日期>. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew all of your certificates, run "certbot renew"……(省略)

注:如果提示与我上面写的不同,请仔细检查证书有没有成功申请并存放到"/etc/letsencrypt/live/实例域名/",若没有申请成功,后面依旧会卡住。

一般这一步出错有两种可能,第一,您没有添加 A 记录,请到域名注册网站添加或修改 A 记录;二,80 和 443 端口未开放,如果这里出现错误"Problem binding to port 80: Could not bind to IPv4 or IPv6.",请检查防火墙端口开放情况(Debian 需要额外安装 ufw):sudo ufw status verbose,如果只有 22 端口开放,请通过 sudo ufw allow 80/tcpsudo ufw allow 443/tcp 将 80 和 443 端口开放。(*备注)

操作完成后,再次输入指令,直到看到正确的成功提示。

4、复制 Pleroma nginx 配置到 nginx 文件夹

cp /opt/pleroma/installation/pleroma.nginx /etc/nginx/sites-available/pleroma.conf
ln -s /etc/nginx/sites-available/pleroma.conf /etc/nginx/sites-enabled/pleroma.conf

5、使用 nano 编辑器打开 Nginx 配置文件

nano /etc/nginx/sites-available/pleroma.conf

6、将文件中所有 example.tld 更改为您的实例的对应名称(操控方向键滑动页面,用 backspace 删除默认名称,并输入新的地址即可)

注(未经验证):location 处只保留后面的$符号,删除前面的$符号):

location / {
  return         301 https://<您的实例名>$request_uri;
}

7、修改完毕后按 ctrl X,选择 Y(yes)保存,然后按 enter 退出

8、确认 nginx 是否配置正确

nginx -t

出现以下内容则配置成功:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

9、启动 nginx(若先前 nginx 已在运行则 reload nginx -s reload

systemctl start nginx

四、设置系统服务

1、将 pleroma.service 复制到适当的文件夹中

cp /opt/pleroma/installation/pleroma.service /etc/systemd/system/pleroma.service

2、启动 pleroma 并设置开机自启动

systemctl start pleroma
systemctl enable pleroma

在地址栏输入您的实例域名,若 pleroma 服务启动成功,则应显示 pleroma 页面;若显示 502 bad gateway,再次执行 systemctl start pleroma ,之后刷新页面

五、设置 Let’s Encrypt 证书的自动更新

这部分内容可以参考通配符域名证书申请与自动更新

1、创建路径

mkdir -p /var/lib/letsencrypt

2、使用 nano 编辑器打开 Nginx 配置文件

nano /etc/nginx/sites-available/pleroma.conf

3、去除
location ~ /.well-known/acme-challenge { root /var/lib/letsencrypt/; }
前的 # 注释符号

内容全部显示为白色后按 ctrl X,选择 Y(yes)保存,然后按 enter 退出

4、确认配置是否正确

nginx -t

5、重启 nginx

systemctl restart nginx

6、确认自动更新任务设置成功(注意修改域名)

certbot renew --cert-name <yourinstance.tld> --webroot -w /var/lib/letsencrypt/ --dry-run --post-hook 'systemctl reload nginx'

显示以下内容则设置成功:
Congratulations, all renewals succeeded. The following certs have been renewed: /etc/letsencrypt/live/yourinstance.tld/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)

7、添加自动任务(注意修改域名)

echo '#!/bin/sh
certbot renew --cert-name <yourinstance.tld> --webroot -w /var/lib/letsencrypt/ --post-hook "systemctl reload nginx"
' > /etc/cron.daily/renew-pleroma-cert
chmod +x /etc/cron.daily/renew-pleroma-cert

8、确认任务添加成功

run-parts --test /etc/cron.daily

若显示内容中包含 /etc/cron.daily/renew-pleroma-cert 则添加成功

六、创建第一个用户并设置为管理员

<joeuser> 处为管理员 ID,后面为管理员邮箱地址,根据自己情况设置

cd /opt/pleroma/bin
su pleroma -s $SHELL -lc "./bin/pleroma_ctl user new <joeuser> <[email protected]> --admin"

选择 [y] 后终端会出现一个链接,复制链接并粘贴到浏览器打开,为管理员账号设置密码,设置完毕即可进入实例页面。

一些可能对您有帮助的文章:

评论

  • 沙发是我的(ノ◕ヮ◕)ノ*:・゚✧
    我超级喜欢的建站教程,谢谢suica

登录注册后才能评论。