Building Enterprise JavaScript Applications
上QQ阅读APP看书,第一时间看更新

Installing and starting Elasticsearch

Go to elastic.co/downloads/elasticsearch and download the latest Elasticsearch version for your machine. For Ubuntu, we can download the official .deb package and install using dpkg:

$ sudo dpkg -i elasticsearch-6.3.2.deb
Your version of Elasticsearch might be different from the one here. That's fine.

Next, we need to configure Elasticsearch to use the Java version we just installed. We have already done this for the entire system, but Elasticsearch also has its own configuration file for specifying the path to the Java binaries. Open up the /etc/default/elasticsearch file and add an entry for the JAVA_HOME variable, just as you did before:

# Elasticsearch Java path
JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

Now, we can start Elasticsearch! Elasticsearch is installed as a service, so we can use systemctl to start and stop it:

sudo systemctl start elasticsearch.service
sudo systemctl stop elasticsearch.service

To simplify development, we can make Elasticsearch start whenever the system is rebooted by enabling it:

sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service

Now, we can check that Elasticsearch is running using systemctl:

$ sudo systemctl start elasticsearch.service
$ sudo systemctl status elasticsearch.service
● elasticsearch.service - Elasticsearch
Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; enabled; vendo
Active: active (running) since Wed 2017-12-27 17:52:06 GMT; 4s ago
Docs: http://www.elastic.co
Main PID: 20699 (java)
Tasks: 42 (limit: 4915)
Memory: 1.1G
CPU: 12.431s
CGroup: /system.slice/elasticsearch.service
└─20699 /usr/lib/jvm/java-8-openjdk-amd64/bin/java -Xms1g -Xmx1g -XX:

Dec 27 17:52:06 nucleolus systemd[1]: Started Elasticsearch.

Alternatively, a more direct approach would simply be to send a query to the Elasticsearch API on its default port of 9200:

$ curl 'http://localhost:9200/?pretty'
{
"name" : "6pAE96Q",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "n6vLxwydTmeN4H6rX0tqlA",
"version" : {
"number" : "6.3.2",
"build_date" : "2018-07-20T05:20:23.451332Z",
"lucene_version" : "7.3.1"
},
"tagline" : "You Know, for Search"

}

We get a reply, which means Elasticsearch is running on your machine!