# วิธีการติดตั้ง Elasticsearch บน Ubuntu 18.04

## Step1 - Prerequsities

ก่อนเริ่มติดตั้งคุณจะต้องเข้าสู่ระบบในฐานะผู้ใช้ที่มีสิทธิ์ sudo ได้เพื่อให้สามารถติดตั้งแพ็คเกจในระบบ Ubuntu ได้&#x20;

* เริ่มต้นโดยการอัพเดต index ของ packageและการติดตั้ง package apt-transport-https ที่ใช้การเข้าถึงข้อมูลผ่าน HTTPS:

```yaml
sudo apt update
sudo apt install apt-transport-https
```

* Elasticsearch ต้องการ Java SDK 8 ในการติดตั้ง ตรวจสอบให้แน่ใจว่าระบบของคุณติดตั้ง Java โดยใช้คำสั่งต่อไปนี้ คำสั่งนี้จะแสดงเวอร์ชัน Java ปัจจุบันที่ใช้งานอยู่

```bash
java -version
```

{% tabs %}
{% tab title="Console Output" %}

```bash
java version "11.0.2" 2019-01-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)
```

{% endtab %}
{% endtabs %}

{% hint style="danger" %}
หากไม่ได้ติดตั้ง Java จะเห็นผลลัพธ์ต่อไปนี้:

```
Command 'java' not found, but can be installed with:

apt install default-jre
apt install openjdk-11-jre-headless
apt install openjdk-8-jre-headless
apt install openjdk-9-jre-headless
```

{% endhint %}

* ตรวจสอบ PATH ของ JAVA\_HOME โดยใช้คำสั่ง:

```bash
echo $JAVA_HOME
```

{% tabs %}
{% tab title="Console Output" %}

```bash
/usr/lib/jvm/java-11-oracle
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
ถ้าตรวจสอบแล้ว java ของคุณไม่ถูกติดตั้งอยู่ให้รันคำสั่งด้านล่างนี้ ถ้าติดตั้งอยู่แล้วให้ข้ามไป Step 2 ได้เลย
{% endhint %}

### Step1.1 - Install JAVA

* ติดตั้ง OpenJDK 8 โดยใช้คำสั่ง:

```yaml
sudo apt install openjdk-8-jdk
```

* ตรวจสอบ JAVA อีกครั้ง

```bash
java -version
```

{% tabs %}
{% tab title="Console Output" %}

```bash
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
```

{% endtab %}
{% endtabs %}

## Step 2 – Install Elasticsearch on Ubuntu

Elasticsearch สามารถติดตั้งผ่าน apt ได้ สามารถใช้คำสั่งด้านล่างเพื่อติดตั้ง Elasticsearch บน Ubuntu หลังจากติดตั้ง Package ด้านล่างและนำเข้า Key GPG สำหรับ Elasticsearch

```bash
sudo apt-get install apt-transport-https
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
```

```bash
add-apt-repository "deb https://artifacts.elastic.co/packages/6.x/apt stable main"
```

* อัพเดต index และติดตั้งแพ็คเกจ Elasticsearch โดยใช้คำสั่ง:

```bash
sudo apt-get update
sudo apt-get install elasticsearch
```

## Step 3 – Configure Elasticsearch

หลังจาก Elasticsearch ติดตั้งแล้ว สามารถปรับแต่งสิ่งนี้ได้โดยแก้ไขไฟล์กำหนดค่า Elasticsearch แก้ไขไฟล์กำหนดค่าในโปรแกรมแก้ไขข้อความที่คุณโปรดปรานและอัปเดต:

```bash
sudo vim /etc/elasticsearch/elasticsearch.yml
```

{% code title="/etc/elasticsearch/elasticsearch.yml" %}

```bash
 network.host: 0.0.0.0
 cluster.name: Cluster01
 node.name: "myNodeTest"
```

{% endcode %}

* **network.host –** ตั้งค่าเป็น 0.0.0.0 เพื่อใช้ Interface ทั้งหมดและทำให้เป็น Public (สามารถใช้ได้แค่ IP ภายใน วง LAN ของเพื่อเข้าถึงเท่านั้น)
* **cluster.name –** ชื่อของ Cluster (สำหรับ Node ที่ต้องการให้อยู่ใน Cluster เดียวกัน ทุก Node ต้องใช้ชื่อ Cluster เดียวกัน)
* **node.name –** ชื่อของ Node ที่จะแสดงใน Cluster

## Step 4 – Launch Elasticsearch

ต้องการให้ Elasticsearch ให้เริ่มต้นโดยอัตโนมัติเมื่อระบบบูทขึ้นให้รันคำสั่งต่อไปนี้:

```bash
sudo systemctl enable elasticsearch.service
```

ต้องการ Start Elasticsearchให้รันคำสั่งต่อไปนี้:

```
sudo systemctl start elasticsearch.service
```

ต้องการ Stop Elasticsearch ให้รันคำสั่งต่อไปนี้:

```bash
sudo systemctl stop elasticsearch.service
```

ต้องการ Restart Elasticsearch ให้รันคำสั่งต่อไปนี้:

```
sudo systemctl restart elasticsearch.service
```

ต้องการเช็คสถานะของ Elasticsearch ให้รันคำสั่งต่อไปนี้:

```
sudo systemctl status elasticsearch.service
```

## Step 5 – Test Setup

หลังจากติดตั้ง Elasticsearch สามารถทดสอบโดยใช้คำสั่ง curl เรียกแบบGET เพื่อตรวจสอบการตั้งค่า Elasticsearch จะแสดงรายละเอียดของ Cluster และ Version ของ Elasticsearch&#x20;

```bash
curl -X GET "http://localhost:9200/?pretty"
```

{% code title="Console Output" %}

```yaml
{
  "name": "raiANra",
  "cluster_name": "iLog-demo",
  "cluster_uuid": "4DBdz2wx22133Ch6r123zrads6g",
  "version": {
    "number": "6.7.0",
    "build_flavor": "default",
    "build_type": "docker",
    "build_hash": "8453f77",
    "build_date": "2019-03-21T15:32:29.844721Z",
    "build_snapshot": false,
    "lucene_version": "7.7.0",
    "minimum_wire_compatibility_version": "5.6.0",
    "minimum_index_compatibility_version": "5.0.0"
  },
  "tagline": "You Know, for Search"
}
```

{% endcode %}

{% hint style="info" %}
Credit : <https://tecadmin.net/setup-elasticsearch-on-ubuntu/>
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ilog.ai/elasticsearch/elasticsearch-ubuntu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
