How to Install Elasticsearch on AlmaLinux 9: A Complete Guide

Elasticsearch is a powerful, distributed search and analytics engine used for a variety of use cases such as full-text search, logging, and monitoring. In this guide, we’ll walk you through the steps to install Elasticsearch on AlmaLinux 9 and demonstrate how to perform basic CRUD (Create, Read, Update, Delete) operations using its RESTful API.

Prerequisites

  • A KVM VPS or dedicated server running AlmaLinux 9
  • Root user access or a normal user with sudo privileges

Step 1: Update Your Server

Before starting, it’s a good practice to update your server to ensure all existing packages are up to date. Open your terminal and run:

sudo dnf update -y

Step 2: Install Elasticsearch

Elasticsearch is not available in the default package repositories, so we’ll need to add the official Elasticsearch repository and install it from there.

1. Import the Elasticsearch GPG Key:

First, import the GPG key to verify the Elasticsearch packages:

sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

If you encounter an error like “warning: Signature not supported. Hash algorithm SHA1 not available,” this is due to SHA-1 being deprecated in Red Hat Enterprise Linux 9 for security reasons. To temporarily allow SHA-1, run:

sudo update-crypto-policies --set DEFAULT:SHA1

Don’t forget to revert this change after installing Elasticsearch:

sudo update-crypto-policies --set DEFAULT

2. Add Elasticsearch Repository:

Create a new repository file for Elasticsearch:

sudo vi /etc/yum.repos.d/elasticsearch.repo

Add the following content to the file:

[elasticsearch]
name=Elasticsearch repository for 8.x packages
baseurl=https://artifacts.elastic.co/packages/8.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=0
autorefresh=1
type=rpm-md

Save and exit the file.

3. Install Elasticsearch:

Now, use the following command to install Elasticsearch:

sudo dnf install --enablerepo=elasticsearch elasticsearch -y

During installation, Elasticsearch will automatically configure security settings, including enabling authentication and generating a superuser password. Make sure to save this password for future use.

Step 3: Configure Elasticsearch

Elasticsearch configuration is managed through several files, including elasticsearch.yml for general settings, jvm.options for Java Virtual Machine settings, and log4j2.properties for logging configurations. For this guide, we’ll focus on modifying the elasticsearch.yml file.

1. Edit the Configuration:

Open the elasticsearch.yml file:

sudo vi /etc/elasticsearch/elasticsearch.yml

Find the line for network.host, uncomment it by removing the #, and set it to localhost:

# ---------------------------------- Network -----------------------------------
#
# By default, Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
network.host: localhost

Save and exit the editor.

2. Start and Enable Elasticsearch Service:

Start the Elasticsearch service and enable it to run on boot:

sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch

Step 4: Test Elasticsearch

To ensure Elasticsearch is running correctly, send a test request using cURL:

curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://localhost:9200

Use the generated password from the installation step when prompted. If successful, you’ll receive a JSON response with details about your Elasticsearch node.

Step 5: Perform CRUD Operations with Elasticsearch

1. Create Data:

Add data to Elasticsearch using the following command:

curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic -X PUT "https://localhost:9200/node/_doc/my-index-000001?pretty" -k -H 'Content-Type: application/json' -d '{"counter" : 1, "tags" : ["Hello World"]}'

2. Read Data:

Retrieve the data using a GET request:

curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic -X GET "https://localhost:9200/node/_doc/my-index-000001?pretty" -k -H 'Content-Type: application/json'

3. Update Data:

Update existing data with:

curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic -X PUT "https://localhost:9200/node/_doc/my-index-000001?pretty" -k -H 'Content-Type: application/json' -d '{"counter" : 1, "tags" : ["Hello HostnExtra"]}'

4. Delete Data:

Finally, delete the data with:

curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic -X DELETE "https://localhost:9200/node/_doc/my-index-000001?pretty" -k -H 'Content-Type: application/json'

Conclusion

You’ve now successfully installed Elasticsearch on AlmaLinux 9 and performed basic CRUD operations using its RESTful API. This setup allows you to start utilizing Elasticsearch for search, analytics, and various other data management tasks in your applications.

Facebook
Pinterest
Twitter
LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *