Install Elasticsearch on CentOS 6.7

Published on Tuesday, 29 December 2015

How to install Elasticsearch on CentOS 6.7. Elasticsearch is a highly scalable open-source full-text search and analytics engine. It allows you to store, search, and analyze big volumes of data quickly and in near real time. In this article we'll go over the steps to install Elasticsearch on CentOS 6.7.

Elasticsearch on CentOS

Elasticsearch is a search server based on Lucene. It is developed in Java and is released as open source under the terms of the Apache License. It is generally used as the underlying engine/technology that powers applications that have complex search features and requirements.

Nowadays you see more and more WooCommerce shops improving their searches with Elasticsearch. There is a great article about this on Pressjitsu, in which they utilize the 10up/ElasticPress WordPress plugin.

Elasticsearch supports RESTful operations. This means that you can use HTTP methods (GET, POST, PUT, DELETE, etc.) in combination with an HTTP URI (/collection/entry) to manipulate your data. The intuitive RESTful approach is both developer and user friendly.

To install Elasticsearch on CentOS, we only need a few commands. I installed Elasticsearch a little while ago, therefore the version used in this article (1.7.2) is older than the current stable: 2.1.1.

Install Java
Since Elasticsearch is developed in Java, we need to have Java installed:

sudo yum install java-1.7.0-openjdk.x86_64

Download Elasticsearch RPM

wget https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.2.noarch.rpm

For Elasticsearch 2.1.1:

wget https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/rpm/elasticsearch/2.1.1/elasticsearch-2.1.1.rpm

See the repository guide for how to add and install Elasticsearch through the repository.

Yum install the package

sudo yum install elasticsearch-1.7.2.noarch.rpm

For Elasticsearch 2.1.1:

sudo yum install elasticsearch-2.1.1.rpm

Activate Elasticsearch as a service

sudo chkconfig --add elasticsearchsudo chkconfig elasticsearch on

Start Elasticsearch

sudo service elasticsearch start

Configuring Elasticsearch

Installing Elasticsearch wasn't that hard and now we need to configure some of its parameters. Per default, you find its configuration in /etc/elasticsearch/, and elasticsearch.yml is where you configure the server settings.

I installed Elasticsearch once, on a single server as a test. Therefore I left almost everything untouched.

Replace

# network.host: 192.168.0.1

with the IP address you want your Elasticsearch server to listen on, network.host: localhost, or network.host: 127.0.0.1 for example. Note that this parameter sets both the bind_host and publish_host configuration settings.

Restart Elasticsearch after making the changes:

sudo service elasticsearch restart

Testing and using Elasticsearch

Testing your newly installed Elasticsearch server is as easy as providing an HTTP GET verb to your server:

curl -X GET 'http://localhost:9200'{  "status" : 200,  "name" : "Perfection",  "cluster_name" : "elasticsearch",  "version" : {    "number" : "1.7.2",    "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",    "build_timestamp" : "2015-09-14T09:49:53Z",    "build_snapshot" : false,    "lucene_version" : "4.10.4"  },  "tagline" : "You Know, for Search"}

Everything works :) and now you can create an index using the HTTP PUT verb (straight from the documentation):

curl -XPUT 'http://localhost:9200/twitter/' -d '{    "settings" : {        "index" : {            "number_of_shards" : 3,            "number_of_replicas" : 2        }    }}'

Elasticsearch then confirms the creation:

{"acknowledged":true}

The GET result is:

curl -X GET 'http://localhost:9200'/twitter/{  "twitter" : {     "aliases" : {},    "mappings" : {},    "settings" : {      "index" : {        "creation_date" : "1451379831681",        "uuid" : "hHXeCbp-TcGwqa1TSqif5w",        "number_of_replicas" : "2",        "number_of_shards" : "3",        "version" : {          "created" : "1070299"        }      }    },    "warmers" : {}  }}

Elasticsearch in WordPress

10up/ElasticPress logo

10up/ElasticPress logo

A number of WordPress plugins for Elasticsearch exist, the earlier mentioned 10up/ElasticPress. There is a Tuts+ article that addresses the Fantastic ElasticSearch plugin, but this plugin isn't updated in a couple of years.

This all eases adding Elasticsearch to your WordPress website.

Securing Elasticsearch

for shared hosting, multiple websites and indexes...

One of my main concerns is: security in Elasticsearch. I work for a hosting company offering shared web hosting, and nearly everything we set up needs to be able to be used by more than one client. I haven't found a way to secure Elasticsearch for more than one client / website / index yet, have you?

Per default, Elasticsearch creates an index for every index name it receives, and since all WordPress Elasticsearch plugins automatically create an index called "wordpress", you understand my concerns. So one important security option is to disable the automatic creation of indices.

The automatic, or dynamic creation of indices is controlled by the index.mapper.dynamic parameter in elasticsearch.yml. When set to true (its default), dynamic creation is allowed, so we need to set this to false. Open up /etc/elasticsearch/elasticsearch.yml in your favorite editor and change

index.mapper.dynamic: true

to

index.mapper.dynamic: false

An index is no longer automatically created, meaning you have to create it manually using the above PUT HTTP verb, for example: curl -XPUT 'http://127.0.0.1:9200/[index_name]/'.

However, for as far as I know, you need to recreate the entire WordPress database scheme in Elasticsearch manually... Whoops. See the post Mapping WordPress Posts to Elasticsearch on gibrown.com for more information on doing that.

Another important security measurement in Elasticsearch is HTTP basic authentication.

Elasticsearch 6.3.2 on CentOS 7.5 with Java-1.8.0-openjdk

Update 18-08-2018: In a nutshell we can use the following commands to install Elasticsearch 6.3.2 on CentOS 7.5 with Java-1.8.0-openjdk. Most of the other information in this article remains the same.

sudo yum install java-1.8.0-openjdk.x86_64sudo rpm -import https://artifacts.elastic.co/GPG-KEY-elasticsearchsudo vim /etc/yum.repos.d/elasticsearch.repo	[elasticsearch-6.x]	name=Elasticsearch repository for 6.x packages	baseurl=https://artifacts.elastic.co/packages/6.x/yum	gpgcheck=1	gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch	enabled=1	autorefresh=1	type=rpm-mdsudo yum install elasticsearchsudo systemctl enable elasticsearch.servicesudo systemctl start elasticsearch.service

Conclusion running Elasticsearch on CentOS

Honestly, Elasticsearch is still new to me. I haven't gone through the entire configuration thoroughly and really need to search for, and read-up on, securing Elasticsearch for shared web hosting. The Elasticsearch installation on CentOS is quite simple and straightforward.

In this article I used an older version of CentOS (6.7), and soon I'll redo these steps on installing a current version Elasticsearch on CentOS 7 and Java 8.

During testing with one index for one website, it speeds up WordPress searches drastically. Which is great! WordPress plugins also offer "and","or" options to the search, allowing you to search for a word in multiple categories except some other categories.

If you have some advice on securing and using Elasticsearch in the kind of environment I mentioned, please post your information, knowhow and links / articles as a comment below. I'd really appreciate that. I think HTTP basic authentication, elasticsearch-jetty and Elasticsearch behind a nginx reverse proxy are pretty good options.

You may also want to install Varnish Cache on CentOS, to further optimize your website content delivery.