How to install Elasticsearch 7 in Ubuntu 20.04

How to install Elasticsearch 7 in Ubuntu 20.04

Elasticsearch is a search engine based on the Lucene library that allows you to store, search and analyze huge volumes of data quickly and in near real-time, providing you with results in just milliseconds. It's used widely on multiple frameworks and e-commerce platforms, as it's a great tool for document indexing and powerful full-text search. In case that you want to use Elasticsearch on some project hosted on your server, you need to know how to install it.

In this article, I will explain to you how to easily install Elasticsearch 7 on your Ubuntu 20.04 server.

1. Register Elasticsearch repository in Ubuntu

The best way to handle software on your server is through the Advanced Package Tool of Ubuntu. You need to register the Elasticsearch package in your repositories so you can install it with a single command. Start by downloading the Elasticsearch GPG Key and registering in your system, you can do this easily with curl:

curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

After registering the key, be sure to add the Elasticsearch repository source in the sources list of your system with the following command:

echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

This will allow you to install Elasticsearch as a package in the next step.

2. Install Elasticsearch

Now, proceed to download package information from all the configured sources:

sudo apt update

And finally, install Elasticsearch using the following command:

sudo apt install elasticsearch

The package's installation will take about 326MB of space in your disk, once it finishes you will be able to configure it and use it on your server.

3. Configure Elasticsearch

By default, Elasticsearch settings are reasonable for everyone. It is only available and accessible at localhost, restricting external access to your instance, preventing it from reading your information, or even disabling it. If you ever need to configure something special on Elasticsearch, you may simply modify the configuration file elasticsearch.yml using a CLI editor like nano:

sudo nano /etc/elasticsearch/elasticsearch.yml

If you already configured what you needed or want to use the default configuration, you may now simply start the Elasticsearch service in your server using the following command:

sudo systemctl start elasticsearch

As usual, you may need the service to start automatically when the server starts, so enable the service with the following command:

sudo systemctl enable elasticsearch

And that's it for now, all you need to do now is to test if the instance is working.

4. Allowing access from another server within the local network (optional)

As I mentioned previously, Elasticsearch is only accessible at localhost. For example, if you request information to Elasticsearch from the same server at https://localhost:9200, it will send the response. However, if you access the Elasticsearch instance hosted at 192.168.20.45 from another server in the same local network e.g 192.168.20.50, then the request will fail. If you want to allow access to the Elasticsearch instance from another server, you need to follow some extra steps.

You need to define from which scopes should Elasticsearch be available. Edit the configuration file with the following command:

sudo nano /etc/elasticsearch/elasticsearch.yml

In case that you want to allow access to Elasticsearch from localhost and computers in the same network, define the following properties:

# Allows access from both localhost and all the computers from the local network 192.168.xx
network.host: [ localhost, _site_ ]
discovery.seed_hosts: [ localhost, _site_ ]

Now restart the Elasticsearch service:

sudo systemctl restart elasticsearch

And you should be able now to request information to the Elasticsearch server from computers that are connected to the same network.

5. Allowing access from other servers outside of the network (optional, be careful here)

Note: only run any of the commands of this step if you understand how the firewall works or you have the firewall already functional.

If your Elasticsearch instance needs to be accessed from servers outside of the network that is currently connected to, you may need to expose it to any globally scoped addresses on the system. In that case, edit the configuration file with the following command:

sudo nano /etc/elasticsearch/elasticsearch.yml

And define the following properties:

# Allows access from both localhost and any other globally-scoped addresses on the system, for example, 8.8.8.8
network.host: [ localhost, _global_ ]
discovery.seed_hosts: [ localhost, _global_ ]

This will expose Elasticsearch to be accessible by anyone everywhere. This isn't probably what you want as it may be quite insecure as anyone could read your data and even shut down the instance of Elasticsearch through the REST API. That's why it's recommended to allow access from another server through the firewall of Ubuntu. Before enabling the firewall in case that it isn't active, be sure to include the default access rules in your server:

# Important: if it's the first time configuring the firewall and you have something hosted on the server
# Don't forget to allow access to the default ports of HTTP and HTTPS
sudo ufw allow 80
sudo ufw allow 443
# As well the SSH port, otherwise you won't be able to access your own server through SSH
sudo ufw allow 22

Then, include the IP of the server that will have the rights to access Elasticsearch in port 9200 with the following command (replace <ip-of-the-server-e.g-192.168.20.50> with the IP of the server that will access Elasticsearch):

sudo ufw allow from <ip-of-the-server-e.g-192.168.20.50> to any port 9200

Enable the firewall if it isn't enabled with the following command:

sudo ufw enable

You can then check the status of the firewall with the following command:

sudo ufw status

Which should output something like this:

Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere
80                         ALLOW       Anywhere
443                        ALLOW       Anywhere
9200                       ALLOW       192.168.20.24
22 (v6)                    ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)

Now the rule allows the mentioned IP (in my case 192.168.20.24) to request information from Elasticsearch as the firewall allows it. This will work as well with IPs outside of the network.

6. Testing Elasticsearch

In order to test if the Elasticsearch server is working properly, is running a request to port 9200. You can do this easily with cURL:

curl -X GET 'http://localhost:9200'

Which should return a JSON response like the following one:

{
  "name" : "ourcodeworld-server",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "wurK1k8iRO6Gpuq68ooM1g",
  "version" : {
    "number" : "7.12.1",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "3186837139b9c6b6d23c3200870651f10d3343b7",
    "build_date" : "2021-04-20T20:56:39.040728659Z",
    "build_snapshot" : false,
    "lucene_version" : "8.8.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

With this we verified that the instance is fully functional, so you may now interact with the Elasticsearch instance with your application to populate data and search for it.

Happy coding ❤️!

This could interest you

Become a more social person