> ## Documentation Index
> Fetch the complete documentation index at: https://docs.apinizer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Ubuntu Elasticsearch Installation

> Step-by-step guide for installing Elasticsearch 7.9.2 on a server with Ubuntu operating system. Ubuntu 2024.04 LTS operating system is recommended.

## Checks Required Before Starting Installation

<Info>
  **Important for Installation**

  For the installation to be healthy, your servers must have access to the following addresses.

  **For Elasticsearch installation:**

  * [https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.2-linux-x86\_64.tar.gz](https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.2-linux-x86_64.tar.gz)
</Info>

<Info>
  **Important**

  When updating Ubuntu packages, it tries to pull from servers in Turkey location. However, there may be problems with [tr.archive.ubuntu.com](http://tr.archive.ubuntu.com) from time to time. In this case, the following change should be made.

  ```bash theme={null}
  sudo vi /etc/apt/sources.list
  ```

  \#Replace all addresses containing tr. with "Replace All".

  \#Example:

  Old: [http://tr.archive.ubuntu.com/ubuntu](http://tr.archive.ubuntu.com/ubuntu)

  New: [http://archive.ubuntu.com/ubuntu](http://archive.ubuntu.com/ubuntu)
</Info>

## Operating System Configurations

<Tip>
  The following steps must be performed on all servers.
</Tip>

```bash theme={null}
# Elasticsearch user is created and authorized
sudo adduser elasticsearch
sudo usermod -aG sudo elasticsearch

# Switch to the user to continue operations
sudo su - elasticsearch

# It is recommended that the following tools be installed on all servers
sudo apt update
sudo apt install -y curl wget net-tools gnupg2 software-properties-common apt-transport-https ca-certificates

# Firewall is disabled
sudo systemctl stop ufw
sudo systemctl disable ufw

# Swap is disabled and the swap line in /etc/fstab file is deleted to prevent it from restarting
sudo swapoff -a
sudo vi /etc/fstab
# Then close the vi file (:wq)
```

## Elasticsearch Installation

### Operating System Configurations and Elasticsearch Application Installation

<Tip>
  The following steps must be performed on all Elasticsearch servers.
</Tip>

```bash theme={null}
sudo vi /etc/security/limits.conf
```

```
elasticsearch - nofile 65535
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
```

```bash theme={null}
sudo sysctl -w vm.swappiness=1
sudo sysctl -w vm.max_map_count=262144
sudo vi /etc/sysctl.conf
```

```
vm.max_map_count=262144
```

```bash theme={null}
sudo sysctl -p
sudo sysctl vm.max_map_count
```

### Elasticsearch Installation

<Tip>
  The following steps must be performed on all Elasticsearch servers.
</Tip>

```bash theme={null}
sudo mkdir /opt/elasticsearch
cd /opt/elasticsearch
sudo wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.2-linux-x86_64.tar.gz
sudo tar -xzf elasticsearch-7.9.2-linux-x86_64.tar.gz
sudo chown -Rf elasticsearch:elasticsearch /opt/elasticsearch
sudo chmod -Rf 775 /opt/elasticsearch

##At this point, pay attention to where the appropriate disk is mounted or ask system administrators to add the disk to the following path
#The following commands can be used for this check
df -h
lsblk

sudo mkdir /mnt/elastic-data/
sudo mkdir /mnt/elastic-snapdata/
sudo chown -Rf elasticsearch:elasticsearch /mnt/elastic-*
sudo chmod -Rf 775 /mnt/elastic-*
```

### Configuring Elasticsearch Parameters According to Environment

<Tip>
  The following steps must be performed on all Elasticsearch servers.
</Tip>

The following parameters must be added by configuring them according to your own environment.

* cluster.initial\_master\_nodes
* network.host
* node.name

```bash theme={null}
sudo vi /opt/elasticsearch/elasticsearch-7.9.2/config/elasticsearch.yml
```

<Warning>
  **Important**

  Here, path.data and path.repo addresses must be given as the address of the disk where your log file will be stored in the system, in accordance with the previous item.
</Warning>

```yaml theme={null}
cluster.name: ApinizerEsCluster
node.name: "<ELASTICSEARCH_IP_ADDRESS>"
node.master: true
node.data: true
network.host: <ELASTICSEARCH_IP_ADDRESS>
http.port: 9200
cluster.initial_master_nodes: ["<ELASTICSEARCH_IP_ADDRESS>"]
discovery.seed_hosts: []
path.data: /mnt/elastic-data/
path.repo: ["/mnt/elastic-snapdata"]
bootstrap.memory_lock: true
http.cors.enabled : true
http.cors.allow-origin : "*"
http.cors.allow-methods : OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers : X-Requested-With,X-Auth-Token,Content-Type, Content-Length
```

You can configure the JVM (Java Virtual Machine) values and other JVM parameters that Elasticsearch will use as follows.

```bash theme={null}
sudo vi /opt/elasticsearch/elasticsearch-7.9.2/config/jvm.options
```

<Warning>
  **Important**

  Here, you can go up to half of the RAM amount that the operating system has, and this value should not exceed 32GB
</Warning>

```
-Xms8g
-Xmx8g
```

### Setting Elasticsearch as Linux Service

<Tip>
  The following steps must be performed on all Elasticsearch servers.
</Tip>

```bash theme={null}
sudo vi /opt/elasticsearch/elasticsearch-7.9.2/bin/elasticsearch-service.sh
```

```bash theme={null}
#!/bin/sh
SERVICE_NAME=elasticsearch
PATH_TO_APP="/opt/elasticsearch/elasticsearch-7.9.2/bin/$SERVICE_NAME"
PID_PATH_NAME="/opt/elasticsearch/elasticsearch-7.9.2/bin/$SERVICE_NAME.pid"
SCRIPTNAME=elasticsearch-service.sh
ES_USER=$SERVICE_NAME
ES_GROUP=$SERVICE_NAME

case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            mkdir $(dirname $PID_PATH_NAME) > /dev/null 2>&1 || true
            chown $ES_USER $(dirname $PID_PATH_NAME)
            $SUDO $PATH_TO_APP -d -p $PID_PATH_NAME
            echo "Return code: $?"
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ..."
            kill -15 $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill -15 $PID;
            sleep 1;
            echo "$SERVICE_NAME stopped ...";
            rm -rf $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            mkdir $(dirname $PID_PATH_NAME) > /dev/null 2>&1 || true
            chown $ES_USER $(dirname $PID_PATH_NAME)
            $SUDO $PATH_TO_APP -d -p $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
        exit 3
    ;;
esac
```

```bash theme={null}
sudo chmod -Rf 775 /opt/elasticsearch/elasticsearch-7.9.2/*
sudo vi /etc/systemd/system/elasticsearch.service
```

```ini theme={null}
[Unit]
Description=ElasticSearch Server
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
Type=forking
ExecStart=/opt/elasticsearch/elasticsearch-7.9.2/bin/elasticsearch-service.sh start
ExecStop=/opt/elasticsearch/elasticsearch-7.9.2/bin/elasticsearch-service.sh stop
ExecReload=/opt/elasticsearch/elasticsearch-7.9.2/bin/elasticsearch-service.sh restart
LimitNOFILE=65536
LimitMEMLOCK=infinity
User=elasticsearch
```

```bash theme={null}
sudo systemctl daemon-reload
sudo systemctl start elasticsearch
sudo systemctl status elasticsearch
sudo systemctl enable elasticsearch
```

You can use the following link for the compatible Kibana version.

```
https://www.elastic.co/downloads/past-releases/kibana-oss-7-9-2
```
