如何在 Ubuntu 20.04 上安装 Fathom Analytics

介绍

Fathom Analytics是一个开源的,自我托管的Web分析应用程序,专注于简单性和隐私,它以Go和船舶写成一个单一的二进制文件,使安装相对简单。

在本教程中,您将安装和配置Fathom,然后安装Nginx作为Fathom应用程序的反向代理程序,最后,您将使用Certbot来启用安全的HTTPS连接,从Let's Encrypt Certificate Authority下载和配置SSL证书。

前提条件

为了完成本教程,您首先需要以下内容:

  • 一个 Ubuntu 20.04 服务器,启用了 UFW 防火墙,并配置了sudo特权的非root 用户。 请阅读我们的 初始服务器设置与 Ubuntu 20.04 以了解有关设置这些要求的更多信息
  • 一个域名指向您的服务器的公共 IP 地址。 例如,这应该是example.comfathom.example.com。 如果您正在使用 DigitalOcean,请参阅我们的 DNS Quickstart 有关在我们的控制面板中创建域资源的信息。

当您满足所有前提条件后,继续到 步骤1 ,您将下载并安装Fathom。

步骤 1 – 下载 Fathom

要安装 Fathom 软件,您首先将下载最新版本,然后将可执行文件提取到 /usr/local/bin 目录中。

首先,移动到您可以写入的目录. /tmp 目录是一个很好的选择:

1cd /tmp

在您的 Web 浏览器中,访问 GitHub 页面 Fathom 最新软件版本,然后找到名为 `fathom_1.2.1_linux_amd64.tar.gz 的文件。

右键单击文件的链接,然后选择 复制链接 (或类似,取决于您的浏览器)。

使用弯曲命令从您刚刚复制的链接下载文件:

1curl -L -O https://github.com/usefathom/fathom/releases/download/v1.2.1/fathom_1.2.1_linux_amd64.tar.gz

你现在应该在你的 /tmp 目录中有 fathom_1.2.1_linux_amd64.tar.gz 文件. 使用 tar 命令来提取 fathom 可执行的文件并将其移动到 /usr/local/bin:

1sudo tar -C /usr/local/bin/ -xzf fathom*.tar.gz fathom

sudo命令是必要的,因为/usr/local/bin是一个受保护的目录,所以你需要超级用户权限来写入它。

现在使用sudochmod来更新fathom二进制的权限:

1sudo chmod +x /usr/local/bin/fathom

这使得fathom可执行. 要测试它,请运行fathom --version:

1fathom --version
1[secondary_label Output]
2Fathom version 1.2.1, commit 8f7c6d2e45ebb28651208e2a7320e29948ecdb2c, built at 2018-11-30T09:21:37Z

该命令将打印Fathom的版本号和一些额外的细节. 你已经成功下载并安装了Fathom二进制。

步骤 2 – 配置和运行 Fathom

在配置Fathom之前,您将在您的系统上创建一个新的 fathom 用户. 这个新的用户帐户将用于运行Fathom服务器,这将有助于隔离和保护服务。

使用adduser命令创建一个名为 fathom 的新用户:

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

这会创建一个特殊的 - 系统用户,这意味着它没有密码,不能像正常的用户一样登录,我们还使用 - 组旗帜创建了一个 ** fathom** 组,并在 / opt / fathom中创建一个主目录。

现在移动到用户的 fathom 主目录:

1cd /opt/fathom

现在我们必须执行一些命令,这些命令需要作为 fathom 用户运行。 要做到这一点,打开一个bash壳作为 ** fathom** 用户使用sudo:

1sudo -u fathom bash

您的提示将更改为fathom@host:~$。直到我们退出这个壳,我们执行的每个命令都将作为 fathom 用户运行。

现在你已经准备好为Fathom设置配置文件了。我们在这个配置文件中需要的一个项目是Fathom将用于签名和加密目的的随机字符串。

1openssl rand --base64 32
1[secondary_label Output]
2iKo/rYHFa2hDINjgCcIeeCe9pNglQreQrzrs+qK5tYg=

将字符串复制到剪辑板上,或在某种类型的临时剪辑文档上记下,然后为配置打开一个新的 .env 文件:

1nano /opt/fathom/.env

這將在「nano」文本編輯器中開啟一個新的空檔案. 您可以自由使用您最喜愛的編輯器。

将以下内容粘贴到文件中,确保将随机字符串更新到您之前生成的字符串:

1[label /opt/fathom/.env]
2FATHOM_SERVER_ADDR="127.0.0.1:8080"
3FATHOM_DATABASE_DRIVER="sqlite3"
4FATHOM_DATABASE_NAME="fathom.db"
5FATHOM_SECRET="your_random_string_here"

此配置首先规定,服务器只应在 localhost (127.0.0.1) 端口 8080 上收听,并且应该使用一个名为 `fathom.db’ 的 SQLite 数据库文件。

