176 lines
5.2 KiB
Bash
176 lines
5.2 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
|
|
|
|
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/optin handeling
|
|
while getopts 'dfpuh' opt; do
|
|
case $opt in
|
|
d) set_mode distrobuilder;;
|
|
f) set_mode fresh ;;
|
|
p) set_mode purge ;;
|
|
u) set_mode upgrade ;;
|
|
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
|
|
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() {
|
|
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)
|
|
echo runnig distrobuilder
|
|
#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
|
|
;;
|
|
esac
|
|
|
|
#Enable service(s) on boot
|
|
rc-update add php-fpm$NewPHPVer
|
|
}
|
|
|
|
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"
|
|
;;
|
|
|
|
esac
|
|
}
|
|
|
|
case $mode in
|
|
distrobuilder)
|
|
#Set config location
|
|
configStore=/opt/Setup/Configs
|
|
run_Install
|
|
;;
|
|
fresh)
|
|
fetch_Config
|
|
#Set config location
|
|
configStore=/tmp
|
|
run_Install
|
|
;;
|
|
purge)
|
|
run_Purge
|
|
;;
|
|
upgrade)
|
|
#Set config location
|
|
configStore=/opt/Setup/Configs
|
|
fetch_Config
|
|
service nginx stop
|
|
run_Purge
|
|
run_Install
|
|
service nginx start
|
|
;;
|
|
esac |