Skip to content

Instantly share code, notes, and snippets.

@patrickmwila
Last active September 15, 2023 09:25
Show Gist options
  • Save patrickmwila/ae99762c3cbd5cfcde9d365611c043fd to your computer and use it in GitHub Desktop.
Save patrickmwila/ae99762c3cbd5cfcde9d365611c043fd to your computer and use it in GitHub Desktop.
notes2023-searchkit_and_elasticsearch_install

[ZNRP] Searchkit and Elasticsearch Installation

Installation of Searchkit

Searchkit is an open source library which helps you build a great search experience with Elasticsearch.

To install searchkit, you will need to clone the inrp_portal repository and execute a series of commands. Here we are using searchkit with React.js. The package.json file in the root project folder show's the dependencies needed for the successful installation of the portal. Skip the prerequisites if you already have Node.js with yarn installed. Note: The installation instructions assume you are running a debian based linux distribution. Specifically, here we are running Ubuntu 20.04.6 LTS.

Prerequisites

# Installation of Node.js using nvm
# Open your teminal and run the following commands sequentially
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
$ source ~/.bashrc
$ nvm install --lts
$ nvm use --lts
$ npm install -g yarn

Clone the inrp_portal and install dependencies (Includes searchkit)

$ sudo apt-get update
$ sudo apt-get install git
$ git clone https://github.com/DataLab-UNZA/inrp_portal.git
$ cd inrp_portal
$ yarn install

Installation of Elasticsearch

Elasticsearch can be installed separately or in a docker container. Both methods are shown below as elasticsearch for the ZNRP installed on AWS Ubuntu 20.04 EC2 instance is running in a docker container.

Standalone Install of Elastisearch

# Update package list and Upgrade
$ sudo apt-get update && sudo apt-get upgrade -y

# Elasticsearch requires Java to run
$ sudo apt install openjdk-11-jre-headless

# Import the Elasticsearch GPG key
$ wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

# Add the Elasticsearch APT repository to your system's sources
$ sudo sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'

# Update package list again
$ sudo apt update

# Now Install Elasticsearch
$ sudo apt install elasticsearch

Configure Elasticsearch

Configure elasticsearch to enable CORS and other settings like the host.network http.port depending on your setup... For CORS, run the following command which opens the elasticsearch.yml configuration file. Note you can replace vim with your text editor of choice.

sudo vim /etc/elasticsearch/elasticsearch.yml

After opening the configuration file, append the following lines:

http.cors.allow-origin:  "*"
http.cors.enabled:  true
http.cors.allow-credentials:  true
http.cors.allow-methods:  OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: X-Requested-With, X-Auth-Token, Content-Type, Content-Length, Authorization, Access-Control-Allow-Headers, Accept, x-elastic-client-meta

If elasticsearch is running on the cloud like AWS EC2 Instance, make sure to edit the following parts of your elasticsearch.yml file

# Set host.network to the private ip address of your AWS EC2 instance
# Set the http.port to your desired port e.g 9200 and make sure that this port is added to the Inbound rules under security settings of your AWS EC2 Instance
# Set the discovery.type to single node

Start and Enable Elasticsearch

$ sudo systemctl start elasticsearch
$ sudo systemctl enable elasticsearch

Note: If you face any errors while starting elasticsearch, make sure to examine the logs in /var/log/elasticsearch

Test Elasticsearch

# If running on local machine with port set to 9200
$ curl -X GET "http://localhost:9200/"

# If running in the cloud with port set to 9200
$ curl -X GET "http://<EC2 instance Public IP Address>:9200/"

Elasticsearch in Docker

# Update the package database and install required packages
$ sudo apt-get update
$ sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common

# Add Docker's official GPG key
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Add Docker repository
$ echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker
$ sudo apt-get update
$ sudo apt-get install -y docker-ce docker-ce-cli containerd.io

# Start the Docker service
$ sudo systemctl start docker

# Enable Docker to start on boot
$ sudo systemctl enable docker

# Pull the elasticsearch image and create the network
$ docker  pull  docker.elastic.co/elasticsearch/elasticsearch:8.6.2
$ docker  network  create  elastic
$ docker  run  --name  elasticsearch  --net  elastic  -p  9200:9200  -p  9300:9300  -e  "discovery.type=single-node"  -e  "xpack.security.enabled=false"  -e  http.cors.enabled=true  -e  "http.cors.allow-origin='*'"  -e  http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization  -e  http.cors.allow-credentials=true  -e  network.publish_host=localhost  -e  xpack.security.enabled=false  docker.elastic.co/elasticsearch/elasticsearch:8.6.2

# Start elastic search in docker
$ sudo su
$ docker ps -a # lists docker containers running 
$ docker start container_id # start elasticsearch container based on id displayed from `docker ps -a`
$ sudo docker update --restart unless-stopped container_id # this enables docker to start the container automatically after reboot

Test Elasticsearch

$ curl -X GET "http://localhost:9200/"
OR
$ curl -X GET "http://<EC2 instance Public IP Address>:9200/"

Conclusion

In conclusion, these installation instructions provide a comprehensive guide for setting up Searchkit and Elasticsearch on an AWS EC2 instance running Ubuntu 20.04. The provided steps cover the installation of necessary dependencies, configuration of Elasticsearch, and options for running Elasticsearch both as a standalone service and within a Docker container.

Note that the ZNRP build version is being served using an Nginx server on the AWS EC2 instance, and this setup will be showcased in an upcoming screen-cast.

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