nano中,您可以按CTRL+O,然后按ENTER来保存,然后按CTRL+X来退。

现在数据库已配置,我们可以将第一个用户添加到我们的 Fathom 实例:

1fathom user add --email="your_email" --password="your_password"

由于这是您第一次在配置数据库时运行fathom,您应该注意到发生了一些初始数据库迁移:

1[secondary_label Output]
2INFO[0000] Fathom version 1.2.1, commit 8f7c6d2e45ebb28651208e2a7320e29948ecdb2c, built at 2018-11-30T09:21:37Z
3INFO[0000] Configuration file: /opt/fathom/.env
4INFO[0000] Connected to sqlite3 database: /opt/fathom/fathom.db
5INFO[0000] Applied 26 database migrations!
6INFO[0000] Created user [email protected]

您的「fathom.db」数据库文件现在创建了,并添加了用户。

现在启动Fathom服务器来测试它:

1fathom server
1[secondary_label Output]
2INFO[0000] Fathom version 1.2.1, commit 8f7c6d2e45ebb28651208e2a7320e29948ecdb2c, built at 2018-11-30T09:21:37Z
3INFO[0000] Configuration file: /opt/fathom/.env
4INFO[0000] Connected to sqlite3 database: /opt/fathom/fathom.db

在**连接到您的服务器的第二台终端中,使用curl获取您的Fathom实例的主页:

1[environment second]
2curl localhost:8080
1[environment second]
2[secondary_label Output]
3<!DOCTYPE html>
4<html class="no-js" lang="en">
5<head>
6  <title>Fathom - simple website analytics</title>
7  <link href="assets/css/styles.css?t=1543569696966" rel="stylesheet">
8. . .

您应该看到几行HTML代码打印到您的屏幕上,这表明服务器已启动并响应 localhost 上的请求。

回到您的第一个终端,通过按CTRL+C退出fathom服务器流程。

你已经完成了运行命令作为 fathom 用户,所以退出该会话也:

1exit

你的壳速率应该恢复正常。

Fathom现在已经完全配置,您已经成功地从命令行手动运行,接下来我们将设置Fathom以作为Systemd服务运行。

步骤 3 – 将 Fathom 设置为 Systemd 服务

要在任何时候运行fathom server,即使你没有登录到服务器,你会将其设置为 service 与 Systemd. Systemd 是一个服务管理器,处理在 Ubuntu 和许多其他 Linux 发行版上启动,停止和重新启动服务。

您创建的 fathom.service 文件将包含 Systemd 需要正确运行服务器的所有配置细节。

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

将以下内容添加到文件中:

 1[label /etc/systemd/system/fathom.service]
 2[Unit]
 3Description=Fathom Analytics server
 4Requires=network.target
 5After=network.target
 6
 7[Service]
 8Type=simple
 9User=fathom
10Group=fathom
11Restart=always
12RestartSec=3
13WorkingDirectory=/opt/fathom
14ExecStart=/usr/local/bin/fathom server
15
16[Install]
17WantedBy=multi-user.target

该文件规定该服务何时启动(‘After=network.target’,即网络关闭后),该服务应作为 fathom 用户和组运行,如果该服务退出,Systemd应始终尝试重新启动过程(‘Restart=always’),该服务应从‘/opt/fathom’目录中运行,以及使用哪个命令来运行服务器(‘ExecStart=/usr/local/bin/fathom server’)。

保存并关闭文件. 重新加载 Systemd 配置:

1sudo systemctl daemon-reload

允许服务:

1sudo systemctl enable fathom.service

启用该服务意味着 Systemd 将在系统启动时自动启动该服务,我们还需要现在手动启动该服务,仅此一次:

1sudo systemctl start fathom

