如何在 Ubuntu 20.04 上安裝 Graylog 服務器

所有系統、應用程序和軟件都會生成存儲在稱為日誌的文件中的信息。 必須持續監控這些日誌,以確保相關係統正常運行並防止錯誤或安全風險。 它們通常分散在多個服務器上,並且隨著數據量的增加,它們的管理變得越來越複雜。

灰色日誌 是一個免費的開源企業級日誌管理系統,由 Elasticsearch、MongoDB 和 Graylog 服務器組成。 它包括從安裝在不同服務器上的客戶端接收數據的主服務器,以及用於查看服務器收集的數據的 Web 界面。 Graylog 是一個類似於 Splunk 和 LogStash 的工具。

在本教程中,我們將學習如何 安裝 Graylog 4Ubuntu 20.04. 我們還將使用 Let’s Encrypt 使用 SSL 證書保護 Graylog 服務器。

要求:

  • Ubuntu 20.04 機器或更高版本
  • 硬件 – 4 個 CPU 內核、8 GB RAM、具有高 IOPS 的 SSD 硬盤,用於 Elasticsearch 日誌存儲
  • 彈性搜索
  • MongoDB
  • Oracle Java SE 8(OpenJDK 8 或更高版本)

步驟 1:在 Ubuntu 20.04 上安裝 MongoDB

Graylog 使用 MongoDB 存儲流、警報、用戶、設置等配置數據。僅存儲元數據,不存儲日誌數據。 讓我們首先在 Ubuntu 20.04 上安裝 MongoDB。

首先,安裝所有先決條件包:

$ sudo apt update
$ sudo apt install apt-transport-https openjdk-11-jre-headless uuid-runtime pwgen dirmngr gnupg wget

現在導入公共 GPG 密鑰:

$ wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

要創建源列表文件,請鍵入:

$ sudo touch /etc/apt/sources.list.d/mongodb-org-5.0.list

現在,添加 Ubuntu 20.04 的存儲庫源:

$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

現在,更新 apt 索引:

$ sudo apt update

要在 Ubuntu 上安裝 MongoDB,請鍵入:

$ sudo apt install mongodb-org

安裝過程完成後,MongoDB 服務不會自動啟動。 要啟動服務並啟用服務,請鍵入:

$ sudo systemctl enable --now mongod.service

驗證 MongoDB 服務的狀態:

$ sudo systemctl status mongod.service

輸出:

● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Sun 2021-08-22 04:47:46 UTC; 3s ago
Docs: https://docs.mongodb.org/manual
Main PID: 17965 (mongod)
Memory: 66.1M
CGroup: /system.slice/mongod.service
└─17965 /usr/bin/mongod --config /etc/mongod.conf
Aug 22 04:47:46 li175-223 systemd[1]: Started MongoDB Database Server.

如輸出所示,MongoDB 已啟動並且服務已啟動並正在運行。

可以查看安裝的MongoDB版本

$ sudo mongod --version

db version v5.0.2
Build Info: {
"version": "5.0.2",
"gitVersion": "6d9ec525e78465dcecadcff99cce953d380fedc8",
"openSSLVersion": "OpenSSL 1.1.1f 31 Mar 2020",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "ubuntu2004",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}

輸出顯示我們已經安裝 MongoDB 版本 5.0.2.

第 2 步:在 Ubuntu 20.04 上安裝 Elasticsearch

Graylog 將所有日誌數據存儲在 Elasticsearch 中。 支持的版本請參考Graylog官網 彈性搜索。

Elasticseach 需要 Java,它帶有 Java 捆綁版本的 OpenJDK

導入存儲庫的 GPG 密鑰:

$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

接下來,添加 Elasticsearch 存儲庫:

# echo "deb https://artifacts.elastic.co/packages/oss-7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

現在更新存儲庫

$ sudo apt update

最後,輸入以下命令安裝開源版本的 elasticsearch:

$ sudo apt install elasticsearch-oss

修改elasticsearch配置文件,設置一些信息,如集群名稱、elasticsearch監聽的IP地址、端口號

