如何在 Ubuntu 20.04 上安装 Umami 网络分析软件

介绍

Umami是一个开源的,自托网页分析应用程序,用Node.js编写,它专注于简单,精心设计,快速,专注于隐私。

在本教程中,您将使用 Docker Compose 安装 Umami 和 PostgreSQL 数据库,然后安装 Nginx 作为 Umami 的反向代理程序。

前提条件

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

  • 一个 Ubuntu 20.04 服务器,有 UFW 防火墙启用. 请阅读我们的 初始服务器设置与 Ubuntu 20.04] 了解有关设置这些要求的更多信息
  • Docker 安装. 您可以使用 [如何在 Ubuntu 20.04 上安装和使用 Docker 的步骤 1**(https://andsky.com/tech/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04) 才能做到这一点。 可选,您可以遵循该教程的步骤 2 如果您希望您的非root用户能够运行 docker 命令而不使用 sudo
  • Docker Compose 安装。 ** 遵循 [如何在 Ubuntu 20.04 上安装和使用 Docker Compose 的步骤 1**([INKL2]) 安装此软件(

<$>[info] 注: 如果您正在使用 DigitalOcean 的 1-Click Docker Image,则可以跳过这些先决步骤。

在您选择的区域启动新的 Docker 图像,然后登录为 root 用户,并继续使用教程。

最后,要启用 SSL,您需要一个域名,指向您的服务器的公共 IP 地址. 例如,这应该是example.comumami.example.com

当您满足所有前提条件后,转到 步骤 1,您将下载并启动Umami软件。

步骤 1 — 使用 Docker Compose 安装 Umami 和 PostgreSQL

你的第一步是克隆Umami Git存储库,更新docker-compose.yml配置文件,然后启动Umami和PostgreSQL容器。

您将下载回复到/opt目录,使用cd命令现在到达那里:

1cd /opt

然后使用git命令从 GitHub 克隆 Repo:

1sudo git clone https://github.com/mikecao/umami.git

这将将所有软件和配置文件拖到 /opt/umami. 现在移动到新创建的 umami 目录:

1cd umami

现在你需要更新项目的docker-compose.yml文件。这个文件是docker-compose命令用于同时配置和启动多个Docker容器的。

在打开docker-compose.yml以编辑它之前,让我们生成一个新的随机哈希来粘贴到文件中:

1openssl rand -base64 32
1[secondary_label Output]
2tCgKyCWc/3C9VH+Ex0TysXsGEKQklQXm0H3nSnlR48g=

这使用openssl命令生成32个随机字符. 将输出复制到您的剪辑板,然后打开配置文件:

1sudo nano docker-compose.yml

查找HASH_SALT选项,删除位置保持者文本,并粘贴您刚刚生成的随机哈希:

1[label docker-compose.yml]
2. . .
3      HASH_SALT: replace-me-with-a-random-string
4. . .

接下来,找到配置的 port: 部分:

1[label docker-compose.yml]
2. . .
3    ports:
4      - "127.0.0.1:3000:3000"
5. . .

更新3000:3000值,以127.0.0.1:为前提,这确保了Umami只在 localhost界面上聆听,并且不公开可用,即使您有UFW防火墙设置,由于Docker网络如何运作的某些奇怪,如果您没有采取这个步骤,您的Umami容器将在端口3000上向公众开放。

完成这些配置更改后,保存文件(‘CTRL+O’然后在‘nano’中‘ENTER’)并关闭编辑器(‘CTRL+X’)。

现在,使用docker-compose来启动你的两个容器:

1sudo docker-compose up --detach

分离的旗帜说docker-compose来创建背景中的容器,从我们的终端会话中分离出来:

1[secondary_label Output]
2. . .
3Creating umami_db_1 ... done
4Creating umami_umami_1 ... done

Umami 和 PostgreSQL 现在正在运行,您可以通过使用弯曲命令来验证此情况,以获取您在 localhost上运行的新 Umami 容器的首页:

1curl localhost:3000
1[secondary_label Output]
2<!DOCTYPE html><html><head><meta charSet="utf-8"/> . . .

如果大量的HTML输出到您的终端,你知道Umami服务器正在运行。

接下来,我们将设置 Nginx 到 reverse proxy Umami 从localhost:3000到公众。

步骤 2:安装和配置 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 配置文件,我们将称呼我们的umami.conf,但您可以使用不同的名称:

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

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

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

此配置目前仅限于 HTTP,因为我们将让Certbot在下一步负责配置SSL。 其余的配置设置了日志位置,然后将所有流量传输到http://localhost:3000,我们在上一步启动的Umami实例。

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

1sudo ln -s /etc/nginx/sites-available/umami.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

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

A screenshot of the Umami login page, with 'Username' and 'Password' textboxes

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

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

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

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

您的网站现在是安全的,它是安全的登录与默认用户 admin和密码 umami. 请立即这样做,并遵循 官方的第一个登录文档登录并更改您的 admin密码更安全。

当你第一次登录时,你会看到一个有点裸体的仪表板:

A screenshot of the Umami dashboard after login, showing a "no websites configured" message

在本教程的结尾,您将找到链接到文档,将让你开始添加您的网站到Umami,并添加Umami跟踪片段到您的网站。

结论

在本教程中,您使用 Docker Compose 启动了 Umami 应用程序和 PostgreSQL 数据库,然后设置了 Nginx 反向代理,并使用 Let's Encrypt SSL 证书保护它。

此外,你应该已经 登录并更新默认密码,如果没有,现在就这样做。

接下来,继续使用官方文档,了解如何将 添加网站添加到Umami,然后开始 收集数据通过在您的网站上安装跟踪代码。

Published At
Categories with 技术
comments powered by Disqus