Files
HomeServerCTs/Scripts/AlpinePHPTool.sh
Bram Prieshof 1d661b3287 Fixes for AlpinePHPTool:
*Added crontab configuration to the script for FreshRSS and Nextcloud CT's
*Added ProxmoxUpdateScript configuration to the script for Nextcloud CT
2025-01-16 23:54:55 +01:00

205 lines
6.6 KiB
Bash

#!/bin/ash
############################################
# @description: #
# (Re)install/update and configure PHP #
# #
# @project: HomeserverCTs #
# @target: Alpine Linux based CT's #
############################################
#ScriptConfig
self=$0
Configurations="nextcloud freshrss heimdall mailbackup"
SupportedAlpineVersion=3.21
NewPHPVer=84
SkipAlpineRelCheck=false
#Git configuration only used for Upgrades and Fresh mode
gitRepo=https://git.bprieshof.nl/brammp/HomeServerCTs
gitBranch=main
#AlpineCheck
if [[ $(cat /etc/os-release | grep -m 1 ^"ID=") != *"alpine"* ]]; then echo Not on Alpine Linux ;exit 1;fi
#AlpineVersionCheck
if $SkipAlpineRelCheck ; then echo "Notice: Alpine releas check skipped"
elif [[ $(grep "^VERSION_ID=" /etc/os-release | awk -F= '{print $2}') != *"$SupportedAlpineVersion"* ]]; then printf "This Alpine Linux is not supported by default,\nto continue set SkipAlpineRelCheck to true and check the targeted php version in the script\n" ;exit 1;fi
#Functions for menu/core use
show_help () {
cat <<END_HELP
Usage: $self [-dfpuh] <Config name>
Options:
-d, Distrobuilder: Install everything and use Local configs (non interactive)
-f, Fresh: Install everything and fetch config
-p, Purge: Remove all PHP packlages and back configuration (handy for distro release upgrades)
-u, Upgrade: Run both Purge and Install steps
-v <PhpVer>, Set PHPVersion: specify the PHP version version (example 84 for 8.4)
Cnfigurations:
$Configurations
END_HELP
}
set_mode () {
if [ ! -z "$mode" ]; then echo "ERROR: Unable to use multiple modes at once";exit 1;fi
mode=$1
}
echo_exit (){
echo
exit 1
}
#Arugment/option handeling
while getopts 'dfpuhv:' opt; do
case $opt in
d) set_mode distrobuilder;;
f) set_mode fresh ;;
p) set_mode purge ;;
u) set_mode upgrade ;;
v) NewPHPVer="${OPTARG}" ;;
h) show_help
exit ;;
*) show_help
exit 1
esac
done
shift "$(( OPTIND - 1 ))"
for config do
config=$config
done
if [ -z "$mode" ]; then
printf "ERROR: No mode argument given\n\n"
show_help
exit 1
elif [ -z "$config" ]; then
printf "ERROR: No configuration argument given\n\n"
show_help
exit 1
elif [[ -z "$(echo "$Configurations" | fgrep -w "$config")" ]]; then
printf "ERROR: Invalid configuration argument given\n\n"
show_help
exit 1
fi
#Functions for mode
#Backup PHP config and purge PHP packages
run_Purge() {
#Check if PHP is installed
if ! command -v php &> /dev/null; then printf "ERROR: PHP is not installed\n\n"; exit 1;fi
#Get current PHP Version
OldPHPVer=$(grep php /etc/apk/world -m 1|sed -e "s/\-.*//" -e "s/php//")
#Create Backup an its location
if [[ ! -d "/opt/PHPCfgBackup/" ]];then mkdir -p "/opt/PHPCfgBackup"; fi
if [[ -f /etc/crontabs/nginx ]]; then cp /etc/crontabs/nginx /opt/PHPCfgBackup/crontabs-nginx; fi
tar -czf "/opt/PHPCfgBackup/PHP$OldPHPVer-Config.tar.gz" -C "/etc/php$OldPHPVer" ./
rm -rf "/etc/php$OldPHPVer"
#Set placeholder in Nginx for PHP Socket
sed -i -e 's/'$OldPHPVer'/PHPver/g' /etc/nginx/nginx.conf
#Purge PHP packages
apk del php*
}
#(re)install PHP packages
run_Install() {
sed -i -e "s/phpPHPver/php$NewPHPVer/g" $configStore/php.pkglist
cat $configStore/php.pkglist | xargs apk add
#Configure Php-Fpm
echo ";Placeholder" > /etc/php$NewPHPVer/php-fpm.d/www.conf
sed -i -e 's/PHPver/'$NewPHPVer'/g' $configStore/php.conf
mv $configStore/php.conf /etc/php$NewPHPVer/php-fpm.d/$config.conf
printf "[Date]\ndate.timezone = Europe/Amsterdam" >/etc/php$NewPHPVer/conf.d/04_date_timezone.ini
#Configure Nginx PHP Socket
sed -i -e 's/PHPver/'$NewPHPVer'/g' /etc/nginx/nginx.conf
#Unique settings for configuration
case $config in
nextcloud)
#Configure Php-Cli
sed -i '/memory_limit =/c\memory_limit = 512M' /etc/php$NewPHPVer/php.ini
echo "apc.enable_cli=1" >> /etc/php$NewPHPVer/php.ini
if [[ -f /opt/ProxMoxToolKitAppUpdate.sh ]]; then
sed -i "/CurPHP=/c\CurPHP=php$NewPHPVer" /opt/ProxMoxToolKitAppUpdate.sh
fi
;;
esac
#Configure Cron
if test -f $configStore/crontab; then
sed -i -e 's/PHPver/'$NewPHPVer'/g' $configStore/crontab
mv $configStore/crontab /etc/crontabs/nginx
fi
#Enable service(s) on boot
rc-update add php-fpm$NewPHPVer
}
run_DistrobuilderOnly() {
#Do things only required when setting up using distrobuilder
##Unique settings for configuration
case $config in
nextcloud)
sed -i -e "s/phpPHPver/php$NewPHPVer/g" /opt/Setup/Scripts/FirstRun.sh #Update FirstRun setup
sed -i -e "s/phpPHPver/php$NewPHPVer/g" /opt/Setup/Scripts/PTKAppUpdate.sh #Update nextcloud update script
;;
esac
}
fetch_Config() {
#Unique settings for configuration
case $config in
mailbackup)
wget https://git.bprieshof.nl/Tools/MailBackup-sys/raw/branch/alpine/Configs/php.conf -O /tmp/php.conf || echo_exit "ERROR: Config download failed"
wget https://git.bprieshof.nl/Tools/MailBackup-sys/raw/branch/alpine/Configs/php.pkglist -O /tmp/php.pkglist || echo_exit "ERROR: Config download failed"
;;
*)
wget "$gitRepo"/raw/branch/"$gitBranch"/CT-Files/$config/Configs/php.conf -O /tmp/php.conf || echo_exit "ERROR: Config download failed"
wget "$gitRepo"/raw/branch/"$gitBranch"/CT-Files/"$config"/Configs/php.pkglist -O /tmp/php.pkglist || echo_exit "ERROR: Config download failed"
#Check if crontab conf exists,if so get it
if wget -q --method=HEAD "$gitRepo"/raw/branch/"$gitBranch"/CT-Files/$config/Configs/crontab;
then
wget "$gitRepo"/raw/branch/"$gitBranch"/CT-Files/"$config"/Configs/crontab -O /tmp/crontab || echo_exit "ERROR: Config download failed"
fi
;;
esac
}
case $mode in
distrobuilder)
#Set config location
configStore=/opt/Setup/Configs
run_DistrobuilderOnly
run_Install
;;
fresh)
fetch_Config
#Set config location
configStore=/tmp
run_Install
;;
purge)
run_Purge
;;
upgrade)
#Set config location
configStore=/tmp
fetch_Config
service nginx stop
run_Purge
run_Install
service nginx start
;;
esac