Skip to content

Instantly share code, notes, and snippets.

@meSingh
Last active September 29, 2016 23:23
Show Gist options
  • Save meSingh/9daeb09baef24358e99eb07032688a5b to your computer and use it in GitHub Desktop.
Save meSingh/9daeb09baef24358e99eb07032688a5b to your computer and use it in GitHub Desktop.
How To Install Elasticsearch on an Ubuntu VPS

Install Java 8

Elasticsearch require Java, so we will install that now. We will install a recent version of Oracle Java 8 because that is what Elasticsearch recommends. It should, however, work fine with OpenJDK, if you decide to go that route.

Add the Oracle Java PPA to apt:

    * sudo add-apt-repository -y ppa:webupd8team/java

Update your apt package database:

Install the latest stable version of Oracle Java 8 with this command (and accept the license agreement that pops up):

    * sudo apt-get -y install oracle-java8-installer

Now that Java 8 is installed, let's install ElasticSearch.

Install Elasticsearch

Elasticsearch can be installed with a package manager by adding Elastic's package source list.

Run the following command to import the Elasticsearch public GPG key into apt:

    * wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

If your prompt seems to hang, it is likely waiting for your user's password (to authorize the sudo command). If this is the case, enter your password.

Create the Elasticsearch source list:

    * echo "deb http://packages.elastic.co/elasticsearch/2.x/debian stable main" | sudo tee -a /etc/apt/sources.list.d/elasticsearch-2.x.list

Update the apt package database again:

Install Elasticsearch with this command:

    * sudo apt-get -y install elasticsearch

Elasticsearch is now installed. Let's edit the configuration:

    * sudo nano /etc/elasticsearch/elasticsearch.yml

You will want to restrict outside access to your Elasticsearch instance (port 9200), so outsiders can't read your data or shutdown your Elasticsearch cluster through the HTTP API. Find the line that specifies network.host, uncomment it, and replace its value with "localhost" so it looks like this:

/etc/elasticsearch/elasticsearch.yml excerpt (updated)

network.host: localhost

Save and exit elasticsearch.yml.

Now, start Elasticsearch:

    * sudo service elasticsearch restart

Elasticsearch is now up and running.

Test your Elasticsearch install

Elasticsearch should now be running on port 9200. Do note that Elasticsearch takes some time to fully start, so running the curl command below immediately might fail. It shouldn't take longer than ten seconds to start responding, so if the below command fails, something else is likely wrong.

Ensure the server is started by running

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

You should see the following response

{
  "ok" : true,
  "status" : 200,
  "name" : "Xavin",
  "version" : {
    "number" : "0.90.7",
    "build_hash" : "36897d07dadcb70886db7f149e645ed3d44eb5f2",
    "build_timestamp" : "2013-11-13T12:06:54Z",
    "build_snapshot" : false,
    "lucene_version" : "4.5.1"
  },
  "tagline" : "You Know, for Search"
}

If you see a response similar to the one above, Elasticsearch is working properly. Alternatively, you can query your install of Elasticsearch from a browser by visiting :9200. You should see the same JSON as you saw when using curl above.

If you installed by the zip or tar.gz archive, the server can be stopped using the RESTful API

curl -X POST 'http://localhost:9200/_cluster/nodes/_local/_shutdown'

The above command also works when Elasticsearch was installed using the Debian package, but you can also stop the server using service elasticsearch stop. You can restart the server with the corresponding service elasticsearch start.

Using Elasticsearch

Elasticsearch is up and running. Now, we'll go over some basic configuration and usage.

Basic configuration

When installed by zip or tar.gz archives, configuration files are found in the config folder inside the resulting directory. When installed via Debian package, configuration files can be found in /etc/elasticsearch/. The two configuration files you will find are elasticsearch.yml and logging.yml. The first is a general Elasticsearch configuration. The provided file contains nothing but comments, so default settings are used. Reading through the file will provide a good overview of the options, but I will make a few suggestions below. None of the settings are necessary. You can work with Elasticsearch without doing any of the following, but it'll be a raw development environment.

The setting "cluster.name" is the method by which Elasticsearch provides auto-discovery. What this means is that if a group of Elasticsearch servers on the same network share the same cluster name, they will automatically discover each other. This is how simple it is to scale Elasticsearch, but be aware that if you keep the default cluster name and there are other Elasticsearch servers on your network that are not under your control, you are likely to wind up in a bad state.

Basic usage

Let's add some data to our Elasticsearch install. Elasticsearch uses a RESTful API, which responds to the usual CRUD commands: Create, Read, Update, and Destroy.

To add an entry

curl -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'

You should see the following response

{"ok":true,"_index":"tutorial","_type":"helloworld","_id":"1","_version":1}

What we have done is send a HTTP POST request to the Elasticserach server. The URI of the request was /tutorial/helloworld/1. It's important to understand the parameters here:

  • "tutorial" is index of the data in Elasticsearch.
  • "helloworld" is the type.
  • "1" is the id of our entry under the above index and type.

If you saw the response above to the curl command, we can now query for the data with

curl -X GET 'http://localhost:9200/tutorial/helloworld/1'

which should respond with

{"_index":"tutorial","_type":"helloworld","_id":"1","_version":1,"exists":true, "_source" : { "message": "Hello World!" }}

Success! We've added to and queried data in Elasticsearch.

One thing to note is that we can get nicer output by appending ?pretty=true to the query. Let's give this a try

curl -X GET 'http://localhost:9200/tutorial/helloworld/1?pretty=true'

Which should respond with

{
  "_index" : "tutorial",
  "_type" : "helloworld",
  "_id" : "1",
  "_version" : 1,
  "exists" : true, "_source" : { "message": "Hello World!" }
}

which is much more readable. The output will also be pretty printed without needing to append the query string if you have set format=yaml in the Elasticsearch configuration file.

Conclusion

We have now installed, configured and begun using Elasticsearch. Since it responds to a basic RESTful API. It is now easy to begin adding to and querying data using Elasticsearch from your application.

Originally Sourced From: https://www.digitalocean.com/community/tutorials/how-to-install-elasticsearch-logstash-and-kibana-elk-stack-on-ubuntu-16-04 https://www.digitalocean.com/community/tutorials/how-to-install-elasticsearch-on-an-ubuntu-vps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment