最新动态 > 详情

php安装部署elasticsearch流程

发布时间:2021-12-02 13:08:16

对phper来说,接触java相关的配置都需要好好记录下来。

因为项目需求,现在要自己搭建es环境

java环境请先自行安装,这里主要讲es安装

安装前,es首页默认时最新版,比如我现在默认最新是8.0.0 。

重要!重要!重要!请先根据自己操作系统选择支持的版本 :【链接:】 支持矩阵 | Elastic

一、根据自己的服务器操作系统下载对应安装包

选择版本:【链接:】Elasticsearch Guide | Elastic

进入下载页:

这里有每一种操作系统的完整安装引导

8.0的linux链接:Install Elasticsearch from archive on Linux or MacOS | Elasticsearch Guide [8.0] | Elastic

二、按照文档进行安装elasticSearch

这里分两种形式安装,一种是tar.gz包安装,一种是rpm安装

(一)rpm安装

1.按照官方的命令安装es

执行命令:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.1-x86_64.rpm
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.1-x86_64.rpm.sha512
yum install perl-Digest-SHA
shasum -a 512 -c elasticsearch-7.17.1-x86_64.rpm.sha512 
sudo rpm --install elasticsearch-7.17.1-x86_64.rpm

[root@localhost opt]# shasum -a 512 -c elasticsearch-7.17.1-x86_64.rpm.sha512
elasticsearch-7.17.1-x86_64.rpm: OK
[root@localhost opt]# sudo rpm --install elasticsearch-7.17.1-x86_64.rpm
警告:elasticsearch-7.17.1-x86_64.rpm: 头V4 RSA/SHA512 Signature, 密钥 ID d88e42b4: NOKEY
Creating elasticsearch group... OK
Creating elasticsearch user... OK
### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
 sudo systemctl daemon-reload
 sudo systemctl enable elasticsearch.service
### You can start elasticsearch service by executing
 sudo systemctl start elasticsearch.service
Created elasticsearch keystore in /etc/elasticsearch/elasticsearch.keystore

2.修改配置文件 /etc/elasticsearch/elasticsearch.yml

[root@localhost opt]# vim /etc/elasticsearch/elasticsearch.yml
[root@localhost opt]# grep -Ev "^#" /etc/elasticsearch/elasticsearch.yml
cluster.name: forpastime
node.name: node-249
path.data: /data/elasticsearch
path.logs: /data/log/elasticsearch
bootstrap.memory_lock: true
network.host: 192.168.2.249
http.port: 9200
discovery.seed_hosts: ["192.168.2.249"]
cluster.initial_master_nodes: ["node-249"]

查看修配置文件生效的命令:grep -Ev "^#" /etc/elasticsearch/elasticsearch.yml

3.因为我们指定了es数据和日志存储目录,所以添加目录

[root@localhost /]# mkdir data
[root@localhost /]# mkdir data/log
[root@localhost /]# mkdir data/elasticsearch
[root@localhost /]# mkdir data/log/elasticsearch

4.安装后默认添加了elasticsearch用户,所以将新增的目录权限给elasticsearch

[root@localhost /]# chown -R elasticsearch:elasticsearch /data/elasticsearch
[root@localhost /]# chown -R elasticsearch:elasticsearch /data/log/elasticsearch

5.修改/etc/elasticsearch/jvm.options文件

[root@localhost log]# vim /etc/elasticsearch/jvm.options

增加如下代码
-Xms512m
-Xmx512m

如果此时启动es,会无法成功,查看日志:

tail -n 1000 /data/log/elasticsearch/elasticsearch.log 
或其他日志文件

遇到memory is not locked,需要作如下6的操作:

6.执行systemctl edit elasticsearch,输入以下内容

[root@localhost log]# systemctl edit elasticsearch
输入:
[Service]
LimitMEMLOCK=infinity

Ctrl + O提示是否写入,Enter回车 

Ctrl + X 

7.启动es,查看启动状态和端口情况