在之前的命令中,请注意,您可以放弃服务名称的.service部分,最后检查服务的状态,以确保该服务正在运行:

1sudo systemctl status fathom
 1[secondary_label Output]
 2 fathom.service - Fathom Analytics server
 3     Loaded: loaded (/etc/systemd/system/fathom.service; enabled; vendor preset: enabled)
 4     Active: active (running) since Wed 2021-11-03 15:32:45 UTC; 13s ago
 5   Main PID: 3748 (fathom)
 6      Tasks: 5 (limit: 1136)
 7     Memory: 10.3M
 8     CGroup: /system.slice/fathom.service
 9             └─3748 /usr/local/bin/fathom server
10
11Nov 03 15:32:45 ubuntu-fathom systemd[1]: Started Fathom Analytics server.
12Nov 03 15:32:46 ubuntu-fathom fathom[3748]: time="2021-11-03T15:32:46Z" level=info msg="Fathom version 1.2.1, commit 8f>
13Nov 03 15:32:46 ubuntu-fathom fathom[3748]: time="2021-11-03T15:32:46Z" level=info msg="Configuration file: /opt/fathom>
14Nov 03 15:32:46 ubuntu-fathom fathom[3748]: time="2021-11-03T15:32:46Z" level=info msg="Connected to sqlite3 database: >

该服务在 localhost 端口 8080 上再次运行,接下来我们将设置 Nginx 作为反向代理,将 Fathom 服务暴露在外部世界。

步骤 4 – 安装和配置 Nginx

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

首先,更新您的包列表,然后使用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 配置文件,我们将称之为 `fathom.conf',但您可以使用不同的名称:

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

将下列内容粘贴到新配置文件中,确保将your_domain_here替换为您已配置的域,以指向您的 Fathom 服务器。

 1[label /etc/nginx/sites-available/fathom.conf]
 2server {
 3    listen 80;
 4    listen       [::]:80;
 5    server_name your_domain_here;
 6
 7    access_log  /var/log/nginx/fathom.access.log;
 8    error_log   /var/log/nginx/fathom.error.log;
 9
10    location / {
11      proxy_pass http://localhost:8080;
12      proxy_set_header X-Real-IP $remote_addr;
13      proxy_set_header X-Forwarded-For $remote_addr;
14      proxy_set_header Host $host;
15  }
16}

此配置目前仅限于HTTP,我们将让Certbot在下一步负责配置SSL,其余的配置设置了日志位置,然后将所有流量传送到我们的Fathom服务器,以http://localhost:8080,在途中添加一些重要的代理转发标题。

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

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

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

1sudo nginx -t
1[secondary_label 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

您的Fathom网站现在应该在简单的HTTP上可用。 加载http://your_domain_here,它将看起来像这样:

A screenshot of the Fathom login page, with 'Email' and 'Password' textboxes

现在你已经有了网站并在 HTTP 上运行,现在是时候使用 Certbot 和 Let's Encrypt 证书确保连接了。

第5步:安装Certbot并设置SSL证书

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

首先,安装 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://Fathom.example.com
 3
 4You should test your configuration at:
 5https://www.ssllabs.com/ssltest/analyze.html?d=Fathom.example.com
 6- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 7
 8IMPORTANT NOTES:
 9 - Congratulations! Your certificate and chain have been saved at:
10   /etc/letsencrypt/live/Fathom.example.com/fullchain.pem
11   Your key file has been saved at:
12   /etc/letsencrypt/live/Fathom.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。

您的网站现在是安全的,您可以安全地使用您在 Step 2 中设置的用户信息登录。

当你成功登录时,你会看到一个提示,让你的第一个网站与Fathom建立:

A screenshot of the Fathom initial setup workflow, asking for the domain of your website

一旦完成,您将看到您刚刚设置的网站的(目前是空的)仪表板:

A screenshot of the Fathom dashboard, showing no data yet

您已成功安装并保护您的 Fathom 分析软件。

结论

在本教程中,您下载、安装并配置了Fathom Analytics应用程序,然后设置了一个Nginx反向代理,并使用Let's Encrypt SSL证书保护它。

您现在已经准备好通过添加Fathom Analytics跟踪脚本来完成网站的设置,请参阅官方Fathom Analytics文档以获取有关使用软件和设置您的网站的更多信息。

Published At
Categories with 技术
comments powered by Disqus