Install Elasticsearch on Ubuntu 22.04 | Secure Elasticsearch

In this tutorial on the Orcacore website, we want to teach you How To Install 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.

1. 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 -y

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

Install Elasticsearch on Ubuntu 22.04

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
...

2. 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 like Vi Editor or Nano Editor:

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:

Test If Elasticsearch Works Correctly

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.

3. 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 -y

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

4. Set up Certbot Nginx for Elasticsearch

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 -y

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:

Set up Certbot Nginx for Elasticsearch

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
Renew SSL Certificates

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. Please subscribe to us on Facebook and YouTube.

You may also like these articles:

How To Set up ElasticSearch on Centos 7

Install and Configure Elasticsearch on AlmaLinux 8

Elasticsearch Installation on AlmaLinux 9

Set up Elasticsearch on Debian 12 Bookworm

Share your love

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!