πŸ›‘οΈ cPanel + CloudLinux 9.5: PHP Hardening & Optimization Guide

A secure and optimized PHP environment is crucial for performance, stability, and protection in shared hosting. This guide provides a structured reference for hardening and tuning PHP on CloudLinux 9.5 with cPanel, leveraging PHP Selector and CageFS.


πŸ“ File Path

Global PHP settings and hardening configurations can be controlled via:

/etc/cl.selector/php.conf

βš™οΈ Important Commands

Run these after modifying PHP settings to ensure changes are propagated:

cagefsctl --rebuild-alt-php-ini
cagefsctl --force-update
cagefsctl -M

πŸ” Security-Related Directives

DirectiveDefaultRecommendedNotes
allow_url_fopenOffβœ… OffPrevents remote file inclusion.
allow_url_includeOffβœ… OffMajor security risk if enabled.
register_globalsOffβœ… OffDeprecatedβ€”ensure it’s off.
magic_quotes_gpcOn❌ OffDeprecated since PHP 5.4.
safe_modeOffβœ… OffRemoved as of PHP 5.4β€”keep disabled.
expose_phpOffβœ… OffHides PHP version in HTTP headers.
open_basedirβ€“βœ… Set pathRestrict file access per domain/account.

πŸ“Š Performance & Error Handling

DirectiveDefaultRecommendedNotes
display_errorsOn❌ Off in productionPrevents error output leakage to users.
error_reportingE_ALL & ~E_NOTICEβœ…Avoid notices, log warnings & criticals.
log_errorsOffβœ… OnAlways log errors instead of showing.
output_bufferingOnβœ… OnEnhances performance, especially for CMS.
zlib.output_compressionOffβœ… On (optional)Compress output; monitor compatibility.

πŸ“¬ Uploads & Mail

DirectiveDefaultRecommendedNotes
file_uploadsOnβœ… OnRequired by most PHP applications.
upload_max_filesize1Gβœ… As neededSet per-app; don’t set too high unnecessarily.
post_max_size1G> upload sizeMust exceed upload_max_filesize.
memory_limit256M512M–1GDepends on workload: WordPress, Magento, etc.
mail.force_extra_parameters–OptionalUse for customized sendmail paths or envelopes.

πŸ•’ Timezone & Sessions

DirectiveDefaultRecommendedNotes
date.timezoneEurope/LondonMatch regionUse regional zone (e.g., Europe/London) for consistency.
session.save_path/tmpSet per accountUse CageFS paths for isolation and security.

πŸ› οΈ Path & Compatibility Settings

DirectiveDefaultRecommendedNotes
include_path.;/path/to/php/pearCustomizeRequired for PEAR or PHP Composer environments.
short_open_tagOn❌ Off (if possible)Encouraged to use full <?php tag in modern code.
mysql.allow_persistentOn⚠️ With careMonitor memory and performance with persistent connections.

🧼 Clean-Up Notes

  • Remove or ignore deprecated settings if running PHP 5.4 or higher.
  • Ensure your configuration supports all enabled applications.
  • Always test on a staging environment before production deployment.

βœ… Final Recommendations

  • Use WHM β†’ MultiPHP INI Editor or CloudLinux PHP Selector for per-version/per-user PHP settings.
  • After making changes: cagefsctl --rebuild-alt-php-ini cagefsctl --force-update cagefsctl -M
  • Audit PHP runtime behavior and module usage with: php -i php -m php -v
Scroll to Top