Made the 'Enabling Services' section of the setup script

This commit is contained in:
Tristan Ancelet 2023-05-03 11:05:54 -05:00
parent 6cd1e8414f
commit 659bd90641

View File

@ -31,9 +31,24 @@ declare -a needed_packages=(
# Check packages # Check packages
bind-utils bind-utils
) )
declare -a packages_to_install
INSTALLED_PACKAGES="$( apt list --installed )"
# Filtering packages from my needed_packages array (in case some are already installed)
for package in ${needed_packages[@]}; do
if [[ ! "$INSTALLED_PACKAGES" == *$package* ]]; then
packages_to_install+=( "$package" )
fi
done
unset needed_packages
# Installing the packages that aren't already installed
apt install -y ${packages_to_install}
# Start Firewall Setup # Start: Firewall Setup
# Setting up the rules for the webserver # Setting up the rules for the webserver
firewall-cmd --zone=public --add-service=http --perm firewall-cmd --zone=public --add-service=http --perm
@ -41,7 +56,8 @@ firewall-cmd --zone=public --add-service=https --perm
# Setup rules for SSH server (omitting from repo-commit for obvious reasons) # Setup rules for SSH server (omitting from repo-commit for obvious reasons)
###Do SSH Rule setup### ###Do SSH Rule setup###
# End Firewall Setup
# End: Firewall Setup
# Wildcard SSL Cert installation # Wildcard SSL Cert installation
@ -82,7 +98,8 @@ if [[ ! "$ALLOWED_ACCESS_CONFIG" =~ ^$IP_REGEX$ ]]; then
exit 1 exit 1
fi fi
echo " site_config_file=/etc/apache2/sites-available/webcron.conf
site_config="
# Redirecting all http traffic to https # Redirecting all http traffic to https
<VirtualHost *:80> <VirtualHost *:80>
ServerAdmin webmaster@tristanancelet.com ServerAdmin webmaster@tristanancelet.com
@ -113,16 +130,57 @@ echo "
SSLOptions +StdEnvVars SSLOptions +StdEnvVars
</FilesMatch> </FilesMatch>
</VirtualHost> </VirtualHost>
</IfModule>" > /etc/apache2/sites-available/web-cron.conf </IfModule>"
# If the site config files doesn't exist or doesn't match what it needs to be then it will be overwritten.
if [[ ! -f $site_config_file ]] || [[ ! "$(<$site_config_file)" == "$site_config" ]] ; then
echo "$site_config" > $site_config_file
fi
# Enable the site with a2ensite
a2ensite web-cron a2ensite web-cron
[[ $? -ne 0 ]] && log " There was an error when enabling the site, please read the error message, that was provided from the a2ensite command" [[ $? -ne 0 ]] && log " There was an error when enabling the site, please read the error message, that was provided from the a2ensite command"
# End apache2 config # End apache2 config
# Begin Post-Setup tests
# Start: Enabling Services
for services in firewalld apache2; do
service_status="$( systemctl status $service )"
service_enabled="$( grep Loaded <<< "$service_status" | awk '{ print $4 }' )"
service_active="$( grep Active <<< "$service_status" | awk '{ pring $2 }' )"
if [[ "$service_enabled" == *disabled* ]]; then
# Enabling the service if it isn't
systemctl enable $service
fi
case $service_active in
*inactive*)
# If the service wasn't started in installation, go ahead and start it (as it would've also been enabled above)
systemctl start $service
continue
;;
*active*)
# If the service was already running then just restart it so it can reload the new configs
systemctl restart $service
;;
esac
# Unsetting these variables to ensure that it doesn't cause an issue (although it's impossible with the current setup)
unset service_status service_enabled service_active
done
# End: Enabling Services
# Start: Post-Setup tests
## Checking that the A record for webcron.tristanancelet.com has been installed/configured on my DNS server ## Checking that the A record for webcron.tristanancelet.com has been installed/configured on my DNS server
echo Doing post setup tests echo Doing post setup tests
@ -145,4 +203,4 @@ else
" "
fi fi
# End Post-Setup tests # End: Post-Setup tests