Skip to content

Instantly share code, notes, and snippets.

@jaircuevajunior
Last active October 27, 2017 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaircuevajunior/a8be5af6b572eb790026a1537eb10629 to your computer and use it in GitHub Desktop.
Save jaircuevajunior/a8be5af6b572eb790026a1537eb10629 to your computer and use it in GitHub Desktop.
Install Elastic Search With Auth

Installing Elastic Search with auth by nginx proxy

Reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html

Requirement JAVA

Install Oracle Java 8 (openjdk-9 doesn't work!!!!)

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

Install Elastic Search

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt-get install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/5.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-5.x.list
apt update
apt install elasticsearch
  • SysV init (Ubuntu 14)
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable elasticsearch.service
  • SystemD (Ubuntu 16) update-rc.d elasticsearch defaults 95 10

  • Bind elasticsearch to accept requets from loopback only

Edit /etc/elasticsearch/elasticsearch.yml

network.host: 127.0.0.1


Setting up Auth through Nginx Proxy

Reference: http://www.minvolai.com/blog/2014/08/Setting-up-a-Secure-Single-Node-Elasticsearch-server-behind-Nginx/Setting-up-a-Secure-Single-Node-Elasticsearch-server-behind-Nginx/

  • Install nginx and apache-utils apt install nginx apache2-utils Apache-utils is needed in this case to give us the 'htpasswd' command

  • Generate a .htpasswd file htpasswd -c /etc/nginx/elasticsearchuser.pwd username

  • Edit the default nginx vhost

server {
    listen 9222;
    server_name localhost;
    location / {
        rewrite ^/(.*) /$1 break;
        proxy_ignore_client_abort on;
        proxy_pass http://localhost:9200;
        proxy_redirect http://localhost:9200 http://localhost/;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  Host $http_host;
        auth_basic "Elasticsearch Authentication";
        auth_basic_user_file /etc/nginx/elasticsearchuser.pwd;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment