Skip to content

Instantly share code, notes, and snippets.

@daif
Created March 21, 2020 11:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daif/27dc4952d84cccda726da2190502b7c8 to your computer and use it in GitHub Desktop.
Save daif/27dc4952d84cccda726da2190502b7c8 to your computer and use it in GitHub Desktop.
MetaBase 0.34.3 installer
#!/bin/bash
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
##################################################################
#
# Project: MetaBase 0.34.3 installer
# Version: 1.0.0
# Date: 2020-03-21
# Author: Daif Alazmi <daif@daif.net>
#
##################################################################
# Default configurations
ZONE='Asia/Riyadh'
USER='metabase'
SITE='metabase.local'
SERVER_OS=`/usr/bin/lsb_release -ds| awk '{print $1}'`
SERVER_VR=`/usr/bin/lsb_release -ds| awk '{print $2}' | cut -d. -f1,2`
# Exit if the current user is not root.
[[ $EUID -ne 0 ]] && echo -e "\033[0;31m \n>\n> Error: This script must be run as root! ... \n>\n\033[0m" && exit 1
# Exit if server is not Ubuntu 18.04
[[ $SERVER_OS != 'Ubuntu' || $SERVER_VR != '18.04' ]] && echo -e "\033[0;31m \n>\n> Error: This script required Ubuntu 18.04 ... \n>\n\033[0m" && exit 1
##################################################################
# 1 - Set timezone
##################################################################
echo -e "\033[0;33m \n>\n> Setting timezone to ${ZONE}... \n>\n\033[0m"
timedatectl set-timezone ${ZONE}
timedatectl
echo -e "Done \n"
##################################################################
# 2 - Updating system
##################################################################
echo -e "\033[0;33m \n>\n> Updating system packages... \n>\n\033[0m"
apt -y update
apt -y upgrade
echo -e "Done \n"
##################################################################
# 3 - Installing Java
##################################################################
echo -e "\033[0;33m \n>\n> Installing Java ... \n>\n\033[0m"
apt -y install default-jdk
echo -e "Done \n"
##################################################################
# 4 - Installing Apache
##################################################################
echo -e "\033[0;33m \n>\n> Installing Apache... \n>\n\033[0m"
apt -y install apache2 apache2-utils
a2enmod actions alias rewrite setenvif headers proxy proxy_http
a2dismod -f status autoindex
echo -e "Done \n"
##################################################################
# 5 - Installing lets-encrypt for the site
##################################################################
echo -e "\033[0;33m \n>\n> Installing lets-encrypt... \n>\n\033[0m"
apt -y install certbot
echo -e "Done \n"
##################################################################
# 6 - Creating MetaBase user and group
##################################################################
echo -e "\033[0;33m \n>\n> Creating ${USER} user and group... \n>\n\033[0m"
groupadd -r ${USER}
useradd -r -s /bin/false -g ${USER} ${USER}
echo -e "Done \n"
##################################################################
# 7 - Creating home directory
##################################################################
echo -e "\033[0;33m \n>\n> Creating home directory... \n>\n\033[0m"
[ -d /home/${USER}/ ] || mkdir /home/${USER}/
echo -e "Done \n"
##################################################################
# 8 - Downloading MetaBase
##################################################################
echo -e "\033[0;33m \n>\n> Downloading MetaBase v0.34.3... \n>\n\033[0m"
wget --no-clobber https://downloads.metabase.com/v0.34.3/metabase.jar -O /home/${USER}/metabase.jar
echo -e "Done \n"
##################################################################
# 9 - Creating log and setting file
##################################################################
echo -e "\033[0;33m \n>\n> Creating log file and setting rsyslog... \n>\n\033[0m"
touch /var/log/metabase.log
cat <<EOT > /etc/rsyslog.d/metabase.conf
if $programname == 'metabase' then /var/log/metabase.log
& stop
EOT
echo -e "Done \n"
##################################################################
# 10 - Creating metabase.env file
##################################################################
echo -e "\033[0;33m \n>\n> Creating metabase.env file... \n>\n\033[0m"
cat <<EOT > /home/${USER}/metabase.env
MB_JETTY_HOST=127.0.0.1
MB_JETTY_PORT=3000
EOT
echo -e "Done \n"
##################################################################
# 8 - Creating service file
##################################################################
echo -e "\033[0;33m \n>\n> Creating service file... \n>\n\033[0m"
cat <<EOT > /etc/systemd/system/metabase.service
[Unit]
Description=Metabase server
After=syslog.target
After=network.target
[Service]
WorkingDirectory=/home/${USER}/
ExecStart=/usr/bin/java -jar /home/${USER}/metabase.jar
EnvironmentFile=/home/${USER}/metabase.env
User=${USER}
Type=simple
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=metabase
SuccessExitStatus=143
TimeoutStopSec=120
Restart=always
[Install]
WantedBy=multi-user.target
EOT
echo -e "Done \n"
##################################################################
# 9 - Setting permissions
##################################################################
echo -e "\033[0;33m \n>\n> Setting permissions... \n>\n\033[0m"
chown -R ${USER}:${USER} /home/${USER}/
chown ${USER}:${USER} /var/log/metabase.log
chmod 640 /home/${USER}/metabase.env
echo -e "Done \n"
##################################################################
# 10 - Restarting services
##################################################################
echo -e "\033[0;33m \n>\n> Restarting services... \n>\n\033[0m"
systemctl daemon-reload
systemctl start metabase.service
systemctl enable metabase.service
echo -e "Done \n"
##################################################################
# 11 - Creating VirtualHost
##################################################################
echo -e "\033[0;33m \n>\n> Creating VirtualHost... \n>\n\033[0m"
cat <<EOT > /etc/apache2/sites-available/${SITE}.conf
<VirtualHost *:80>
ServerName ${SITE}
ServerAdmin webmaster@${SITE}
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>
EOT
a2ensite ${SITE}
CHECK_HOST=`cat /etc/hosts | grep ${SITE}`
if [[ $CHECK_HOST == '' ]]; then
echo -e "127.0.0.1 ${SITE}" >> /etc/hosts
fi
systemctl restart apache2
echo -e "Done \n"
##################################################################
# 11 - Install SSL for $SITE
##################################################################
echo -e "\033[0;33m \n>\n> Install SSL for $SITE... \n>\n\033[0m"
# Exit if server ip is not equal site ip.
SRVR_ADDR=`curl -s -4 ifconfig.co`
SITE_ADDR=`dig +short $SITE`
[[ $SITE_ADDR != $SRVR_ADDR ]] && echo -e "\033[0;31m \n>\n> Error: The server IP ($SRVR_ADDR) is not equal the site ($SITE) IP ($SITE_ADDR)! ... \n>\n\033[0m" && exit 1
certbot --non-interactive --agree-tos --redirect --email webmaster@${SITE} -d ${SITE}
echo -e "Done \n"
##################################################################
# 11 - Finished
##################################################################
echo -e "\033[0;33m \n>\n> Finished, open https://$SITE ... \n>\n\033[0m"
@TurkerTunali
Copy link

Nice one. Thanks.

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