$ sudo vi /etc/elasticsearch/elasticsearch.yml

cluster.name: graylog-server
network.host: 0.0.0.0
http.port: 9200
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
action.auto_create_index: false

Elasticsearch 的主要配置文件和目錄有:

數據存儲 – /var/lib/elasticsearch 目錄。
配置文件 – /etc/elasticsearch 目錄
Java 啟動選項 – /etc/default/elasticsearch 文件

Elasticsearch 附帶一個捆綁版本的 OpenJDK。 要使用您自己的 Java 版本,請設置 ES_JAVA_HOME 環境變量。

您可以使用一個命令啟用和啟動 Elasticsearch 服務:

$ sudo systemctl enable --now elasticsearch.service

現在使用以下命令驗證服務是否正常運行:

$ sudo systemctl status elasticsearch.service

輸出:

● elasticsearch.service - Elasticsearch
Loaded: loaded (/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
Active: active (running) since Sun 2021-08-22 12:38:24 UTC; 11min ago
Docs: https://www.elastic.co
Main PID: 19502 (java)
Tasks: 41 (limit: 1071)
Memory: 833.2M
CGroup: /system.slice/elasticsearch.service
├─19502 /bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negat>
└─19565 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller
Aug 22 12:38:24 li663-124 systemd[1]: Started Elasticsearch.

按 q 退出,返回命令提示符。

Elasticsearch 默認在端口上工作 9200,你應該在防火牆上打開它。

$ sudo ufw allow 9200

你可以檢查它是否工作

$ sudo curl -XGET 'https://localhost:9200'

"name" : "Ubuntunode",
"cluster_name" : "graylog-server",
"cluster_uuid" : "sz3jP3rKTPWZlasWwD-rBg",
"version" : {
"number" : "7.10.2",
"build_flavor" : "oss",
"build_type" : "deb",
"build_hash" : "747e1cc71def077253878a59143c1f785afa92b9",
"build_date" : "2021-01-13T00:42:12.435326Z",
"build_snapshot" : false,
"lucene_version" : "8.7.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

隨著命令的輸出,您可以看到該行 "number": "7.10.2" 顯示彈性搜索版本。

第 3 步:在 Ubuntu 20.04 上安裝 Graylog

Graylog 從各種輸入收集日誌,並將輸出提供給它用來管理日誌的 Web 界面。

首先,我們需要使用 wget 命令下載 Graylog Repo 文件:

$ wget https://packages.graylog2.org/repo/packages/graylog-4.1-repository_1-3_all.deb

要配置 Graylog 存儲庫,請鍵入:

$ sudo dpkg -i graylog-4.1-repository_1-3_all.deb

現在我們可以安裝 Graylog 服務器

$ apt update
$ sudo apt install graylog-server

啟用並啟動 graylog 服務器服務

$ sudo systemctl enable --now graylog-server.service

編輯配置文件

現在我們需要設置 password_secret 以及您的根灰度日誌的價值 root_password_sha2.

要設置機密以保護用戶密碼,您可以使用 pwgen 命令:

$ pwgen -N 1 -s 96

8KOikhjxetTdcJ7qhtPlgtLgBqCX5y4vL36Ig423pqUQn32QatiecCcYol5UEw3XDzwgWDvcFOmMBJzkBZKC52aEpBrItwke

現在,編輯配置文件以設置密碼

$ sudo vi /etc/graylog/server/server.conf

password_secret = RlTRqWSBENLKeg89iAWlxSaf1zfqLvBW7VX5SH1d2ji3RMKyeXb8bmfOXLl3GaWkxp9oDRfvbjXiEr36AFd6T9CMmnjdG7dn

我們現在必須定義您的 Graylog 根密碼帳戶的哈希值

$ echo -n GraylogRootPassword | shasum -a 256

4b09467e174a03d5ebd720d514f57783ad1e03b4877fff5e0dc45356340ab215 -

現在您可以復制並粘貼它

sudo vi /etc/graylog/server/server.conf

root_password_sha2 = 4b09467e174a03d5ebd720d514f57783ad1e03b4877fff5e0dc45356340ab215

您可以添加其他信息作為 Graylog 根電子郵件地址和 Graylog HTTP 接口使用的網絡接口

root_email = "[email protected]"
root_timezone = UTC
http_bind_address = 0.0.0.0:9000

您應該打開防火牆上的端口

$ sudo ufw allow 9000

啟用並啟動 graylog 服務器服務

$ sudo systemctl enable --now graylog-server.service

您可以找到 Graylog /var/log/graylog-server/server.log 的日誌數據,這對於調試或服務器無法啟動時很有用。

您可以通過輸入服務器的公共 ip 和瀏覽器的端口號(即 https://:9000/)來查看主頁。 graylog的默認root登錄名是 admin 密碼是用於哈希值的密碼

第 4 步:將 Nginx 配置為 SSL 終止代理(可選)

建議使用 HTTPS 保護您的 Graylog Web 界面。 這裡將使用 Nginx 作為反向代理,並使用 Let’s Encrypt 為域生成一個免費的 SSL 證書。

首先,我們需要安裝 Nginx,然後安裝 cerbot 來加密證書。

現在讓我們安裝 Nginx

$ sudo apt install nginx

現在讓我們在防火牆上允許 Nginx

$ sudo ufw allow 'Nginx Full'

現在讓我們編輯我們的域名配置文件的內容。 不要忘記用您的域名替換文件名

$ sudo vim /etc/nginx/sites-available/websitefortesting.com.conf

server {
listen 80;
server_name websitefortesting.com;
add_header Strict-Transport-Security max-age=2592000;
rewrite ^ https://$server_name$request_uri? permanent;
access_log /var/log/nginx/websitefortesting.com.access.log combined;
error_log /var/log/nginx/websitefortesting.com.error.log;
}

現在讓我們激活它

$ sudo ln -s /etc/nginx/sites-available/websitefortesting.com.conf /etc/nginx/sites-enabled/websitefortesting.com.conf

然後驗證配置是否正常

$ sudo nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

現在我們需要使用 nginx 所需的包安裝 cerbot

$ sudo apt install certbot python3-certbot-nginx

現在我們需要為 Nginx 運行 cerbot

$ sudo certbot --nginx

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: N
Which names would you like to activate HTTPS for?

1: websitefortesting.com

Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for websitefortesting.com
Waiting for verification…
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/websitefortesting.com.conf
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
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/websitefortesting.com.conf

Congratulations! You have successfully enabled https://websitefortesting.com
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=websitefortesting.com

IMPORTANT NOTES:
Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/websitefortesting.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/websitefortesting.com/privkey.pem
Your cert will expire on 2021-11-18. 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"
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

從輸出中,您可以看到 SSL 證書和私鑰的位置。 您可以使用此信息為 Nginx 配置 SSL。

$ sudo vi /etc/nginx/sites-available/websitefortesting.com.conf

server {
listen 80;
server_name websitefortesting.com;
add_header Strict-Transport-Security max-age=2592000;
rewrite ^ https://$server_name$request_uri? permanent;
access_log /var/log/nginx/websitefortesting.com.access.log combined;
error_log /var/log/nginx/websitefortesting.com.error.log;
}
server {
listen 443 ssl; # managed by Certbot
server_name websitefortesting.com;
ssl_certificate /etc/letsencrypt/live/websitefortesting.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/websitefortesting.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location / { proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass https://127.0.0.1:9000;
}
}

現在讓我們再次驗證

$ sudo nginx -t

並重啟 Nginx 服務

$ sudo systemctl restart nginx.service

現在使用帶有 HTTPS 的域名訪問您的 Graylog URL。

登錄後,您可以看到您的主頁

結論

在本教程中,我們學習瞭如何在 Ubuntu 20.04 上安裝 Graylog 服務器,使用 Nginx 作為反向代理配置 SSL。

如果您在設置過程中遇到任何挑戰,請隨時在評論部分發表評論或提出任何問題。