如何在 Ubuntu 20.04 上安装 Etherpad 协作式网页编辑器

介绍

Etherpad 是一个允许在浏览器中实时协作文本编辑的 Web 应用程序,它是用 Node.js 编写的,可以在各种平台和操作系统上自动托管。

在本教程中,我们将安装Etherpad在Ubuntu 20.04服务器上,使用SQLite数据库引擎来存储我们的数据,我们还将安装和配置Nginx作为应用程序的反向代理,我们将从Let's Encrypt证书授权机构获取和安装SSL证书,以允许安全的HTTPS连接到我们的Etherpad实例。

<$>[info] 注: 如果您更愿意使用我们的 App Platform 服务来自我托管 Etherpad,请参阅我们的 部署 Etherpad 协作 Web 编辑器到 App 平台教程,在那里我们创建了一个 App Platform 应用程序来运行 Etherpad Docker 容器,并将其连接到受管理的 PostgreSQL 数据库。

前提条件

在开始本教程之前,您将需要以下内容:

<$>[info] 注: 如果您正在使用DigitalOcean,我们的DNS文档(https://docs.digitalocean.com/products/networking/dns/how-to/)可以帮助您在控制面板中设置域名。

当您有前提条件时,继续步骤1,在那里我们将下载和配置Etherpad应用程序。

步骤 1 – 下载和配置 Etherpad

要安装 Etherpad,您需要下载源代码,安装依赖,并配置 systemd 来运行服务器。

Etherpad维护者建议将软件运行为自己的用户,所以你的第一步是使用adduser命令创建一个新的 etherpad 用户:

1sudo adduser --system --group --home /opt/etherpad etherpad

这会创建一个系统用户,这意味着它不能直接登录,没有密码或壳分配给它,我们给它一个/opt/etherpad的首页目录,这就是我们下载和配置Etherpad软件的地方。

现在你需要运行几个命令作为 etherpad 用户。 要做到这一点,你将使用sudo命令打开一个bash作为 etherpad 用户. 然后你将目录(cd)更改为 /opt/etherpad:

1sudo -u etherpad bash
2cd /opt/etherpad

您的壳提示会更新,以显示您是 etherpad 用户. 它应该看起来类似于 etherpad@host:~$

现在用 Git 将 Etherpad 存储库克隆成 `/opt/etherpad:

1git clone --branch master https://github.com/ether/etherpad-lite.git .

这将将Etherpad源代码的分支拖入当前目录(.)。当完成时,运行Etherpad的installDeps.sh脚本来安装依赖:

1./bin/installDeps.sh

这可能需要一分钟。完成后,您需要手动安装最后一个依赖程序。我们需要将cd插入Etherpad的src文件夹并安装sqlite3包,以便使用SQLite作为我们的数据库。

首先,转到src目录:

1cd src

然后安装使用npmsqlite3包:

1npm install sqlite3

您作为 etherpad 用户的最后任务是更新 Etherpad settings.json 文件以使用 SQLite 用于其数据库,并与 Nginx 一起工作。

1cd /opt/etherpad

然后使用您最喜欢的文本编辑器打开设置文件:

1nano settings.json

该文件被格式化为 JSON,但在解释每个设置的过程中有广泛的评论. 您可以配置很多,但目前我们对更新数据库配置的两个值感兴趣:

1[label settings.json]
2  "dbType": "dirty",
3  "dbSettings": {
4    "filename": "var/dirty.db"
5  },

向下滚动,搜索这里所示的dbTypedbSettings部分,更新设置为sqlite和您所选择的文件名,如下:

1[label settings.json]
2  "dbType": "sqlite",
3  "dbSettings": {
4    "filename": "var/sqlite.db"
5  },

最后,向下滚动,找到trustProxy设置,并将其更新为true:

1[label settings.json]
2"trustProxy": true,

nano中,您可以通过键入CTRL+O,然后键入ENTER来保存,然后键入CTRL+X来退出来保存和关闭。

完成后,请确保退出 etherpad 用户壳:

1exit

您将被返回您的正常用户壳。

Etherpad 已安装并配置,接下来我们将创建一个 systemd 服务来启动和管理 Etherpad 流程。

步骤 2:为 Etherpad 创建 Systemd 服务

为了启动 Etherpad 并使用systemctl来管理该过程,我们需要创建一个 systemd 服务文件。

1sudo nano /etc/systemd/system/etherpad.service

我们将基于 Etherpad 文档 wiki 的信息创建服务定义,该 如何部署 Etherpad Lite 作为服务页面为我们提供一个示例配置,只需要几个更改才能使其为我们工作。

将以下内容添加到文本编辑器中,然后保存并关闭文件:

 1[label /etc/systemd/system/etherpad.service]
 2[Unit]
 3Description=Etherpad, a collaborative web editor.
 4After=syslog.target network.target
 5
 6[Service]
 7Type=simple
 8User=etherpad
 9Group=etherpad
10WorkingDirectory=/opt/etherpad
11Environment=NODE_ENV=production
12ExecStart=/usr/bin/node --experimental-worker /opt/etherpad/node_modules/ep_etherpad-lite/node/server.js
13Restart=always
14
15[Install]
16WantedBy=multi-user.target

此文件为 systemd 提供运行 Etherpad 所需的信息,包括运行它作为的用户和组,以及启动过程所用的命令(ExecStart=...)。

关闭文件后,重新加载 systemd daemon 以拖入新配置:

1sudo systemctl daemon-reload

接下来,启用etherpad服务,这意味着服务会在您的服务器重新启动时启动:

1sudo systemctl enable etherpad

最后,我们可以开始服务:

1sudo systemctl start etherpad

检查是否使用systemctl status正确启动服务:

1sudo systemctl status etherpad
1[secondary_label Output]
2 etherpad.service - Etherpad, a collaborative web editor.
3     Loaded: loaded (/etc/systemd/system/etherpad.service; enabled; vendor preset: enabled)
4     Active: active (running) since Thu 2021-09-09 14:12:43 UTC; 18min ago
5   Main PID: 698 (node)
6      Tasks: 13 (limit: 1136)
7     Memory: 152.0M
8     CGroup: /system.slice/etherpad.service
9             └─698 /usr/bin/node --experimental-worker /opt/etherpad/node_modules/ep_etherpad-lite/node/server.js

输出应表示服务是活跃(运行)

现在Etherpad正在运行,但它无法向公众开放,因为端口9001被你的防火墙阻止了,在下一步,我们将通过在Etherpad进程前设置 Nginx作为反向代理程序来使Etherpad成为公众。

步骤 3:安装和配置 Nginx

将 Nginx 等 Web 服务器放在 Node.js 服务器前面可以通过下载缓存、压缩和静态文件来提高性能,从而实现更高效的流程。

首先,更新您的包列表,然后使用apt安装 Nginx:

1sudo apt update
2sudo apt install nginx

使用Nginx Full UFW 应用程序配置文件,允许流量到端口80443 (HTTP 和 HTTPS):

1sudo ufw allow "Nginx Full"
1[secondary_label Output]
2Rule added
3Rule added (v6)

接下来,在 /etc/nginx/sites-available 目录中打开一个新的 Nginx 配置文件,我们将称呼我们的 `etherpad.conf',但您可以使用不同的名称:

1sudo nano /etc/nginx/sites-available/etherpad.conf

将下列内容粘贴到新的配置文件中,确保将your_domain_here替换为指向您的 Etherpad 服务器的域名,例如:etherpad.example.com。

 1[label /etc/nginx/sites-available/etherpad.conf]
 2server {
 3    listen 80;
 4    listen       [::]:80;
 5    server_name your_domain_here;
 6
 7    access_log  /var/log/nginx/etherpad.access.log;
 8    error_log   /var/log/nginx/etherpad.error.log;
 9
10    location / {
11        proxy_pass http://127.0.0.1:9001;
12        proxy_buffering off;
13        proxy_set_header Host $host;
14        proxy_pass_header Server;
15
16        # proxy headers
17        proxy_set_header X-Real-IP $remote_addr;
18        proxy_set_header X-Forwarded-For $remote_addr;
19        proxy_set_header X-Forwarded-Proto $scheme;
20        proxy_http_version 1.1;
21
22        # websocket proxying
23        proxy_set_header Upgrade $http_upgrade;
24        proxy_set_header Connection "upgrade";
25    }
26}

此配置完全基于 Etherpad 维基中提供的配置。 目前仅限 HTTP,因为我们将让 Certbot 在下一步负责配置 SSL。 其余的配置设置登录位置,然后将所有流量传输到 http://127.0.0.1:9001,我们在上一步启动的 Etherpad 实例。

保存并关闭文件,然后通过将其链接到 /etc/nginx/sites-enabled/ 来启用配置:

1sudo ln -s /etc/nginx/sites-available/etherpad.conf /etc/nginx/sites-enabled/

使用nginx -t来验证配置文件语法是否正确:

1sudo nginx -t
1[secondary_lable Output]
2nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
3nginx: configuration file /etc/nginx/nginx.conf test is successful

最后,重新加载nginx服务以获取新配置:

1sudo systemctl reload nginx

您的 Etherpad 网站现在应该在简单的 HTTP 上可用,它将看起来像这样:

The default homepage of an Etherpad instance, with a button for "New Pad" and a text box for creating a named pad

现在我们已经在 HTTP 上运行了我们的网站,现在是时候通过 Certbot 和 Let's Encrypt 证书来确保连接了。

步骤 4 – 安装 Certbot 和设置 SSL 证书

感谢 Certbot 和 Let’s Encrypt 免费证书授权,将 SSL 加密添加到我们的 Etherpad 应用程序只需要两个命令。

首先,安装 Certbot 及其 Nginx 插件:

1sudo apt install certbot python3-certbot-nginx

接下来,在--nginx模式下运行certbot,并指定您在 Nginxserver_name配置中使用的相同域名:

1sudo certbot --nginx -d your_domain_here

您将被要求同意 Let's Encrypt 服务条款,并输入电子邮件地址。

之后,您将被问及是否要将所有HTTP流量重定向到HTTPS。

之后,Let’s Encrypt 将确认您的请求,Certbot 将下载您的证书:

 1[secondary_label Output]
 2Congratulations! You have successfully enabled https://etherpad.example.com
 3
 4You should test your configuration at:
 5https://www.ssllabs.com/ssltest/analyze.html?d=etherpad.example.com
 6- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 7
 8IMPORTANT NOTES:
 9 - Congratulations! Your certificate and chain have been saved at:
10   /etc/letsencrypt/live/etherpad.example.com/fullchain.pem
11   Your key file has been saved at:
12   /etc/letsencrypt/live/etherpad.example.com/privkey.pem
13   Your cert will expire on 2021-12-06. To obtain a new or tweaked
14   version of this certificate in the future, simply run certbot again
15   with the "certonly" option. To non-interactively renew *all* of
16   your certificates, run "certbot renew"
17 - Your account credentials have been saved in your Certbot
18   configuration directory at /etc/letsencrypt. You should make a
19   secure backup of this folder now. This configuration directory will
20   also contain certificates and private keys obtained by Certbot so
21   making regular backups of this folder is ideal.
22 - If you like Certbot, please consider supporting our work by:
23
24   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
25   Donating to EFF:                    https://eff.org/donate-le

Certbot 会自动重新加载 Nginx 以获取新的配置和证书. 重新加载您的网站,如果您选择了重定向选项,它应该自动将您切换到 HTTPS。

The default Etherpad editor, with a textbox and placeholder text

你完成了!尝试你的新的Etherpad编辑器,并邀请一些合作者。

结论

在本教程中,我们设置了Etherpad,使用 Nginx和Let's Encrypt SSL证书. 您的Etherpad现在已经准备好使用,但您可能需要做更多的配置,包括添加身份验证的用户,添加插件,并通过 _skins_定制用户界面。

您的 SQLite 支持的 Etherpad 实例将能够处理适度数量的活跃用户,但如果您预计流量非常高,您可能想考虑配置 MySQL 或 PostgreSQL 数据库。

所有这些配置选项都记录在 官方Etherpad wiki上。

Published At
Categories with 技术
comments powered by Disqus