基于 Linux 环境(CentOS/Ubuntu 通用)。

前提条件

  1. 安装 Java 11(ELK/Logstash 必需):

    bash

    运行

    # CentOS
    yum install -y java-11-openjdk-devel
    # Ubuntu
    apt install -y openjdk-11-jdk
    # 验证Java
    java -version  # 输出openjdk version "11.x.x"即可
    
  2. 关闭防火墙 / 放行端口(5601-Kibana、9200-ES、5044-Filebeat→Logstash):

    bash

    运行

    # CentOS
    systemctl stop firewalld && systemctl disable firewalld
    setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
    # Ubuntu
    ufw disable
    
  3. 创建统一部署目录(方便管理):

    bash

    运行

    mkdir -p /usr/local/elk
    cd /usr/local/elk
    

第一步:下载 ELK + Filebeat 安装包

推荐下载8.x 稳定版(所有组件版本保持一致,避免兼容性问题):

bash

运行

# 下载地址(可替换为国内镜像,如阿里云:https://mirrors.aliyun.com/elasticstack/8.x/yum/)
# Elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.14.0-linux-x86_64.tar.gz
# Logstash
wget https://artifacts.elastic.co/downloads/logstash/logstash-8.14.0-linux-x86_64.tar.gz
# Kibana
wget https://artifacts.elastic.co/downloads/kibana/kibana-8.14.0-linux-x86_64.tar.gz
# Filebeat
wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.14.0-linux-x86_64.tar.gz

# 解压所有包
tar -zxvf elasticsearch-8.14.0-linux-x86_64.tar.gz
tar -zxvf logstash-8.14.0-linux-x86_64.tar.gz
tar -zxvf kibana-8.14.0-linux-x86_64.tar.gz
tar -zxvf filebeat-8.14.0-linux-x86_64.tar.gz

# 简化目录名(方便后续操作)
mv elasticsearch-8.14.0 elasticsearch
mv logstash-8.14.0 logstash
mv kibana-8.14.0-linux-x86_64 kibana
mv filebeat-8.14.0-linux-x86_64 filebeat

第二步:配置 Elasticsearch

2.1 创建 ES 专用用户(ES 不允许 root 启动)

bash

运行

useradd -r -s /sbin/nologin elasticsearch
chown -R elasticsearch:elasticsearch /usr/local/elk/elasticsearch

2.2 修改 ES 配置文件

编辑 /usr/local/elk/elasticsearch/config/elasticsearch.yml

yaml

# 核心配置
cluster.name: elk-cluster
node.name: node-1
path.data: /usr/local/elk/elasticsearch/data  # 数据存储目录
path.logs: /usr/local/elk/elasticsearch/logs  # 日志目录
network.host: 0.0.0.0
http.port: 9200
discovery.type: single-node  # 单节点部署
xpack.security.enabled: false  # 关闭安全认证(新手简化)
xpack.security.enrollment.enabled: false
# 解决内核参数限制
bootstrap.memory_lock: false
bootstrap.system_call_filter: false

2.3 修改系统内核参数(解决 ES 启动报错)

编辑 /etc/security/limits.conf,添加:

ini

elasticsearch soft nofile 65535
elasticsearch hard nofile 65535
elasticsearch soft nproc 4096
elasticsearch hard nproc 4096

编辑 /etc/sysctl.conf,添加:

ini

vm.max_map_count=262144
fs.file-max=655350

生效配置:

bash

运行

sysctl -p
ulimit -n 65535

2.4 启动 ES + 设置开机自启

1. 启动 ES

bash

运行

# 切换到elasticsearch用户启动
su - elasticsearch -c "/usr/local/elk/elasticsearch/bin/elasticsearch -d"
# 验证启动(等待30秒,ES启动较慢)
curl http://localhost:9200
# 输出{"name":"node-1","cluster_name":"elk-cluster",...}即成功
2. 创建 systemd 服务(实现开机自启)

编辑 /usr/lib/systemd/system/elasticsearch.service

ini

[Unit]
Description=Elasticsearch
After=network.target

[Service]
Type=simple
User=elasticsearch
Group=elasticsearch
ExecStart=/usr/local/elk/elasticsearch/bin/elasticsearch -d
ExecStop=/usr/local/elk/elasticsearch/bin/elasticsearch-stop.sh
Restart=on-failure
LimitNOFILE=65535
LimitNPROC=4096

[Install]
WantedBy=multi-user.target

设置开机自启:

bash

运行

systemctl daemon-reload
systemctl enable elasticsearch

第三步:配置 Logstash

3.1 创建 Logstash 配置文件

bash

运行

mkdir -p /usr/local/elk/logstash/conf.d

编辑 /usr/local/elk/logstash/conf.d/nginx-log.conf

ruby

input {
  beats {
    port => 5044
    host => "0.0.0.0"
  }
}

filter {
  grok {
    match => { "message" => "%{COMBINEDAPACHELOG}" }  # 匹配Nginx默认日志格式
    remove_field => ["message"]
  }
  date {
    match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z" ]
    target => "@timestamp"
  }
  mutate {
    add_field => { "log_source" => "nginx_access" }
  }
}

output {
  elasticsearch {
    hosts => ["http://localhost:9200"]
    index => "nginx-logs-%{+YYYY.MM.dd}"
  }
  # stdout { codec => rubydebug }  # 调试用,可选开启
}