[root@localhost log]# systemctl daemon-reload
[root@localhost log]# systemctl start elasticsearch.service
[root@localhost log]# systemctl status elasticsearch
● elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/elasticsearch.service.d
           └─override.conf
   Active: active (running) since 三 2022-03-02 11:09:27 CST; 12s ago
     Docs: https://www.elastic.co
 Main PID: 19592 (java)
    Tasks: 69
   CGroup: /system.slice/elasticsearch.service
           ├─19592 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10
           └─19796 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller

3月 02 11:09:18 localhost.localdomain systemd[1]: Starting Elasticsearch...
3月 02 11:09:27 localhost.localdomain systemd[1]: Started Elasticsearch.
[root@localhost log]# ss -lntup|grep 9200
tcp    LISTEN     0      128      [::ffff:192.168.2.249]:9200               [::]:*                   users:(("java",pid=19592,fd=295))

rmp安装常见问题:

一般情况9200端口应该是禁用的,所以开放一下9200端口

[root@localhost log]# vim /etc/sysconfig/iptables
追加:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9200 -j ACCEPT
重启防火墙[root@localhost log]# systemctl restart iptables.service

注意:服务器重启后重启服务可能要重启防火墙配置,否则9200还是没法访问

systemctl restart iptables.service

生产环境配置遇到问题及日志打印问题:

解决办法:

服务器安全组开启9200端口,elasticsearch.yml配置文件内的参数network.host配置为内网ip或0.0.0.0

8.如果是谷歌或者Edge浏览器,可以安装elasticsearch扩展,这里就可以看到安装启动成功信息。

(二)tar包安装

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.0.0-linux-x86_64.tar.gz
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.0.0-linux-x86_64.tar.gz.sha512
shasum -a 512 -c elasticsearch-8.0.0-linux-x86_64.tar.gz.sha512

如果没有安装 shasum 无法执行第三条命令,需要安装一下perl-Digest-SHA,linux执行以下命令

yum install perl-Digest-SHA

接着执行命令

tar -xzf elasticsearch-8.0.0-linux-x86_64.tar.gz
cd elasticsearch-8.0.0/
##以下是运行elasticSearch命令
./bin/elasticsearch

第一次执行./bin/elasticsearch会报错,原因是不能以root运行

新增用户或者修改为其他用户,并将目录修改为对应用户权限

再次执行启动es命令

./bin/elasticsearch

查看打印日志发现:

下图中的参数很重要:!!!!

复制出来

━
✅ Elasticsearch security features have been automatically configured!
✅ Authentication is enabled and cluster connections are encrypted.

ℹ️  Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
  7FB6Cmd0_GTxJ=_Sa5JD

ℹ️  HTTP CA certificate SHA-256 fingerprint:
  aa1fc51876c2605769cd8d87b1f7391e27cf7b9117369a52a375f7b5e90278ca

ℹ️  Configure Kibana to use this cluster:
• Run Kibana and click the configuration link in the terminal when Kibana starts.
• Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):
  eyJ2ZXIiOiI4LjAuMCIsImFkciI6WyIxOTIuMTY4LjIuMjUxOjkyMDAiXSwiZmdyIjoiYWExZmM1MTg3NmMyNjA1NzY5Y2Q4ZDg3YjFmNzM5MWUyN2NmN2I5MTE3MzY5YTUyYTM3NWY3YjVlOTAyNzhjYSIsImtleSI6InlNcHlMMzhCMHVybmw5V1htV3c4OlNCUThvczJZVEFlUE91MzlFRkFHcVEifQ==

ℹ️  Configure other nodes to join this cluster:
• On this node:
  ⁃ Create an enrollment token with `bin/elasticsearch-create-enrollment-token -s node`.
  ⁃ Uncomment the transport.host setting at the end of config/elasticsearch.yml.
  ⁃ Restart Elasticsearch.
• On other nodes:
  ⁃ Start Elasticsearch with `bin/elasticsearch --enrollment-token <token>`, using the enrollment token that you generated.

启动elasticsearch遇到的问题

1.修改config/elasticsearch.yml后出现如下问题

