How To Install Elasticsearch on Ubuntu 22.04

In this tutorial, we want to teach you How To Install and Configure Elasticsearch on Ubuntu 22.04. Also, you will learn to configure SSL to your Elasticsearch installation with Nginx reverse proxy on Ubuntu 22.04.

Elasticsearch is a distributed, open-source search and analytics engine built on Apache Lucene and developed in Java.

Elasticsearch allows you to store, search, and analyze huge volumes of data quickly and in near real-time and give back answers in milliseconds. It’s able to achieve fast search responses because instead of searching the text directly, it searches an index.

It uses a structure based on documents instead of tables and schemas and comes with extensive REST APIs for storing and searching the data. At its core, you can think of Elasticsearch as a server that can process JSON requests and give you back JSON data.

Steps To Install and Configure Elasticsearch on Ubuntu 22.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Ubuntu 22.04.

Also, you need a domain name that is pointed to your server’s IP address.

Install ElasticSearch on Ubuntu 22.04

First, you need to update your local package index with the command below:

sudo apt update 

Java is already included with the Elasticsearch package, so you don’t want to install Java manually.

Import Elasticsearch GPG Key

Then, you need to import the Elasticsearch repository’s GPG key by running the command below:

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg

Add Elasticsearch Repository

Now use the command below to add the latest Elasticsearch repository on Ubuntu 22.04. At the current time, the latest release of Elasticsearch is 8.x.

To do this, run the command below:

echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list

Install Elasticsearch

Update your local package index with the following command:

sudo apt update

Then, use the following command to install Elasticsearch on your Ubuntu server:

sudo apt install elasticsearch

When your installation is completed, you will get the following output:

Output
--------------------------- Security autoconfiguration information -------------                                                                                                                                                             -----------------

Authentication and authorization are enabled.
TLS for the transport and HTTP layers is enabled and configured.

The generated password for the elastic built-in superuser is : T1UqrnauAUIRzQqQMgDX                                                                                                                                                   
If this node should join an existing cluster, you can reconfigure this with
'/usr/share/elasticsearch/bin/elasticsearch-reconfigure-node --enrollment-token                                                                                                                                                              <token-here>'
after creating an enrollment token on your existing cluster.

You can complete the following actions at any time:

Reset the password of the elastic built-in superuser with
'/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic'.

Generate an enrollment token for Kibana instances with
 '/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana'.

Generate an enrollment token for Elasticsearch nodes with
'/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node'.

--------------------------------------------------------------------------------                                                                                                                                                             -----------------
...

Note down the password that is generated for the superuser from your output.

Start and Enable Elasticsearch Service

Elasticsearch doesn’t start automatically, so you need to start and enable it to start on boot. To do this, run the following commands:

# sudo systemctl daemon-reload
# sudo systemctl enable elasticsearch.service
# sudo systemctl start elasticsearch.service

Verify that Elasticsearch is active and running on Ubuntu 22.04:

sudo systemctl status elasticsearch.service
Output
● elasticsearch.service - Elasticsearch
     Loaded: loaded (/lib/systemd/system/elasticsearch.service; enabled; vendor>
     Active: active (running) since Tue 2022-11-01 09:55:22 UTC; 10s ago
       Docs: https://www.elastic.co
   Main PID: 3046 (java)
      Tasks: 92 (limit: 4575)
     Memory: 2.3G
        CPU: 1min 16.643s
...

Configure Elasticsearch on Ubuntu 22.04

At this point, you can restrict port 9200 from outside access by editing the elasticsearch.yml file.

Open the file with your favorite text editor, here we use vi:

sudo vi /etc/elasticsearch/elasticsearch.yml 

At the file, uncomment the network.host and replace the value with Internal IP or any IP or localhost.

network.host: INTERNAL_IP

Note: You can also use localhost as the host or any IP address you wish.

When you are done, save and close the file.

Then, restart Elasticsearch on Ubuntu 22.04 to apply the changes:

sudo systemctl restart elasticsearch

Test If Elasticsearch Works Correctly

At this point, you can test your installation by sending an HTTPs request by attaching the certificate using the below command. To do this, run the following command:

Take note of the password you received earlier, you will need to use that while prompted.

# sudo su
# curl --cacert /etc/elasticsearch/certs/http_ca.crt -u elastic https://INTERNAL_IP:9200

You will get the following output:

Enter host password for user 'elastic':

Output
{
  "name" : "your-hostname",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "-kn1oEl9RTm_rFTr15NpMA",
  "version" : {
    "number" : "8.4.3",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "42f05b9372a9a4a470db3b52817899b99a76ee73",
    "build_date" : "2022-10-04T07:17:24.662462378Z",
    "build_snapshot" : false,
    "lucene_version" : "9.3.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

As you can see from the output, your Elaticsearch is working correctly.

Let’s see how to configure SSL to your Elasticsearch installation with Nginx reverse proxy on Ubuntu 22.04.

Configure Nginx For Elasticsearch on Ubuntu 22.04

At this point, you need to install Nginx on your server by using the following command:

sudo apt install nginx

Now you can configure Nginx reverse proxy for your Elasticsearch on Ubuntu 22.04. To do this, follow the steps below.

First, remove the default Nginx configurations by running the commands below:

# sudo rm /etc/nginx/sites-available/default
# sudo rm /etc/nginx/sites-enabled/default

Then, create a new Nginx configuration file with your favorite text editor, here we use vi:

sudo vi /etc/nginx/sites-available/search.conf

Add the following content to the file:

Note: You need to use exact same IP or localhost that you used in the host of Elasticsearch configuration.

server {
     listen [::]:80;
     listen 80;

     server_name your-domain;

location / {
     proxy_pass http://INTERNAL_IP:9200;
     proxy_redirect off;
     proxy_read_timeout    90;
     proxy_connect_timeout 90;
     proxy_set_header  X-Real-IP  $remote_addr;
     proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header  Host $http_host;
} 
}

When you are done, save and close the file.

Next, enable your Nginx configuration on Ubuntu 22.04:

sudo ln -s /etc/nginx/sites-available/search.conf /etc/nginx/sites-enabled/search.conf

Set up Certbot Nginx on Ubuntu 22.04

At this point, you need to install Certbot by Let’s Encrypt for Ubuntu 22.04 by using the command below:

sudo apt install python3-certbot-nginx

Then, receive your SSL certificates by using the command below:

sudo certbot --nginx --agree-tos --no-eff-email --redirect -m youremail@email.com -d domainname.com

When it is finished, you will get the following output:

Output
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/your-domain/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/your-domain/privkey.pem
This certificate expires on 2023-01-30.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for cwp.orcacore.net to /etc/nginx/sites-enabled/search.conf
Congratulations! You have successfully enabled HTTPS on https://your-domain

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Renew SSL Certificates

Certificates provided by Let’s Encrypt are valid for 90 days only, so you need to renew them often. To do this, run the command below:

sudo certbot renew --dry-run
Output
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
  /etc/letsencrypt/live/your-domain/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --

Conclusion

At this point, you have learned to Install and Configure Elasticsearch on Ubuntu 22.04. Also, you have learned to configure SSL to your Elasticsearch installation with Nginx reverse proxy on Ubuntu 22.04.

Hope you enjoy it.

You may be like these articles:

How To Set up ElasticSearch on Centos 7

Install and Configure Elasticsearch on AlmaLinux 8

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!