本页目录
- 先决条件
- 添加新用户
- 安装 NVM(Node Version Manager)
- 安装 Nodejs LTS 版本
- 下载并安装 Uptime-Kuma
- 使用 pm2 设置 Uptime-Kuma
- 将 Apache 设置为反向代理
- 使用 SSL Letsencrypt 保护 Uptime-kuma 安全
- 验证 Uptime-Kuma 安装
- 结论
Uptime-Kuma 是一个类似“Uptime Robot”的开源监控工具,用 Nodejs 编写。它是一个自托管的监控工具,拥有华丽、美观的仪表板,并支持多种通知方式。Uptime-Kuma 将通过 HTTP(s)、TCP 和 Ping 协议监控主机或服务器的正常运行时间。如果无法通过这些协议定时访问主机,uptime 主机将通过 Webhooks、Telegram、Discord、Gotify、Slack、Pushover、Email (SMTP) 等发送通知。
在本指南中,你将学习如何在 Ubuntu 服务器上安装 Uptime-Kuma,并使用 Apache web 服务器作为反向代理,在其前面部署免费的 Let's Encrypt SSL 证书。在安装过程中,你还将学习如何通过 nvm(node version manager)安装 nodejs。
先决条件
- 一台 Ubuntu server 20.04。
- 一个具有 root 权限的用户。你将使用该用户安装新软件包并修改系统设置。
添加新用户
首先,你将向 Ubuntu 系统添加一个新用户。出于安全原因,应用 Uptime-Kuma 将以非 root 用户身份运行。
1. 执行下面的命令添加一个新用户。在本示例中,我们将使用用户名“ johndoe ”。请务必将用户名更改为你自己的用户名。
useradd -m -s /bin/bash johndoe
你必须知道的选项:
- -m = 为该用户名自动创建主目录,该目录将位于 '/home' 目录下。
- -s = 为该用户定义 shell 的路径,大多数 Linux 都使用 `/bin/bash shell。
2. 之后,运行以下命令为用户“johndoe”设置密码。
passwd johndoe
现在输入一个新的强密码并重复输入,然后按“ Enter ”确认。
3. 接下来,使用下面的命令将用户“johndoe”添加到“ sudo ”组。
usermod -aG sudo johndoe
通过将用户添加到“ sudo ”组,将允许用户执行“sudo su”命令并获得 root 权限。
安装 NVM(Node Version Manager)
NVM 或 Node Version Manager 是一个用于在一个系统上安装多个 nodejs 版本的命令行工具。该工具使你能够为你的应用安装特定的 nodejs 版本,使你的开发和部署环境更加灵活。
在这一阶段,你将在非 root 用户“johndoe”下安装 nvm。
1. 从你的 root shell 中,使用以下命令登录到用户“johndoe”。
su - johndoe
2. 如下下载并执行 nvm 安装脚本。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

3. 成功后,使用下面的命令重新加载 bash 环境。
source ~/.bashrc
4. 现在通过运行下面的命令验证“ nvm ”安装。
command -v nvm
如果安装成功,你将看到结果“ nvm ”。否则,你将看不到任何内容或显示空白结果。
现在执行如下的 nvm 命令。
nvm --help
然后你将看到 nvm 命令的手册帮助页面。
Node Version Manager (v0.38.0) Note: <version> refers to any version-like string nvm understands. This includes: - full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1) - default (built-in) aliases: node, stable, unstable, iojs, system - custom aliases you define with `nvm alias foo` Any options that produce colorized output should respect the `--no-colors` option. Usage: nvm --help Show this message --no-colors Suppress colored output ..... Note: to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
安装 Nodejs LTS 版本
要安装 uptime-Kuma 应用,你需要安装 nodejs >= 14、git 和 pm2。在这一步,你将使用 nvm 命令安装最新的 nodejs LTS 版本。
1. 执行下面的 nvm 命令安装最新的 nodejs LTS 版本。
nvm install --lts
安装完成后,你将看到如下类似的输出。

你安装的第一个 nodejs 版本将成为你开发环境中的默认版本。
2. 现在运行以下命令验证 nodejs 安装。
node --version
npm --version
如果安装成功,你将看到与下面类似的输出。
# nodejs version v14.17.4 # npm version 6.14.14
下载并安装 Uptime-Kuma
至此,你已经准备好安装 Uptime-Kuma 应用。
1. 使用下面的 git 命令克隆 uptime-Kuma 源代码。
git clone https://github.com/louislam/uptime-kuma.git
2. 之后,将工作目录切换到“ uptime-kuma/ ”并执行 npm 命令,安装 uptime-kuma 所需的所有软件包。
cd uptime-kuma/
npm run setup
然后你将看到如下类似的输出。

使用 pm2 设置 Uptime-Kuma
什么是 pm2?它是一个 nodejs 进程管理器,可帮助你管理 nodejs 应用并使其永久保持运行。pm2 具有一些内置功能,使 nodejs 应用部署变得容易,它允许你监控应用状态、日志,甚至为你的应用设置服务文件。
1. 使用下面的 npm 命令安装 pm2 软件包。
npm install pm2
2. 安装完成后,执行下面的 pm2 命令启动 uptime-kuma 应用。
pm2 start npm --name uptime-kuma -- run start-server -- --port=3001 --hostname=127.0.0.1
然后你将看到如下输出信息。

如图所示,应用名称“ uptime-kuma ”正在运行,状态为“ online ”。
3. 要在 pm2 中检查应用状态,请运行以下命令。
检查 nodejs 应用状态。
pm2 status

检查 nodejs 应用日志。
pm2 logs

实时 nodejs 应用监控。
pm2 monit

4. 接下来,执行以下 pm2 命令为 uptime-kuma 应用生成服务文件。
pm2 startup
之后,你应该按照输出中的提示运行命令。
sudo env PATH=$PATH:/home/johndoe/.nvm/versions/node/v14.17.4/bin /home/johndoe/.nvm/versions/node/v14.17.4/lib/node_modules/pm2/bin/pm2 startup systemd -u johndoe --hp /home/johndoe

请务必使用“ sudo ”命令并输入你的用户的正确密码以获得 root 权限。
命令完成后,你将看到以下输出。

如截图所示,pm2 会基于该用户生成一个 systemd 服务,本示例中为“ pm2-johndoe.service ”。
现在执行以下命令生成并保存 uptime-kuma 应用状态。该命令很重要,可让 uptime-kuma 应用始终在后台运行,即使系统启动后也是如此。
pm2 save
5. 要验证服务 ' pm2-johndoe',请运行下面的命令。
sudo systemctl is-enabled pm2-johndoe
你将看到输出 ` enabled `,这意味着 uptime-kuma 应用将在系统启动后自动运行。
此外,你可以尝试重启系统并再次登录服务器,然后检查 uptime-kuma 服务。
sudo systemctl status pm2-johndoe
你应该会看到如下类似的输出。
? pm2-johndoe.service - PM2 process manager Loaded: loaded (/etc/systemd/system/pm2-johndoe.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2021-08-10 01:52:37 UTC; 3min 57s ago Docs: https://pm2.keymetrics.io/ Main PID: 745 (PM2 v5.1.0: God) Tasks: 34 (limit: 2343) Memory: 120.4M CGroup: /system.slice/pm2-johndoe.service ??745 PM2 v5.1.0: God Daemon (/home/johndoe/.pm2) ??772 npm ??873 sh -c node server/server.js "--port=3001" "--hostname=127.0.0.1" ??874 node server/server.js --port=3001 --hostname=127.0.0.1 Aug 10 01:52:36 ubuntu64 pm2[633]: [PM2] Spawning PM2 daemon with pm2_home=/home/johndoe/.pm2 Aug 10 01:52:37 ubuntu64 pm2[633]: [PM2] PM2 Successfully daemonized Aug 10 01:52:37 ubuntu64 pm2[633]: [PM2] Resurrecting Aug 10 01:52:37 ubuntu64 pm2[633]: [PM2] Restoring processes located in /home/johndoe/.pm2/dump.pm2 Aug 10 01:52:37 ubuntu64 pm2[633]: [PM2] Process /home/johndoe/.nvm/versions/node/v14.17.4/bin/npm restored
“ pm2-johndoe.service ”在系统启动时已启动并运行。
将 Apache 设置为反向代理
在本指南中,你将安装并配置 Apache web 服务器,作为 uptime-Kuma 应用的反向代理。
1. 执行以下命令安装 apache web 服务器软件包。
sudo apt install apache2
输入 “y” 并按 “ Enter ” 安装 apache web 服务器。
2. 之后,使用下面的命令启用一些 apache 模块。
sudo a2enmod ssl proxy proxy_ajp proxy_wstunnel proxy_http rewrite deflate headers proxy_balancer proxy_connect proxy_html
3. 接下来,使用下面的 nano 编辑器命令创建一个新的虚拟主机配置 ' uptime-kuma.conf '。
sudo nano /etc/apache2/sites-available/uptime-kuma.conf
将以下配置粘贴到你的终端 shell 中,并将域名 “ uptime.example.net ” 更改为你的域名,例如 “ domain.com ”。
<VirtualHost *:80>
ServerName uptime.example.net
ProxyPass / http://localhost:3001/
RewriteEngine on
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://localhost:3001/$1" [P,L]
</VirtualHost>
按 “ Ctrl+x ” 并输入 “ Y ” 保存配置。
4. 现在使用下面的命令激活 apache 虚拟主机配置 “uptime-kuma”。
sudo a2ensite uptime-kuma
之后,执行以下命令验证 apache 配置,确保没有任何错误。
sudo apachectl configtest
5. 现在使用下面的命令重启 apache 服务以应用新配置。
sudo systemctl restart apache2

使用 SSL Letsencrypt 保护 Uptime-kuma 安全
为了保护 Uptime-Kuma 的访问安全,你将为 apache web 服务器设置来自 Letsencrypt 的 SSL。
1. 首先,运行以下命令安装 certbot 工具。
sudo apt install python3-certbot-apache
输入 “ y ” 并按 “ Enter ” 开始安装。
2. 安装完成后,使用如下 certbot 命令生成 SSL Let's encrypt。
sudo certbot --apache yourdomain.com
输入你的电子邮件地址以注册 Letsencrypt,然后按 “ Enter ” 继续。每当你的 SSL 证书到期时,Letsencrypt 都会向你的电子邮件地址发送邮件。
``` Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator apache, Installer apache Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [email protected]
对于 Letsencrypt TOS(Terms of Service),输入 “ A ” 表示同意,然后按 “ Enter ” 继续。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (A)gree/(C)ancel: A
对于与 EFF(Electronic Frontier Foundation)共享电子邮件,你可以输入 “ Y ” 表示同意,“ N ” 表示不同意。在本示例中,我们不会与 EFF 共享电子邮件地址。
Would you be willing to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: N
现在 certbot 工具将为 Uptime-Kuma 域名生成 SSL Letsencrypt。此外,它将为 apache 虚拟主机配置添加一些附加配置。
你将被要求设置从 HTTP 到 HTTPS 的自动重定向。输入数字 “2” 设置 apache 将 HTTP 自动重定向到 HTTPS,然后按 “ Enter ”。
``` Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
部署过程成功后,你将看到如下类似的输出。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Congratulations! You have successfully enabled https://uptime.example.net You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=uptime.example.net - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/uptime.example.net/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/uptime.example.net/privkey.pem Your cert will expire on 2021-10-10. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le - We were unable to subscribe you the EFF mailing list because your e-mail address appears to be invalid. You can try again later by visiting https://act.eff.org.
验证 Uptime-Kuma 安装
1. 打开你的 web 浏览器,在地址栏输入 Uptime-Kuma 的安装 URL。
https://uptime.example.net/
你将被重定向到 HTTPS 安全连接。
2. 在第一个页面为 Uptime-kuma 创建一个新的管理员用户。输入你的管理员用户和密码,然后点击 “ Create ” 按钮

3. 之后,使用你的新用户名和密码登录,然后点击 “ Login ” 按钮。

现在,你将看到 uptime-kuma 仪表板。
4. 要向监控添加新主机,请点击 “ Add New Monitor ” 按钮。

5. 在本示例中,你将监控网站,因此选择 “ Monitor Type ” 为 “ HTTP(s) ”,输入网站的 URL 地址,点击 “ Setup Notification ” 以获得通知支持,然后点击页面底部的 “ Save ” 按钮。

6. 你将被重定向到 uptime-Kuma 仪表板,并在仪表板页面上看到你的主机。

下面是添加新类型 “ TCP/IP ” 且端口关闭后的另一个示例,结果为 “ ENCONNREFUSED ” 或连接错误,因为端口被拒绝/关闭。

结论
恭喜!你已成功安装 Uptime-Kuma,使用 Apache web 服务器作为反向代理,并使用 SSL Letsencrypt 保护了 Uptime-Kuma 部署的安全。在下一阶段,你可以添加主机并为任何宕机事件启用通知。