diff --git a/setupninja.sh b/setupninja.sh index c5f40ca..395b7ef 100644 --- a/setupninja.sh +++ b/setupninja.sh @@ -5,6 +5,188 @@ passlen=22 emailforle="" #FUNCTIONS +function applysettings { + echo "Reading JSON manifest..." + ipaddress=$( jq -r '.ipaddress' "$origpath/setupninja.json" ) + if [[ $ipaddress == "null" ]]; then + getipaddress + fi + fqdn=$( jq -r '.fqdn' "$origpath/setupninja.json" ) + if [[ $fqdn == "null" ]]; then + errorcheck 1 "Invalid JSON file" "EXIT" + fi + ishttps=$( jq -r '.ishttps' "$origpath/setupninja.json" ) + if [[ $ishttps == "null" ]]; then + ishttps=true + fi + mysqlpass=$( jq -r '.mysqlpass' "$origpath/setupninja.json" ) + if [[ $mysqlpass == "null" ]]; then + mysqlpass=$( generatepassword ) + fi + appdbname=$( jq -r '.appdbname' "$origpath/setupninja.json" ) + if [[ $appdbname == "null" ]]; then + appdbname=$( generatedbname ) + fi + appdbuser=$( jq -r '.appdbuser' "$origpath/setupninja.json" ) + if [[ $appdbuser == "null" ]]; then + appdbuser=$appdbname + fi + appdbpass=$( jq -r '.appdbpass' "$origpath/setupninja.json" ) + if [[ $appdbpass == "null" ]]; then + appdbpass=$( generatepassword ) + fi + mailhost=$( jq -r '.mailhost' "$origpath/setupninja.json" ) + if [[ $mailhost == "null" ]]; then + errorcheck 1 "Invalid JSON file" "EXIT" + fi + mailssl=$( jq -r '.mailssl' "$origpath/setupninja.json" ) + if [[ $mailssl == "null" ]]; then + mailssl=true + fi + mailport=$( jq -r '.mailport' "$origpath/setupninja.json" ) + if [[ $mailport == "null" ]]; then + mailport="465" + fi + mailuser=$( jq -r '.mailuser' "$origpath/setupninja.json" ) + if [[ $mailuser == "null" ]]; then + errorcheck 1 "Invalid JSON file" "EXIT" + fi + mailpass=$( jq -r '.mailpass' "$origpath/setupninja.json" ) + if [[ $mailpass == "null" ]]; then + errorcheck 1 "Invalid JSON file" "EXIT" + fi + mailfrom=$( jq -r '.mailfrom' "$origpath/setupninja.json" ) + if [[ $mailfrom == "null" ]]; then + mailfrom=$mailuser + fi + mailname=$( jq -r '.mailname' "$origpath/setupninja.json" ) + if [[ $mailname == "null" ]]; then + errorcheck 1 "Invalid JSON file" "EXIT" + fi + echo + buildjson +} + +function buildjson { + echo -n "{\"ipaddress\":\"$ipaddress\",\"fqdn\":\"$fqdn\",\"ishttps\":$ishttps,\"mysqlpass\":\"$mysqlpass\",\"appdbname\":\"$appdbname\",\"appdbuser\":\"$appdbuser\",\"appdbpass\":\"$appdbpass\",\"mailhost\":\"$mailhost\",\"mailssl\":$mailssl,\"mailport\":\"$mailport\",\"mailuser\":\"$mailuser\",\"mailpass\":\"$mailpass\",\"mailfrom\":\"$mailfrom\",\"mailname\":\"$mailname\"}" > "$origpath/setupninja.json" +} + +function confapachephp { + #CONFIGURE APACHE2 AND PHP + echo "Configuring Apache2 and PHP-FPM..." + systemctl stop apache2 > /dev/null 2>&1 + errorcheck $? "systemctl stop apache2" "EXIT" + a2dismod php8.2 > /dev/null 2>&1 + errorcheck $? "a2dismod php8.2" "EXIT" + a2dismod mpm_prefork > /dev/null 2>&1 + errorcheck $? "a2dismod mpm_prefork" "EXIT" + a2enmod mpm_event proxy proxy_fcgi setenvif rewrite > /dev/null 2>&1 + errorcheck $? "a2enmod mpm_event proxy proxy_fcgi setenvif rewrite" "EXIT" + a2enconf php8.2-fpm > /dev/null 2>&1 + errorcheck $? "a2enconf php8.2-fpm" "EXIT" + systemctl restart apache2 > /dev/null 2>&1 + errorcheck $? "systemctl restart apache2" "EXIT" + echo "8" > "$origpath/setupninja.step" + echo +} + +function confmariadb { + #CONFIGURE MARIADB + echo "Hardening MariaDB..." + systemctl enable mariadb > /dev/null 2>&1 + errorcheck $? "systemctl enable mariadb" "EXIT" + systemctl start mariadb > /dev/null 2>&1 + errorcheck $? "systemctl start mariadb" "EXIT" + echo "UPDATE mysql.global_priv SET priv=json_set(priv, '$.password_last_changed', UNIX_TIMESTAMP(), '$.plugin', 'mysql_native_password', '$.authentication_string', 'invalid', '$.auth_or', json_array(json_object(), json_object('plugin', 'unix_socket'))) WHERE User='root';" | mysql + errorcheck $? "UPDATE mysql.global_priv SET priv=json_set(priv, '$.password_last_changed', UNIX_TIMESTAMP(), '$.plugin', 'mysql_native_password', '$.authentication_string', 'invalid', '$.auth_or', json_array(json_object(), json_object('plugin', 'unix_socket'))) WHERE User='root';" "EXIT" + echo "FLUSH PRIVILEGES;" | mysql + errorcheck $? "FLUSH PRIVILEGES;" "EXIT" + echo "UPDATE mysql.global_priv SET priv=json_set(priv, '$.plugin', 'mysql_native_password', '$.authentication_string', PASSWORD('basic_single_escape \"$mysqlpass\"')) WHERE User='root';" | mysql + errorcheck $? "UPDATE mysql.global_priv SET priv=json_set(priv, '$.plugin', 'mysql_native_password', '$.authentication_string', PASSWORD('basic_single_escape \"$mysqlpass\"')) WHERE User='root';" "EXIT" + echo "DELETE FROM mysql.global_priv WHERE User='';" | mysql + errorcheck $? "DELETE FROM mysql.global_priv WHERE User='';" "EXIT" + echo "DELETE FROM mysql.global_priv WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');" | mysql + errorcheck $? "DELETE FROM mysql.global_priv WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');" "EXIT" + echo "DROP DATABASE IF EXISTS test;" | mysql + errorcheck $? "DROP DATABASE IF EXISTS test;" "EXIT" + echo "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" | mysql + errorcheck $? "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" "EXIT" + echo "FLUSH PRIVILEGES;" | mysql + errorcheck $? "FLUSH PRIVILEGES;" "EXIT" + echo "9" > "$origpath/setupninja.step" + echo +} + +function confninja { + #PRECONFIGURNG INVOICE NINJA + echo "Creating .env file..." + chown -R www-data:www-data /var/www/$appdbname > /dev/null 2>&1 + chmod -R 755 /var/www/$appdbname > /dev/null 2>&1 + cd /var/www/$appdbname + if [[ -f .env ]]; then + rm -f .env + fi + touch .env + while read -r line; do + if [[ ${line:0:7} == "APP_URL" ]]; then + if [[ ishttps == true ]]; then + echo "APP_URL=\"https://$fqdn/public\"" >> .env + else + echo "APP_URL=\"http://$fqdn/public\"" >> .env + fi + elif [[ ${line:0:11} == "DB_DATABASE" ]]; then + echo "DB_DATABASE=\"$appdbname\"" >> .env + elif [[ ${line:0:11} == "DB_USERNAME" ]]; then + echo "DB_USERNAME=\"$appdbuser\"" >> .env + elif [[ ${line:0:11} == "DB_PASSWORD" ]]; then + echo "DB_PASSWORD=\"$appdbpass\"" >> .env + elif [[ ${line:0:9} == "MAIL_HOST" ]]; then + echo "MAIL_HOST=\"$mailhost\"" >> .env + elif [[ ${line:0:9} == "MAIL_PORT" ]]; then + echo "MAIL_HOST=\"$mailport\"" >> .env + elif [[ ${line:0:13} == "MAIL_USERNAME" ]]; then + echo "MAIL_USERNAME=\"$mailuser\"" >> .env + elif [[ ${line:0:13} == "MAIL_PASSWORD" ]]; then + echo "MAIL_PASSWORD=\"$mailpass\"" >> .env + elif [[ ${line:0:17} == "MAIL_FROM_ADDRESS" ]]; then + echo "MAIL_FROM_ADDRESS=\"$mailfrom\"" >> .env + elif [[ ${line:0:14} == "MAIL_FROM_NAME" ]]; then + echo "MAIL_FROM_NAME=\"$mailname\"" >> .env + elif [[ ${line:0:15} == "MAIL_ENCRYPTION" ]]; then + if [[ $mailssl == true ]]; then + echo "MAIL_ENCRYPTION=\"ssl\"" >> .env + else + echo $line >> .env + fi + elif [[ ${line:0:11} == "REQUIRE_HTTPS" ]]; then + if [[ ishttps == true ]]; then + echo "REQUIRE_HTTPS=\"true\"" >> .env + else + echo "REQUIRE_HTTPS=\"false\"" >> .env + fi + else + echo $line >> .env + fi + done < .env.example + chown www-data:www-data .env > /dev/null 2>&1 + echo "13" > "$origpath/setupninja.step" + echo +} + +function downloadninja { + #DOWNLOAD INVOICE NINJA + if [[ -f "$origpath/invoiceninja.tar" ]]; then + rm -f "$origpath/invoiceninja.tar" + fi + appversion=$(curl -s https://api.github.com/repos/invoiceninja/invoiceninja/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//') + errorcheck $? "curl -s https://api.github.com/repos/invoiceninja/invoiceninja/releases/latest" "EXIT" + echo "Downloading Invoice Ninja version $appversion..." + wget -q "https://github.com/invoiceninja/invoiceninja/releases/download/v$appversion/invoiceninja.tar" + errorcheck $? "wget -q "https://github.com/invoiceninja/invoiceninja/releases/download/v$appversion/invoiceninja.tar"" "EXIT" + echo "11" > "$origpath/setupninja.step" + echo +} + function errorcheck { if [[ $1 == 0 ]] || [[ -z $1 ]]; then echo "SUCCESS: $2" >> "$origpath/setupninja.log" @@ -22,70 +204,6 @@ function errorcheck { fi } -function generatepassword { - passcharacterset=('A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z' 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0') - temp="" - test="" - for ((n=0; n<$passlen; n++)); do - passcharacterset=($(shuf -e ${passcharacterset[@]})) - if [[ $test == ${passcharacterset[0]} ]]; then - pass="$temp${passcharacterset[1]}" - test=${passcharacterset[1]} - else - pass="$temp${passcharacterset[0]}" - test=${passcharacterset[0]} - fi - temp=$pass - done - echo $pass -} - -function generatedbname { - dbcharacterset=('1' '2' '3' '4' '5' '6' '7' '8' '9' '0') - temp="ninja" - for ((n=0; n<5; n++)); do - dbcharacterset=($(shuf -e ${dbcharacterset[@]})) - name="$temp${dbcharacterset[0]}" - temp=$name - done - echo $name -} - -function getipaddress { - size=$( cat /proc/net/dev | wc -l ) - ((size++)) - for ((n=4; n<$size; n++)); do - temp=($( cat /proc/net/dev | head -n $n | tail -1 )) - netdev[$((n-4))]=${temp[0]} - done - size=${#netdev[@]} - c=0 - for ((n=0; n<$size; n++)); do - temp=$( ip a | grep ${netdev[$n]} | grep "state UP" ) - if [[ ! -z $temp ]]; then - netdevup[$c]=${netdev[$n]} - ((c++)) - fi - done - size=${#netdevup[@]} - if [[ $size == 0 ]]; then - errorcheck 1 "No interfaces are connected" "EXIT" - elif [[ $size == 1 ]]; then - ipaddress=$( ip -o -4 addr list ${netdevup[0]:0:-1} | awk '{print $4}' | cut -d/ -f1 ) - else - echo "Available Interfaces..." - for ((n=0; n<$size; n++)); do - echo "$n ${netdevup[$n]:0:-1} $( ip -o -4 addr list ${netdevup[$n]:0:-1} | awk '{print $4}' | cut -d/ -f1 )" - done - echo -n "Which interface do you want to use? (choose 0-$((size-1)) | default is 0) " - read reply - if [[ $reply == "" ]]; then - reply=0 - fi - ipaddress=$( ip -o -4 addr list ${netdevup[$reply]:0:-1} | awk '{print $4}' | cut -d/ -f1 ) - fi -} - function gathersettings { #GATHER SETTINGS getipaddress @@ -160,116 +278,70 @@ function gathersettings { buildjson } -function buildjson { - echo -n "{\"ipaddress\":\"$ipaddress\",\"fqdn\":\"$fqdn\",\"ishttps\":$ishttps,\"mysqlpass\":\"$mysqlpass\",\"appdbname\":\"$appdbname\",\"appdbuser\":\"$appdbuser\",\"appdbpass\":\"$appdbpass\",\"mailhost\":\"$mailhost\",\"mailssl\":$mailssl,\"mailport\":\"$mailport\",\"mailuser\":\"$mailuser\",\"mailpass\":\"$mailpass\",\"mailfrom\":\"$mailfrom\",\"mailname\":\"$mailname\"}" > "$origpath/setupninja.json" +function generatedbname { + dbcharacterset=('1' '2' '3' '4' '5' '6' '7' '8' '9' '0') + temp="ninja" + for ((n=0; n<5; n++)); do + dbcharacterset=($(shuf -e ${dbcharacterset[@]})) + name="$temp${dbcharacterset[0]}" + temp=$name + done + echo $name } -function applysettings { - echo "Reading JSON manifest..." - ipaddress=$( jq -r '.ipaddress' "$origpath/setupninja.json" ) - if [[ $ipaddress == "null" ]]; then - getipaddress - fi - fqdn=$( jq -r '.fqdn' "$origpath/setupninja.json" ) - if [[ $fqdn == "null" ]]; then - errorcheck 1 "Invalid JSON file" "EXIT" - fi - ishttps=$( jq -r '.ishttps' "$origpath/setupninja.json" ) - if [[ $ishttps == "null" ]]; then - ishttps=true - fi - mysqlpass=$( jq -r '.mysqlpass' "$origpath/setupninja.json" ) - if [[ $mysqlpass == "null" ]]; then - mysqlpass=$( generatepassword ) - fi - appdbname=$( jq -r '.appdbname' "$origpath/setupninja.json" ) - if [[ $appdbname == "null" ]]; then - appdbname=$( generatedbname ) - fi - appdbuser=$( jq -r '.appdbuser' "$origpath/setupninja.json" ) - if [[ $appdbuser == "null" ]]; then - appdbuser=$appdbname - fi - appdbpass=$( jq -r '.appdbpass' "$origpath/setupninja.json" ) - if [[ $appdbpass == "null" ]]; then - appdbpass=$( generatepassword ) - fi - mailhost=$( jq -r '.mailhost' "$origpath/setupninja.json" ) - if [[ $mailhost == "null" ]]; then - errorcheck 1 "Invalid JSON file" "EXIT" - fi - mailssl=$( jq -r '.mailssl' "$origpath/setupninja.json" ) - if [[ $mailssl == "null" ]]; then - mailssl=true - fi - mailport=$( jq -r '.mailport' "$origpath/setupninja.json" ) - if [[ $mailport == "null" ]]; then - mailport="465" - fi - mailuser=$( jq -r '.mailuser' "$origpath/setupninja.json" ) - if [[ $mailuser == "null" ]]; then - errorcheck 1 "Invalid JSON file" "EXIT" - fi - mailpass=$( jq -r '.mailpass' "$origpath/setupninja.json" ) - if [[ $mailpass == "null" ]]; then - errorcheck 1 "Invalid JSON file" "EXIT" - fi - mailfrom=$( jq -r '.mailfrom' "$origpath/setupninja.json" ) - if [[ $mailfrom == "null" ]]; then - mailfrom=$mailuser - fi - mailname=$( jq -r '.mailname' "$origpath/setupninja.json" ) - if [[ $mailname == "null" ]]; then - errorcheck 1 "Invalid JSON file" "EXIT" - fi - echo - buildjson +function generatepassword { + passcharacterset=('A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z' 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' '1' '2' '3' '4' '5' '6' '7' '8' '9' '0') + temp="" + test="" + for ((n=0; n<$passlen; n++)); do + passcharacterset=($(shuf -e ${passcharacterset[@]})) + if [[ $test == ${passcharacterset[0]} ]]; then + pass="$temp${passcharacterset[1]}" + test=${passcharacterset[1]} + else + pass="$temp${passcharacterset[0]}" + test=${passcharacterset[0]} + fi + temp=$pass + done + echo $pass } -function sethostname { - #SETTING UP HOSTNAME - if [[ $sethost == true ]]; then - echo "Setting up hostname..." - hostnamectl set-hostname ${fqdn%%.*} > /dev/null 2>&1 - errorcheck $? "define hostname" "EXIT" - echo "$ipaddress $fqdn ${fqdn%%.*}" >> /etc/hosts - errorcheck $? "define FQDN" "EXIT" - echo "1" > "$origpath/setupninja.step" - echo +function getipaddress { + size=$( cat /proc/net/dev | wc -l ) + ((size++)) + for ((n=4; n<$size; n++)); do + temp=($( cat /proc/net/dev | head -n $n | tail -1 )) + netdev[$((n-4))]=${temp[0]} + done + size=${#netdev[@]} + c=0 + for ((n=0; n<$size; n++)); do + temp=$( ip a | grep ${netdev[$n]} | grep "state UP" ) + if [[ ! -z $temp ]]; then + netdevup[$c]=${netdev[$n]} + ((c++)) + fi + done + size=${#netdevup[@]} + if [[ $size == 0 ]]; then + errorcheck 1 "No interfaces are connected" "EXIT" + elif [[ $size == 1 ]]; then + ipaddress=$( ip -o -4 addr list ${netdevup[0]:0:-1} | awk '{print $4}' | cut -d/ -f1 ) + else + echo "Available Interfaces..." + for ((n=0; n<$size; n++)); do + echo "$n ${netdevup[$n]:0:-1} $( ip -o -4 addr list ${netdevup[$n]:0:-1} | awk '{print $4}' | cut -d/ -f1 )" + done + echo -n "Which interface do you want to use? (choose 0-$((size-1)) | default is 0) " + read reply + if [[ $reply == "" ]]; then + reply=0 + fi + ipaddress=$( ip -o -4 addr list ${netdevup[$reply]:0:-1} | awk '{print $4}' | cut -d/ -f1 ) fi } -function initsetup { - #INITIAL SETUP - echo "Updating System..." - apt -y update > /dev/null 2>&1 - errorcheck $? "apt -y update" "EXIT" - apt -y upgrade > /dev/null 2>&1 - errorcheck $? "apt -y upgrade" "EXIT" - echo "2" > "$origpath/setupninja.step" - echo -} - -function gostandard { - #INSTALLING STANDING REQUIREMENTS - echo "Installing packages to do install..." - apt -y install jq extrepo curl screen rsync unzip > /dev/null 2>&1 - errorcheck $? "apt -y install extrepo curl screen rsync unzip" "EXIT" - echo "3" > "$origpath/setupninja.step" - echo -} - -function gosury { - #ENABLE SURY FOR PHP - echo "Setting up Sury Repo for PHP..." - extrepo enable sury > /dev/null 2>&1 - errorcheck $? "extrepo enable sury" "EXIT" - apt -y update > /dev/null 2>&1 - errorcheck $? "apt -y update" "EXIT" - echo "4" > "$origpath/setupninja.step" - echo -} - function goapache { #INSTALL APACHE2 echo "Installing Apache2..." @@ -279,6 +351,28 @@ function goapache { echo } +function gocron { + #SETUP CRONJOBS + echo "Creating Cron Jobs..." + echo "#Invoice Ninja Cron Jobs" >> /etc/crontab + echo "0 6 * * * www-data /usr/bin/php /var/www/$appdbname/artisan ninja:send-recurring > /dev/null" >> /etc/crontab + echo "0 6 * * * www-data /usr/bin/php /var/www/$appdbname/artisan ninja:send-reminders > /dev/null" >> /etc/crontab + echo "* * * * * www-data /usr/bin/php /var/www/$appdbname/artisan schedule:run >> /dev/null 2>&1" >> /etc/crontab + echo "17" > "$origpath/setupninja.step" + echo +} + +function gohousekeeping { + #HOUSEKEEPING + echo "Cleaning up around the house..." + apt -y autoremove > /dev/null 2>&1 + apt -y clean > /dev/null 2>&1 + rm -f "$origpath/invoiceninja.tar" > /dev/null 2>&1 + rm -f "$origpath/setupninja.sh" > /dev/null 2>&1 + echo "19" > "$origpath/setupninja.step" + echo +} + function gomariadb { #INSTALL MARIADB echo "Installing MariaDB..." @@ -297,175 +391,26 @@ function gophp { echo } -function confapachephp { - #CONFIGURE APACHE2 AND PHP - echo "Configuring Apache2 and PHP-FPM..." - systemctl stop apache2 > /dev/null 2>&1 - errorcheck $? "systemctl stop apache2" "EXIT" - a2dismod php8.2 > /dev/null 2>&1 - errorcheck $? "a2dismod php8.2" "EXIT" - a2dismod mpm_prefork > /dev/null 2>&1 - errorcheck $? "a2dismod mpm_prefork" "EXIT" - a2enmod mpm_event proxy proxy_fcgi setenvif rewrite > /dev/null 2>&1 - errorcheck $? "a2enmod mpm_event proxy proxy_fcgi setenvif rewrite" "EXIT" - a2enconf php8.2-fpm > /dev/null 2>&1 - errorcheck $? "a2enconf php8.2-fpm" "EXIT" - systemctl restart apache2 > /dev/null 2>&1 - errorcheck $? "systemctl restart apache2" "EXIT" - echo "8" > "$origpath/setupninja.step" - echo -} - -function confmariadb { - #CONFIGURE MARIADB - echo "Hardening MariaDB..." - systemctl enable mariadb > /dev/null 2>&1 - errorcheck $? "systemctl enable mariadb" "EXIT" - systemctl start mariadb > /dev/null 2>&1 - errorcheck $? "systemctl start mariadb" "EXIT" - echo "UPDATE mysql.global_priv SET priv=json_set(priv, '$.password_last_changed', UNIX_TIMESTAMP(), '$.plugin', 'mysql_native_password', '$.authentication_string', 'invalid', '$.auth_or', json_array(json_object(), json_object('plugin', 'unix_socket'))) WHERE User='root';" | mysql - errorcheck $? "UPDATE mysql.global_priv SET priv=json_set(priv, '$.password_last_changed', UNIX_TIMESTAMP(), '$.plugin', 'mysql_native_password', '$.authentication_string', 'invalid', '$.auth_or', json_array(json_object(), json_object('plugin', 'unix_socket'))) WHERE User='root';" "EXIT" - echo "FLUSH PRIVILEGES;" | mysql - errorcheck $? "FLUSH PRIVILEGES;" "EXIT" - echo "UPDATE mysql.global_priv SET priv=json_set(priv, '$.plugin', 'mysql_native_password', '$.authentication_string', PASSWORD('basic_single_escape \"$mysqlpass\"')) WHERE User='root';" | mysql - errorcheck $? "UPDATE mysql.global_priv SET priv=json_set(priv, '$.plugin', 'mysql_native_password', '$.authentication_string', PASSWORD('basic_single_escape \"$mysqlpass\"')) WHERE User='root';" "EXIT" - echo "DELETE FROM mysql.global_priv WHERE User='';" | mysql - errorcheck $? "DELETE FROM mysql.global_priv WHERE User='';" "EXIT" - echo "DELETE FROM mysql.global_priv WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');" | mysql - errorcheck $? "DELETE FROM mysql.global_priv WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');" "EXIT" - echo "DROP DATABASE IF EXISTS test;" | mysql - errorcheck $? "DROP DATABASE IF EXISTS test;" "EXIT" - echo "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" | mysql - errorcheck $? "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'" "EXIT" - echo "FLUSH PRIVILEGES;" | mysql - errorcheck $? "FLUSH PRIVILEGES;" "EXIT" - echo "9" > "$origpath/setupninja.step" - echo -} - -function ninjadb { - #SETUP DATABASE FOR NINJA - echo "Setting up database for Invoice Ninja..." - echo "CREATE DATABASE $appdbname; CREATE USER $appdbuser@localhost IDENTIFIED BY '$appdbpass'; GRANT ALL PRIVILEGES ON $appdbname.* TO $appdbuser@localhost; FLUSH PRIVILEGES;" | mysql - errorcheck $? "CREATE DATABASE $appdbname; CREATE USER $appdbuser@localhost IDENTIFIED BY '$appdbpass'; GRANT ALL PRIVILEGES ON $appdbname.* TO $appdbuser@localhost; FLUSH PRIVILEGES;" "EXIT" - echo "10" > "$origpath/setupninja.step" - echo -} - -function downloadninja { - #DOWNLOAD INVOICE NINJA - if [[ -f "$origpath/invoiceninja.zip" ]]; then - rm -f "$origpath/invoiceninja.zip" - fi - appversion=$(curl -s https://api.github.com/repos/invoiceninja/invoiceninja/releases/latest|grep tag_name|cut -d '"' -f 4|sed 's/v//') - errorcheck $? "curl -s https://api.github.com/repos/invoiceninja/invoiceninja/releases/latest" "EXIT" - echo "Downloading Invoice Ninja version $appversion..." - wget -q "https://github.com/invoiceninja/invoiceninja/releases/download/v$appversion/invoiceninja.zip" - errorcheck $? "wget -q "https://github.com/invoiceninja/invoiceninja/releases/download/v$appversion/invoiceninja.zip"" "EXIT" - echo "11" > "$origpath/setupninja.step" - echo -} - -function unzipninja { - #EXTRACTING INVOICE NINJA - echo "Unzipping files..." - if [[ -d "/var/www/$appdbname" ]]; then - rm -R "/var/www/$appdbname" - fi - mkdir "/var/www/$appdbname" - unzip invoiceninja.zip -d /var/www/$appdbname > /dev/null 2>&1 - errorcheck $? "unzip invoiceninja.zip -d /var/www/$appdbname" "EXIT" - echo "12" > "$origpath/setupninja.step" - echo -} - -function confninja { - #PRECONFIGURNG INVOICE NINJA - echo "Creating .env file..." - chown -R www-data:www-data /var/www/$appdbname > /dev/null 2>&1 - chmod -R 755 /var/www/$appdbname > /dev/null 2>&1 - cd /var/www/$appdbname - if [[ -f .env ]]; then - rm -f .env - fi - touch .env - while read -r line; do - if [[ ${line:0:7} == "APP_URL" ]]; then - if [[ ishttps == true ]]; then - echo "APP_URL=\"https://$fqdn/public\"" >> .env - else - echo "APP_URL=\"http://$fqdn/public\"" >> .env - fi - elif [[ ${line:0:11} == "DB_DATABASE" ]]; then - echo "DB_DATABASE=\"$appdbname\"" >> .env - elif [[ ${line:0:11} == "DB_USERNAME" ]]; then - echo "DB_USERNAME=\"$appdbuser\"" >> .env - elif [[ ${line:0:11} == "DB_PASSWORD" ]]; then - echo "DB_PASSWORD=\"$appdbpass\"" >> .env - elif [[ ${line:0:9} == "MAIL_HOST" ]]; then - echo "MAIL_HOST=\"$mailhost\"" >> .env - elif [[ ${line:0:9} == "MAIL_PORT" ]]; then - echo "MAIL_HOST=\"$mailport\"" >> .env - elif [[ ${line:0:13} == "MAIL_USERNAME" ]]; then - echo "MAIL_USERNAME=\"$mailuser\"" >> .env - elif [[ ${line:0:13} == "MAIL_PASSWORD" ]]; then - echo "MAIL_PASSWORD=\"$mailpass\"" >> .env - elif [[ ${line:0:17} == "MAIL_FROM_ADDRESS" ]]; then - echo "MAIL_FROM_ADDRESS=\"$mailfrom\"" >> .env - elif [[ ${line:0:14} == "MAIL_FROM_NAME" ]]; then - echo "MAIL_FROM_NAME=\"$mailname\"" >> .env - elif [[ ${line:0:15} == "MAIL_ENCRYPTION" ]]; then - if [[ $mailssl == true ]]; then - echo "MAIL_ENCRYPTION=\"ssl\"" >> .env - else - echo $line >> .env - fi - elif [[ ${line:0:11} == "REQUIRE_HTTPS" ]]; then - if [[ ishttps == true ]]; then - echo "REQUIRE_HTTPS=\"true\"" >> .env - else - echo "REQUIRE_HTTPS=\"false\"" >> .env - fi - else - echo $line >> .env - fi - done < .env.example - chown www-data:www-data .env > /dev/null 2>&1 - echo "13" > "$origpath/setupninja.step" - echo -} - -function vhostninja { - #SETTING UP VHOST - echo "Creating Apache2 VHost and Disabling the default VHost..." - if [[ -f /etc/apache2/sites-available/$appdbname.conf ]]; then - rm -f /etc/apache2/sites-available/$appdbname.conf - fi - touch /etc/apache2/sites-available/$appdbname.conf - echo "" >> /etc/apache2/sites-available/$appdbname.conf - echo " ServerName $fqdn" >> /etc/apache2/sites-available/$appdbname.conf - echo " DocumentRoot /var/www/$appdbname" >> /etc/apache2/sites-available/$appdbname.conf - echo " " >> /etc/apache2/sites-available/$appdbname.conf - echo " DirectoryIndex index.php" >> /etc/apache2/sites-available/$appdbname.conf - echo " Options +FollowSymLinks" >> /etc/apache2/sites-available/$appdbname.conf - echo " AllowOverride All" >> /etc/apache2/sites-available/$appdbname.conf - echo " Require all granted" >> /etc/apache2/sites-available/$appdbname.conf - echo " " >> /etc/apache2/sites-available/$appdbname.conf - echo "" >> /etc/apache2/sites-available/$appdbname.conf - echo " " >> /etc/apache2/sites-available/$appdbname.conf - echo " SetHandler \"proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost\"" >> /etc/apache2/sites-available/$appdbname.conf - echo " " >> /etc/apache2/sites-available/$appdbname.conf - echo "" >> /etc/apache2/sites-available/$appdbname.conf - echo " ErrorLog ${APACHE_LOG_DIR}/$appdbname_error.log" >> /etc/apache2/sites-available/$appdbname.conf - echo " CustomLog ${APACHE_LOG_DIR}/$appdbname_access.log combined" >> /etc/apache2/sites-available/$appdbname.conf - echo "" >> /etc/apache2/sites-available/$appdbname.conf - a2ensite $appdbname.conf > /dev/null 2>&1 - errorcheck $? "a2ensite $appdbname.conf" "EXIT" - a2dissite 000-default.conf > /dev/null 2>&1 - errorcheck $? "a2dissite 000-default.conf" "EXIT" - systemctl restart apache2 > /dev/null 2>&1 - errorcheck $? "systemctl restart apache2" "EXIT" - echo "14" > "$origpath/setupninja.step" +function goreport { + #FINISHED + cd $origpath + echo "MariaDB root password: $mysqlpass" >> "setupninja.log" + echo "Database: $appdbname" >> "setupninja.log" + echo "Username: $appdbuser" >> "setupninja.log" + echo "Password: $appdbpass" >> "setupninja.log" + echo "URL: $fqdn/public" >> "setupninja.log" + echo "Login: $mailfrom" >> "setupninja.log" + echo "Password: $adminpass" >> "setupninja.log" + echo "FINISHED!!!" + echo "MariaDB root password: $mysqlpass" + echo "Database: $appdbname" + echo "Username: $appdbuser" + echo "Password: $appdbpass" + echo "URL: $fqdn/public" + echo "Login: $mailfrom" + echo "Password: $adminpass" + echo "This is all saved in $origpath/setupninja.log" + echo "18" > "$origpath/setupninja.step" echo } @@ -486,6 +431,26 @@ function gossl { fi } +function gostandard { + #INSTALLING STANDING REQUIREMENTS + echo "Installing packages to do install..." + apt -y install jq extrepo curl screen rsync tar > /dev/null 2>&1 + errorcheck $? "apt -y install extrepo curl screen rsync tar" "EXIT" + echo "3" > "$origpath/setupninja.step" + echo +} + +function gosury { + #ENABLE SURY FOR PHP + echo "Setting up Sury Repo for PHP..." + extrepo enable sury > /dev/null 2>&1 + errorcheck $? "extrepo enable sury" "EXIT" + apt -y update > /dev/null 2>&1 + errorcheck $? "apt -y update" "EXIT" + echo "4" > "$origpath/setupninja.step" + echo +} + function goweb { #INSTALL VIA WEB echo "Configuring Invoice Ninja via Web Interface..." @@ -550,48 +515,83 @@ function goweb { echo } -function gocron { - #SETUP CRONJOBS - echo "Creating Cron Jobs..." - echo "#Invoice Ninja Cron Jobs" >> /etc/crontab - echo "0 6 * * * www-data /usr/bin/php /var/www/$appdbname/artisan ninja:send-recurring > /dev/null" >> /etc/crontab - echo "0 6 * * * www-data /usr/bin/php /var/www/$appdbname/artisan ninja:send-reminders > /dev/null" >> /etc/crontab - echo "* * * * * www-data /usr/bin/php /var/www/$appdbname/artisan schedule:run >> /dev/null 2>&1" >> /etc/crontab - echo "17" > "$origpath/setupninja.step" +function initsetup { + #INITIAL SETUP + echo "Updating System..." + apt -y update > /dev/null 2>&1 + errorcheck $? "apt -y update" "EXIT" + apt -y upgrade > /dev/null 2>&1 + errorcheck $? "apt -y upgrade" "EXIT" + echo "2" > "$origpath/setupninja.step" echo } -function goreport { - #FINISHED - cd $origpath - echo "MariaDB root password: $mysqlpass" >> "setupninja.log" - echo "Database: $appdbname" >> "setupninja.log" - echo "Username: $appdbuser" >> "setupninja.log" - echo "Password: $appdbpass" >> "setupninja.log" - echo "URL: $fqdn/public" >> "setupninja.log" - echo "Login: $mailfrom" >> "setupninja.log" - echo "Password: $adminpass" >> "setupninja.log" - echo "FINISHED!!!" - echo "MariaDB root password: $mysqlpass" - echo "Database: $appdbname" - echo "Username: $appdbuser" - echo "Password: $appdbpass" - echo "URL: $fqdn/public" - echo "Login: $mailfrom" - echo "Password: $adminpass" - echo "This is all saved in $origpath/setupninja.log" - echo "18" > "$origpath/setupninja.step" +function ninjadb { + #SETUP DATABASE FOR NINJA + echo "Setting up database for Invoice Ninja..." + echo "CREATE DATABASE $appdbname; CREATE USER $appdbuser@localhost IDENTIFIED BY '$appdbpass'; GRANT ALL PRIVILEGES ON $appdbname.* TO $appdbuser@localhost; FLUSH PRIVILEGES;" | mysql + errorcheck $? "CREATE DATABASE $appdbname; CREATE USER $appdbuser@localhost IDENTIFIED BY '$appdbpass'; GRANT ALL PRIVILEGES ON $appdbname.* TO $appdbuser@localhost; FLUSH PRIVILEGES;" "EXIT" + echo "10" > "$origpath/setupninja.step" echo } -function gohousekeeping { - #HOUSEKEEPING - echo "Cleaning up around the house..." - apt -y autoremove > /dev/null 2>&1 - apt -y clean > /dev/null 2>&1 - rm -f "$origpath/invoiceninja.zip" > /dev/null 2>&1 - rm -f "$origpath/setupninja.sh" > /dev/null 2>&1 - echo "19" > "$origpath/setupninja.step" +function sethostname { + #SETTING UP HOSTNAME + if [[ $sethost == true ]]; then + echo "Setting up hostname..." + hostnamectl set-hostname ${fqdn%%.*} > /dev/null 2>&1 + errorcheck $? "define hostname" "EXIT" + echo "$ipaddress $fqdn ${fqdn%%.*}" >> /etc/hosts + errorcheck $? "define FQDN" "EXIT" + echo "1" > "$origpath/setupninja.step" + echo + fi +} + +function unpackninja { + #EXTRACTING INVOICE NINJA + echo "Unpacking files..." + if [[ -d "/var/www/$appdbname" ]]; then + rm -R "/var/www/$appdbname" + fi + mkdir "/var/www/$appdbname" + tar -xf invoiceninja.tar -C /var/www/$appdbname > /dev/null 2>&1 + errorcheck $? "tar -xf invoiceninja.tar -C /var/www/$appdbname" "EXIT" + echo "12" > "$origpath/setupninja.step" + echo +} + +function vhostninja { + #SETTING UP VHOST + echo "Creating Apache2 VHost and Disabling the default VHost..." + if [[ -f /etc/apache2/sites-available/$appdbname.conf ]]; then + rm -f /etc/apache2/sites-available/$appdbname.conf + fi + touch /etc/apache2/sites-available/$appdbname.conf + echo "" >> /etc/apache2/sites-available/$appdbname.conf + echo " ServerName $fqdn" >> /etc/apache2/sites-available/$appdbname.conf + echo " DocumentRoot /var/www/$appdbname" >> /etc/apache2/sites-available/$appdbname.conf + echo " " >> /etc/apache2/sites-available/$appdbname.conf + echo " DirectoryIndex index.php" >> /etc/apache2/sites-available/$appdbname.conf + echo " Options +FollowSymLinks" >> /etc/apache2/sites-available/$appdbname.conf + echo " AllowOverride All" >> /etc/apache2/sites-available/$appdbname.conf + echo " Require all granted" >> /etc/apache2/sites-available/$appdbname.conf + echo " " >> /etc/apache2/sites-available/$appdbname.conf + echo "" >> /etc/apache2/sites-available/$appdbname.conf + echo " " >> /etc/apache2/sites-available/$appdbname.conf + echo " SetHandler \"proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost\"" >> /etc/apache2/sites-available/$appdbname.conf + echo " " >> /etc/apache2/sites-available/$appdbname.conf + echo "" >> /etc/apache2/sites-available/$appdbname.conf + echo " ErrorLog ${APACHE_LOG_DIR}/$appdbname_error.log" >> /etc/apache2/sites-available/$appdbname.conf + echo " CustomLog ${APACHE_LOG_DIR}/$appdbname_access.log combined" >> /etc/apache2/sites-available/$appdbname.conf + echo "" >> /etc/apache2/sites-available/$appdbname.conf + a2ensite $appdbname.conf > /dev/null 2>&1 + errorcheck $? "a2ensite $appdbname.conf" "EXIT" + a2dissite 000-default.conf > /dev/null 2>&1 + errorcheck $? "a2dissite 000-default.conf" "EXIT" + systemctl restart apache2 > /dev/null 2>&1 + errorcheck $? "systemctl restart apache2" "EXIT" + echo "14" > "$origpath/setupninja.step" echo } @@ -648,7 +648,7 @@ if [[ $step -lt "11" ]]; then downloadninja fi if [[ $step -lt "12" ]]; then - unzipninja + unpackninja fi if [[ $step -lt "13" ]]; then confninja