File: //opt/cwprads/cwp_ini_update.sh
#!/bin/bash
# Grab the timezone from timedatectl and prompt the agent to set it in CWP admin panel if needed
zone=$(timedatectl | awk '/Time zone/ {print $3}')
echo -e "Timezone detected as: $zone \n"
if [[ "$zone" != *"/"* ]]; then
echo "Timezone detected as ('$zone'). Format wrong please correct via 'Change Server Date & Time' in CWP Admin and resubmit"
exit 1
fi
# Grab a list of php.ini files on the server, print that list and then confirm the user wants to proceed with the update
echo "Searching for ini files, this may take a few minutes..."
ini_list=$(find / -type f -name php.ini 2>/dev/null)
echo "The following php.ini files were found:"
echo -e "$ini_list \n"
read -r -p "Do you want to update these files to use the server timezone ($zone)? [y/n]: " response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
echo "No changes were made."
exit 0
fi
# Update date.timezone in each php.ini file
echo -e "\nUpdating php.ini files, please standby...\n"
for file in $ini_list;
do
echo "Modifying $file"
if grep -q "^[^#]*date\.timezone" "$file"; then
sed -i "s|date\.timezone\s*=.*|date.timezone = ${zone}|g" "${file}"
echo "Updated date.timezone in $file"
else
echo "date.timezone = ${zone}" > "${file}"
echo "Appended date.timezone to $file"
fi
done
echo -e "\nAll php.ini updates completed."
# Grab a list of PHP-FPM services and restart each of them
php_services=$(systemctl list-units --type=service --state=running | grep "^php-fpm" |awk '{print $1}')
echo -e "\nRestarting the following PHP-FPM services:"
echo -e "$php_services \n"
for service in $php_services;
do
echo "Restarting $service"
sudo systemctl restart "$service"
done
# Restart CWP
echo -e "\nRestarting CWP"
sudo /scripts/restart_cwpsrv
echo -e "\nAll Changes Completed :)"