#!/bin/bash # server-setup.sh # OpenDRS Online Discrepancy Reporting System # Copyright (C) 2020 Rod Wright # SPDX-License-Identifier: GPL-2.0 inst_name="OpenDRS" docroot_path="/var/www" apache_user="www-data" apache_group="www-data" echo "" echo "" echo "" echo "" echo "This script will configure a Raspberry Pi to be an OpenDRS server." echo "" echo "" echo "You should have already run the terminal-setup.sh script in this directory." echo "" if [ ! -e "/etc/drs.conf" ] then echo "It looks like the terminal-setup.sh script has not been run yet." echo "This is required before running the server-setup.sh script." echo "Aborting." exit 1 fi echo "It looks like you have. Continuing." echo "" echo "" echo "Updating Raspbian..." sleep 10 apt update && apt dist-upgrade -y echo "...done." echo "" echo "IMPORTANT!: Be sure to make note of usernames and passwords you provide below." echo "You will need them later." echo "" echo "Installing required server packages. Select apache2 as the web server to configure" echo "automatically. Choose Yes when prompted to install phpmyadmin database and allow it to" echo "generate a random password(leave the field blank)." echo -n "Press enter to continue." read continueok sleep 10 apt install -y apache2 php php-mcrypt mariadb-server phpmyadmin uuid-runtime cp etc/apache2/mods-available/alias.conf /etc/apache2/mods-available service apache2 restart echo "...done." echo "" echo "" echo "Configuring the mysql installation. Accept the defaults at the following prompts." echo "Remember the password you set here. You'll need it during the OpenDRS install." echo -n "Press enter to proceed." read continueok mysql_secure_installation mysql mysql -e "update user set plugin='';flush privileges;" echo "" echo "" # This is a fresh install operation="installed" # prompt for mysql information bad_mysql_adm=1 while [ $bad_mysql_adm -eq 1 ] do admintestdb="drsadm`date +%Y%m%d`" echo "We need to know the username and password of a MySQL administrator" echo "to be able to create the database and user for OpenDRS. Note that this" echo "is not necessarily the same as the login and password for the computer." echo -n "MySQL admin username: " read mysql_adm_user echo "" echo -n "MySQL admin password: " read -s mysql_adm_pass export MYSQL_PWD="$mysql_adm_pass" mysql -u"$mysql_adm_user" -e "create database $admintestdb;drop database $admintestdb" if [ $? -eq 0 ] then bad_mysql_adm=0 else echo "ERROR: Either the username/password you supplied were incorrect or the user" echo -n "is not a MySQL administrator. Try again? [Y/N]: " read admtry if [ "$admtry" != "Y" -o "$admtry" != "y" ] then exit fi fi done echo "" echo "What would you like to call this installation of OpenDRS? If you accept" echo "the default, it will be called $inst_name. If you will be running multiple" echo "instances of OpenDRS on this server, you should choose a distinctive name." echo -n "Installation name [$inst_name]: " read inst_name echo "" # Create initial database db_create_fail=1 while [ $db_create_fail -eq 1 ] do name_valid=0 while [ $name_valid -eq 0 ] do echo "What would you like to call the database for this installation of OpenDRS?" echo "If you accept the default or make it blank, it will be $inst_name." echo "Spaces or special characters are not allowed in the name." echo -n "database name [$inst_name]: " read new_db_name if [ "$new_db_name" = "" ] then new_db_name=$inst_name fi echo $new_db_name|egrep -q "\W" if [ $? -eq 0 ] then echo "The name you entered seems to contain whitespace or special" echo "characters. Try again or just accept the default." else name_valid=1 fi done echo "" export MYSQL_PWD="$mysql_adm_pass" mysql -u"$mysql_adm_user" -e "create database $new_db_name" if [ $? -eq 0 ] then db_create_fail=0 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 exit fi fi done # Create user and give rights to database drs_user="$new_db_name-user" drs_pass=`uuidgen` export MYSQL_PWD="$mysql_adm_pass" mysql -u"$mysql_adm_user" -e "create user if not exists '$drs_user'@'localhost' identified by '$drs_pass'" mysql -u"$mysql_adm_user" -e "grant all on $new_db_name.* to '$drs_user'@'localhost'" mysql -u"$mysql_adm_user" -e "flush privileges" # Populate initial database mysql -u"$mysql_adm_user" $new_db_name < ../opendrs-initial.sql # Set the installation name export MYSQL_PWD="$drs_pass" mysql -u"$drs_user" $new_db_name -e "update config set value=\"$inst_name\",defaultvalue=\"$inst_name\" where name=\"app_name\"" # Create install path echo "Creating installation directory . . ." install_path=$docroot_path/$inst_name 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/$drs_user/g" $install_path/db.php sed -i "s/DBPASS/$drs_pass/g" $install_path/db.php fi # copy distribution to install location echo "Installing distribution . . ." cp -Rv ../distfiles/* $install_path ln -s $install_path/writeups.php $install_path/index.php # 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 OpenDRS installation" echo -n "directory [$apache_user:$apache_group] :" read httpd_user_group echo "" echo "Setting file ownership . . ." chown -R $httpd_user_group $install_path echo "" # tell user to launch web browser to continue echo "" echo "" echo "" echo "OpenDRS has been installed." echo "# OpenDRS default Apache configuration" > /etc/apache2/conf-available/$inst_name.conf echo "" >> /etc/apache2/conf-available/$inst_name.conf echo "Alias /$inst_name $install_path" >> /etc/apache2/conf-available/$inst_name.conf echo "" >> /etc/apache2/conf-available/$inst_name.conf echo "" >> /etc/apache2/conf-available/$inst_name.conf echo " Options FollowSymLinks" >> /etc/apache2/conf-available/$inst_name.conf echo " DirectoryIndex index.php" >> /etc/apache2/conf-available/$inst_name.conf echo "" >> /etc/apache2/conf-available/$inst_name.conf a2enconf $inst_name service apache2 reload echo "OpenDRS server setup is complete." echo "" echo "Once the reboot is complete you will need to do the final installation of $inst_name through" echo "the browser. You will need the mysql username and password you set during the mysql installation above." echo "" echo "" echo "Once the final install is done, you will need to log in to $inst_name to configure it." echo "IMPORTANT: The default login credentials for the initial admin user are:" echo "" echo "login: drsadmin" echo "password: OpenDRS-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." echo "DRS_SERVER=\"http://localhost/$inst_name\"" > /etc/drs.conf