213 lines
6.2 KiB
Bash
213 lines
6.2 KiB
Bash
#!/bin/bash
|
|
|
|
# install.sh
|
|
# Adminer-Installer
|
|
# Copyright (C) 2025 Rod Wright
|
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
APP_NAME="Adminer-Installer"
|
|
APP_VERSION="1.0"
|
|
DOC_ROOT="/var/www"
|
|
INSTALL_LOC="/opt/adminer"
|
|
APACHE_USER="www-data"
|
|
APACHE_GROUP="www-data"
|
|
APACHE_CONF_DIR="/etc/apache2/conf-available"
|
|
|
|
|
|
function get_new_user() {
|
|
# Stores new username in the variable $newusername
|
|
ungood=false
|
|
while ! $ungood
|
|
do
|
|
read -p "New username: " -r newusername
|
|
if [[ $newusername == "root" || $newusername == "mysql" ]]
|
|
then
|
|
echo "Username cannot be $newusername. Please try again."
|
|
else
|
|
ungood=true
|
|
fi
|
|
done
|
|
}
|
|
|
|
function get_new_password() {
|
|
# Stores new password in the variable $newpassword
|
|
pwgood=false
|
|
while ! $pwgood
|
|
do
|
|
read -p "New password: " -s -r newpassword
|
|
echo ""
|
|
read -p "Re-type new password: " -s -r rnewpw
|
|
echo ""
|
|
if [ "$newpassword" = "" -o "$rnewpw" = "" ]
|
|
then
|
|
echo "Password cannot be blank. Please try again."
|
|
echo ""
|
|
elif [ "$newpassword" != "$rnewpw" ]
|
|
then
|
|
echo "Passwords do not match. Please try again."
|
|
echo ""
|
|
else
|
|
pwgood=true
|
|
fi
|
|
done
|
|
}
|
|
|
|
function install_required_packages() {
|
|
# Positional parameters supplied to this function should be quoted
|
|
# apt-get installable package names.
|
|
# Note: Each package supplied to the for loop below can either be a
|
|
# single package name or a list of alternatives separated by the word "or".
|
|
# If supplying a list, the last one in the list will be the one that
|
|
# gets installed.
|
|
for prereq in "$@"
|
|
do
|
|
echo "Checking for $prereq installation..."
|
|
gpattern=""
|
|
for pkg_option in $(echo $prereq)
|
|
do
|
|
if [ "$pkg_option" != "or" ]
|
|
then
|
|
gpattern+=" -e $pkg_option"
|
|
preferred_pkg=$pkg_option
|
|
fi
|
|
done
|
|
if dpkg -l|grep $gpattern >/dev/null 2>&1
|
|
then
|
|
echo "$prereq is installed. Continuing."
|
|
else
|
|
echo "$APP_NAME requires $prereq, but it doesn't seem to be installed. "
|
|
echo -n "Install $preferred_pkg now? [Y/n]: "
|
|
read reqinstall
|
|
if [ "$reqinstall" = "N" -o "$reqinstall" = "n" ]
|
|
then
|
|
echo "$APP_NAME requires $prereq, but you have elected not to install it."
|
|
echo "Installation cannot continue."
|
|
abort_exit
|
|
else
|
|
echo ""
|
|
echo "Proceeding with $preferred_pkg installation"
|
|
echo ""
|
|
sleep 3
|
|
apt-get install -y $preferred_pkg
|
|
echo ""
|
|
echo "Finished with $preferred_pkg installation"
|
|
echo ""
|
|
sleep 3
|
|
|
|
fi
|
|
fi
|
|
done
|
|
}
|
|
|
|
function clean_exit() {
|
|
# Re-enable unattended-upgrades
|
|
if $restart_uu; then systemctl start unattended-upgrades; fi
|
|
exit 0
|
|
}
|
|
|
|
function abort_exit() {
|
|
# Re-enable unattended-upgrades
|
|
if $restart_uu; then systemctl start unattended-upgrades; fi
|
|
exit 1
|
|
}
|
|
|
|
function spinner() {
|
|
local pid="$1"
|
|
local delay="0.1"
|
|
local spinstr='|/-\\'
|
|
local i=0
|
|
|
|
while ps -p "$pid" > /dev/null; do
|
|
i=$(( (i+1) % 4 ))
|
|
printf "\r[%c]" "${spinstr:$i:1}"
|
|
sleep "$delay"
|
|
done
|
|
printf "\r \r" # Clear the spinner line
|
|
}
|
|
|
|
# test for superuser rights
|
|
if [[ $EUID -ne 0 ]]; then
|
|
echo "This script must be run with superuser privileges. Try sudo ./install.sh"
|
|
abort_exit
|
|
fi
|
|
|
|
echo ""
|
|
echo ""
|
|
echo "* * * * * Welcome to $APP_NAME $APP_VERSION Installation * * * * *"
|
|
echo ""
|
|
echo ""
|
|
sleep 3
|
|
echo "Preparing for installation. Please wait..."
|
|
|
|
# Prevent unattended-upgrades from interfering
|
|
restart_uu=false
|
|
while dpkg -l|grep unattended-upgrades >/dev/null 2>&1
|
|
do
|
|
systemctl stop unattended-upgrades
|
|
sleep 5
|
|
restart_uu=true
|
|
done
|
|
|
|
apt-get update
|
|
|
|
# Check for prerequisites and prompt to install
|
|
echo "Checking for required packages..."
|
|
echo ""
|
|
install_required_packages "apache2" "php" "mysql-server or mariadb-server" "php-mysql" "uuid-runtime rsync"
|
|
echo ""
|
|
echo "Finished required package installation."
|
|
echo ""
|
|
|
|
# Install distfiles
|
|
rsync -avz distfiles/* /
|
|
|
|
# Get latest adminer from the official site
|
|
wget -O /opt/adminer/adminer.php https://www.adminer.org/latest-mysql-en.php
|
|
|
|
# Set symlink to point to adminer
|
|
ln -s $INSTALL_LOC $DOC_ROOT/adminer
|
|
|
|
# Rename htaccess
|
|
mv $INSTALL_LOC/_.htaccess $INSTALL_LOC/.htaccess
|
|
|
|
# Set permissions
|
|
chown -R $APACHE_USER:$APACHE_GROUP $INSTALL_LOC $DOC_ROOT/adminer
|
|
|
|
# Create new database user
|
|
echo "IMPORTANT: The default mysql/mariadb installation has only one user,"
|
|
echo "the root user, which doesn't have a password, and so cannot be used"
|
|
echo "to login using Adminer. You'll need to create another database admin"
|
|
echo -n "user. Would you like to do that now? [Y/n] :"
|
|
read newusercreate
|
|
if [[ $newusercreate == "" || $newusercreate == "Y" || $newusercreate == "y" ]]
|
|
then
|
|
get_new_user
|
|
get_new_password
|
|
mysql mysql -e "CREATE USER '$newusername'@'localhost' IDENTIFIED BY '$newpassword'" --batch --disable-column-names
|
|
mysql mysql -e "GRANT ALL PRIVILEGES on *.* TO '$newusername'@'localhost' WITH GRANT OPTION" --batch --disable-column-names
|
|
mysql mysql -e "FLUSH PRIVILEGES" --batch --disable-column-names
|
|
else
|
|
echo "You will need to manually create a new admin user for the database"
|
|
echo "to be able to log in using Adminer."
|
|
fi
|
|
|
|
echo -n "The apache2 adminer configuration must be enabled. Would you like to do that now? [Y/n] :"
|
|
read enconf
|
|
if [ "$enconf" = "" -o "$enconf" = "Y" -o "$enconf" = "y" ]
|
|
then
|
|
a2enconf adminer.conf >/dev/null
|
|
systemctl reload apache2
|
|
echo "You can now point a web browser to databases on your web server "
|
|
echo ""
|
|
echo "This release includes an an updater that fetches the latest stable"
|
|
echo "release from the Adminer website. Just run sudo upgrade-adminer.sh"
|
|
else
|
|
echo "You will need to manually enable the configuration by executing:"
|
|
echo " a2enconf adminer.conf"
|
|
echo " systemctl reload apache2"
|
|
echo "as a superuser before it can be accessed."
|
|
fi
|
|
|
|
clean_exit
|