fevangelou
4/24/2019 - 1:53 PM

Install ElasticSearch with the Cerebro UI on CentOS 6 & 7 (with or without cPanel)

Install ElasticSearch with the Cerebro UI on CentOS 6 & 7 (with or without cPanel)

#!/bin/bash

# /**
#  * @version    1.0
#  * @package    Install ElasticSearch with the Cerebro UI on CentOS 6 & 7 (with or without cPanel)
#  * @author     Fotis Evangelou (https://kodeka.io)
#  * @url        https://engintron.com
#  * @copyright  Copyright (c) 2018 - 2019 Kodeka OÜ. All rights reserved.
#  * @license    GNU/GPL license: https://www.gnu.org/copyleft/gpl.html
#  */

# Script Parameters
CEREBRO_VERSION="0.8.3"             # Get latest version number from: https://github.com/lmenezes/cerebro/releases
OPEN_FIREWALL_PORT_FOR_CEREBRO="no" # Options: yes|no - Opens port 9000 in the firewall for Cerebro upon installation

# How to use (as root user):
# $ wget -O ~/install_elasticsearch_in_centos.sh https://gist.githubusercontent.com/fevangelou/1d959592a1107455f69cfe22791c9d70/raw/install_elasticsearch_in_centos.sh
# $ chmod +x ~/install_elasticsearch_in_centos.sh
# $ ~/install_elasticsearch_in_centos.sh



# ========================================= #
# === Nothing to change below this line === #
# ========================================= #

# Constants
CENTOS_VERSION=$(rpm -q --qf "%{VERSION}" $(rpm -q --whatprovides redhat-release))

# Install Java
yum -y update
yum -y install java-1.8.0-openjdk.x86_64

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

if [ ! -f "/etc/yum.repos.d/elasticsearch.repo" ]; then
    touch /etc/yum.repos.d/elasticsearch.repo
fi

cat > "/etc/yum.repos.d/elasticsearch.repo" << EOF
[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
EOF

yum -y update
yum install elasticsearch

# --- Register & start in CentOS 6 --
if [ "$CENTOS_VERSION" = "6" ]; then
    chkconfig --add elasticsearch
    chkconfig elasticsearch on
    service elasticsearch start
fi

# --- Register & start in CentOS 7 --
if [ "$CENTOS_VERSION" = "7" ]; then
    systemctl daemon-reload
    systemctl enable elasticsearch
    systemctl start elasticsearch
fi

# Install Cerebro
cd ~/
wget https://github.com/lmenezes/cerebro/releases/download/v$CEREBRO_VERSION/cerebro-$CEREBRO_VERSION.zip
unzip cerebro-$CEREBRO_VERSION.zip
rm -f cerebro-$CEREBRO_VERSION.zip

touch /usr/local/bin/cerebroctl
chmod +x /usr/local/bin/cerebroctl

cat > "/usr/local/bin/cerebroctl" << EOF
#!/bin/bash

case \$1 in
start)
    nohup /root/cerebro-$CEREBRO_VERSION/bin/cerebro &> /root/cerebroctl.out &
    echo ""
    echo " --- Cerebro started --- "
    echo ""
    echo " Access Cerebro at http://localhost:9000 "
    echo ""
    ;;
stop)
    fuser -k 9000/tcp
    echo ""
    echo " --- Cerebro stopped --- "
    echo ""
    ;;
*)
    echo ""
    echo " Use 'cerebroctl start' OR 'cerebroctl stop' to control Cerebro. "
    echo ""
    ;;
esac
EOF

# Open firewall port for Cerebro
if [ "$OPEN_FIREWALL_PORT_FOR_CEREBRO" = "yes" ]; then
    # --- For CentOS 6 --
    if [ "$CENTOS_VERSION" = "6" ]; then
        iptables -I INPUT -p tcp -m tcp --dport 9000 -j ACCEPT
        service iptables save
    fi

    # --- For CentOS 7 --
    if [ "$CENTOS_VERSION" = "7" ]; then
        firewall-cmd --zone=public --add-port=9000/tcp --permanent
        firewall-cmd --reload
    fi

    /usr/local/bin/cerebroctl start
fi

echo ""
echo " --- ElasticSearch & Cerebro are now installed on your server --- "

if [ "$OPEN_FIREWALL_PORT_FOR_CEREBRO" = "no" ]; then
    echo ""
    echo " To access Cerebro (the ElasticSearch UI), you should now "
    echo " open port 9000 for Cerebro on your firewall and then start "
    echo " Cerebro with 'cerebroctl start' from the terminal. "
else
    echo ""
    echo " To access Cerebro (the ElasticSearch UI) go to: "
    echo " http://YOUR_SERVER_S_HOSTNAME_OR_IP:9000/ "
    echo ""
    echo " If you have Engintron installed on your CentOS/cPanel server, "
    echo " you can create a custom rule to proxy a subdomain directly to: "
    echo " http://YOUR_SERVER_S_HOSTNAME_OR_IP:9000/ "
    echo ""
    echo " See the Engintron Docs for more: https://engintron.com/docs "
fi

echo ""

exit 0



# If you are on CentOS/cPanel with Engintron installed and you wish to load
# Cerebro from a subdomain (to mask port 9000), create a DNS entry for your
# subdomain and then simply add a custom rule in Engintron like this:
#
# if ($host ~ "cerebro.domain.tld") {
#     set $PROXY_SCHEME "http";
#     set $PROXY_DOMAIN_OR_IP "127.0.0.1";
#     set $PROXY_TO_PORT 9000;
# }