Skip to content

Instantly share code, notes, and snippets.

@dansleboby
Created July 4, 2024 20:50
Show Gist options
  • Save dansleboby/2d800cd190ff4e89aaaa0f78964d2117 to your computer and use it in GitHub Desktop.
Save dansleboby/2d800cd190ff4e89aaaa0f78964d2117 to your computer and use it in GitHub Desktop.
PHP Configuration Update Script cPanel/WHM
#!/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
@dansleboby
Copy link
Author

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 and alt-php packages provided by CloudLinux.

Features

  • Command-line Arguments: The script accepts the PHP configuration variable and its new value as command-line arguments.
  • Interactive Mode: If no command-line arguments are provided, the script will prompt for input.
  • Validation: Ensures the variable name does not contain spaces and both the variable and value are not empty.
  • Preview Changes: Displays a preview of the changes for each PHP version, highlighting current and new values.
  • Handling Missing Variables:
    • Warns if the variable is not present in the file and indicates it will be added.
    • If the variable is commented out, it will be uncommented and set to the new value.
    • If the variable does not exist, it will be appended to the end of the file.
  • Confirmation Prompt: Asks for user confirmation before applying changes.
  • Restart Apache: Automatically restarts Apache to apply the changes.

Usage

./update_php_config.sh [VARIABLE] [NEW_VALUE]

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 to 64M for all PHP versions, run:

./update_php_config.sh post_max_size 64M

If no arguments are provided, the script will prompt for the necessary inputs.

Requirements

  • This script should be run with root or sudo privileges.
  • It is intended for use on cPanel/WHM servers with CloudLinux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment