Thursday, June 18, 2020

Elasticsearch - Ubuntu 20

Java runtime is required for the Elasticsearch.

Install Java

java -version
sudo apt update && sudo apt upgrade -y
apt search openjdk
sudo apt install default-jre
sudo apt install openjdk-11-jre-headless
sudo apt install openjdk-11-jdk-headless
sudo apt install openjdk-14-jre-headless
sudo apt install openjdk-14-jdk-headless
java -version
javac -version

Java Environment setup

You need to setup global config in /etc/profile OR /etc/bash.bashrc file for all users:
sudo vi /etc/bash.bashrc
# At the end
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/
export PATH=$PATH:$JAVA_HOME/bin

Uninstall Elasticsearch


You need to setup global config in /etc/profile OR /etc/bash.bashrc file for all users:
sudo vi /etc/bash.bashrc

Installing Elasticsearch ref1

sudo apt-get install apt-transport-https
Download and install the Debian package manually
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.7.1-amd64.deb
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.7.1-amd64.deb.sha512
shasum -a 512 -c elasticsearch-7.7.1-amd64.deb.sha512 
sudo dpkg -i elasticsearch-7.7.1-amd64.deb
Elasticsearch is not started automatically after installation. How to start and stop Elasticsearch depends on whether your system uses SysV init or systemd (used by newer distributions). You can tell which is being used by running this command:
ps -p 1

Running Elasticsearch with systemd

To configure Elasticsearch to start automatically when the system boots up, run the following commands:
sudo /bin/systemctl daemon-reload
sudo /bin/systemctl enable elasticsearch.service
Elasticsearch can be started and stopped as follows:
sudo systemctl stop elasticsearch.service
sudo systemctl start elasticsearch.service
sudo systemctl status elasticsearch.service
sudo systemctl restart elasticsearch
These commands provide no feedback as to whether Elasticsearch was started successfully or not. Instead, this information will be written in the log files located in /var/log/elasticsearch/.

Elasticsearch loads its configuration from the /etc/elasticsearch/elasticsearch.yml file by default. The format of this config file is explained in Configuring Elasticsearch.
sudo vi /etc/elasticsearch/elasticsearch.yml
Test the Elasticsearch working: (no need to open port 9200)
curl -XGET http://localhost:9200/
To verify that Elasticsearch is working, enter the following command on the server on which it is running:
curl -XGET '<host>:9200/_cat/health?v&pretty'
curl -XGET 'localhost:9200/_cat/health?v&pretty'

Set up a proxy for Apache 2.4

This section discusses how to configure an Elasticsearch proxy using a virtual host.

Enable mod_proxy as follows:
a2enmod proxy_http
Use a text editor to open /etc/apache2/sites-available/000-default.conf
Add the following directive at the top of the file:
Listen 8080
Add the following at the bottom of the file:
<VirtualHost *:8080>
    ProxyPass "/" "http://localhost:9200/"
    ProxyPassReverse "/" "http://localhost:9200/"
</VirtualHost>
Test config and Restart Apache:
sudo apache2ctl configtest
sudo service apache2 reload
Verify the proxy works by entering the following command:
curl -i http://localhost:<proxy port>/_cluster/health
For example, if your proxy uses port 8080:
curl -i http://localhost:8080/_cluster/health
Messages similar to the following display to indicate success:
HTTP/1.1 200 OK
Date: Tue, 23 Feb 2016 20:38:03 GMT
Content-Type: application/json; charset=UTF-8
Content-Length: 389
Connection: keep-alive
{"cluster_name":"elasticsearch","status":"yellow","timed_out":false,"number_of_nodes":1,"number_of_data_nodes":1,"active_primary_shards":5,"active_shards":5,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":5,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":50.0}

Configure Magento 2 Elasticsearch

Reindexing catalog search and refreshing the full page cache.

After you change Magento’s Elasticsearch configuration, you must reindex the catalog search index and refresh the full page cache using the Admin or command line.
bin/magento cache:clean
bin/magento indexer:reindex catalogsearch_fulltext
bin/magento indexer:reindex
Wait until reindexing completes.
Unlike the cache, indexers are updated by a cron job. Make sure cron is enabled before you start using Elasticsearch.

Secure communication with Apache

Step 1: Create a password

which htpasswd
/usr/bin/htpasswd
If a path displays, it is installed; if the command returns no output, htpasswd is not installed.
If necessary, install htpasswd:
Ubuntu: apt-get -y install apache2-utils
Create a password file(http://httpd.apache.org/docs/2.4/howto/auth.html)
sudo mkdir -p /usr/local/apache/password
Example 2: Elasticsearch You must set up authentication for two users: one with access to Nginx and one with access to Elasticsearch. To create password files for these users, enter the following commands:
mkdir -p /usr/local/apache/password
htpasswd -c /usr/local/apache/password/.<password file name> <username>
sudo htpasswd -c /usr/local/apache/password/.htpasswd_elasticsearch haanz_online
sudo htpasswd    /usr/local/apache/password/.htpasswd_elasticsearch ejamuna_com
Add additional users: To add another user to your password file, enter the following command as a user with root privileges:
htpasswd /usr/local/apache/password/.htpasswd <username>
sudo htpasswd /usr/local/apache/password/.htpasswd_elasticsearch mg

Step 2: Secure communication with Apache

This section discusses how to specify who can access the Apache server.
Use a text editor to add the following contents to your secure virtual host.

Apache 2.4:
sudo vi /etc/apache2/sites-available/000-default.conf

Listen 8080

<VirtualHost *:8080>
    ProxyPass "/" "http://localhost:9200/"
    ProxyPassReverse "/" "http://localhost:9200/"
</VirtualHost>

<Proxy "*">
    Order deny,allow
    Allow from all

    AuthType Basic
    AuthName "Elastic Server"
    AuthBasicProvider file
    AuthUserFile /usr/local/apache/password/.htpasswd_elasticsearch
    Require valid-user

  # This allows OPTIONS-requests without authorization
  <LimitExcept OPTIONS>
        Require valid-user
  </LimitExcept>
</Proxy>
<VirtualHost *:80>...
Verify communication is secure
Test config and Restart Apache:
sudo apache2ctl configtest
sudo service apache2 reload
Use a curl command to verify cluster status
curl -i http://<hostname, ip, or localhost>:<proxy port>/_cluster/health
For example, if you enter the command on the Elasticsearch server and your proxy uses port 8080:
curl -i http://localhost:8080/_cluster/health
The following message displays to indicate authentication failed:
HTTP/1.1 401 Unauthorized
Date: Tue, 23 Feb 2016 20:35:29 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Restricted"
Now try the following command:
curl -i -u <username>:<password> http://<hostname, ip, or localhost>:<proxy port>/_cluster/health
curl -i -u magento_elasticsearch:mypassword http://localhost:8080/_cluster/health
curl -i -u gcrecruitment_site:55Uu*^ http://localhost:8080/_cluster/health

HTTP/1.1 200 OK
Date: Thu, 18 Jun 2020 07:10:49 GMT
Server: Apache/2.4.29 (Ubuntu)
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
content-type: application/json; charset=UTF-8
content-length: 389
{"cluster_name":"elasticsearch","status":"yellow","timed_out":false,"number_of_nodes":1,"number_of_data_nodes":1,"active_primary_shards":1,"active_shards":1,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":1,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":
Perform the same tasks as discussed in Configure Magento to use Elasticsearch


No comments:

Post a Comment