如何在 Debian 11 上安装 Grafana 8 监控工具
在此页
- 先决条件
- 开始
- 安装 Grafana 8
- 安装 Nginx 作为 Grafana 的反向代理
- 访问 Grafana 仪表板
- 使用 Lets Encrypt 保护 Grafana
- 结论
Grafana 是一个免费的开源数据可视化工具,用于监控来自其他主机的指标。它是用 Typescript 和 Go 编写的,允许您创建和编辑日志和数据图以及创建指标。它可以从时间序列数据库(包括 Graphite、InfluxDB 或 OpenTSDB)生成图形和仪表板,并允许您与其他用户共享它们。
特征
- 仪表板模板
- 供应注解
- Kiosk 模式和播放列表
- 自定义插件
- 警报和警报挂钩
在这篇文章中,我们将向您展示如何在 Debian 11 上安装 Grafana 8。
先决条件
- 运行 Debian 11 的服务器。
- 用您的服务器 IP 指向的有效域名。
- 在服务器上配置了根密码。
入门
在开始之前,建议将您的系统更新到更新版本。您可以使用以下命令更新所有包:
apt-get update -y
更新所有包后,使用以下命令安装其他所需的依赖项:
apt-get install gnupg2 curl wget git software-properties-common -y
安装所有软件包后,您可以继续下一步。
安装格拉法纳 8
默认情况下,Grafana 不包含在 Debian 11 默认存储库中。因此,您需要将 Grafana 存储库添加到 APT。您可以使用以下命令添加它:
curl https://packages.grafana.com/gpg.key | apt-key add -
add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
添加存储库后,更新存储库缓存并使用以下命令安装 Grafana:
apt-get update -y
apt-get install grafana -y
安装 Grafana 后,使用以下命令启动并启用 Grafana 服务:
systemctl start grafana-server
systemctl enable grafana-server
您还可以使用以下命令检查 Grafana 服务的状态:
systemctl status grafana-server
您将获得以下输出:
? grafana-server.service - Grafana instance
Loaded: loaded (/lib/systemd/system/grafana-server.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2022-02-04 04:08:25 UTC; 5s ago
Docs: http://docs.grafana.org
Main PID: 8602 (grafana-server)
Tasks: 9 (limit: 2341)
Memory: 29.6M
CPU: 1.299s
CGroup: /system.slice/grafana-server.service
??8602 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/run/grafana/grafana-server.pid --packaging=deb cfg:>
Feb 04 04:08:27 debian11 grafana-server[8602]: t=2022-02-04T04:08:27+0000 lvl=info msg="migrations completed" logger=migrator performed=381 s>
Feb 04 04:08:27 debian11 grafana-server[8602]: t=2022-02-04T04:08:27+0000 lvl=info msg="Created default admin" logger=sqlstore user=admin
Feb 04 04:08:27 debian11 grafana-server[8602]: t=2022-02-04T04:08:27+0000 lvl=info msg="Created default organization" logger=sqlstore
Feb 04 04:08:27 debian11 grafana-server[8602]: t=2022-02-04T04:08:27+0000 lvl=info msg="Initialising plugins" logger=plugin.manager
Feb 04 04:08:27 debian11 grafana-server[8602]: t=2022-02-04T04:08:27+0000 lvl=info msg="Plugin registered" logger=plugin.manager pluginId=inp>
Feb 04 04:08:27 debian11 grafana-server[8602]: t=2022-02-04T04:08:27+0000 lvl=info msg="Live Push Gateway initialization" logger=live.push_ht>
Feb 04 04:08:27 debian11 grafana-server[8602]: t=2022-02-04T04:08:27+0000 lvl=info msg="Writing PID file" logger=server path=/run/grafana/gra>
Feb 04 04:08:27 debian11 grafana-server[8602]: t=2022-02-04T04:08:27+0000 lvl=info msg="HTTP Server Listen" logger=http.server address=[::]:3>
Feb 04 04:08:27 debian11 grafana-server[8602]: t=2022-02-04T04:08:27+0000 lvl=info msg="warming cache for startup" logger=ngalert
Feb 04 04:08:27 debian11 grafana-server[8602]: t=2022-02-04T04:08:27+0000 lvl=info msg="starting MultiOrg Alertmanager" logger=ngalert.multio>
Grafana默认监听3000端口,可以通过以下命令查看:
ss -antpl | grep 3000
您将获得以下输出:
LISTEN 0 4096 *:3000 *:* users:(("grafana-server",pid=8602,fd=8))
安装 Nginx 作为 Grafana 的反向代理
接下来,您需要安装和配置 Nginx 作为 Grafana 的反向代理。首先,使用以下命令安装 Nginx:
apt-get install nginx -y
安装 Nginx 后,使用以下命令创建 Nginx 虚拟主机配置文件:
nano /etc/nginx/conf.d/grafana.conf
添加以下行:
server {
server_name grafana.example.com;
listen 80;
access_log /var/log/nginx/grafana.log;
location / {
proxy_pass http://localhost:3000;
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
完成后保存并关闭文件。然后,使用以下命令验证 Nginx 是否存在任何语法错误:
nginx -t
如果一切正常,您将获得以下输出:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
接下来,重新启动 Nginx 以应用配置更改。
systemctl restart nginx
您还可以使用以下命令检查 Nginx 状态:
systemctl status nginx
您将获得以下输出:
? nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-02-04 04:09:20 UTC; 4s ago
Docs: man:nginx(8)
Process: 8631 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 8632 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 8633 (nginx)
Tasks: 2 (limit: 2341)
Memory: 2.5M
CPU: 35ms
CGroup: /system.slice/nginx.service
??8633 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
??8634 nginx: worker process
Feb 04 04:09:20 debian11 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 04 04:09:20 debian11 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 04 04:09:20 debian11 systemd[1]: Started A high performance web server and a reverse proxy server.
此时,Nginx 已安装并配置为 Grafana 的反向代理。您现在可以继续下一步。
访问 Grafana 仪表板
现在,打开您的 Web 浏览器并使用 URL http://grafana.example.com 访问 Grafana Web 界面。您将被重定向到 Grafana 登录页面:

提供您的管理员用户名、密码,然后单击“登录”按钮。您应该会看到 Grafana 密码重置屏幕:

设置您的新密码,然后单击“提交”按钮。您应该会在以下屏幕上看到 Grafana 仪表板:

使用 Lets Encrypt 保护 Grafana
接下来,您需要安装 Certbot 客户端包来安装和管理 Lets Encrypt SSL。
首先,使用以下命令安装 Certbot:
apt-get install certbot python3-certbot-nginx -y
安装完成后,运行以下命令在您的网站上安装 Lets Encrypt SSL:
certbot --nginx -d grafana.example.com
您将被要求提供一个有效的电子邮件地址并接受服务条款,如下所示:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for grafana.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/grafana.conf
接下来,选择是否将 HTTP 流量重定向到 HTTPS,如下所示:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
键入 2 并按 Enter 键以完成安装。您应该看到以下输出:
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/grafana.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://grafana.example.com
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=grafana.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/grafana.example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/grafana.example.com/privkey.pem
Your cert will expire on 2022-05-07. 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.
结论
恭喜!您已经在 Debian 11 上成功安装了带有 Nginx 和 Lets Encrypt SSL 的 Grafana 8。您现在可以添加远程主机并开始从 Grafana 仪表板监控它们。如果您有任何问题,请随时问我。