🔥 Sizzling Summer Deals! Up to 78% Off Hosting + Free Domain & Website Migration
Expires In: 23h 59m 38s
View Deals
HosterOcean.com
Knowledge Base

How to Update PHP Settings for All Versions in cPanel & CloudLinux Using a Bash Script?

Automatically apply your desired PHP configuration values across all installed versions — in one go — using a custom Bash script.

Uncategorized Knowledge Base

If you manage a cPanel server with CloudLinux, you probably have multiple PHP versions installed — some via EasyApache 4 (EA-PHP) and others via ALT-PHP.

Manually editing each php.ini file can be time-consuming and prone to mistakes.
In this guide, we'll show you how to automatically apply your desired PHP configuration values across all installed versions — in one go — using a custom Bash script.

🚀 Why You Need This Script

After a new cPanel installation or migration, default PHP settings often cause issues such as:

  • File upload errors due to small upload_max_filesize
  • Long-running scripts timing out
  • Security risks from allow_url_include
  • Websites showing PHP warnings because display_errors is enabled

This script automatically sets your preferred values for every PHP version installed on your server.

⚙️ What the Script Does

Updates PHP settings for both:

  • EA-PHP (cPanel's EasyApache 4)
  • ALT-PHP (CloudLinux PHP Selector)
  • Creates backups of all php.ini files
  • Applies uniform PHP directives (like memory_limit, max_execution_time, etc.)
  • Restarts Apache and PHP-FPM services to apply changes automatically

🧩 PHP Settings Used in This Script

Here's what the script enforces across all PHP versions:

Directive Value Description
allow_url_fopen On Allows opening URLs with file functions
allow_url_include Off Prevents remote file inclusion
display_errors Off Hides PHP warnings from users
enable_dl On Enables dynamic loading of extensions
file_uploads On Allows file uploads via forms
max_execution_time 30 Limits script runtime
max_input_time -1 No limit on input parsing time
max_input_vars 1000 Controls number of input variables
memory_limit 512M Increases PHP memory usage
post_max_size 64M Controls POST request size
upload_max_filesize 64M Allows large file uploads
session.gc_maxlifetime 1440 Session duration (24 min)
zlib.output_compression Off Disables gzip output compression

💻 The Bash Script

Save the following script as /root/update-php-settings.sh:

#!/bin/bash
# Update PHP directives for EA-PHP (cPanel EasyApache4) and ALT-PHP (CloudLinux)

# Desired settings
ALLOW_URL_FOPEN="On"
ALLOW_URL_INCLUDE="Off"
DISPLAY_ERRORS="Off"
ENABLE_DL="On"
FILE_UPLOADS="On"
MAX_EXECUTION_TIME="30"
MAX_INPUT_TIME="-1"
MAX_INPUT_VARS="1000"
MEMORY_LIMIT="512M"
POST_MAX_SIZE="64M"
SESSION_GC_MAXLIFETIME="1440"
UPLOAD_MAX_FILESIZE="64M"
ZLIB_OUTPUT_COMPRESSION="Off"

echo "Updating PHP settings for EA-PHP and ALT-PHP..."

#####################################
# Update EA-PHP (EasyApache 4)
#####################################
for php_ini in /opt/cpanel/ea-php*/root/etc/php.ini; do
    if [[ -f "$php_ini" ]]; then
        echo "Updating $php_ini ..."
        cp -p "$php_ini" "$php_ini.bak.$(date +%F_%T)"
        sed -i "s/^allow_url_fopen.*/allow_url_fopen = $ALLOW_URL_FOPEN/" "$php_ini"
        sed -i "s/^allow_url_include.*/allow_url_include = $ALLOW_URL_INCLUDE/" "$php_ini"
        sed -i "s/^display_errors.*/display_errors = $DISPLAY_ERRORS/" "$php_ini"
        sed -i "s/^enable_dl.*/enable_dl = $ENABLE_DL/" "$php_ini"
        sed -i "s/^file_uploads.*/file_uploads = $FILE_UPLOADS/" "$php_ini"
        sed -i "s/^max_execution_time.*/max_execution_time = $MAX_EXECUTION_TIME/" "$php_ini"
        sed -i "s/^max_input_time.*/max_input_time = $MAX_INPUT_TIME/" "$php_ini"
        sed -i "s/^max_input_vars.*/max_input_vars = $MAX_INPUT_VARS/" "$php_ini"
        sed -i "s/^memory_limit.*/memory_limit = $MEMORY_LIMIT/" "$php_ini"
        sed -i "s/^post_max_size.*/post_max_size = $POST_MAX_SIZE/" "$php_ini"
        sed -i "s/^session.gc_maxlifetime.*/session.gc_maxlifetime = $SESSION_GC_MAXLIFETIME/" "$php_ini"
        sed -i "s|^upload_max_filesize.*|upload_max_filesize = $UPLOAD_MAX_FILESIZE|" "$php_ini"
        sed -i "s/^zlib.output_compression.*/zlib.output_compression = $ZLIB_OUTPUT_COMPRESSION/" "$php_ini"
    fi
done

#####################################
# Update ALT-PHP (CloudLinux)
#####################################
for alt_ini in /opt/alt/php*/etc/php.ini; do
    if [[ -f "$alt_ini" ]]; then
        echo "Updating $alt_ini ..."
        cp -p "$alt_ini" "$alt_ini.bak.$(date +%F_%T)"
        sed -i "s/^allow_url_fopen.*/allow_url_fopen = $ALLOW_URL_FOPEN/" "$alt_ini"
        sed -i "s/^allow_url_include.*/allow_url_include = $ALLOW_URL_INCLUDE/" "$alt_ini"
        sed -i "s/^display_errors.*/display_errors = $DISPLAY_ERRORS/" "$alt_ini"
        sed -i "s/^enable_dl.*/enable_dl = $ENABLE_DL/" "$alt_ini"
        sed -i "s/^file_uploads.*/file_uploads = $FILE_UPLOADS/" "$alt_ini"
        sed -i "s/^max_execution_time.*/max_execution_time = $MAX_EXECUTION_TIME/" "$alt_ini"
        sed -i "s/^max_input_time.*/max_input_time = $MAX_INPUT_TIME/" "$alt_ini"
        sed -i "s/^max_input_vars.*/max_input_vars = $MAX_INPUT_VARS/" "$alt_ini"
        sed -i "s/^memory_limit.*/memory_limit = $MEMORY_LIMIT/" "$alt_ini"
        sed -i "s/^post_max_size.*/post_max_size = $POST_MAX_SIZE/" "$alt_ini"
        sed -i "s/^session.gc_maxlifetime.*/session.gc_maxlifetime = $SESSION_GC_MAXLIFETIME/" "$alt_ini"
        sed -i "s|^upload_max_filesize.*|upload_max_filesize = $UPLOAD_MAX_FILESIZE|" "$alt_ini"
        sed -i "s/^zlib.output_compression.*/zlib.output_compression = $ZLIB_OUTPUT_COMPRESSION/" "$alt_ini"
    fi
done

echo "Restarting Apache and PHP-FPM..."
systemctl restart httpd
systemctl restart ea-php*-php-fpm.service 2>/dev/null

echo "✅ All PHP settings updated successfully!"

🧱 How to Run the Script

1. Login to SSH as root

ssh root@your-server-ip

2. Create the script file

nano /root/update-php-settings.sh

3. Paste the code above and save (Ctrl+O, Enter, Ctrl+X)

4. Make it executable

chmod +x /root/update-php-settings.sh

5. Run the script

sh /root/update-php-settings.sh

6. Wait for the confirmation message:

✅ All PHP settings updated successfully!

🧠 Pro Tip

If you want to change specific PHP directives later, just modify the variable values at the top of the script and rerun it.
It will reapply the new values across all PHP versions safely, keeping old backups with timestamps.

🔍 Conclusion

This Bash script is a time-saver for hosting providers, sysadmins, and resellers managing multiple PHP versions.
Instead of manually editing each php.ini, you can standardize your PHP configurations in one clean, automated step.

✅ Works on:

  • CloudLinux
  • cPanel servers
  • LiteSpeed or Apache setups

Tags:

alt-php cloudlinux bash script php cpanel php optimization ea-php php configuration linux server php max_execution_time php php handler fix php memory limit fix php security settings php upload_max_filesize php.ini automation php.ini settings server php configuration update php settings wordpress php performance

Related Articles

Business Guide

How to Choose the Best Web Hosting for Your Business 2026

Complete guide to selecting the perfect web hosting solution for your business needs, budget, and growth plans in 2026.

Read More →
Troubleshooting

How to Fix PHP Files Downloading Instead of Executing

Troubleshoot and resolve PHP files downloading instead of executing properly on your web server.

Read More →

Take your website online with HosterOcean!

Still on the fence? Give it a shot for just $0.95/month for the first month!

Get Started