Introduction
At the present time Elasticsearch is a powerful search and analytics engine that can help you extract valuable insights from your data. Installing and configuring Elasticsearch on your Ubuntu Server 22.04 can be a game-changer for data analysis and search functionality. For that reason this easy-to-follow step-by-step guide, we will walk you through the process of how to install and configure Elasticsearch on Ubuntu Server 22.04. Lastly of this tutorial, you’ll have Elasticsearch up and running smoothly on your Ubuntu Server.
Table of Contents
- Introduction
- Why Elasticsearch on Ubuntu Server 22.04?
- Prerequisites
- Install Elasticsearch on Ubuntu Server 22.04
- Step 1: Update the System on Ubuntu 22.04
- Step 2: Install Java
- Step 3: Add the Elasticsearch Repository
- Step 4: Install Elasticsearch
- Step 5: Configure Elasticsearch
- Step 6: Start and Enable Elasticsearch
- Step 7: Test Elasticsearch
- Step 8: Configure Firewall for Elasticsearch
- Step 9: Install Kibana (Optional)
- Conclusion
Why Elasticsearch on Ubuntu Server 22.04?
Before we dive into the installation and configuration, let’s briefly discuss why Elasticsearch on Ubuntu Server 22.04 is a great choice. Given that Ubuntu Server 22.04 is a stable and secure operating system, making it an ideal platform for hosting Elasticsearch and ensuring a reliable search engine for your applications.
Prerequisites
Important to realize this guide assumes that you are using Ubuntu Server 22.04. Before we dive into install and configure Elasticsearch on Ubuntu Server 22.04 there are prerequisite you need to ensure are in place:
- Before you begin, you should have a non-root user account with sudo privileges set up on your system. You can learn how to do this by following the Initial Server Setup Guide with Ubuntu Server 22.04 tutorial.
- We will work with the minimum amount of CPU and RAM required to run Elasticsearch. Note that the amount of CPU, RAM, and storage that your Elasticsearch server will require depends on the volume of logs that you expect.
Install Elasticsearch on Ubuntu Server 22.04
Here’s a simple guide on how to install and configure Elasticsearch on Ubuntu Server 22.04. By following these steps, you can tap into the advantages it offers:
Step 1: Update the System on Ubuntu 22.04
To ensure that your Ubuntu Server is up to date, start by running the following commands in your terminal:
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install apt-transport-https ca-certificates wget
Additionally, these commands will update your package lists and upgrade installed packages to their latest versions.
Step 2: Install Java
Elasticsearch is built on Java, so you need to have Java installed on your server. You can install OpenJDK 11, a popular choice, with the following command:
$ sudo apt install -y openjdk-11-jre-headless
Furthermore, this command installs the Java Runtime Environment (JRE) without unnecessary graphical components.
Step 3: Add the Elasticsearch Repository
Basically The Elasticsearch components are not available in Ubuntu’s default package repositories. However, you can install them with APT after adding Elastic’s package source list.
Moreover, all packages are signed with the Elasticsearch signing key, providing an added layer of security to safeguard your system against package spoofing. Once authenticated using the key, your package manager will trust these packages. Use the following command:
$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
Furthermore, the above command should yield an “OK” output, signifying the successful import of the key.
Because of this, you can consider packages from this repository to be reliable and dependable.
Then, we need to add the Elastic APT repository. Since the lsb_release
-cs sub-command will return the name of your Ubuntu distribution, the command will add the correct repository:
$ 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
The output will be similar to this, for instance:
deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main
Step 4: Install Elasticsearch
Now that the repository has been added, you can proceed to install Elasticsearch. To do this, update the package lists once more.
$ sudo apt update
Then install Elasticsearch:
$ sudo apt install elasticsearch
The output will be similar to this:
$ sudo apt install elasticsearch
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
elasticsearch
0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
Need to get 606 MB of archives.
After this operation, 1,246 MB of additional disk space will be used.
Get:1 https://artifacts.elastic.co/packages/8.x/apt stable/main amd64 elasticsearch amd64 8.9.0 [606 MB]
Fetched 606 MB in 1min 42s (5,964 kB/s)
Selecting previously unselected package elasticsearch.
(Reading database ... 86173 files and directories currently installed.)
Preparing to unpack .../elasticsearch_8.9.0_amd64.deb ...
Creating elasticsearch group... OK
Creating elasticsearch user... OK
Unpacking elasticsearch (8.9.0) ...
Setting up elasticsearch (8.9.0) ...
-------------------------------------------------------------------------------------------------
### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
### You can start elasticsearch service by executing
sudo systemctl start elasticsearch.service
Scanning processes...
Scanning candidates...
Scanning linux images...
Step 5: Configure Elasticsearch
Before we start Elasticsearch for the first time, we need to configure a few things. Open the Elasticsearch configuration, file is located at /etc/elasticsearch/elasticsearch.yml
.
$ sudo vi /etc/elasticsearch/elasticsearch.yml
Adjust the following configurations to enhance security and performance:
Set the cluster name to identify your Elasticsearch cluster:
cluster.name: my-elasticsearch-cluster
After that configure the network host to allow connections from localhost:
network.host: 172.32.1.221
Then save and close the configuration file.
Step 6: Start and Enable Elasticsearch
Once you complete the installation, initiate and enable the Elasticsearch service.
$ sudo systemctl enable elasticsearch
$ sudo systemctl start elasticsearch
To check the status of the service, if it is up and running, execute the following command:
$ sudo systemctl status elasticsearch
● elasticsearch.service - Elasticsearch
Loaded: loaded (/lib/systemd/system/elasticsearch.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2023-08-03 20:05:01 WIB; 11s ago
Docs: https://www.elastic.co
Main PID: 44806 (java)
Tasks: 83 (limit: 4556)
Memory: 2.3G
CPU: 47.342s
CGroup: /system.slice/elasticsearch.service
├─44806 /usr/share/elasticsearch/jdk/bin/java -Xms4m -Xmx64m -XX:+UseSerialGC -Dcli.name=server -Dcli.script=/usr/share/elasticsearch/bin/elasticsearch -Dcli.libs=lib/tools/server-cli -Des.path.hom>
├─44864 /usr/share/elasticsearch/jdk/bin/java -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -Djava.security.manager=allow -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless>
└─44888 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller
Aug 03 20:04:32 elasticsearch systemd[1]: Starting Elasticsearch...
Aug 03 20:05:01 elasticsearch systemd[1]: Started Elasticsearch.
Step 7: Test Elasticsearch
To verify the operational status of Elasticsearch, you can use the “curl” command. This command dispatches an HTTP request to port 9200 on either localhost or your IP address.
$ curl -X GET "172.32.1.221:9200/"
If the setup is correct, then your Elasticsearch cluster will generate a JSON response containing relevant information.
{
"name" : "elasticsearch",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "3JIbJ_6tRv2ru3Br-VOqPQ",
"version" : {
"number" : "8.9.0",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "8aa461beb06aa0417a231c345a1b8c38fb498a0d",
"build_date" : "2023-07-19T14:43:58.555259655Z",
"build_snapshot" : false,
"lucene_version" : "9.7.0",
"minimum_wire_compatibility_version" : "7.17.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "You Know, for Search"
}
Step 8: Configure Firewall for Elasticsearch
Elasticsearch data finds its storage within the /var/lib/elasticsearch
directory. Additionally, configuration files are positioned at /etc/elasticsearch
, while Java start-up options can be tailored through the /etc/default/elasticsearch
file.
By default, Elasticsearch is configured to listen on localhost only. If the client connecting to the database is also running on the same host and you are setting up a single node cluster, you don’t need to change the default configuration file.
By default, Elasticsearch doesn’t implement authentication, which means it grants access to anyone who can reach the HTTP API. Therefore, to enable remote access to your Elasticsearch server, you’ll need to configure your firewall and open TCP port 9200.
Usually, you’d prefer to grant access to the Redis server exclusively from a specific IP address or IP range. For instance, to permit connections solely from the 172.32.1.0/24 subnet, you would execute the following command:
$ sudo ufw allow from 1172.32.1.0/24 to any port 9200
$ sudo ufw reload
Once the firewall is configured, the next step is to edit the Elasticsearch configuration and allow Elasticsearch to listen for external connections.
To do so, open the elasticsearch.yml
configuration file:
$ sudo vi /etc/elasticsearch/elasticsearch.yml
Search for the line that contains network.host
, and change the value to 0.0.0.0
:
network.host: 0.0.0.0
If you have multiple network interfaces on your machine, you can specify the interface IP address. This will ensure identically that Elasticsearch listens exclusively on the provided interface.
Then restart the Elasticsearch service for the changes to take effect:
$ sudo systemctl restart elasticsearch
That’s it. You can now connect to the Elasticsearch server from your remote location.
Step 9: Install Kibana (Optional)
Kibana, a data visualization tool, pairs well with Elasticsearch. To install Kibana, follow these steps:
Add Kibana repository:
$ sudo sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'
Install Kibana:
$ sudo apt update
$ sudo apt install kibana
Configure Kibana, open the Kibana configuration file:
$ sudo vi /etc/kibana/kibana.yml
#Set the server host to localhost:
server.host: "localhost"
Start and enable Kibana:
$ sudo systemctl start kibana
$ sudo systemctl enable kibana
Conclusion
Finally Congratulations! You’ve successfully installed and configured Elasticsearch on your Ubuntu Server 22.04. Elasticsearch is now ready to index and search your data, making it a valuable tool for data analysis and search functionality in your applications.
In this guide, we covered the essential steps for installing and configuring Elasticsearch, including updating your system, installing Java, downloading and installing Elasticsearch, starting and enabling Elasticsearch, configuring Elasticsearch, testing the installation, and optionally installing Kibana and plugins.
With Elasticsearch at your disposal, you have a powerful tool to enhance your data-driven applications and gain valuable insights from your data. Happy searching and analyzing!
To learn more about Elasticsearch, visit the official documentation page.
Also Read Our Other Guides :
- How To Install and Configure Go (Golang) on Ubuntu 22.04
- How To Install and Configure NFS Server Client on Ubuntu 22.04
- How To Install Nextcloud with Apache and MariaDB on Ubuntu Server 22.04
- How To Install Composer in Ubuntu 22.04: A Comprehensive Guide
As I have shown, now you have learned how to install and configure Elasticsearch on Ubuntu 22.04.