本教程适用于以下操作系统版本
- Ubuntu 24.04 (Noble Numbat)
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 18.04 (Bionic Beaver)
本页内容
- 前提条件
- 准备工作
- 在 Ubuntu 22.04 上安装 Grafana 8
- 安装 Nginx 作为 Grafana 的反向代理
- 访问 Grafana Dashboard
- 使用 Let's Encrypt 保护 Grafana
- 结论
Grafana 是一款免费、开源且功能丰富的指标 dashboard 和数据可视化工具。它专为 Graphite、Elasticsearch、OpenTSDB、Prometheus 和 InfluxDB 设计,可通过基于 Web 的界面监控指标。它支持多平台,并拥有 100 多个用于数据收集、存储、可视化和共享的插件。它允许你为数据创建告警和通知,并通过共享功能使与团队成员的协作更加轻松。
本教程将介绍如何在 Ubuntu 22.04 上安装 Grafana 8。
前提条件
- 一台运行 Ubuntu 22.04 的服务器。
- 一个指向你的服务器 IP 的有效域名。
- 服务器上已配置 root 密码。
准备工作
在开始之前,建议将系统更新到最新版本。你可以使用以下命令更新所有软件包:
apt-get update -y
apt-get upgrade -y
所有软件包升级完成后,使用以下命令安装其他所需的依赖项:
apt-get install gnupg2 curl wget git software-properties-common -y
所有软件包安装完成后,即可进入下一步。
在 Ubuntu 22.04 上安装 Grafana 8
默认情况下,Ubuntu 22.04 的默认仓库中不包含 Grafana。因此你需要将 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 Sat 2022-08-06 09:33:06 UTC; 7s ago
Docs: http://docs.grafana.org
Main PID: 69737 (grafana-server)
Tasks: 9 (limit: 2242)
Memory: 37.1M
CPU: 1.580s
CGroup: /system.slice/grafana-server.service
??69737 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/run/grafana/grafana-server.pid --packaging=deb cfg>
Aug 06 09:33:08 ubuntu2204 grafana-server[69737]: logger=secrets t=2022-08-06T09:33:08.517381147Z level=info msg="Envelope encryption state" >
Aug 06 09:33:08 ubuntu2204 grafana-server[69737]: logger=query_data t=2022-08-06T09:33:08.527197639Z level=info msg="Query Service initializa>
Aug 06 09:33:08 ubuntu2204 grafana-server[69737]: logger=live.push_http t=2022-08-06T09:33:08.544920469Z level=info msg="Live Push Gateway in>
Aug 06 09:33:13 ubuntu2204 grafana-server[69737]: logger=ngalert t=2022-08-06T09:33:13.723313517Z level=warn msg="failed to delete old am con>
Aug 06 09:33:13 ubuntu2204 grafana-server[69737]: logger=infra.usagestats.collector t=2022-08-06T09:33:13.885398731Z level=info msg="register>
Aug 06 09:33:13 ubuntu2204 grafana-server[69737]: logger=server t=2022-08-06T09:33:13.886028388Z level=info msg="Writing PID file" path=/run/>
Aug 06 09:33:13 ubuntu2204 grafana-server[69737]: logger=http.server t=2022-08-06T09:33:13.900761945Z level=info msg="HTTP Server Listen" add>
Aug 06 09:33:13 ubuntu2204 grafana-server[69737]: logger=ngalert t=2022-08-06T09:33:13.901994976Z level=info msg="warming cache for startup"
Aug 06 09:33:13 ubuntu2204 grafana-server[69737]: logger=ngalert.multiorg.alertmanager t=2022-08-06T09:33:13.902506414Z level=info msg="start>
Aug 06 09:33:13 ubuntu2204 grafana-server[69737]: logger=grafanaStorageLogger t=2022-08-06T09:33:13.926571246Z level=info msg="storage starti>
默认情况下,Grafana 监听 3000 端口。你可以使用以下命令检查:
ss -antpl | grep 3000
你将看到以下输出:
LISTEN 0 4096 *:3000 *:* users:(("grafana-server",pid=69737,fd=8))
安装 Nginx 作为 Grafana 的反向代理
默认情况下,Grafana 可通过 3000 端口访问。因此,你需要安装并配置 Nginx 作为 Grafana 的反向代理,以便通过 80 端口访问它。首先,使用以下命令安装 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 Host $http_host;
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 Sat 2022-08-06 09:35:32 UTC; 4s ago
Docs: man:nginx(8)
Process: 70326 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 70327 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 70328 (nginx)
Tasks: 2 (limit: 2242)
Memory: 2.6M
CPU: 42ms
CGroup: /system.slice/nginx.service
??70328 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
??70329 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
Aug 06 09:35:32 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
Aug 06 09:35:32 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.
至此,Nginx 已安装并配置为 Grafana 的反向代理。现在可以进入下一步。
访问 Grafana Dashboard
现在,打开 Web 浏览器,使用 URL http://grafana.example.com 访问 Grafana Web 界面。你将被重定向到 Grafana 登录页面:

输入默认管理员用户名和密码 admin/admin,然后点击 Log in 按钮。你将看到 Grafana 密码重置界面:

设置新密码并点击 Submit 按钮。你将在接下来的界面中看到 Grafana dashboard:

使用 Let's Encrypt 保护 Grafana
接下来,你需要安装 Certbot 客户端软件包,以安装和管理 Let's Encrypt SSL。
首先,使用以下命令安装 Certbot:
apt-get install certbot python3-certbot-nginx -y
安装完成后,运行以下命令为你的网站安装 Let's 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): [email protected] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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.
结论
恭喜!你已在 Ubuntu 22.04 上成功安装了 Grafana 8,并配置了 Nginx 作为反向代理以及 Let's Encrypt SSL。现在你可以将外部数据源添加到 Grafana,并从 Grafana dashboard 开始监控它们。如有任何问题,欢迎随时向我提问。