Created
July 4, 2024 20:50
-
-
Save dansleboby/2d800cd190ff4e89aaaa0f78964d2117 to your computer and use it in GitHub Desktop.
PHP Configuration Update Script cPanel/WHM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Define color codes | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
YELLOW='\033[0;33m' | |
NC='\033[0m' # No Color | |
# Function to display help | |
display_help() { | |
echo "Usage: $0 [VARIABLE] [NEW_VALUE]" | |
echo | |
echo "Update a PHP configuration variable for all PHP versions." | |
echo | |
echo " VARIABLE The PHP configuration variable to change (e.g., post_max_size)" | |
echo " NEW_VALUE The new value for the PHP configuration variable (e.g., 64M)" | |
echo | |
echo "If no arguments are provided, the script will prompt for input." | |
echo | |
echo "Options:" | |
echo " -h, --help Display this help message" | |
exit 0 | |
} | |
# Function to check for spaces in a string | |
contains_space() { | |
if [[ $1 =~ \ ]]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
# Parse command-line arguments | |
if [[ "$1" == "-h" || "$1" == "--help" ]]; then | |
display_help | |
fi | |
if [[ -n "$1" && -n "$2" ]]; then | |
VARIABLE=$1 | |
NEW_VALUE=$2 | |
else | |
# Ask for the variable and its new value if not provided as arguments | |
read -p "Enter the PHP configuration variable you want to change (e.g., post_max_size): " VARIABLE | |
read -p "Enter the new value for $VARIABLE (e.g., 64M): " NEW_VALUE | |
fi | |
# Validate the input | |
if [[ -z "$VARIABLE" || -z "$NEW_VALUE" ]]; then | |
echo -e "${RED}Error: Both variable and value must be provided and not empty.${NC}" | |
exit 1 | |
fi | |
if contains_space "$VARIABLE"; then | |
echo -e "${RED}Error: The variable name must not contain spaces.${NC}" | |
exit 1 | |
fi | |
# Preview the changes | |
echo -e "${YELLOW}You are about to change $VARIABLE to $NEW_VALUE for all PHP versions.${NC}" | |
echo -e "${YELLOW}Preview of changes:${NC}" | |
for version in /opt/cpanel/ea-php*/root/etc/php.ini /opt/alt/php*/etc/php.ini; do | |
if grep -qE "^$VARIABLE" $version; then | |
current_value=$(grep -E "^$VARIABLE" $version | cut -d' ' -f 3) | |
echo -e "${GREEN}In file $version:${NC}" | |
echo -e "${RED}$VARIABLE = $current_value${NC} => ${GREEN}$VARIABLE = $NEW_VALUE${NC}" | |
elif grep -qE "^;$VARIABLE" $version; then | |
current_value=$(grep -E "^;$VARIABLE" $version | cut -d' ' -f 3) | |
echo -e "${GREEN}In file $version:${NC}" | |
echo -e "${RED};$VARIABLE = $current_value${NC} => ${GREEN}$VARIABLE = $NEW_VALUE${NC} (uncommented)" | |
else | |
echo -e "${GREEN}In file $version:${NC}" | |
echo -e "${RED}$VARIABLE not present${NC} => ${GREEN}$VARIABLE = $NEW_VALUE${NC} (added)" | |
fi | |
echo "" | |
done | |
# Ask for confirmation | |
read -p "Do you want to continue? (yes/no): " CONFIRM | |
if [ "$CONFIRM" = "yes" ]; then | |
# Update ea-php and alt-php versions | |
for version in /opt/cpanel/ea-php*/root/etc/php.ini /opt/alt/php*/etc/php.ini; do | |
echo -e "${YELLOW}Updating $version${NC}" | |
if grep -qE "^$VARIABLE" $version; then | |
sed -i "s/^$VARIABLE = .*/$VARIABLE = $NEW_VALUE/" $version | |
elif grep -qE "^;$VARIABLE" $version; then | |
sed -i "s/^;$VARIABLE = .*/$VARIABLE = $NEW_VALUE/" $version | |
else | |
echo "$VARIABLE = $NEW_VALUE" >> $version | |
fi | |
done | |
# Restart Apache to apply changes | |
service httpd restart | |
echo -e "${GREEN}$VARIABLE updated to $NEW_VALUE for all PHP versions.${NC}" | |
else | |
echo -e "${RED}Operation cancelled.${NC}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script allows you to update a PHP configuration variable for all installed PHP versions on a cPanel/WHM server. It supports both
ea-php
andalt-php
packages provided by CloudLinux.Features
Usage
Parameters
VARIABLE
: The PHP configuration variable to change (e.g.,post_max_size
).NEW_VALUE
: The new value for the PHP configuration variable (e.g.,64M
).Options
-h
,--help
: Display the help message and usage instructions.Example
To change the
post_max_size
to64M
for all PHP versions, run:If no arguments are provided, the script will prompt for the necessary inputs.
Requirements