1. 安装JDK

jdk8下载地址:

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

安装依赖工具:

for ubuntu:

sudo apt-get install vim wget lsof curl git

for centos:

sudo yum install vim wget lsof curl git

wget命令下载:

wget -c --no-check-certificate https://download.oracle.com/otn-pub/java/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/jdk-8u191-linux-x64.tar.gz?AuthParam=1545381787_076fe948da7f81e0fdd1b0b99cf164f0 -O jdk-8u191-linux-x64.tar.gz

其中, jdk安装包的下载链接可以点击相应的tar.gz包, 然后浏览器弹出下载框下载, 在下载页面右键拷贝获取, 一定时候内会失效, 失效则重新获取, -O表示重命名

解压并安装:

tar -xzvf jdk-8u191-linux-x64.tar.gz
sudo mkdir -p /usr/local/java/
sudo mv jdk1.8.0_191 /usr/local/java/

配置环境变量:

sudo vim /etc/profile
# for zsh
sudo vim /etc/zsh/zprofile

在文件最后追加:

export JAVA_HOME=/usr/local/java/jdk1.8.0_191
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

注意, JAVA_HOME修改为你自己的路径

检测java是否安装成功:

$ java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

注意, 如果是ubuntu系统, 在安装elasticsearch时若报如下错误:

could not find java; set JAVA_HOME or ensure java is in PATH

则需要设置默认的JDK才可解决:

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.8.0_191/bin/java" 300

2. 安装elasticsearch

2.1. for ubuntu

官方安装方法(deb包拉取): https://www.elastic.co/guide/en/elasticsearch/reference/current/deb.html

以ubuntu16.04.5为例。

导入elasticsearch密钥:

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

通过APT仓库安装:

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

安装elasticsearch:

sudo apt-get update
sudo apt-get install elasticsearch
2.2. for centos

官方安装方法(rpm包拉取): https://www.elastic.co/guide/en/elasticsearch/reference/current/rpm.html

安装公钥:

rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

在目录/etc/yum.repos.d/下创建创建elasticsearch仓库文件elasticsearch.repo

sudo 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-md

从rpm仓库中安装elasticsearch:

sudo yum update
sudo yum install elasticsearch 

3. systemctl管理elasticsearch服务

配置elasticsearch服务开机自启动:

sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service

elasticsearch服务的开启/关闭/重启:

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

lsof命令通过端口检测进程:

$ sudo lsof -i:9200                           
COMMAND   PID          USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    28767 elasticsearch  250u  IPv6 214565      0t0  TCP localhost:9200 (LISTEN)
java    28767 elasticsearch  251u  IPv6 214564      0t0  TCP ip6-localhost:9200 (LISTEN)

测试:

# for curl
curl http://127.0.0.1:9200
# or
curl -XGET localhost:9200
# for httpie
http get http://127.0.0.1:9200

结果:

{
    "cluster_name": "elasticsearch",
    "cluster_uuid": "AQQKRvPORQGdwcrLKZQcMw",
    "name": "lev6ACu",
    "tagline": "You Know, for Search",
    "version": {
        "build_date": "2018-12-17T21:17:40.758843Z",
        "build_flavor": "default",
        "build_hash": "d2ef93d",
        "build_snapshot": false,
        "build_type": "deb",
        "lucene_version": "7.5.0",
        "minimum_index_compatibility_version": "5.0.0",
        "minimum_wire_compatibility_version": "5.6.0",
        "number": "6.5.4"
    }
}

4. 附elasticsearch.service文件内容

for ubuntu, 以ubuntu16.04.5为例
elasticsearch.service:

[Unit]
Description=Elasticsearch
Documentation=http://www.elastic.co
Wants=network-online.target
After=network-online.target

[Service]
RuntimeDirectory=elasticsearch
PrivateTmp=true
Environment=ES_HOME=/usr/share/elasticsearch
Environment=ES_PATH_CONF=/etc/elasticsearch
Environment=PID_DIR=/var/run/elasticsearch
EnvironmentFile=-/etc/default/elasticsearch

WorkingDirectory=/usr/share/elasticsearch

User=elasticsearch
Group=elasticsearch

ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet

# StandardOutput is configured to redirect to journalctl since
# some error messages may be logged in standard output before
# elasticsearch logging system is initialized. Elasticsearch
# stores its logs in /var/log/elasticsearch and does not use
# journalctl by default. If you also want to enable journalctl
# logging, you can simply remove the "quiet" option from ExecStart.
StandardOutput=journal
StandardError=inherit

# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=65536

# Specifies the maximum number of processes
LimitNPROC=4096

# Specifies the maximum size of virtual memory
LimitAS=infinity

# Specifies the maximum file size
LimitFSIZE=infinity

# Disable timeout logic and wait until process is stopped
TimeoutStopSec=0

# SIGTERM signal is used to stop the Java process
KillSignal=SIGTERM

# Send the signal only to the JVM rather than its control group
KillMode=process

# Java process is never killed
SendSIGKILL=no

# When a JVM receives a SIGTERM signal it exits with code 143
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

# Built for packages-6.5.4 (packages)