ERROR: [2] bootstrap checks failed. You must address the points described in the following [2] lines before starting Elasticsearch.
bootstrap check failure [1] of [2]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
bootstrap check failure [2] of [2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
ERROR: Elasticsearch did not exit normally - check the logs at /opt/elasticsearch-8.0.0/logs/elasticsearch.log

解决办法:

sudo su - 
#查看软限制 #查看硬限制ulimit -H -n
ulimit -S -n

ulimit -S -n默认软限制1024,硬限制默认4096,修改**/etc/security/limits.conf**

vim /etc/security/limits.conf

文件末追加:

* soft nofile 65535* hard nofile 65537

退出用户登陆重新登陆

2.启动出现如下问题

ERROR: [1] bootstrap checks failed. You must address the points described in the following [1] lines before starting Elasticsearch.
bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
ERROR: Elasticsearch did not exit normally - check the logs at /opt/elasticsearch-8.0.0/logs/my-application.log

解决办法

vim /etc/sysctl.conf
#文件末追加下面代码
vm.max_map_count = 262145
#执行
sysctl -p

三、安装kibana

官网地址:Download Kibana Free | Get Started Now | Elastic

(一)rpm安装

【地址:】Install Kibana with RPM | Kibana Guide [7.17] | Elastic

wget https://artifacts.elastic.co/downloads/kibana/kibana-7.17.1-x86_64.rpm
shasum -a 512 kibana-7.17.1-x86_64.rpm 
sudo rpm --install kibana-7.17.1-x86_64.rpm

注意同样需要把5601端口防火墙配置允许访问

1.修改配置文件:

[root@localhost elasticsearch]# vim /etc/kibana/kibana.yml

去掉多行配置前面的‘#’,然后执行 grep -Ev "^#" /etc/kibana/kibana.yml

[root@localhost elasticsearch]# grep -Ev "^#" /etc/kibana/kibana.yml
server.port: 5601

server.host: "192.168.2.249"

server.name: "forpastime-kibana"

elasticsearch.hosts: ["http://192.168.2.249:9200"]

kibana.index: ".kibana"

kibana.defaultAppId: "home"

2.启动kibana

[root@localhost elasticsearch]# sudo -i service kibana start

**关闭kibana命令:

[root@localhost elasticsearch]# sudo -i service kibana stop

3.查看5601端口

[root@localhost elasticsearch]# lsof -i:5601
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    6298 kibana   51u  IPv4  87238      0t0  TCP localhost.localdomain:esmagent (LISTEN)

(二)tar.gz安装

获取到地址后下载到服务器解压,修改config/kibana.yml 进行配置

(三)访问kibana控制台

使用devtool

之后即可使用这个工具配合php进行文档和索引增删改查。

遇到的问题及解决方法

1.这里注意我的上面配置文件格式有问题会报以下错误,因为yml文件冒号后面需要有空格

**can not read a block mapping entry; a multiline key may not be an implicit key
**

2.elasticsearch.hosts: ["http://192.168.2.251:9200"\]我写成了

elasticsearch.host: ["http://192.168.2.251:9200"\] 就报错了

3.修改es相关配置时报

“ERROR: Failed to determine the health of the cluster.”

解决办法:先启动es服务

4.生产环境启动kibana后无法5601端口无法访问

修改配置文件项,或者最好的是设置为你需要登陆控制台的公网ip比较保险

server.host: 0.0.0.0

5. 生产环境 Kibana server is not ready yet

elasticsearch.hosts: ["http://内网IP:9200"]

6.生产环境 No alive nodes found in your cluster

$hosts = explode(',',Env::get('elastic.hosts'));

        $loger = new Logger('es_log');
        $logPath = ROOT.'es_log.log';
        if(!file_exists($logPath)){
            fopen($logPath,'a+');
        }

        $loger->pushHandler(new StreamHandler($logPath));

        $serializer = '\Elasticsearch\Serializers\SmartSerializer';

        $this->client = ClientBuilder::create()           // 实例化 ClientBuilder
            ->setLogger($loger)         //日志
            ->setSerializer($serializer)        //序列化
            ->setHosts($hosts)      // 设置主机信息
            ->build();              // 构建客户端对象

->setHosts(hosts)我这里hosts) 我这里hosts)我这里hosts使用了环境配置文件,配置文件内的ip+端口没有改为生成环境

上一篇: nginx 搭配 frp内网穿透配置,微信授权域名调试本地项目

下一篇:PHP elasticsearch get() delete()报错问题