#!/bin/bash # install.sh # OpenLog Online Logbook # Copyright (C) 2025 Rod Wright # SPDX-License-Identifier: GPL-2.0 APP_NAME="OpenLog" DEFAULT_DB_NAME="openlog" APP_VERSION="5.2.4" DOC_ROOT="/var/www" INSTALL_LOC="openlog" APACHE_USER="www-data" APACHE_GROUP="www-data" APACHE_CONF_DIR="/etc/apache2/conf-available" BACKUP_DIR="openlog-previous-installation" INDEX_FILE="lognotes.php" CONFIG_TABLE="configs" CONFIG_NAME_COLUMN="confname" CONFIG_VALUE_COLUMN="confvalue" CONFIG_DEFAULT_COLUMN="" OFFER_OPTIONAL_PKGS=false 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 } # 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 systemctl status 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" "uuid-runtime" echo "" echo "Finished required package installation." echo "" if $OFFER_OPTIONAL_PKGS then # Check for optional packages and prompt to install echo "Checking for optional packages..." echo "" echo "Checking for phpmyadmin installation..." echo "" if dpkg -l|grep phpmyadmin >/dev/null 2>&1 then echo "" echo "phpmyadmin is installed. Continuing." echo "" else echo "phpmyadmin doesn't seem to be installed." echo "It is an optional package that enables administration of the mysql/mariadb" echo "database server using a friendly GUI. It is highly recommended." echo "Note that this has the potential to be a security risk, especially" echo "if your passwords aren't sufficiently strong." echo -n "Install it now? [Y/n]: " read reqinstall if [ "$reqinstall" = "N" -o "$reqinstall" = "n" ] then echo "Proceeding without installing phpmyadmin. Database administration" echo "will require the use of the mysql/mariadb command line tools." else echo "" echo "Proceeding with phpmyadmin installation" echo "" sleep 3 echo "" echo "During the following installation of phpmyadmin:" echo "" echo "- Select apache2 as the web server to configure automatically." echo "" echo "- Choose Yes when prompted to install phpmyadmin database and allow it to" echo " generate a random password(leave the field blank)." echo "" echo -n "Press enter to continue." read continueok echo "" apt-get install -y phpmyadmin echo "" echo "" echo "In order to log in to the database using phpmyadmin, there must be a" echo "database user with a password set who has administrator privileges." echo "After a fresh installation of the MySQL or MariaDB server, the root" echo "user doesn't have a password, and is unable to log in to the phpmyadmin" echo "web GUI. You should create a new user with administrative rights now." echo "" read -p "New MySQL/MariaDB administrator username: " -r newdbadminuser echo "" get_new_password newdbadminpass=$newpassword unset newpassword mysql -e "CREATE USER '$newdbadminuser'@'localhost' IDENTIFIED VIA mysql_native_password USING password('$newdbadminpass'); GRANT ALL PRIVILEGES ON *.* TO '$newdbadminuser'@'localhost' REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; flush privileges" echo "" echo "" echo "You can now access phpmyadmin using a web browser pointed to:" echo "" echo " http://$HOSTNAME/phpmyadmin" echo "" echo "*** WARNING!! this url is accessible via unencrypted (http) connections." echo "This is a huge security risk since the username and password you enter" echo "on the phpmyadmin login page can be sent in the clear for any potential" echo "attacker to sniff! You should enable redirection to https for phpmyadmin" echo "in your apache2 configuration if your site is reachable from the internet." echo "" echo -n "Press enter to continue." read continueok echo "" echo "Finished with phpmyadmin installation" echo "" sleep 3 fi fi echo "" echo "Checking for webmin installation..." echo "" if dpkg -l|grep webmin >/dev/null 2>&1 then echo "" echo "webmin is installed. Continuing." echo "" else echo "webmin doesn't seem to be installed." echo "It is an optional package that enables system administration" echo "using a friendly GUI. It is highly recommended, especially for" echo "headless servers without any way to log in directly on the machine." echo "Note that this has the potential to be a security risk, especially" echo "if your passwords aren't sufficiently strong." echo -n "Install it now? [Y/n]: " read reqinstall if [ "$reqinstall" = "N" -o "$reqinstall" = "n" ] then echo "Proceeding without installing webmin. System administration" echo "will require login to the server either directly or via ssh." else echo "" echo "Proceeding with webmin installation" echo "" sleep 3 echo "Installing webmin..." skipwebmin="false" if ! curl -o setup-repos.sh https://raw.githubusercontent.com/webmin/webmin/master/setup-repos.sh then echo "Warning: failed to download Webmin repo setup script." echo -n "Ignore and [c]ontinue or [A]bort? [c/A]: " read wmfailchoice if [ "$wmfailchoice" = "C" -o "$wmfailchoice" = "c" ] then skipwebmin="true" echo "Continuing without installing Webmin. You should investigate the failure" echo "and install Webmin manually after the server is set up if you would like" echo "to be able to manage the server from another computer with a GUI instead" echo "of through ssh only." echo -n "Press enter to continue." read continueok echo "" fi fi if [ "$skipwebmin" = "false" ] then sh setup-repos.sh -f apt-get install -y --install-recommends webmin fi echo "...done." echo "" echo "You can now access webmin using a web browser pointed to:" echo "https://$HOSTNAME:10000" echo "Unless you have obtained valid ssl certificates, your server is" echo "using self-signed certs. This is not necessarily a problem, but" echo "your browser will complain. You can tell the browser to accept the" echo "self signed certificates and the traffic will still be encrypted," echo "however you may be vulnerable to man-in-the-middle attacks. You can" echo "get free valid certificates through LetsEncrypt." echo "See http://letsencrypt.org for more information." echo "" echo -n "Press enter to continue." read continueok echo "" echo "Finished with webmin installation" echo "" sleep 3 fi fi echo "" echo "Finished optional package installation." fi echo "" sleep 3 # prompt for install location echo "Where would you like to install this version of $APP_NAME? This should" echo "be somewhere the webserver can find it. If you don't know, refer to the" echo "webserver's documentation and check the server's configuration files." echo "Note that this is where the directory containing $APP_NAME's files will be" echo "created." echo -n "Enter the webserver's install location [$DOC_ROOT]:" read doc_root_in echo "" echo -n "Enter name of $APP_NAME installation directory [$INSTALL_LOC] :" read install_loc_in echo "" if [ "$doc_root_in" = "" ] then doc_root_in=$DOC_ROOT fi if [ "$install_loc_in" = "" ] then install_loc_in=$INSTALL_LOC fi install_path="$doc_root_in/$install_loc_in" if [ -d "$install_path" ] then # Install path exists. if [ -e "$install_path/db.php" ] then # A db.php file was found in the install path if grep -q -e "$APP_NAME" $install_path/db.php then # the db.php file is part of a previous installation echo "The installation path you chose already exists." echo "Would you like to upgrade an existing installation or cancel " echo "and restart the install using a new installation location." echo -n "[U]pgrade or [C]ancel? [C] :" read upgrade_choice if [ "$upgrade_choice" = "U" -o "$upgrade_choice" = "u" ] # Upgrade chosen then # proceed with upgrade operation="upgraded" # back up existing database echo "Backing up the current database to the package directory." curr_dbname=`grep dbname $install_path/db.php | head -1 | cut -d "\"" -f2` curr_username=`grep username $install_path/db.php | head -1 | cut -d "\"" -f2` curr_dbpass=`grep dbpass $install_path/db.php | head -1 | cut -d "\"" -f2` export MYSQL_PWD="$curr_dbpass" mysqldump --no-tablespaces -u"$curr_username" $curr_dbname > $curr_dbname-`date +%Y-%m-%d_%H:%M:%S`-backup.sql # back up existing installation echo "Backing up current installation to the package directory." cp -R $install_path $install_loc_in-`date +%Y-%m-%d_%H:%M:%S`-backup # delete all existing installation files except db.php find $install_path/ -mindepth 1 -not -name 'db.php' -delete # apply database updates mysql -u"$curr_username" -f -D $curr_dbname < database-update.sql >/dev/null 2>&1 # apply updates to existing db.php bash ./db.php_update $install_path # copy distribution to install location echo "Installing distribution . . ." cp -R distfiles/* $install_path # set symlink for jquery-ui if [ ! -L "$install_path/jquery-ui" ] then ln -s $install_path/jquery-ui* $install_path/jquery-ui fi # set symlink for index.php if [ ! -L "$install_path/index.php" ] then ln -s $install_path/$INDEX_FILE $install_path/index.php fi # set ownership echo "In order to set the ownership correctly, we need to know the user and" echo "group that the webserver expects its files to be owned by. This is" echo "usually not the root account. Refer to the ownership of other files in" echo "your webserver's document tree to see what this should be." echo "Enter the user and group separated by a colon (username:groupname)." echo "Enter the user:group of the $APP_NAME installation" echo -n "directory [$APACHE_USER:$APACHE_GROUP] :" read httpd_user_group if [ "$httpd_user_group" = "" ] then httpd_user_group="$APACHE_USER:$APACHE_GROUP" fi echo "" echo "Setting file ownership . . ." chown -R $httpd_user_group $install_path else echo "Cancelling installation" abort_exit fi else # This was a mistake. Exit now to avoid clobbering another app. echo "The installation directory you chose exists, but no previous installation" echo "of $APP_NAME was found. Please investigate the situation. Aborting now." abort_exit fi else # This was a mistake. Exit now to avoid clobbering another app. echo "The installation directory you chose exists, but no previous installation" echo "of $APP_NAME was found. Please investigate the situation. Aborting now." abort_exit fi else # This is a fresh install operation="installed" echo "" echo "What would you like to call this installation of $APP_NAME? If you accept" echo "the default, it will be called $APP_NAME. If you will be running multiple" echo "instances of $APP_NAME on this server, you should choose a distinctive name." echo "This can be changed later in the application itself." echo -n "Installation name [$APP_NAME]: " read new_inst_name if [ "$new_inst_name" = "" ] then new_inst_name="$APP_NAME" fi echo "" # Create initial database db_create_fail=true while $db_create_fail do echo "What would you like to call the database for this installation of $APP_NAME?" echo "If you accept the default, it will be called $DEFAULT_DB_NAME. Again, this is fine" echo "if you will only be running this one instance, but if you will be running" echo "multiple instances of $APP_NAME on this server, you should choose a " echo "distinctive name." echo -n "database name [$DEFAULT_DB_NAME]: " read new_db_name if [ "$new_db_name" = "" ] then new_db_name=$DEFAULT_DB_NAME fi echo "" if mysql -e "create database $new_db_name" then db_create_fail=false else echo "An error was encountered when trying to create the database." echo "This probably means the database already exists." echo -n "Try again with a different database name? [Y/N]: " read dbcreatetry if [ "$dbcreatetry" != "Y" -o "$dbcreatetry" != "y" ] then abort_exit fi fi done # Create user and give rights to database db_user="$new_db_name`date +%Y%m%d%H%M%S`" db_pass=`uuidgen` mysql -e "create user if not exists '$db_user'@'localhost' identified by '$db_pass'" mysql -e "grant all on $new_db_name.* to '$db_user'@'localhost'" mysql -e "flush privileges" # Populate initial database mysql $new_db_name < database-initial.sql # Set the installation name export MYSQL_PWD="$db_pass" mysql -u"$db_user" $new_db_name -e "update $CONFIG_TABLE set $CONFIG_VALUE_COLUMN=\"$new_inst_name\" where $CONFIG_NAME_COLUMN=\"log_name\"" if [ "$CONFIG_DEFAULT_COLUMN" != "" ] then mysql -u"$db_user" $new_db_name -e "update $CONFIG_TABLE set $CONFIG_DEFAULT_COLUMN=\"$new_inst_name\" where $CONFIG_NAME_COLUMN=\"log_name\"" fi # Create install path echo "Creating installation directory . . ." mkdir $install_path # Create db.php if it doesn't exist if [ ! -f "$install_path/db.php" ] then cp db.php.template $install_path/db.php sed -i "s/DBNAME/$new_db_name/g" $install_path/db.php sed -i "s/DBUSER/$db_user/g" $install_path/db.php sed -i "s/DBPASS/$db_pass/g" $install_path/db.php sed -i "s/APPNAME/$APP_NAME/g" $install_path/db.php fi # copy distribution to install location echo "Installing distribution . . ." cp -R distfiles/* $install_path if [ ! -L "$install_path/jquery-ui" ] then ln -s $install_path/jquery-ui* $install_path/jquery-ui fi if [ ! -L "$install_path/index.php" ] then ln -s $install_path/lognotes.php $install_path/index.php fi # set ownership echo "In order to set the ownership correctly, we need to know the user and" echo "group that the webserver expects its files to be owned by. This is" echo "usually not the root account. Refer to the ownership of other files in" echo "your webserver's document tree to see what this should be." echo "Enter the user and group separated by a colon (username:groupname)." echo "Enter the user:group of the $APP_NAME installation" echo -n "directory [$APACHE_USER:$APACHE_GROUP] :" read httpd_user_group if [ "$httpd_user_group" = "" ] then httpd_user_group="$APACHE_USER:$APACHE_GROUP" fi echo "" echo "Setting file ownership . . ." chown -R $httpd_user_group $install_path fi echo "" # tell user to launch web browser to continue echo "" echo "" echo "" echo "$APP_NAME has been $operation." base_url=$install_loc_in if [ "$operation" = "installed" ] then echo "# $APP_NAME default Apache configuration" > $APACHE_CONF_DIR/$base_url.conf echo "" >> $APACHE_CONF_DIR/$base_url.conf echo "Alias /$base_url $doc_root_in/$base_url" >> $APACHE_CONF_DIR/$base_url.conf echo "" >> $APACHE_CONF_DIR/$base_url.conf echo "" >> $APACHE_CONF_DIR/$base_url.conf echo " Options FollowSymLinks" >> $APACHE_CONF_DIR/$base_url.conf echo " DirectoryIndex index.php" >> $APACHE_CONF_DIR/$base_url.conf echo "" >> $APACHE_CONF_DIR/$base_url.conf echo -n "The apache2 $base_url 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 $base_url.conf >/dev/null service apache2 reload echo "You can now point a web browser to $base_url on your web server " echo "to set up and configure $APP_NAME." echo "" echo "IMPORTANT: The default login credentials for the initial admin user are:" echo "" echo "login: ${APP_NAME}Admin" echo "password: $APP_NAME-1" echo "" echo "Remember these credentials. Otherwise, you will not be able to log in and" echo "configure your new installation." echo "It is highly recommended that you change the initial password to something" echo "secure after logging in for the first time." else echo "You will need to manually enable the configuration by executing:" echo " a2enconf $base_url.conf" echo " systemctl reload apache2" echo "as a superuser before it can be accessed." fi else echo "You can now point a web browser to http://$HOSTNAME/$base_url " echo "to use your upgraded $APP_NAME." fi clean_exit