Elasticsearch is an open-source disseminated full-text search and investigation motor. It upholds RESTful activities and permits you to store, search, and investigate large volumes of information progressively.
Elasticsearch is quite possibly the most mainstream web crawlers fueling applications that have complex hunt prerequisites, for example, enormous web based business stores and scientific applications.
In this post, We will install and secure elasticsearch with nginx basic auth on ubuntu 18.04 LTS
You need to first update the ubuntu current repository by using the command.
sudo apt-get update
Elastic search required java and apt-transport-https to install the packages, Use the following commands.
sudo apt-get install openjdk-8-jdk apt-transport-https -y
Here you need to add the elasticsearch key in ubuntu by using the commands.
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
After this you need to add the repository by using the given command.
sudo sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'
Once the repository added So then we need to update the ubuntu’s repository and install the elasticsearch by execution of given commands.
sudo apt-get update
sudo apt-get install elasticsearch -y
By default elasticsearch service you will get in stop state, You need to start it and enable it to auto start on boot time, To do that you need to use the given commands.
To start the service.
sudo systemctl start elasticsearch.service
To start on boot.
sudo systemctl enable elasticsearch.service
To get the service status
sudo systemctl status elasticsearch.service
If everything is good, So then you will get output like this.
Now we need to test the elasticsearch with curl command to get basic elasticsearch details, and by default elasticsearch bind with 9200 port, Use the given command to basic test.
curl -X GET "localhost:9200/"
You should get output like this.
You need to configure nginx basic auth configuration on elasticsearh to secure it, We are going to install nginx with apache2-utils to do that, Follow the given steps.
sudo apt-get install nginx apache2-utils -y
After this you need to create a virtualhost for elasticsearch, Use the given command for that.
sudo nano /etc/nginx/sites-available/elasticsearch.conf
Paste the given virtualhost configuration nginx’s elasticsearch file and nginx reverse proxy port is 9201
server {
listen 9201;
server_name localhost;
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.elasticsearch;
location / {
proxy_pass http://localhost:9200;
}
}
Save and exit from nano text editor and create user and password for nginx reverse-proxy by using the given command.
sudo htpasswd -c /etc/nginx/.elasticsearch elasticsearch
Lets enable the elasticsearch virtualhost
sudo ln -s /etc/nginx/sites-available/elasticsearch.conf /etc/nginx/sites-enabled/
Verify the nginx configuration file.
sudo nginx -t
Now you need to reload the ngnx service to get changes for elasticsearch virtualhost.
sudo systemctl reload nginx.service
Verify the nginx service status
sudo systemctl status nginx.service
Verify the reverse-proxy port
netstat -plntu | grep 9201
Right now you only access the elasticsearch with localhost without any kind of user authentication, If you want to access from the network, So you need to provide username and password also, Lets testing for the same.
Here i am using curl command with switch -u to authenticate.
curl -u elasticsearch:password -X GET "10.2.0.4:9201/"
You should get the output like this.
Conclusion
You have effectively introduced Elasticsearch on your Ubuntu 18.04. You would now be able to visit the authority Elasticsearch Documentation page and figure out how to begin with Elasticsearch.
Usefull Information.