Fix OPcache toggle failure - enhance config file discovery for LiteSpeed paths

main
Anthony 2026-02-28 15:18:59 +08:00
parent 0f819513e2
commit d126152db6
2 changed files with 82 additions and 23 deletions

View File

@ -652,8 +652,23 @@ actions:
disable_opcache: disable_opcache:
- cmd[cp]: - cmd[cp]:
user: root user: root
commands: commands:
- php /home/litespeed/mbmanager/scripts/toggle_opcache.php disable - |
set -euo pipefail
TMP_OUTPUT="$(mktemp)"
cleanup() {
rm -f "${TMP_OUTPUT}"
}
trap cleanup EXIT
php /home/litespeed/mbmanager/scripts/toggle_opcache.php disable > "${TMP_OUTPUT}"
if grep -Eq '"status"[[:space:]]*:[[:space:]]*"error"' "${TMP_OUTPUT}"; then
cat "${TMP_OUTPUT}"
exit 1
fi
cat "${TMP_OUTPUT}"
- restartNodes: - restartNodes:
- nodeGroup: "cp" - nodeGroup: "cp"
reboot: true reboot: true

View File

@ -35,14 +35,49 @@ function find_php_config_files() {
'/etc/php/8.2/cli/conf.d/', '/etc/php/8.2/cli/conf.d/',
'/etc/php/8.1/cli/conf.d/', '/etc/php/8.1/cli/conf.d/',
'/etc/php/8.0/cli/conf.d/', '/etc/php/8.0/cli/conf.d/',
'/usr/local/lsws/lsphp/etc/php.d/',
'/usr/local/lsws/lsphp84/etc/php.d/', '/usr/local/lsws/lsphp84/etc/php.d/',
'/usr/local/lsws/lsphp83/etc/php.d/', '/usr/local/lsws/lsphp83/etc/php.d/',
'/usr/local/lsws/lsphp82/etc/php.d/', '/usr/local/lsws/lsphp82/etc/php.d/',
'/usr/local/lsws/lsphp81/etc/php.d/', '/usr/local/lsws/lsphp81/etc/php.d/',
'/usr/local/lsws/lsphp80/etc/php.d/' '/usr/local/lsws/lsphp80/etc/php.d/'
]; ];
$possible_files = [
'/usr/local/lsws/lsphp/etc/php.d/10-opcache.ini',
'/etc/php.d/10-opcache.ini'
];
$config_files = []; $config_files = [];
foreach ($possible_files as $file) {
if (is_file($file)) {
$config_files[] = $file;
}
}
$loaded_ini = php_ini_loaded_file();
if ($loaded_ini !== false && is_file($loaded_ini)) {
$content = file_get_contents($loaded_ini);
if ($content !== false && preg_match('/^\s*;?\s*opcache\.enable\s*=\s*/mi', $content)) {
$config_files[] = $loaded_ini;
}
}
$scanned_ini = php_ini_scanned_files();
if ($scanned_ini !== false && trim($scanned_ini) !== '') {
foreach (explode(',', $scanned_ini) as $ini_file) {
$ini_file = trim($ini_file);
if ($ini_file === '' || !is_file($ini_file)) {
continue;
}
if (stripos(basename($ini_file), 'opcache') !== false) {
$config_files[] = $ini_file;
}
}
}
foreach ($possible_paths as $path) { foreach ($possible_paths as $path) {
if (is_dir($path)) { if (is_dir($path)) {
$files = glob($path . '*opcache*'); $files = glob($path . '*opcache*');
@ -52,7 +87,7 @@ function find_php_config_files() {
} }
} }
return $config_files; return array_values(array_unique($config_files));
} }
// Function to modify OPcache configuration // Function to modify OPcache configuration
@ -65,26 +100,35 @@ function modify_opcache_config($enable) {
$success = false; $success = false;
foreach ($config_files as $file) { foreach ($config_files as $file) {
if (is_writable($file)) { if (!is_file($file)) {
$content = file_get_contents($file); continue;
if ($content !== false) { }
// Replace opcache.enable setting
if ($enable) { $content = file_get_contents($file);
$content = preg_replace('/^opcache\.enable\s*=\s*.*$/m', 'opcache.enable=1', $content); if ($content === false) {
$content = preg_replace('/^;opcache\.enable\s*=\s*.*$/m', 'opcache.enable=1', $content); continue;
} else { }
$content = preg_replace('/^opcache\.enable\s*=\s*.*$/m', 'opcache.enable=0', $content);
} $replacement_count = 0;
$updated_content = preg_replace(
// If no opcache.enable line exists, add it '/^\s*;?\s*opcache\.enable\s*=\s*.*$/mi',
if (!preg_match('/^opcache\.enable\s*=/m', $content)) { 'opcache.enable=' . ($enable ? '1' : '0'),
$content .= "\nopcache.enable=" . ($enable ? '1' : '0') . "\n"; $content,
} -1,
$replacement_count
if (file_put_contents($file, $content) !== false) { );
$success = true;
} if ($updated_content === null) {
} continue;
}
// If no opcache.enable line exists, add it once.
if ($replacement_count === 0) {
$updated_content = rtrim($updated_content) . "\nopcache.enable=" . ($enable ? '1' : '0') . "\n";
}
if (file_put_contents($file, $updated_content) !== false) {
$success = true;
} }
} }