3.2 启动 Logstash + 设置开机自启

1. 启动 Logstash(root 用户即可)

bash

运行

/usr/local/elk/logstash/bin/logstash -f /usr/local/elk/logstash/conf.d/nginx-log.conf &
# 验证启动(查看日志)
tail -f /usr/local/elk/logstash/logs/logstash-plain.log
# 无ERROR即正常
2. 创建 systemd 服务(开机自启)

编辑 /usr/lib/systemd/system/logstash.service

ini

[Unit]
Description=Logstash
After=network.target elasticsearch.service

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/elk/logstash/bin/logstash -f /usr/local/elk/logstash/conf.d/nginx-log.conf
ExecStop=/bin/kill -9 `ps -ef | grep logstash | grep -v grep | awk '{print $2}'`
Restart=on-failure

[Install]
WantedBy=multi-user.target

设置开机自启:

bash

运行

systemctl daemon-reload
systemctl enable logstash

第四步:配置 Kibana

4.1 修改 Kibana 配置文件

编辑 /usr/local/elk/kibana/config/kibana.yml

yaml

server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://localhost:9200"]
kibana.index: ".kibana"
i18n.locale: "zh-CN"  # 中文界面

4.2 启动 Kibana + 设置开机自启

1. 启动 Kibana

bash

运行

/usr/local/elk/kibana/bin/kibana --daemonize  # 后台启动
# 验证启动(访问http://服务器IP:5601,首次加载约1分钟)
2. 创建 systemd 服务(开机自启)

编辑 /usr/lib/systemd/system/kibana.service

ini

[Unit]
Description=Kibana
After=network.target elasticsearch.service

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/elk/kibana/bin/kibana --daemonize
ExecStop=/bin/kill -9 `ps -ef | grep kibana | grep -v grep | awk '{print $2}'`
Restart=on-failure

[Install]
WantedBy=multi-user.target

设置开机自启:

bash

运行

systemctl daemon-reload
systemctl enable kibana

第五步:配置 Filebeat(采集 Nginx 日志)

5.1 修改 Filebeat 配置文件

编辑 /usr/local/elk/filebeat/filebeat.yml

yaml

filebeat.inputs:
- type: filestream
  enabled: true
  paths:
    - /elk/nginx/logs/access.log
    - /elk/nginx/logs/error.log
  # 自定义标签,方便后续筛选
  tags: ["nginx-logs"]

# 关闭直接发送ES
output.elasticsearch:
  enabled: false

# 发送到Logstash
output.logstash:
  hosts: ["localhost:5044"]

# 启用Nginx模块(可选)
filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false
- module: nginx
  access:
    enabled: true
    var.paths: ["/var/log/nginx/access.log"]
  error:
    enabled: true
    var.paths: ["/var/log/nginx/error.log"]

5.2 启动 Filebeat + 设置开机自启

1. 启动 Filebeat

bash

运行

/usr/local/elk/filebeat/filebeat -c /usr/local/elk/filebeat/filebeat.yml &
# 验证状态
ps -ef | grep filebeat
2. 创建 systemd 服务(开机自启)

编辑 /usr/lib/systemd/system/filebeat.service

ini

[Unit]
Description=Filebeat
After=network.target logstash.service

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/elk/filebeat/filebeat -c /usr/local/elk/filebeat/filebeat.yml
ExecStop=/bin/kill -9 `ps -ef | grep filebeat | grep -v grep | awk '{print $2}'`
Restart=on-failure

[Install]
WantedBy=multi-user.target

设置开机自启:

bash

运行

systemctl daemon-reload
systemctl enable filebeat

第六步:验证全流程

  1. 启动所有组件(按顺序):

    bash

    运行

    systemctl start elasticsearch
    systemctl start logstash
    systemctl start kibana
    systemctl start filebeat
    
  2. 访问 Nginx 生成日志:curl http://localhost
  3. 检查 ES 是否有数据:

    bash

    运行

    curl http://localhost:9200/nginx-logs-$(date +%Y.%m.%d)/_count
    
    输出"count":1(或更大)表示数据写入成功。
  4. 浏览器访问http://服务器IP:5601,创建索引模式nginx-logs-*,即可在 Discover 查看实时日志。

常见问题解决

  1. ES 启动失败:检查/usr/local/elk/elasticsearch/logs下的日志,确保内核参数vm.max_map_count=262144已生效。
  2. Filebeat 无权限读取 Nginx 日志

    bash

    运行

    chmod 644 /var/log/nginx/access.log
    chown -R root:root /var/log/nginx  # 确保root能读取
    
  3. Logstash 端口 5044 被占用

    bash

    运行

    netstat -tlnp | grep 5044  # 查看占用进程
    kill -9 进程ID  # 杀死占用进程后重启Logstash
    

总结

  1. 手动部署核心是:下载解压→配置文件→创建 systemd 服务实现开机自启,所有组件需版本一致(8.14.0)。
  2. ES 必须用非 root 用户启动,需提前配置系统内核参数和权限;其他组件(Logstash/Kibana/Filebeat)可直接用 root 启动。
  3. 核心流程不变:Filebeat 采集 Nginx 日志→Logstash 解析→ES 存储→Kibana 可视化,关键是确保 Logstash 的 grok 规则匹配 Nginx 日志格式。
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