本文简要介绍了如何在服务器上安装并配置 Umami 对博客网站进行网络分析。
简介
Umami 是一款开源、可本地部署的网络分析解决方案,致力于帮助我们轻松地收集、分析和理解网站的网络数据,同时注重访问者的隐私安全和网络数据的所有权。
相较于广泛使用的 Google Analytics,Umami 强调隐私保护和数据安全:
部署
信息
服务器操作系统:Ubuntu 20.04.6 LTS
MySQL
MySQL 是一个开源数据库管理系统,通常被安装在流行的 LAMP(Linux、Apache、MySQL、PHP/Python/Perl)中,作为其堆栈的一部分。
MySQL 应用关系模型和结构化查询语言(SQL: Structured Query Language)来管理其数据库内存储的数据。
安装 MySQL
1
2
3
4
5
6
7
8
9
10
11
12
|
# 更新软件包源和软件包
$ sudo apt update
$ sudo apt upgrade
# 安装 MySQL
$ sudo apt install mysql-server
# 开启 MySQL 服务
$ sudo systemctl start mysql.service
# 针对 MySQL 的安全性配置
$ sudo mysql_secure_installation
|
上述针对 MySQL 的安全性配置程序会提示用户:
- 设置用户密码复杂度安全等级;
- 删除数据库测试用的匿名账户;
- 禁用管理员账户远程登录权限;
- 删除默认的测试数据库;
- 刷新 privilege 表,使上述修改立即生效。
创建新用户
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# 以管理员身份登录 MySQL
$ sudo mysql
# 添加新用户 umami
> CREATE USER 'umami'@'localhost' IDENTIFIED BY 'password';
# 为新用户 umami 添加数据库操作权限
> GRANT CREATE, ALTER, DROP, INSERT, UPDATE, INDEX, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'umami'@'localhost' WITH GRANT OPTION;
# 刷新 privilege 表,使上述修改立即生效
> FLUSH PRIVILEGES;
# 退出 MySQL
> exit
|
创建数据库
1
2
3
4
5
6
7
8
|
# 以新用户 umami 登录 MySQL
$ mysql -u umami -p
# 为 Umami 网络分析创建数据库
> CREATE DATABASE IF NOT EXISTS umami
# 退出 MySQL
> exit
|
测试 MySQL
1
2
3
4
5
|
# 查看 MySQL 服务运行状态
$ systemctl status mysql.service
# 查看 umami 用户状态
$ sudo mysqladmin -p -u umami version
|
Node.js
Node.js 是主要用于服务器端的 JavaScript 运行时环境,它允许开发人员使用 JavaScript 创建可扩展的后端功能。
警告
Ubuntu 20.04 默认 APT 中 Node.js 的版本为 10.19,目前已过时,官方不再支持和维护。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# 更新软件包源和软件包
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install -y ca-certificates curl gnupg
# 下载并导入 Nodesource GPG key
$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
# 创建 deb repository
$ NODE_MAJOR=20
$ echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
# 更新软件包源
$ sudo apt update
# 安装 Node.js
$ sudo apt install nodejs -y
# 查看 Node.js 版本号
$ node -v
|
Nginx
Nginx 是世界上最受欢迎的轻量级网络服务器之一,也可用作反向代理。
Umami
安装 Umami
Yarn 是一个成熟的开源软件包管理器,用于管理 JavaScript 项目中的依赖性。
Yarn 辅助我们安装、更新、配置和删除软件包依赖项,帮助我们以更少的干扰更快地实现目标。
1
2
3
4
5
6
7
8
9
|
# 安装 Yarn 包管理器
$ sudo npm install -g yarn
# 获取 Umami 源代码
$ git clone https://github.com/umami-software/umami.git
# 安装 Umami
$ cd umami
$ sudo yarn install
|
安装失败
若安装过程报错,可使用 sudo yarn install --verbose 查看详细错误信息,以对症下药。
报错处理
笔者遇到的报错信息如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
verbose 74.983659555 Error: /home/ubuntu/umami/node_modules/sharp: Command failed.
Exit code: 1
Command: (node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy)
Arguments:
Directory: /home/ubuntu/umami/node_modules/sharp
Output:
sharp: Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.14.5/libvips-8.14.5-linux-x64.tar.br
sharp: Installation error: aborted
sharp: Please see https://sharp.pixelplumbing.com/install for required dependencies
at ProcessTermError.ExtendableBuiltin (/usr/lib/node_modules/yarn/lib/cli.js:721:66)
at ProcessTermError.MessageError (/usr/lib/node_modules/yarn/lib/cli.js:750:123)
at new ProcessTermError (/usr/lib/node_modules/yarn/lib/cli.js:790:113)
at ChildProcess.<anonymous> (/usr/lib/node_modules/yarn/lib/cli.js:25787:17)
at ChildProcess.emit (node:events:514:28)
at maybeClose (node:internal/child_process:1105:16)
at ChildProcess._handle.onexit (node:internal/child_process:305:5)
|
即无法从 GitHub 上下载 sharp 源代码。
sharp - Installation # Chinese mirror 给出了阿里提供的国内镜像站点。
参考 解决前端安装 sharp 出错问题 sharp: Command failed. 给出的解决方案。
1
2
3
4
5
6
7
8
9
|
# 打开或创建项目目录下的 yarn 配置文件 .yarnrc
$ vim .yarnrc
# 设置 sharp 以及 libvips 的镜像地址
sharp_binary_host "https://npmmirror.com/mirrors/sharp"
sharp_libvips_binary_host "https://npmmirror.com/mirrors/sharp-libvips"
# 重新安装 Umami
$ sudo yarn install
|
配置数据库
在项目目录中创建 .env 文件:
1
2
3
4
|
$ vim .env
# 配置数据库地址
DATABASE_URL=mysql://umami:password@localhost:3306/umami
|
数据库地址中配置好之前创建的 MySQL 用户名、密码、数据库。
构建 Umami
首次运行构建时,Umami 将在数据库中创建所有必需的数据库表。
Umami 还将创建一个带有用户名 admin 和密码 umami 的登录帐户。
笔者遇到的报错信息如下:
✓ DATABASE_URL is defined.
✗ Unable to connect to the database.
error Command failed with exit code 1.
解决方案:
运行 Umami
PM2 是一个守护进程管理器,用于管理和保持应用程序的在线状态。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# 安装 pm2
$ sudo yarn global add pm2
# 运行 Umami
$ cd umami
$ pm2 start yarn --name umami -- start
# 系统重启时自动运行 pm2 管理的服务
$ pm2 startup
$ pm2 save
# pm2 常用操作命令,app_name 也可用进程在 pm2 中的 id 代替
$ pm2 start python-app.py --interpreter python3 --attach
$ pm2 logs
$ pm2 status
$ pm2 restart app_name
$ pm2 reload app_name
$ pm2 stop app_name
$ pm2 delete app_name
|
默认情况下,Umami 将在 http://localhost:3000 启动应用程序。
我们需要从 Web 服务器代理(云服务器提供商的设置页面)请求或更改端口以直接为应用程序提供服务。
PM2 的其他用法可参考 PM2 Process Management - Quick Start
Nginx 代理
目前,Umami 已在服务器后台持续运行,我们可以使用 Nginx 将网站的访问请求代理至 Umami。
注意
如果我们希望监测分析的网站已经安装 SSL 证书,通过 HTTPS 访问,则同样需要为 Umami 服务器所在的域名申请并安装 SSL 证书,否则浏览器会因 HTTPS 安全问题屏蔽网页中植入的 Umami 跟踪代码。
在服务器的 Nginx 配置文件中加入以下配置,将 umami.yourdomain.com 发送所有请求代理至 Umami。
1
2
3
4
5
6
7
8
9
10
|
server {
server_name umami.yourdomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
|
设置
修改登录账户密码
Umami 构建运行时将自动创建一个带有用户名 admin 和密码 umami 的登录帐户。我们需要登录账户后修改密码。
添加网站
- 登录 Umami 后点击
设置 -> 网站 -> + 添加网站。
- 填写希望通过 Umami 进行分析的网站名称和域名,点击 保存。
收集数据
- 点击新添加的网址项右侧的 编辑。
- 将
跟踪代码 标签下的追踪代码嵌入网址代码 <head> 部分。
- 访问网站的数据将及时更新至 Umami 仪表盘。
共享链接
可通过共享链接将网站数据状态公开。
- 登录 Umami 后点击
设置 -> 网站 -> 编辑 -> 共享链接 -> 启用共享链接,即可生成网站数据仪表盘的共享链接。
更新
1
2
3
4
5
6
7
8
9
10
11
|
# 从 Git 仓库拉取更新
git pull
# 安装、更新依赖项
yarn install
# 重新构建 Umami
yarn build
# 重启 Umami
pm2 restart umami
|
旧站评论快照,仅供阅读和样式对照;新增评论请前往新站。
“This guide was incredibly helpful! I was struggling to set up Umami with MySQL and Nginx on my Ubuntu server, but your detailed instructions made everything clear. The troubleshooting tips for the Yarn installation error were especially valuable, as I faced the same issue with sharp.
I also wanted to share another resource that helped me secure my Nginx server. If you’re loo
“This guide was incredibly helpful! I was struggling to set up Umami with MySQL and Nginx on my Ubuntu server, but your detailed instructions made everything clear. The troubleshooting tips for the Yarn installation error were especially valuable, as I faced the same issue with sharp. I’m now up and running with Umami, thanks to this tutorial.
“I recently explored Umami for web analytics and found it a great alternative to mainstream tools like Google Analytics due to its focus on privacy and data ownership. While setting it up, I was initially overwhelmed, especially when dealing with MySQL and Node.js dependencies.
Switching to Vultr for my server needs made things so much easier. Vultr’s **[guide to obvious on Ubuntu