How to Install and Use Elasticsearch on Ubuntu 20.04
Elasticsearch is an open-source distributed analytics engine built on Apache Lucene. It takes unstructured data from different locations and stores it according to user-specified mapping and indexes it. It supports RESTful operations and allows you to search and analyze huge volumes of data in real time.
In this tutorial, we will show you how to install Elasticsearch on Ubuntu 20.04.
Prerequisites
- A server running Ubuntu 20.04 server.
- A root password is configured on the server.
Getting Started
Before starting, you will need to update your system to the latest version. You can update it with the following command:
apt-get update -y
Once your system is updated, install other required packages with the following command:
apt-get install curl gnupg2 apt-transport-https unzip -y
Once all the packages are installed, you can proceed to the next step.
Install Elasticsearch
By default, the Elasticsearch package is not available in the Ubuntu default repository. So you will need to add the Elasticsearch repository to your system. First, import the GPG key with the following command:
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | apt-key add -
Next, add the Elasticsearch repository with the following command:
sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'
Once the repository is added, update the repository cache and install the Elasticsearch with the following command:
apt-get update -y
apt-get install elasticsearch -y
Once the installation is finished, start the Elasticsearch service and enable it to start at system reboot:
systemctl start elasticsearch
systemctl enable elasticsearch
You can now verify the status of Elasticsearch service with the following command:
systemctl status elasticsearch
You should get the following output:
? elasticsearch.service - Elasticsearch Loaded: loaded (/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled) Active: active (running) since Sat 2021-01-09 11:13:09 UTC; 5s ago Docs: https://www.elastic.co Main PID: 5110 (java) Tasks: 65 (limit: 2353) Memory: 1.2G CGroup: /system.slice/elasticsearch.service ??5110 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl> ??5304 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller Jan 09 11:12:50 ubuntu2004 systemd[1]: Starting Elasticsearch... Jan 09 11:13:09 ubuntu2004 systemd[1]: Started Elasticsearch.
Verify Elasticsearch
By default, Elasticsearch listens on port 9200. You can verify it using the following command:
ss -antpl | grep 9200
You should get the following output:
LISTEN 0 4096 [::ffff:127.0.0.1]:9200 *:* users:(("java",pid=5110,fd=257)) LISTEN 0 4096 [::1]:9200 [::]:* users:(("java",pid=5110,fd=255))
You can also verify the Elasticsearch using the command below:
curl -X GET "localhost:9200/"
You should get the following response:
{ "name" : "ubuntu2004", "cluster_name" : "elasticsearch", "cluster_uuid" : "mToloP2UQGilY7nUCeBnjg", "version" : { "number" : "7.10.1", "build_flavor" : "default", "build_type" : "deb", "build_hash" : "1c34507e66d7db1211f66f3513706fdf548736aa", "build_date" : "2020-12-05T01:00:33.671820Z", "build_snapshot" : false, "lucene_version" : "8.7.0", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
Configure Elasticsearch
Elasticsearch main configuration file is located at /etc/elasticsearch/elasticsearch.yml. You can set your cluster name, port and allow remote connection by editing this file.
Open the elasticsearch.yml file in your nano editor:
nano /etc/elasticsearch/elasticsearch.yml
Change the following lines:
cluster.name: my-cluster network.host: 172.16.0.10 discovery.seed_hosts: 172.16.0.10
Save and close the file then restart the Elasticsearch service to apply the configuration:
systemctl restart elasticsearch
At this point, Elasticsearch is configured to accept the connection from the remote host. You can now proceed to the next step.
Configure UFW Firewall
By default, UFW is installed in Ubuntu 20.04. If not installed, you can install it with the following command:
apt-get install ufw -y
Once the UFW is installed, allow SSH connection and Elasticsearch port for the remote host (172.16.0.100) with the following command:
ufw allow ssh
ufw allow from 172.16.0.100 to any port 9200
Next, enable the UFW firewall with the following command:
ufw enable
Next, verify the UFW firewall rules status with the following command:
ufw status
You should get the following output:
Status: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere 9200 ALLOW 172.16.0.100 22/tcp (v6) ALLOW Anywhere (v6)
Conclusion
Congratulations! you have successfully installed Elasticsearch and configured it for remote connection on Ubuntu 20.04 server. You can now integrate Elasticsearch with your application. Feel free to ask me if you have any questions.